From 2aa264f30a22c6bb05e7d99791ee094ec3ec3c9a Mon Sep 17 00:00:00 2001 From: msincenselee Date: Thu, 25 Feb 2021 20:12:18 +0800 Subject: [PATCH] =?UTF-8?q?[update]=20=E6=8F=90=E5=89=8D=E5=88=87=E6=8D=A2?= =?UTF-8?q?=E4=B8=BB=E5=8A=9B=E5=90=88=E7=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- prod/jobs/check_dominat_symbol.py | 8 ++++- vnpy/data/tdx/tdx_future_data.py | 59 +++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/prod/jobs/check_dominat_symbol.py b/prod/jobs/check_dominat_symbol.py index 65ac0a4c..1aebad10 100644 --- a/prod/jobs/check_dominat_symbol.py +++ b/prod/jobs/check_dominat_symbol.py @@ -79,8 +79,14 @@ if __name__ == "__main__": new_mi_symbol = contract_info.get('mi_symbol') new_exchange = contract_info.get('exchange') + # 提前计算满足条件得次主力合约 + next_mi_symbol = get_pre_switch_mi_symbol(contract_info) + if next_mi_symbol > new_mi_symbol: + print(f'使用提前切换得次主力合约{new_mi_symbol} => {next_mi_symbol}') + next_mi_symbol = new_mi_symbol + new_vt_symbol = '.'.join([new_mi_symbol, new_exchange]) - new_full_symbol = contract_info.get('full_symbol', '').upper() + new_full_symbol =get_full_symbol(new_mi_symbol).upper() if full_symbol >= new_full_symbol: print(f'{account_name}策略配置:长合约{full_symbol}, 主力长合约{new_full_symbol},不更新') continue diff --git a/vnpy/data/tdx/tdx_future_data.py b/vnpy/data/tdx/tdx_future_data.py index 16363904..266b6506 100644 --- a/vnpy/data/tdx/tdx_future_data.py +++ b/vnpy/data/tdx/tdx_future_data.py @@ -129,6 +129,61 @@ def convert_cffex_symbol(mi_symbol): return f'{underly_symbol}{next_year_month}' +def get_pre_switch_mi_symbol(d): + """ + 根据配置,获取提前换月得主力合约 + 例如, 主力合约和次主力合约相差30%,即可返回次主力合约 + :param d:{ + "underlying_symbol": "RB", + "mi_symbol": "rb2105", + "full_symbol": "RB2105", + "exchange": "SHFE", + "margin_rate": 0.1, + "symbol_size": 10, + "price_tick": 1.0, + "open_interesting": 1264478, + "symbols": { + "RB2103": 9974, + "RB2104": 10404, + "RB2105": 1264478, + "RB2106": 63782, + "RB2107": 76065, + "RB2108": 19033, + "RB2109": 25809, + "RB2110": 318754, + "RB2111": 101, + "RB2112": 154, + "RB2201": 31221, + "RB2202": 51 + } + } + :return: + """ + full_symbol = d.get('full_symbol') + mi_symbol = d.get('mi_symbol') + symbols = d.get('symbols') + vn_exchange = Exchange(d.get('exchange')) + if vn_exchange in [Exchange.CFFEX]: + return mi_symbol + + # 根据持仓量排倒序 + sorted_symbols = [(k,v) for k, v in sorted(symbols.items(), key=lambda item: item[1], reverse=True)] + if len(sorted_symbols) < 2: + return mi_symbol + # 主力、次主力 + s1, s2 = sorted_symbols[:2] + # 次主力合约是旧合约 + if s1[0] > s2[0]: + return mi_symbol + # 次主力合约超过主力合约得80% + if s1[1] * 0.8 < s2[1]: + next_full_symbol = s2[0] + + # 根据合约全路径、交易所 => 真实合约 + next_mi_symbol = get_real_symbol_by_exchange(next_full_symbol, vn_exchange) + return next_mi_symbol + return mi_symbol + class TdxFutureData(object): exclude_ips = [] @@ -622,8 +677,8 @@ class TdxFutureData(object): self.write_log(f'移除旧版symbols列表') cache_symbols = {} - # 获取 {合约:总量} - new_symbols = {c.get('code'): c.get('ZongLiang') for c in v} + # 获取 {合约:总量}ChiCangLiang + new_symbols = {c.get('code'): c.get('ChiCangLiang') for c in v} # 更新字典 cache_symbols.update(new_symbols)