#!/usr/bin/env python3
"""
在容器内调用API执行回测 - 使用内置urllib，不用requests
"""

import json
from urllib.request import Request, urlopen
from urllib.error import URLError

# 策略代码 - 关羽的单票止损策略
strategy_code = '''
"""
单票固定比例止损策略 - vnpy CTA回测
"""

from vnpy_ctastrategy import (
    CtaTemplate, StopOrder, TickData, BarData, TradeData, OrderData,
    BarGenerator, ArrayManager
)
from vnpy.trader.constant import Direction, Offset


class SingleStockStopLossStrategy(CtaTemplate):
    """单票固定比例止损策略 - 均线趋势跟踪+固定比例止损"""
    
    author = "关羽 (云长)"
    parameters = ["fast_window", "slow_window", "stop_loss_pct"]
    variables = ["fast_ma", "slow_ma", "cost_price", "in_position"]
    
    def __init__(self, cta_engine, strategy_name, vt_symbol, setting):
        super().__init__(cta_engine, strategy_name, vt_symbol, setting)
        self.bg = BarGenerator(self.on_bar)
        self.am = ArrayManager(max(self.slow_window + 10, 100))
        self.fast_ma = 0.0
        self.slow_ma = 0.0
        self.cost_price = 0.0
        self.in_position = False
        
    def on_init(self):
        self.write_log(f"策略初始化，fast={self.fast_window}, slow={self.slow_window}, stop_loss={self.stop_loss_pct:.1%}")
        self.put_event()
        
    def on_bar(self, bar):
        self.am.update_bar(bar)
        if not self.am.inited:
            return
        
        self.fast_ma = self.am.sma(self.fast_window)
        self.slow_ma = self.am.sma(self.slow_window)
        
        have_signal = True
        if self.in_position and self.cost_price > 0:
            current_drawdown = (bar.close_price - self.cost_price) / self.cost_price
            if current_drawdown <= -self.stop_loss_pct:
                if self.pos > 0:
                    self.sell(bar.close_price, self.pos)
                    self.in_position = False
                    self.write_log(f"🔴 触发止损：成本{self.cost_price:.2f}，当前{bar.close_price:.2f}，回撤{current_drawdown:.1%}")
                have_signal = False
        
        if have_signal:
            if not self.in_position:
                if self.fast_ma > self.slow_ma:
                    self.buy(bar.close_price, 10000)
                    self.cost_price = bar.close_price
                    self.in_position = True
                    self.write_log(f"🟢 金叉开多：价格{bar.close_price:.2f}")
            else:
                if self.fast_ma < self.slow_ma:
                    if self.pos > 0:
                        self.sell(bar.close_price, self.pos)
                        self.in_position = False
                        self.write_log(f"🔵 死叉平仓：价格{bar.close_price:.2f}")
        
        self.put_event()
'''

# 请求数据
request_data = {
    'strategy_code': strategy_code,
    'symbol': '510300.SSE',
    'interval': '1d',
    'start': 1609459200,
    'end': 1772515200,
    'capital': 1000000,
    'rate': 3e-5,
    'slippage': 0.002,
    'size': 10000,
    'pricetick': 0.001,
    'data_source': 'sqlite'
}

# 发送请求
try:
    url = 'http://127.0.0.1:8088/api/backtest/run'
    data = json.dumps(request_data).encode('utf-8')
    
    req = Request(url, data=data, method='POST')
    req.add_header('Content-Type', 'application/json')
    
    print("🔄 发送回测请求...")
    with urlopen(req, timeout=300) as f:
        response = f.read().decode('utf-8')
        result = json.loads(response)
        print(json.dumps(result, indent=2, ensure_ascii=False))
        
except URLError as e:
    print(f"❌ 请求失败: {e}")
except Exception as e:
    print(f"❌ 错误: {e}")
    import traceback
    traceback.print_exc()
