From db50cc11e45776ac16637091c5f2c1da3302eea4 Mon Sep 17 00:00:00 2001 From: msincenselee Date: Fri, 17 Sep 2021 10:22:57 +0800 Subject: [PATCH] =?UTF-8?q?[bug=20fix]=20=E8=82=A1=E7=A5=A8=E5=BC=95?= =?UTF-8?q?=E6=93=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/app/cta_stock/back_testing.py | 2 +- vnpy/app/cta_stock/engine.py | 15 +++------------ vnpy/app/cta_stock/portfolio_testing.py | 6 +++--- vnpy/app/cta_stock/template.py | 4 ++++ 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/vnpy/app/cta_stock/back_testing.py b/vnpy/app/cta_stock/back_testing.py index 03a0b01c..d6011a5e 100644 --- a/vnpy/app/cta_stock/back_testing.py +++ b/vnpy/app/cta_stock/back_testing.py @@ -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) diff --git a/vnpy/app/cta_stock/engine.py b/vnpy/app/cta_stock/engine.py index 437f1711..452fc6f2 100644 --- a/vnpy/app/cta_stock/engine.py +++ b/vnpy/app/cta_stock/engine.py @@ -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") diff --git a/vnpy/app/cta_stock/portfolio_testing.py b/vnpy/app/cta_stock/portfolio_testing.py index 097eab3d..585dce40 100644 --- a/vnpy/app/cta_stock/portfolio_testing.py +++ b/vnpy/app/cta_stock/portfolio_testing.py @@ -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"], diff --git a/vnpy/app/cta_stock/template.py b/vnpy/app/cta_stock/template.py index 09bd97ed..2414ffa3 100644 --- a/vnpy/app/cta_stock/template.py +++ b/vnpy/app/cta_stock/template.py @@ -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)