[bug fix] 股票引擎

This commit is contained in:
msincenselee 2021-09-17 10:22:57 +08:00
parent 4804d476e0
commit db50cc11e4
4 changed files with 11 additions and 16 deletions

View File

@ -565,7 +565,7 @@ class BackTestingEngine(object):
for vt_symbol, symbol_data in data_dict.items():
self.write_log(u'配置{}数据:{}'.format(vt_symbol, symbol_data))
self.set_price_tick(vt_symbol, symbol_data.get('price_tick', 0.01))
self.set_volume_tick(vt_symbol, symbol_data.get('min_volume', 10))
self.set_volume_tick(vt_symbol, symbol_data.get('min_volume', 100))
self.set_slippage(vt_symbol, symbol_data.get('slippage', 0))
self.set_size(vt_symbol, symbol_data.get('symbol_size', 1))
margin_rate = symbol_data.get('margin_rate', 1)

View File

@ -402,8 +402,8 @@ class CtaEngine(BaseEngine):
# strategy.pos -= trade.volume
# 根据策略名称,写入 data\straetgy_name_trade.csv文件
strategy_name = getattr(strategy, 'strategy_name')
trade_fields = ['datetime', 'symbol', 'exchange', 'vt_symbol', 'tradeid', 'vt_tradeid', 'orderid', 'vt_orderid',
'direction', 'offset', 'price', 'volume', 'idx_price']
trade_fields = ['datetime', 'symbol', 'exchange', 'vt_symbol', 'name', 'tradeid', 'vt_tradeid', 'orderid', 'vt_orderid',
'direction', 'offset', 'price', 'volume']
trade_dict = OrderedDict()
try:
for k in trade_fields:
@ -418,15 +418,6 @@ class CtaEngine(BaseEngine):
else:
trade_dict[k] = getattr(trade, k, '')
# 添加指数价格
symbol = trade_dict.get('symbol')
idx_symbol = get_underlying_symbol(symbol).upper() + '99.' + trade_dict.get('exchange')
idx_price = self.get_price(idx_symbol)
if idx_price:
trade_dict.update({'idx_price': idx_price})
else:
trade_dict.update({'idx_price': trade_dict.get('price')})
if strategy_name is not None:
trade_file = str(get_folder_path('data').joinpath('{}_trade.csv'.format(strategy_name)))
append_data(file_name=trade_file, dict_data=trade_dict)
@ -1164,7 +1155,7 @@ class CtaEngine(BaseEngine):
if len(adj_list) > 0:
self.write_log(f'需要对{vt_symbol}进行前复权处理')
for row in adj_list:
row.update({'dividOperateDate': row.get('dividOperateDate')[:10] + ' 09:31:00'})
row.update({'dividOperateDate': row.get('dividOperateDate')[:10] + ' 09:30:00'})
# list -> dataframe, 转换复权日期格式
adj_data = pd.DataFrame(adj_list)
adj_data["dividOperateDate"] = pd.to_datetime(adj_data["dividOperateDate"], format="%Y-%m-%d %H:%M:%S")

View File

@ -34,7 +34,7 @@ from vnpy.trader.utility import (
extract_vt_symbol,
get_csv_last_dt
)
from vnpy.data.common import stock_to_adj
from .back_testing import BackTestingEngine, stock_to_adj
@ -108,7 +108,7 @@ class PortfolioTestingEngine(BackTestingEngine):
if isinstance(last_dt, datetime):
if last_dt.strftime('%Y-%m-%d') < self.test_end_date:
self.write_log(f'加载数据[{vt_symbol}], 使用{fq_name}文件:{fq_bar_file}')
symbol_df = pd.read_csv(bar_file, dtype=data_types)
symbol_df = pd.read_csv(fq_bar_file, dtype=data_types)
# 转换时间str =》 datetime
symbol_df["datetime"] = pd.to_datetime(symbol_df["datetime"], format="%Y-%m-%d %H:%M:%S")
# 设置时间为索引
@ -139,7 +139,7 @@ class PortfolioTestingEngine(BackTestingEngine):
for row in adj_list:
d = row.get('dividOperateDate', "")[0:10]
if len(d) == 10:
row.update({'dividOperateDate': d + ' 09:31:00'})
row.update({'dividOperateDate': d + ' 09:30:00'})
# list -> dataframe, 转换复权日期格式
adj_data = pd.DataFrame(adj_list)
adj_data["dividOperateDate"] = pd.to_datetime(adj_data["dividOperateDate"],

View File

@ -1092,6 +1092,10 @@ class CtaStockTemplate(CtaTemplate):
symbol_tick.bid_volume_5]):
market_ask_volumes = symbol_tick.ask_volume_1 + symbol_tick.ask_volume_2 + symbol_tick.ask_volume_3 + symbol_tick.ask_volume_4 + symbol_tick.ask_volume_5
market_bid_volumes = symbol_tick.bid_volume_1 + symbol_tick.bid_volume_2 + symbol_tick.bid_volume_3 + symbol_tick.bid_volume_4 + symbol_tick.bid_volume_5
# 乘以基本系数
market_ask_volumes = market_ask_volumes * symbol_volume_tick
market_bid_volumes = market_bid_volumes * symbol_volume_tick
org_sell_volume = sell_volume
if market_bid_volumes > 0 and market_ask_volumes > 0 and org_sell_volume >= 2 * symbol_volume_tick:
sell_volume = min(market_bid_volumes / 4, market_ask_volumes / 4, sell_volume)