feat: 港股通赛道——HSI全历史入库+29只港股通池回填+分市场情景判断(A股看上证/港股看恒指)+13个港股自建板块指数+港股行业映射
This commit is contained in:
+20
-9
@@ -275,12 +275,16 @@ _SECTOR_CTX = {}
|
||||
_STOCK_SECTOR = {}
|
||||
|
||||
def prepare_market_context(start_date, end_date):
|
||||
"""大盘指数(sh000001)每日状态: 是否在MA20上、MA20斜率、ROC"""
|
||||
global _MKT_CTX
|
||||
_MKT_CTX = {}
|
||||
bars = prepare_bars('sh000001', start_date, end_date)
|
||||
"""大盘指数每日状态: A股用上证(sh000001),港股用恒生(hkHSI)"""
|
||||
global _MKT_CTX, _MKT_CTX_HK
|
||||
_MKT_CTX = _load_index_ctx('sh000001', start_date, end_date)
|
||||
_MKT_CTX_HK = _load_index_ctx('hkHSI', start_date, end_date)
|
||||
|
||||
def _load_index_ctx(index_code, start_date, end_date):
|
||||
ctx = {}
|
||||
bars = prepare_bars(index_code, start_date, end_date)
|
||||
if not bars:
|
||||
return
|
||||
return ctx
|
||||
for i, b in enumerate(bars):
|
||||
slope = None
|
||||
if i >= 5:
|
||||
@@ -288,11 +292,15 @@ def prepare_market_context(start_date, end_date):
|
||||
if m0 and m1:
|
||||
slope = round((m1 - m0) / m0 * 100, 3)
|
||||
ma20 = b.get('ma20') or 0
|
||||
_MKT_CTX[b['date']] = {
|
||||
ctx[b['date']] = {
|
||||
'above_ma20': (b.get('close') or 0) > ma20 if ma20 > 0 else None,
|
||||
'ma20_slope': slope,
|
||||
'roc': b.get('roc'),
|
||||
}
|
||||
return ctx
|
||||
|
||||
def is_hk_code(code):
|
||||
return len(code) == 5 and code.startswith('0')
|
||||
|
||||
def prepare_sector_context(start_date, end_date):
|
||||
"""行业上下文: sector_index_daily(全历史) 提供板块趋势; sector_snapshots(近期) 补充净流入"""
|
||||
@@ -353,7 +361,10 @@ def prepare_sector_context(start_date, end_date):
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def mkt_ctx(date):
|
||||
def mkt_ctx(date, code=None):
|
||||
"""按市场取大盘情景:港股用恒指,A股用上证"""
|
||||
if code and is_hk_code(code):
|
||||
return _MKT_CTX_HK.get(date, {})
|
||||
return _MKT_CTX.get(date, {})
|
||||
|
||||
def sector_ctx(code, date):
|
||||
@@ -614,9 +625,9 @@ def run_backtest(strategy_version, start_date, end_date, capital=1000000, save=T
|
||||
|
||||
if total_score >= entry_cfg['min_score'] and comp['momentum'] >= entry_cfg['min_momentum']:
|
||||
factors = calc_factors(bars, i)
|
||||
# 附加大盘/行业上下文
|
||||
# 附加大盘/行业上下文(港股用恒指,A股用上证)
|
||||
date = last.get('date')
|
||||
mk = mkt_ctx(date)
|
||||
mk = mkt_ctx(date, code)
|
||||
sc_ctx = sector_ctx(code, date)
|
||||
factors['mkt_above_ma20'] = mk.get('above_ma20')
|
||||
factors['mkt_slope'] = mk.get('ma20_slope')
|
||||
|
||||
Reference in New Issue
Block a user