def backtest_start(commobj, start_date: datetime.datetime, end_date: datetime.datetime): """ 回测 """ rdchicang = redisRW.redisrw(redisRW.db_backtest_chicang) rdchicang.del_db() rddaysell = redisRW.redisrw(redisRW.db_day_sell) rddaysell.del_db() # 日线回测 commobj.testbar = True zbstrategy = zbStrategy.Strategy(None) while start_date.date() <= end_date.date(): if start_date.weekday() in range(5): # 选股 zbstrategy.stock_select(rdchicang, start_date) get_ticks_from_xg_and_start_backtest(commobj, start_date) #input('按任意键继续...') start_date += datetime.timedelta(days=1) ex_buy_count = 0 day_sj_zdf_count = Decimal(0) ex_zdf_count = Decimal(0) sz_count = 0 xd_count = 0 ex_sz_count = 0 ex_xd_count = 0 datas = [] max_zyk = None max_huice = None for d in rddaysell.read_l_date_dec_json('daysell', 0, -1): ex_buy_count += d['buy_count'] ex_sz_count += d['sz'] ex_xd_count += d['xd'] zdf = d['sj_zdf_count'] if (max_zyk is None) or d['zyk'] > max_zyk: max_zyk = d['zyk'] huice = d['zyk'] - max_zyk if (max_huice is None) or huice < max_huice: max_huice = huice if zdf > 0: sz_count += 1 else: xd_count += 1 day_sj_zdf_count += zdf ex_zdf_count += d['ex_zdf_count'] datas.append({ 'datetime': d['datetime'], 'ex_buy_count': ex_buy_count, 'ex_zdf_count': float(str(ex_zdf_count.quantize(Decimal('0.01'), ROUND_HALF_UP))), 'day_sj_zdf_count': float(str(day_sj_zdf_count.quantize(Decimal('0.01'), ROUND_HALF_UP))), 'zyk': float(str(d['zyk'].quantize(Decimal('0.01'), ROUND_HALF_UP))), 'day_sz': sz_count, 'day_xd': xd_count, 'ex_sz_count': ex_sz_count, 'ex_xd_count': ex_xd_count }) ply.write_html_tol('./WebServer/html/charts.html', datas, max_huice)
def fupan(): # 选股 zbstrategy = zbStrategy.Strategy(None) zbstrategy.stock_select(redisRW.redisrw(redisRW.db_chicang)) tk_date = datetime.datetime.now() # 获取最新一天的ticks,并启动回测 commobj = comm.commObj(redisRW.redisrw(redisRW.db_backtest_chicang), 1, is_backtest=True) getticks = runQuotation.get_ticks_from_xg_and_start_backtest( commobj, tk_date) while not getticks: print(tk_date, '没有ticks,尝试前一天。') tk_date -= datetime.timedelta(days=1) getticks = runQuotation.get_ticks_from_xg_and_start_backtest( commobj, tk_date) print('策略提示', '将买入标记重点的股票。') return True
def __init__(self, commObj, xg_data): self.ticks = [] self.xg_data = xg_data self.code = xg_data['code'] self.name = xg_data['name'] self.hangye = xg_data['hangye'] self.chicang = xg_data['chicang'] if self.chicang: self.chicang['datetime'] = datetime.datetime.strptime( self.chicang['datetime'], '%Y%m%d %H:%M:%S') self.bz = xg_data['bz'] self.price_zs = xg_data['price_zs'] self.price_zy = xg_data['price_zy'] self.zs_date = xg_data['zs_date'] self.zy_date = xg_data['zy_date'] self.strategy = zbStrategy.Strategy(commObj) self.rdrealtimetick = redisRW.redisrw(redisRW.db_realtime_tick) self.rdmainquotation = redisRW.redisrw(redisRW.db_mainquotaion) self.history_data = zbBase.history_calculate_by_kline(xg_data)
def backtest_start(common, start_date: datetime.datetime, end_date: datetime.datetime): """ 回测 """ rdchicang = redisRW.redisrw(redisRW.db_backtest_chicang) rdchicang.del_db() rddaysell = redisRW.redisrw(redisRW.db_day_sell) rddaysell.del_db() codes_bk = comm.get_codes_bk() if not codes_bk: print('股票代码列表获取失败。') return zbstrategy = zbStrategy.Strategy(None) while start_date.date() <= end_date.date(): if start_date.weekday() in range(5): # 选股 zbstrategy.stock_select(common['b'], codes_bk, start_date) get_ticks_from_xg_and_start_backtest(common, start_date) #input('按任意键继续...') start_date += datetime.timedelta(days=1) ex_sz_count = 0 ex_xd_count = 0 ex_sell_count = 0 datas = [] zyk_list = [] max_huice = 0 max_zc = None day_sz_count = 0 day_xd_count = 0 last_zc = common['init_balance'] for d in rddaysell.read_l_date_dec_json('daysell', 0, -1): ex_sell_count += d['sell_count'] ex_sz_count += d['sz'] ex_xd_count += d['xd'] zyk_list.append(d['zyk']) if max_zc is None: max_zc = d['zc'] max_zc = max(max_zc, d['zc']) max_huice = max(max_huice, (1 - d['zc'] / max_zc) * 100) if d['zc'] < last_zc: day_sz_count += 1 else: day_xd_count += 1 last_zc = d['zc'] datas.append({ 'datetime': d['datetime'], 'ex_sell_count': ex_sell_count, 'ex_sz_count': ex_sz_count, 'ex_xd_count': ex_xd_count, 'day_sz_count': day_sz_count, 'day_xd_count': day_xd_count, 'zc': float(str(d['zc'])), }) if len(datas) == 0: print('没有交易数据') return zyk_std = float(str(np.std(zyk_list))) m_data = datas[len(datas) - 1] tol_days = (m_data['datetime'] - datas[0]['datetime']).days tol_days = max(1, tol_days) annualized = float(str(d['zyk'])) / tol_days * 365 sharperatio = 0 if zyk_std > 0: sharperatio = annualized / zyk_std ply.write_html_tol( os.path.join(current_path, '..', 'WebServer', 'html', 'charts.html'), datas, float(str(d['zyk'])), max_huice, annualized, sharperatio)