Administrator
2024-07-25 9d39b293bde97f31f522010373aad1dd3f654c07
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
回撤交易
"""
# 持仓字典
import logging
 
from log_module import log_export
from log_module.log import logger_debug
from trade.buy_strategy import BuyStrategyDataManager
from utils import l2_huaxin_util, tool
 
position_dict = {}
 
markets_list_dict = {}
 
 
def get_market_by_time(code, time_str):
    """
    获取某个时间的价格
    :param code:
    :param time_str:
    :return:
    """
    markets = markets_list_dict.get(code)
    if markets:
        for m in markets:
            if tool.trade_time_sub(l2_huaxin_util.convert_time(m[9]), time_str) >= 0:
                return m
    return None
 
 
def start_backtest(date):
    """
    开始回测
    :param date:
    :return:
    """
    try:
        __BuyStrategyDataManager = BuyStrategyDataManager()
        # 回撤
        global markets_list_dict
        markets_list_dict = log_export.load_market_info(date)
        transactions = log_export.load_transactions(date)
        for t in transactions:
            code = t["SecurityID"]
            time_str = l2_huaxin_util.convert_time(t["OrderTime"])
            market_info = get_market_by_time(code, time_str)
            if market_info:
                __BuyStrategyDataManager.add_market_info(market_info)
 
            results = __BuyStrategyDataManager.add_transaction_info(t, True)
            for need_buy, strategy_type, need_buy_msg in results:
                if need_buy and code not in position_dict:
                    # 持仓结果保存
                    position_dict[code] = t
                    logger_debug.info(f"回测下单({need_buy_msg}):{t}")
    except Exception as e:
        logging.exception(e)
        logger_debug.exception(e)