MAPatternStrategy_v002.py 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  1. # 导入函数库
  2. from jqdata import *
  3. from jqdata import finance
  4. import pandas as pd
  5. import numpy as np
  6. import math
  7. from datetime import date, datetime, timedelta, time
  8. import re
  9. MA_ADDITIONAL_HISTORY_DAYS = 30 # 计算均线时额外获取的交易日数量,用于支持更长周期
  10. # 均线聚合度阈值(基于MA5/MA10/MA20),用于动态调整单标的保证金上限
  11. MA_COMPACTION_THRESHOLDS = {
  12. 'tight': 0.0045,
  13. 'balanced': 0.0073,
  14. 'loose': 0.0116
  15. }
  16. # 顺势交易策略 v001
  17. # 基于均线走势(前提条件)+ K线形态(开盘价差、当天价差)的期货交易策略
  18. #
  19. # 核心逻辑:
  20. # 1. 开盘时检查均线走势(MA30<=MA20<=MA10<=MA5为多头,反之为空头)
  21. # 2. 检查开盘价差是否符合方向要求(多头>=0.5%,空头<=-0.5%)
  22. # 3. 14:35和14:55检查当天价差(多头>0,空头<0),满足条件则开仓
  23. # 4. 应用固定止损和动态追踪止盈
  24. # 5. 自动换月移仓
  25. # 设置以便完整打印 DataFrame
  26. pd.set_option('display.max_rows', None)
  27. pd.set_option('display.max_columns', None)
  28. pd.set_option('display.width', None)
  29. pd.set_option('display.max_colwidth', 20)
  30. ## 初始化函数,设定基准等等
  31. def initialize(context):
  32. # 设定沪深300作为基准
  33. set_benchmark('000300.XSHG')
  34. # 开启动态复权模式(真实价格)
  35. set_option('use_real_price', True)
  36. # 输出内容到日志
  37. log.info('=' * 60)
  38. log.info('均线形态交易策略 v001 初始化开始')
  39. log.info('策略类型: 均线走势 + K线形态')
  40. log.info('=' * 60)
  41. ### 期货相关设定 ###
  42. # 设定账户为金融账户
  43. set_subportfolios([SubPortfolioConfig(cash=context.portfolio.starting_cash, type='index_futures')])
  44. # 期货类每笔交易时的手续费是: 买入时万分之0.23,卖出时万分之0.23,平今仓为万分之23
  45. set_order_cost(OrderCost(open_commission=0.000023, close_commission=0.000023, close_today_commission=0.0023), type='index_futures')
  46. # 设置期货交易的滑点
  47. set_slippage(StepRelatedSlippage(2))
  48. # 初始化全局变量
  49. g.reserve_percentage = 0.2 # 预留资金比例
  50. g.financial_reserve_amount = 180000 # 金融期货预留资金
  51. g.max_margin_per_position = 30000 # 单个标的最大持仓保证金(元)
  52. g.base_max_margin_per_position = g.max_margin_per_position # 记录基础保证金上限,便于动态调整
  53. g.ma_compaction_thresholds = MA_COMPACTION_THRESHOLDS.copy()
  54. # 均线策略参数
  55. g.ma_periods = [5, 10, 20, 30, 60] # 均线周期(新增MA60)
  56. g.ma_cross_periods = [5, 10, 20, 30] # 参与均线穿越判断的均线周期
  57. g.ma_historical_days = 60 + MA_ADDITIONAL_HISTORY_DAYS # 额外增加30天,确保MA60计算稳定
  58. g.ma_open_gap_threshold = 0.001 # 方案1开盘价差阈值(0.2%)
  59. g.ma_pattern_lookback_days = 10 # 历史均线模式一致性检查的天数
  60. g.ma_pattern_consistency_threshold = 0.8 # 历史均线模式一致性阈值(80%)
  61. g.check_intraday_spread = False # 是否检查日内价差(True: 检查, False: 跳过)
  62. g.ma_proximity_min_threshold = 8 # MA5与MA10贴近计数和的最低阈值
  63. g.ma_pattern_extreme_days_threshold = 4 # 极端趋势天数阈值
  64. g.ma_distribution_lookback_days = 5 # MA5分布过滤回溯天数
  65. g.ma_distribution_min_ratio = 0.4 # MA5分布满足比例阈值
  66. g.enable_ma_distribution_filter = True # 是否启用MA5分布过滤
  67. g.ma_cross_threshold = 1 # 均线穿越数量阈值
  68. g.enable_open_gap_filter = True # 是否启用开盘价差过滤
  69. # 均线价差策略方案选择
  70. g.ma_gap_strategy_mode = 3 # 策略模式选择(1: 原方案, 2: 新方案, 3: 方案3)
  71. g.ma_open_gap_threshold2 = 0.001 # 方案2开盘价差阈值(0.2%)
  72. g.ma_intraday_threshold_scheme2 = 0.005 # 方案2日内变化阈值(0.5%)
  73. # 止损止盈策略参数
  74. g.fixed_stop_loss_rate = 0.01 # 固定止损比率(1%)
  75. g.ma_offset_ratio_normal = 0.003 # 均线跟踪止盈常规偏移量(0.3%)
  76. g.ma_offset_ratio_close = 0.01 # 均线跟踪止盈收盘前偏移量(1%)
  77. g.days_for_adjustment = 4 # 持仓天数调整阈值
  78. # 输出策略参数
  79. log.info("均线形态策略参数:")
  80. log.info(f" 均线周期: {g.ma_periods}")
  81. log.info(f" 均线穿越判定周期: {g.ma_cross_periods}")
  82. log.info(f" 策略模式: 方案{g.ma_gap_strategy_mode}")
  83. log.info(f" 方案1开盘价差阈值: {g.ma_open_gap_threshold:.1%}")
  84. log.info(f" 方案2开盘价差阈值: {g.ma_open_gap_threshold2:.1%}")
  85. log.info(f" 方案2日内变化阈值: {g.ma_intraday_threshold_scheme2:.1%}")
  86. log.info(f" 历史均线模式检查天数: {g.ma_pattern_lookback_days}天")
  87. log.info(f" 历史均线模式一致性阈值: {g.ma_pattern_consistency_threshold:.1%}")
  88. log.info(f" 极端趋势天数阈值: {g.ma_pattern_extreme_days_threshold}")
  89. log.info(f" 均线贴近计数阈值: {g.ma_proximity_min_threshold}")
  90. log.info(f" MA5分布过滤天数: {g.ma_distribution_lookback_days}")
  91. log.info(f" MA5分布最低比例: {g.ma_distribution_min_ratio:.0%}")
  92. log.info(f" 启用MA5分布过滤: {g.enable_ma_distribution_filter}")
  93. log.info(f" 是否检查日内价差: {g.check_intraday_spread}")
  94. log.info(f" 均线穿越阈值: {g.ma_cross_threshold}")
  95. log.info(f" 是否启用开盘价差过滤: {g.enable_open_gap_filter}")
  96. log.info(f" 固定止损: {g.fixed_stop_loss_rate:.1%}")
  97. log.info(f" 均线跟踪止盈常规偏移: {g.ma_offset_ratio_normal:.1%}")
  98. log.info(f" 均线跟踪止盈收盘前偏移: {g.ma_offset_ratio_close:.1%}")
  99. log.info(f" 持仓天数调整阈值: {g.days_for_adjustment}天")
  100. log.info(f" 预留资金比例: {g.reserve_percentage:.0%}")
  101. log.info(f" 金融期货预留资金: {g.financial_reserve_amount:.0f}")
  102. # 期货品种完整配置字典
  103. g.futures_config = {
  104. # 贵金属
  105. 'AU': {'has_night_session': True, 'margin_rate': {'long': 0.21, 'short': 0.21}, 'multiplier': 1000, 'trading_start_time': '21:00'},
  106. 'AG': {'has_night_session': True, 'margin_rate': {'long': 0.22, 'short': 0.22}, 'multiplier': 15, 'trading_start_time': '21:00'},
  107. # 有色金属
  108. 'CU': {'has_night_session': True, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 5, 'trading_start_time': '21:00'},
  109. 'AL': {'has_night_session': True, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 5, 'trading_start_time': '21:00'},
  110. 'ZN': {'has_night_session': True, 'margin_rate': {'long': 0.14, 'short': 0.14}, 'multiplier': 5, 'trading_start_time': '21:00'},
  111. 'PB': {'has_night_session': True, 'margin_rate': {'long': 0.14, 'short': 0.14}, 'multiplier': 5, 'trading_start_time': '21:00'},
  112. 'NI': {'has_night_session': True, 'margin_rate': {'long': 0.16, 'short': 0.16}, 'multiplier': 1, 'trading_start_time': '21:00'},
  113. 'SN': {'has_night_session': True, 'margin_rate': {'long': 0.17, 'short': 0.17}, 'multiplier': 1, 'trading_start_time': '21:00'},
  114. 'SS': {'has_night_session': True, 'margin_rate': {'long': 0.07, 'short': 0.07}, 'multiplier': 5, 'trading_start_time': '21:00'},
  115. # 黑色系
  116. 'RB': {'has_night_session': True, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 10, 'trading_start_time': '21:00'},
  117. 'HC': {'has_night_session': True, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 10, 'trading_start_time': '21:00'},
  118. 'I': {'has_night_session': True, 'margin_rate': {'long': 0.16, 'short': 0.16}, 'multiplier': 100, 'trading_start_time': '21:00'},
  119. 'JM': {'has_night_session': True, 'margin_rate': {'long': 0.17, 'short': 0.17}, 'multiplier': 100, 'trading_start_time': '21:00'},
  120. 'J': {'has_night_session': True, 'margin_rate': {'long': 0.25, 'short': 0.25}, 'multiplier': 60, 'trading_start_time': '21:00'},
  121. # 能源化工
  122. 'SP': {'has_night_session': True, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 10, 'trading_start_time': '21:00'},
  123. 'FU': {'has_night_session': True, 'margin_rate': {'long': 0.16, 'short': 0.16}, 'multiplier': 10, 'trading_start_time': '21:00'},
  124. 'BU': {'has_night_session': True, 'margin_rate': {'long': 0.17, 'short': 0.17}, 'multiplier': 10, 'trading_start_time': '21:00'},
  125. 'RU': {'has_night_session': True, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 10, 'trading_start_time': '21:00'},
  126. 'BR': {'has_night_session': True, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 5, 'trading_start_time': '21:00'},
  127. 'SC': {'has_night_session': True, 'margin_rate': {'long': 0.17, 'short': 0.17}, 'multiplier': 1000, 'trading_start_time': '21:00'},
  128. 'NR': {'has_night_session': True, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 10, 'trading_start_time': '21:00'},
  129. 'LU': {'has_night_session': True, 'margin_rate': {'long': 0.17, 'short': 0.17}, 'multiplier': 10, 'trading_start_time': '21:00'},
  130. 'LC': {'has_night_session': False, 'margin_rate': {'long': 0.16, 'short': 0.16}, 'multiplier': 1, 'trading_start_time': '09:00'},
  131. # 化工
  132. 'FG': {'has_night_session': True, 'margin_rate': {'long': 0.16, 'short': 0.16}, 'multiplier': 20, 'trading_start_time': '21:00'},
  133. 'TA': {'has_night_session': True, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 5, 'trading_start_time': '21:00'},
  134. 'MA': {'has_night_session': True, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 10, 'trading_start_time': '21:00'},
  135. 'SA': {'has_night_session': True, 'margin_rate': {'long': 0.14, 'short': 0.14}, 'multiplier': 20, 'trading_start_time': '21:00'},
  136. 'L': {'has_night_session': True, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 5, 'trading_start_time': '21:00'},
  137. 'V': {'has_night_session': True, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 5, 'trading_start_time': '21:00'},
  138. 'EG': {'has_night_session': True, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 10, 'trading_start_time': '21:00'},
  139. 'PP': {'has_night_session': True, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 5, 'trading_start_time': '21:00'},
  140. 'EB': {'has_night_session': True, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 5, 'trading_start_time': '21:00'},
  141. 'PG': {'has_night_session': True, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 20, 'trading_start_time': '21:00'},
  142. 'PX': {'has_night_session': True, 'margin_rate': {'long': 0.1, 'short': 0.1}, 'multiplier': 5, 'trading_start_time': '21:00'},
  143. # 农产品
  144. 'RM': {'has_night_session': True, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 10, 'trading_start_time': '21:00'},
  145. 'OI': {'has_night_session': True, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 10, 'trading_start_time': '21:00'},
  146. 'CF': {'has_night_session': True, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 5, 'trading_start_time': '21:00'},
  147. 'SR': {'has_night_session': True, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 10, 'trading_start_time': '21:00'},
  148. 'PF': {'has_night_session': True, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 5, 'trading_start_time': '21:00'},
  149. 'C': {'has_night_session': True, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 10, 'trading_start_time': '21:00'},
  150. 'CS': {'has_night_session': True, 'margin_rate': {'long': 0.11, 'short': 0.11}, 'multiplier': 10, 'trading_start_time': '21:00'},
  151. 'CY': {'has_night_session': True, 'margin_rate': {'long': 0.11, 'short': 0.11}, 'multiplier': 5, 'trading_start_time': '21:00'},
  152. 'A': {'has_night_session': True, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 10, 'trading_start_time': '21:00'},
  153. 'B': {'has_night_session': True, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 10, 'trading_start_time': '21:00'},
  154. 'M': {'has_night_session': True, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 10, 'trading_start_time': '21:00'},
  155. 'Y': {'has_night_session': True, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 10, 'trading_start_time': '21:00'},
  156. 'P': {'has_night_session': True, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 10, 'trading_start_time': '21:00'},
  157. # 无夜盘品种
  158. 'IF': {'has_night_session': False, 'margin_rate': {'long': 0.17, 'short': 0.17}, 'multiplier': 300, 'trading_start_time': '09:30', 'is_financial': True},
  159. 'IH': {'has_night_session': False, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 300, 'trading_start_time': '09:30', 'is_financial': True},
  160. 'IC': {'has_night_session': False, 'margin_rate': {'long': 0.17, 'short': 0.17}, 'multiplier': 200, 'trading_start_time': '09:30', 'is_financial': True},
  161. 'IM': {'has_night_session': False, 'margin_rate': {'long': 0.17, 'short': 0.17}, 'multiplier': 200, 'trading_start_time': '09:30', 'is_financial': True},
  162. 'AP': {'has_night_session': False, 'margin_rate': {'long': 0.16, 'short': 0.16}, 'multiplier': 10, 'trading_start_time': '09:00'},
  163. 'CJ': {'has_night_session': False, 'margin_rate': {'long': 0.14, 'short': 0.14}, 'multiplier': 5, 'trading_start_time': '09:00'},
  164. 'PK': {'has_night_session': False, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 5, 'trading_start_time': '09:00'},
  165. 'JD': {'has_night_session': False, 'margin_rate': {'long': 0.11, 'short': 0.11}, 'multiplier': 10, 'trading_start_time': '09:00'},
  166. 'LH': {'has_night_session': False, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 16, 'trading_start_time': '09:00'},
  167. 'T': {'has_night_session': False, 'margin_rate': {'long': 0.03, 'short': 0.03}, 'multiplier': 1000000, 'trading_start_time': '09:30', 'is_financial': True},
  168. 'PS': {'has_night_session': False, 'margin_rate': {'long': 0.16, 'short': 0.16}, 'multiplier': 3, 'trading_start_time': '09:00'},
  169. 'UR': {'has_night_session': False, 'margin_rate': {'long': 0.14, 'short': 0.14}, 'multiplier': 20, 'trading_start_time': '09:00'},
  170. 'MO': {'has_night_session': True, 'margin_rate': {'long': 0.17, 'short': 0.17}, 'multiplier': 100, 'trading_start_time': '21:00'},
  171. # 'LF': {'has_night_session': False, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 1, 'trading_start_time': '09:30'},
  172. 'HO': {'has_night_session': False, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 100, 'trading_start_time': '09:30'},
  173. # 'LR': {'has_night_session': True, 'margin_rate': {'long': 0.21, 'short': 0.21}, 'multiplier': 20, 'trading_start_time': '21:00'},
  174. 'LG': {'has_night_session': True, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 90, 'trading_start_time': '21:00'},
  175. # 'FB': {'has_night_session': True, 'margin_rate': {'long': 0.14, 'short': 0.14}, 'multiplier': 10, 'trading_start_time': '21:00'},
  176. # 'PM': {'has_night_session': True, 'margin_rate': {'long': 0.2, 'short': 0.2}, 'multiplier': 50, 'trading_start_time': '21:00'},
  177. 'EC': {'has_night_session': False, 'margin_rate': {'long': 0.23, 'short': 0.23}, 'multiplier': 50, 'trading_start_time': '09:00'},
  178. # 'RR': {'has_night_session': True, 'margin_rate': {'long': 0.11, 'short': 0.11}, 'multiplier': 10, 'trading_start_time': '21:00'},
  179. # 'OP': {'has_night_session': False, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 40, 'trading_start_time': '09:00'},
  180. # 'IO': {'has_night_session': True, 'margin_rate': {'long': 0.17, 'short': 0.17}, 'multiplier': 1, 'trading_start_time': '21:00'},
  181. 'BC': {'has_night_session': True, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 5, 'trading_start_time': '21:00'},
  182. # 'WH': {'has_night_session': False, 'margin_rate': {'long': 0.2, 'short': 0.2}, 'multiplier': 20, 'trading_start_time': '09:00'},
  183. 'SH': {'has_night_session': True, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 30, 'trading_start_time': '21:00'},
  184. # 'RI': {'has_night_session': False, 'margin_rate': {'long': 0.21, 'short': 0.21}, 'multiplier': 20, 'trading_start_time': '09:00'},
  185. 'TS': {'has_night_session': False, 'margin_rate': {'long': 0.015, 'short': 0.015}, 'multiplier': 2000000, 'trading_start_time': '09:30', 'is_financial': True},
  186. # 'JR': {'has_night_session': False, 'margin_rate': {'long': 0.21, 'short': 0.21}, 'multiplier': 20, 'trading_start_time': '09:00'},
  187. 'AD': {'has_night_session': False, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 10, 'trading_start_time': '09:00'},
  188. # 'BB': {'has_night_session': False, 'margin_rate': {'long': 0.19, 'short': 0.19}, 'multiplier': 500, 'trading_start_time': '09:00'},
  189. 'PL': {'has_night_session': False, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 20, 'trading_start_time': '09:00'},
  190. # 'RS': {'has_night_session': False, 'margin_rate': {'long': 0.26, 'short': 0.26}, 'multiplier': 10, 'trading_start_time': '09:00'},
  191. 'SI': {'has_night_session': False, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 5, 'trading_start_time': '09:00'},
  192. # 'ZC': {'has_night_session': True, 'margin_rate': {'long': 0.56, 'short': 0.56}, 'multiplier': 100, 'trading_start_time': '21:00'},
  193. 'SM': {'has_night_session': False, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 5, 'trading_start_time': '09:00'},
  194. 'AO': {'has_night_session': True, 'margin_rate': {'long': 0.17, 'short': 0.17}, 'multiplier': 20, 'trading_start_time': '21:00'},
  195. 'TL': {'has_night_session': False, 'margin_rate': {'long': 0.045, 'short': 0.045}, 'multiplier': 1000000, 'trading_start_time': '09:00', 'is_financial': True},
  196. 'SF': {'has_night_session': False, 'margin_rate': {'long': 0.14, 'short': 0.14}, 'multiplier': 5, 'trading_start_time': '09:00'},
  197. # 'WR': {'has_night_session': False, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 10, 'trading_start_time': '09:00'},
  198. 'PR': {'has_night_session': True, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 15, 'trading_start_time': '21:00'},
  199. 'TF': {'has_night_session': False, 'margin_rate': {'long': 0.022, 'short': 0.022}, 'multiplier': 1000000, 'trading_start_time': '09:00', 'is_financial': True},
  200. # 'VF': {'has_night_session': False, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 1, 'trading_start_time': '09:00'},
  201. 'BZ': {'has_night_session': False, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 30, 'trading_start_time': '09:00'},
  202. }
  203. # 策略品种选择策略配置
  204. # 方案1:全品种策略 - 考虑所有配置的期货品种
  205. g.strategy_focus_symbols = ['SC'] # 空列表表示考虑所有品种
  206. # 方案2:精选品种策略 - 只交易流动性较好的特定品种(如需使用请取消下行注释)
  207. # g.strategy_focus_symbols = ['RM', 'CJ', 'CY', 'JD', 'L', 'LC', 'SF', 'SI']
  208. log.info(f"品种选择策略: {'全品种策略(覆盖所有配置品种)' if not g.strategy_focus_symbols else '精选品种策略(' + str(len(g.strategy_focus_symbols)) + '个品种)'}")
  209. # 交易记录和数据存储
  210. g.trade_history = {} # 持仓记录 {symbol: {'entry_price': xxx, 'direction': xxx, ...}}
  211. g.daily_ma_candidates = {} # 通过均线和开盘价差检查的候选品种 {symbol: {'direction': 'long'/'short', 'open_price': xxx, ...}}
  212. g.today_trades = [] # 当日交易记录
  213. g.excluded_contracts = {} # 每日排除的合约缓存 {dominant_future: {'reason': 'ma_trend'/'open_gap', 'trading_day': xxx}}
  214. g.ma_checked_underlyings = {} # 记录各品种在交易日的均线检查状态 {symbol: trading_day}
  215. g.last_ma_trading_day = None # 最近一次均线检查所属交易日
  216. # 夜盘禁止操作标志
  217. g.night_session_blocked = False # 标记是否禁止当晚操作
  218. g.night_session_blocked_trading_day = None # 记录被禁止的交易日
  219. # 定时任务设置
  220. # 夜盘开始(21:05) - 均线和开盘价差检查
  221. run_daily(check_ma_trend_and_open_gap, time='21:05:00', reference_security='IF1808.CCFX')
  222. # 日盘开始 - 均线和开盘价差检查
  223. run_daily(check_ma_trend_and_open_gap, time='09:05:00', reference_security='IF1808.CCFX')
  224. run_daily(check_ma_trend_and_open_gap, time='09:35:00', reference_security='IF1808.CCFX')
  225. # 夜盘开仓和止损止盈检查
  226. run_daily(check_open_and_stop, time='21:05:00', reference_security='IF1808.CCFX')
  227. run_daily(check_open_and_stop, time='21:35:00', reference_security='IF1808.CCFX')
  228. run_daily(check_open_and_stop, time='22:05:00', reference_security='IF1808.CCFX')
  229. run_daily(check_open_and_stop, time='22:35:00', reference_security='IF1808.CCFX')
  230. # 日盘开仓和止损止盈检查
  231. run_daily(check_open_and_stop, time='09:05:00', reference_security='IF1808.CCFX')
  232. run_daily(check_open_and_stop, time='09:35:00', reference_security='IF1808.CCFX')
  233. run_daily(check_open_and_stop, time='10:05:00', reference_security='IF1808.CCFX')
  234. run_daily(check_open_and_stop, time='10:35:00', reference_security='IF1808.CCFX')
  235. run_daily(check_open_and_stop, time='11:05:00', reference_security='IF1808.CCFX')
  236. run_daily(check_open_and_stop, time='11:25:00', reference_security='IF1808.CCFX')
  237. run_daily(check_open_and_stop, time='13:35:00', reference_security='IF1808.CCFX')
  238. run_daily(check_open_and_stop, time='14:05:00', reference_security='IF1808.CCFX')
  239. run_daily(check_open_and_stop, time='14:35:00', reference_security='IF1808.CCFX')
  240. run_daily(check_open_and_stop, time='14:55:00', reference_security='IF1808.CCFX')
  241. run_daily(check_ma_trailing_reactivation, time='14:55:00', reference_security='IF1808.CCFX')
  242. # 收盘后
  243. run_daily(after_market_close, time='15:30:00', reference_security='IF1808.CCFX')
  244. log.info('=' * 60)
  245. ############################ 主程序执行函数 ###################################
  246. def get_current_trading_day(current_dt):
  247. """根据当前时间推断对应的期货交易日"""
  248. current_date = current_dt.date()
  249. current_time = current_dt.time()
  250. trade_days = get_trade_days(end_date=current_date, count=1)
  251. if trade_days and trade_days[0] == current_date:
  252. trading_day = current_date
  253. else:
  254. next_days = get_trade_days(start_date=current_date, count=1)
  255. trading_day = next_days[0] if next_days else current_date
  256. if current_time >= time(20, 59):
  257. next_trade_days = get_trade_days(start_date=trading_day, count=2)
  258. if len(next_trade_days) >= 2:
  259. return next_trade_days[1]
  260. if len(next_trade_days) == 1:
  261. return next_trade_days[0]
  262. return trading_day
  263. def normalize_trade_day_value(value):
  264. """将交易日对象统一转换为 datetime.date"""
  265. if isinstance(value, date) and not isinstance(value, datetime):
  266. return value
  267. if isinstance(value, datetime):
  268. return value.date()
  269. if hasattr(value, 'to_pydatetime'):
  270. return value.to_pydatetime().date()
  271. try:
  272. return pd.Timestamp(value).date()
  273. except Exception:
  274. return value
  275. def check_ma_trend_and_open_gap(context):
  276. """阶段一:开盘时均线走势和开盘价差检查(一天一次)"""
  277. log.info("=" * 60)
  278. current_trading_day = get_current_trading_day(context.current_dt)
  279. log.info(f"执行均线走势和开盘价差检查 - 时间: {context.current_dt}, 交易日: {current_trading_day}")
  280. log.info("=" * 60)
  281. # 换月移仓检查(在所有部分之前)
  282. position_auto_switch(context)
  283. # ==================== 第一部分:基础数据获取 ====================
  284. # 步骤1:交易日检查和缓存清理
  285. if g.last_ma_trading_day != current_trading_day:
  286. if g.excluded_contracts:
  287. log.info(f"交易日切换至 {current_trading_day},清空上一交易日的排除缓存")
  288. g.excluded_contracts = {}
  289. g.ma_checked_underlyings = {}
  290. g.last_ma_trading_day = current_trading_day
  291. # 步骤2:获取当前时间和筛选可交易品种
  292. current_time = str(context.current_dt.time())[:5] # HH:MM格式
  293. focus_symbols = g.strategy_focus_symbols if g.strategy_focus_symbols else list(g.futures_config.keys())
  294. tradable_symbols = []
  295. # 根据当前时间确定可交易的时段
  296. # 21:05 -> 仅接受21:00开盘的合约
  297. # 09:05 -> 接受09:00或21:00开盘的合约
  298. # 09:35 -> 接受所有时段(21:00, 09:00, 09:30)的合约
  299. for symbol in focus_symbols:
  300. trading_start_time = get_futures_config(symbol, 'trading_start_time', '09:05')
  301. should_trade = False
  302. if current_time == '21:05':
  303. should_trade = trading_start_time.startswith('21:00')
  304. elif current_time == '09:05':
  305. should_trade = trading_start_time.startswith('21:00') or trading_start_time.startswith('09:00')
  306. elif current_time == '09:35':
  307. should_trade = True
  308. if should_trade:
  309. tradable_symbols.append(symbol)
  310. if not tradable_symbols:
  311. log.info(f"当前时间 {current_time} 无品种开盘,跳过检查")
  312. return
  313. log.info(f"当前时间 {current_time} 开盘品种: {tradable_symbols}")
  314. # 步骤3:对每个品种循环处理
  315. for symbol in tradable_symbols:
  316. # 步骤3.1:检查是否已处理过
  317. if g.ma_checked_underlyings.get(symbol) == current_trading_day:
  318. log.info(f"{symbol} 已在交易日 {current_trading_day} 完成均线检查,跳过本次执行")
  319. continue
  320. try:
  321. g.ma_checked_underlyings[symbol] = current_trading_day
  322. # 步骤3.2:获取主力合约
  323. dominant_future = get_dominant_future(symbol)
  324. if not dominant_future:
  325. log.info(f"{symbol} 未找到主力合约,跳过")
  326. continue
  327. # 步骤3.3:检查排除缓存
  328. if dominant_future in g.excluded_contracts:
  329. excluded_info = g.excluded_contracts[dominant_future]
  330. if excluded_info['trading_day'] == current_trading_day:
  331. continue
  332. else:
  333. # 新的一天,从缓存中移除(会在after_market_close统一清理,这里也做兜底)
  334. del g.excluded_contracts[dominant_future]
  335. # 步骤3.4:检查是否已有持仓
  336. if check_symbol_prefix_match(dominant_future, context, set(g.trade_history.keys())):
  337. log.info(f"{symbol} 已有持仓,跳过")
  338. continue
  339. # 步骤3.5:获取历史数据和前一交易日数据(合并优化)
  340. # 获取历史数据(需要足够计算MA30)
  341. historical_data = get_price(dominant_future, end_date=context.current_dt,
  342. frequency='1d', fields=['open', 'close', 'high', 'low'],
  343. count=g.ma_historical_days)
  344. if historical_data is None or len(historical_data) < max(g.ma_periods):
  345. log.info(f"{symbol} 历史数据不足,跳过")
  346. continue
  347. # 获取前一交易日并在历史数据中匹配
  348. previous_trade_days = get_trade_days(end_date=current_trading_day, count=2)
  349. previous_trade_days = [normalize_trade_day_value(d) for d in previous_trade_days]
  350. previous_trading_day = None
  351. if len(previous_trade_days) >= 2:
  352. previous_trading_day = previous_trade_days[-2]
  353. elif len(previous_trade_days) == 1 and previous_trade_days[0] < current_trading_day:
  354. previous_trading_day = previous_trade_days[0]
  355. if previous_trading_day is None:
  356. log.info(f"{symbol} 无法确定前一交易日,跳过")
  357. continue
  358. # 在历史数据中匹配前一交易日
  359. historical_dates = historical_data.index.date
  360. match_indices = np.where(historical_dates == previous_trading_day)[0]
  361. if len(match_indices) == 0:
  362. earlier_indices = np.where(historical_dates < previous_trading_day)[0]
  363. if len(earlier_indices) == 0:
  364. log.info(f"{symbol} 历史数据缺少 {previous_trading_day} 之前的记录,跳过")
  365. continue
  366. match_indices = [earlier_indices[-1]]
  367. # 提取截至前一交易日的数据,并一次性提取所有需要的字段
  368. data_upto_yesterday = historical_data.iloc[:match_indices[-1] + 1]
  369. yesterday_data = data_upto_yesterday.iloc[-1]
  370. yesterday_close = yesterday_data['close']
  371. yesterday_open = yesterday_data['open']
  372. # 步骤3.6:获取当前价格数据
  373. current_data = get_current_data()[dominant_future]
  374. today_open = current_data.day_open
  375. # ==================== 第二部分:核心指标计算 ====================
  376. # 步骤4:计算均线相关指标(合并优化)
  377. ma_values = calculate_ma_values(data_upto_yesterday, g.ma_periods)
  378. ma_proximity_counts = calculate_ma_proximity_counts(data_upto_yesterday, g.ma_periods, g.ma_pattern_lookback_days)
  379. ma_compaction = calculate_ma_compaction_from_values(ma_values, periods=[5, 10, 20])
  380. log.info(f"{symbol}({dominant_future}) 均线检查:")
  381. log.info(f" 均线贴近统计: {ma_proximity_counts}")
  382. if ma_compaction is not None:
  383. log.info(f" 均线聚合度(MA5/MA10/MA20): {ma_compaction:.4f}")
  384. if ma_compaction > g.ma_compaction_thresholds['loose']:
  385. log.info(
  386. f" {symbol}({dominant_future}) ✗ 均线聚合度 {ma_compaction:.4f} 超过阈值 "
  387. f"{g.ma_compaction_thresholds['loose']:.4f},跳过"
  388. )
  389. add_to_excluded_contracts(dominant_future, 'ma_compaction', current_trading_day)
  390. continue
  391. else:
  392. log.info(" 均线聚合度(MA5/MA10/MA20) 无法计算,使用默认值")
  393. # 检查均线贴近计数
  394. proximity_sum = ma_proximity_counts.get('MA5', 0) + ma_proximity_counts.get('MA10', 0)
  395. if proximity_sum < g.ma_proximity_min_threshold:
  396. log.info(f" {symbol}({dominant_future}) ✗ 均线贴近计数不足,MA5+MA10={proximity_sum} < {g.ma_proximity_min_threshold},跳过")
  397. add_to_excluded_contracts(dominant_future, 'ma_proximity', current_trading_day)
  398. continue
  399. # 步骤5:计算极端趋势天数
  400. extreme_above_count, extreme_below_count = calculate_extreme_trend_days(
  401. data_upto_yesterday,
  402. g.ma_periods,
  403. g.ma_pattern_lookback_days
  404. )
  405. extreme_total = extreme_above_count + extreme_below_count
  406. min_extreme = min(extreme_above_count, extreme_below_count)
  407. filter_threshold = max(2, g.ma_pattern_extreme_days_threshold)
  408. log.info(
  409. f" 极端趋势天数统计: 收盘在所有均线上方 {extreme_above_count} 天, 收盘在所有均线下方 {extreme_below_count} 天, "
  410. f"合计 {extreme_total} 天, min(A,B)={min_extreme} (过滤阈值: {filter_threshold})"
  411. )
  412. if extreme_above_count > 0 and extreme_below_count > 0 and min_extreme >= filter_threshold:
  413. log.info(
  414. f" {symbol}({dominant_future}) ✗ 极端趋势多空同时出现且 min(A,B)={min_extreme} ≥ {filter_threshold},跳过"
  415. )
  416. add_to_excluded_contracts(dominant_future, 'ma_extreme_trend', current_trading_day)
  417. continue
  418. # 步骤6:判断均线走势
  419. direction = None
  420. if check_ma_pattern(ma_values, 'long'):
  421. direction = 'long'
  422. elif check_ma_pattern(ma_values, 'short'):
  423. direction = 'short'
  424. else:
  425. add_to_excluded_contracts(dominant_future, 'ma_trend', current_trading_day)
  426. continue
  427. # 步骤7:检查MA5分布过滤
  428. if g.enable_ma_distribution_filter:
  429. distribution_passed, distribution_stats = check_ma5_distribution_filter(
  430. data_upto_yesterday,
  431. g.ma_distribution_lookback_days,
  432. direction,
  433. g.ma_distribution_min_ratio
  434. )
  435. log.info(
  436. f" MA5分布过滤: 方向 {direction}, 有效天数 "
  437. f"{distribution_stats['valid_days']}/{distribution_stats['lookback_days']},"
  438. f"满足天数 {distribution_stats['qualified_days']}/{distribution_stats['required_days']}"
  439. )
  440. if not distribution_passed:
  441. insufficiency = distribution_stats['valid_days'] < distribution_stats['lookback_days']
  442. reason = "有效数据不足" if insufficiency else "满足天数不足"
  443. log.info(
  444. f" {symbol}({dominant_future}) ✗ MA5分布过滤未通过({reason})"
  445. )
  446. add_to_excluded_contracts(dominant_future, 'ma5_distribution', current_trading_day)
  447. continue
  448. # 步骤8:检查历史均线模式一致性
  449. consistency_passed, consistency_ratio = check_historical_ma_pattern_consistency(
  450. historical_data, direction, g.ma_pattern_lookback_days, g.ma_pattern_consistency_threshold
  451. )
  452. if not consistency_passed:
  453. log.info(f" {symbol}({dominant_future}) ✗ 历史均线模式一致性不足 "
  454. f"({consistency_ratio:.1%} < {g.ma_pattern_consistency_threshold:.1%}),跳过")
  455. add_to_excluded_contracts(dominant_future, 'ma_consistency', current_trading_day)
  456. continue
  457. else:
  458. log.info(f" {symbol}({dominant_future}) ✓ 历史均线模式一致性检查通过 "
  459. f"({consistency_ratio:.1%} >= {g.ma_pattern_consistency_threshold:.1%})")
  460. # 步骤9:检查开盘价差(可配置开关)
  461. if g.enable_open_gap_filter:
  462. gap_check_passed = check_open_gap_filter(
  463. symbol=symbol,
  464. dominant_future=dominant_future,
  465. direction=direction,
  466. yesterday_close=yesterday_close,
  467. today_open=today_open,
  468. current_trading_day=current_trading_day
  469. )
  470. if not gap_check_passed:
  471. continue
  472. else:
  473. log.info(" 已关闭开盘价差过滤(enable_open_gap_filter=False),跳过该检查")
  474. # 步骤10:将通过检查的品种加入候选列表
  475. g.daily_ma_candidates[dominant_future] = {
  476. 'symbol': symbol,
  477. 'direction': direction,
  478. 'open_price': today_open,
  479. 'yesterday_close': yesterday_close,
  480. 'yesterday_open': yesterday_open,
  481. 'ma_values': ma_values,
  482. 'ma_compaction': ma_compaction
  483. }
  484. log.info(f" ✓✓ {symbol} 通过均线和开盘价差检查,加入候选列表")
  485. except Exception as e:
  486. g.ma_checked_underlyings.pop(symbol, None)
  487. log.warning(f"{symbol} 检查时出错: {str(e)}")
  488. continue
  489. log.info(f"候选列表更新完成,当前候选品种: {list(g.daily_ma_candidates.keys())}")
  490. log.info("=" * 60)
  491. def check_open_and_stop(context):
  492. """统一的开仓和止损止盈检查函数"""
  493. # 先检查换月移仓
  494. log.info("=" * 60)
  495. current_trading_day = get_current_trading_day(context.current_dt)
  496. log.info(f"执行开仓和止损止盈检查 - 时间: {context.current_dt}, 交易日: {current_trading_day}")
  497. log.info("=" * 60)
  498. log.info(f"先检查换月:")
  499. position_auto_switch(context)
  500. # 获取当前时间
  501. current_time = str(context.current_dt.time())[:2]
  502. # 判断是否为夜盘时间
  503. is_night_session = (current_time in ['21', '22', '23', '00', '01', '02'])
  504. # 检查是否禁止当晚操作
  505. if is_night_session and g.night_session_blocked:
  506. blocked_trading_day = normalize_trade_day_value(g.night_session_blocked_trading_day) if g.night_session_blocked_trading_day else None
  507. current_trading_day_normalized = normalize_trade_day_value(current_trading_day)
  508. if blocked_trading_day == current_trading_day_normalized:
  509. log.info(f"当晚操作已被禁止(订单状态为'new',无夜盘),跳过所有操作")
  510. return
  511. # 得到当前未完成订单
  512. orders = get_open_orders()
  513. # 循环,撤销订单
  514. if len(orders) == 0:
  515. log.debug(f"无未完成订单")
  516. else:
  517. for _order in orders.values():
  518. log.debug(f"order: {_order}")
  519. cancel_order(_order)
  520. # 第一步:检查开仓条件
  521. log.info(f"检查开仓条件:")
  522. if g.daily_ma_candidates:
  523. log.info("=" * 60)
  524. log.info(f"执行开仓检查 - 时间: {context.current_dt}, 候选品种数量: {len(g.daily_ma_candidates)}")
  525. # 遍历候选品种
  526. candidates_to_remove = []
  527. for dominant_future, candidate_info in g.daily_ma_candidates.items():
  528. try:
  529. symbol = candidate_info['symbol']
  530. direction = candidate_info['direction']
  531. open_price = candidate_info['open_price']
  532. yesterday_close = candidate_info.get('yesterday_close')
  533. yesterday_open = candidate_info.get('yesterday_open')
  534. ma_compaction = candidate_info.get('ma_compaction')
  535. # 检查是否已有持仓
  536. if check_symbol_prefix_match(dominant_future, context, set(g.trade_history.keys())):
  537. log.info(f"{symbol} 已有持仓,从候选列表移除")
  538. candidates_to_remove.append(dominant_future)
  539. continue
  540. # 获取当前价格
  541. current_data = get_current_data()[dominant_future]
  542. current_price = current_data.last_price
  543. # 计算当天价差
  544. intraday_diff = current_price - open_price
  545. intraday_diff_ratio = intraday_diff / open_price
  546. log.info(f"{symbol}({dominant_future}) 开仓条件检查:")
  547. log.info(f" 方向: {direction}, 开盘价: {open_price:.2f}, 当前价: {current_price:.2f}, "
  548. f"当天价差: {intraday_diff:.2f}, 变化比例: {intraday_diff_ratio:.2%}")
  549. margin_limit = adjust_max_margin_per_position(ma_compaction)
  550. if margin_limit is None:
  551. compaction_str = f"{ma_compaction:.4f}" if ma_compaction is not None else "NA"
  552. log.info(
  553. f" ✗ 均线聚合度 {compaction_str} 超出允许范围(>{g.ma_compaction_thresholds['loose']:.4f}),跳过开仓"
  554. )
  555. candidates_to_remove.append(dominant_future)
  556. continue
  557. else:
  558. log.info(f" 动态单标的保证金上限: {margin_limit:.0f}")
  559. # 判断是否满足开仓条件 - 仅检查均线穿越得分
  560. should_open = True
  561. log.info(f" 开仓条件简化:仅检查均线穿越得分,跳过策略1,2,3的判断")
  562. if should_open:
  563. ma_values = candidate_info.get('ma_values') or {}
  564. avg_5day_change = calculate_recent_average_change(dominant_future, days=5)
  565. entry_snapshot = {
  566. 'yesterday_close': yesterday_close,
  567. 'today_open': open_price,
  568. 'ma_values': ma_values.copy() if ma_values else {},
  569. 'avg_5day_change': avg_5day_change
  570. }
  571. cross_score, score_details = calculate_ma_cross_score(open_price, current_price, ma_values, direction)
  572. # 根据当前时间调整所需的均线穿越得分阈值
  573. current_time_str = str(context.current_dt.time())[:5] # HH:MM格式
  574. required_cross_score = g.ma_cross_threshold
  575. if current_time_str != '14:55':
  576. # 在14:55以外的时间,需要更高的得分阈值
  577. required_cross_score = g.ma_cross_threshold + 1
  578. log.info(f" 均线穿越得分: {cross_score}, 得分详情: {score_details}, 当前时间: {current_time_str}, 所需阈值: {required_cross_score}")
  579. # 检查得分是否满足条件
  580. score_passed = False
  581. if cross_score >= required_cross_score:
  582. # 如果得分达到阈值,检查特殊情况:只有1分且来自MA5
  583. if cross_score == 1 and required_cross_score == 1:
  584. # 检查这一分是否来自MA5
  585. from_ma5_only = len(score_details) == 1 and score_details[0]['period'] == 5
  586. if not from_ma5_only:
  587. score_passed = True
  588. log.info(f" ✓ 均线穿越得分检查通过:1分且非来自MA5")
  589. else:
  590. log.info(f" ✗ 均线穿越得分检查未通过:1分且仅来自MA5")
  591. else:
  592. score_passed = True
  593. log.info(f" ✓ 均线穿越得分检查通过")
  594. if not score_passed:
  595. log.info(f" ✗ 均线穿越得分不足或不符合条件({cross_score} < {required_cross_score} 或 1分来自MA5),跳过开仓")
  596. continue
  597. if current_time_str == '14:55':
  598. positive_cross_periods = {detail['period'] for detail in score_details if detail.get('delta', 0) > 0}
  599. if positive_cross_periods == {30}:
  600. log.info(" ✗ 尾盘仅穿越MA30,跳过开仓")
  601. continue
  602. # 执行开仓
  603. log.info(f" 准备开仓: {symbol} {direction}")
  604. target_hands, single_hand_margin, single_hand_exceeds_limit = calculate_target_hands(
  605. context,
  606. dominant_future,
  607. direction,
  608. max_margin_override=margin_limit
  609. )
  610. if target_hands > 0:
  611. success = open_position(
  612. context,
  613. dominant_future,
  614. target_hands,
  615. direction,
  616. single_hand_margin,
  617. single_hand_exceeds_limit,
  618. f'均线形态开仓',
  619. crossed_ma_details=score_details,
  620. entry_snapshot=entry_snapshot
  621. )
  622. if success:
  623. log.info(f" ✓✓ {symbol} 开仓成功,从候选列表移除")
  624. candidates_to_remove.append(dominant_future)
  625. else:
  626. log.warning(f" ✗ {symbol} 开仓失败")
  627. else:
  628. log.warning(f" ✗ {symbol} 计算目标手数为0,跳过开仓")
  629. except Exception as e:
  630. log.warning(f"{dominant_future} 处理时出错: {str(e)}")
  631. continue
  632. finally:
  633. adjust_max_margin_per_position(None)
  634. # 从候选列表中移除已开仓的品种
  635. for future in candidates_to_remove:
  636. if future in g.daily_ma_candidates:
  637. del g.daily_ma_candidates[future]
  638. log.info(f"剩余候选品种: {list(g.daily_ma_candidates.keys())}")
  639. log.info("=" * 60)
  640. # 第二步:检查止损止盈
  641. log.info(f"检查止损止盈条件:")
  642. subportfolio = context.subportfolios[0]
  643. long_positions = list(subportfolio.long_positions.values())
  644. short_positions = list(subportfolio.short_positions.values())
  645. closed_count = 0
  646. skipped_count = 0
  647. for position in long_positions + short_positions:
  648. security = position.security
  649. underlying_symbol = security.split('.')[0][:-4]
  650. # 检查交易时间适配性
  651. has_night_session = get_futures_config(underlying_symbol, 'has_night_session', False)
  652. # 如果是夜盘时间,但品种不支持夜盘交易,则跳过
  653. if is_night_session and not has_night_session:
  654. skipped_count += 1
  655. continue
  656. # 执行止损止盈检查
  657. if check_position_stop_loss_profit(context, position):
  658. closed_count += 1
  659. if closed_count > 0:
  660. log.info(f"执行了 {closed_count} 次止损止盈")
  661. if skipped_count > 0:
  662. log.info(f"夜盘时间跳过 {skipped_count} 个日间品种的止损止盈检查")
  663. def check_ma_trailing_reactivation(context):
  664. """检查是否需要恢复均线跟踪止盈"""
  665. subportfolio = context.subportfolios[0]
  666. positions = list(subportfolio.long_positions.values()) + list(subportfolio.short_positions.values())
  667. if not positions:
  668. return
  669. reenabled_count = 0
  670. current_data = get_current_data()
  671. for position in positions:
  672. security = position.security
  673. trade_info = g.trade_history.get(security)
  674. if not trade_info or trade_info.get('ma_trailing_enabled', True):
  675. continue
  676. direction = trade_info['direction']
  677. ma_values = calculate_realtime_ma_values(security, [5])
  678. ma5_value = ma_values.get('ma5')
  679. if ma5_value is None or security not in current_data:
  680. continue
  681. today_price = current_data[security].last_price
  682. if direction == 'long' and today_price > ma5_value:
  683. trade_info['ma_trailing_enabled'] = True
  684. reenabled_count += 1
  685. log.info(f"恢复均线跟踪止盈: {security} {direction}, 当前价 {today_price:.2f} > MA5 {ma5_value:.2f}")
  686. elif direction == 'short' and today_price < ma5_value:
  687. trade_info['ma_trailing_enabled'] = True
  688. reenabled_count += 1
  689. log.info(f"恢复均线跟踪止盈: {security} {direction}, 当前价 {today_price:.2f} < MA5 {ma5_value:.2f}")
  690. if reenabled_count > 0:
  691. log.info(f"恢复均线跟踪止盈持仓数量: {reenabled_count}")
  692. def check_position_stop_loss_profit(context, position):
  693. """检查单个持仓的止损止盈"""
  694. log.info(f"检查持仓: {position.security}")
  695. security = position.security
  696. if security not in g.trade_history:
  697. return False
  698. trade_info = g.trade_history[security]
  699. direction = trade_info['direction']
  700. entry_price = trade_info['entry_price']
  701. entry_time = trade_info['entry_time']
  702. entry_trading_day = trade_info.get('entry_trading_day')
  703. if entry_trading_day is None:
  704. entry_trading_day = get_current_trading_day(entry_time)
  705. trade_info['entry_trading_day'] = entry_trading_day
  706. if entry_trading_day is not None:
  707. entry_trading_day = normalize_trade_day_value(entry_trading_day)
  708. current_trading_day = normalize_trade_day_value(get_current_trading_day(context.current_dt))
  709. current_price = position.price
  710. # 计算当前盈亏比率
  711. if direction == 'long':
  712. profit_rate = (current_price - entry_price) / entry_price
  713. else:
  714. profit_rate = (entry_price - current_price) / entry_price
  715. # 检查固定止损
  716. log.info("=" * 60)
  717. log.info(f"检查固定止损:")
  718. log.info("=" * 60)
  719. if profit_rate <= -g.fixed_stop_loss_rate:
  720. log.info(f"{security} {direction} 触发固定止损 {g.fixed_stop_loss_rate:.3%}, 当前亏损率: {profit_rate:.3%}, "
  721. f"成本价: {entry_price:.2f}, 当前价格: {current_price:.2f}")
  722. close_position(context, security, direction)
  723. return True
  724. else:
  725. log.debug(f"{security} {direction} 未触发固定止损 {g.fixed_stop_loss_rate:.3%}, 当前亏损率: {profit_rate:.3%}, "
  726. f"成本价: {entry_price:.2f}, 当前价格: {current_price:.2f}")
  727. if entry_trading_day is not None and entry_trading_day == current_trading_day:
  728. log.info(f"{security} 建仓交易日内跳过动态止盈检查")
  729. return False
  730. # 检查是否启用均线跟踪止盈
  731. log.info("=" * 60)
  732. log.info(f"检查是否启用均线跟踪止盈:")
  733. log.info("=" * 60)
  734. if not trade_info.get('ma_trailing_enabled', True):
  735. log.debug(f"{security} {direction} 未启用均线跟踪止盈")
  736. return False
  737. # 检查均线跟踪止盈
  738. # 获取持仓天数
  739. entry_date = entry_time.date()
  740. current_date = context.current_dt.date()
  741. all_trade_days = get_all_trade_days()
  742. holding_days = sum((entry_date <= d <= current_date) for d in all_trade_days)
  743. # 计算变化率
  744. today_price = get_current_data()[security].last_price
  745. avg_daily_change_rate = calculate_average_daily_change_rate(security)
  746. historical_data = attribute_history(security, 1, '1d', ['close'])
  747. yesterday_close = historical_data['close'].iloc[-1]
  748. today_change_rate = abs((today_price - yesterday_close) / yesterday_close)
  749. # 根据时间判断使用的偏移量
  750. current_time = context.current_dt.time()
  751. target_time = datetime.strptime('14:55:00', '%H:%M:%S').time()
  752. if current_time > target_time:
  753. offset_ratio = g.ma_offset_ratio_close
  754. log.debug(f"当前时间是:{current_time},使用偏移量: {offset_ratio:.3%}")
  755. else:
  756. offset_ratio = g.ma_offset_ratio_normal
  757. log.debug(f"当前时间是:{current_time},使用偏移量: {offset_ratio:.3%}")
  758. # 选择止损均线
  759. close_line = None
  760. if today_change_rate >= 1.5 * avg_daily_change_rate:
  761. close_line = 'ma5' # 波动剧烈时用短周期
  762. elif holding_days <= g.days_for_adjustment:
  763. close_line = 'ma5' # 持仓初期用短周期
  764. else:
  765. close_line = 'ma5' if today_change_rate >= 1.2 * avg_daily_change_rate else 'ma10'
  766. # 计算实时均线值
  767. ma_values = calculate_realtime_ma_values(security, [5, 10])
  768. ma_value = ma_values[close_line]
  769. # 应用偏移量
  770. if direction == 'long':
  771. adjusted_ma_value = ma_value * (1 - offset_ratio)
  772. else:
  773. adjusted_ma_value = ma_value * (1 + offset_ratio)
  774. # 判断是否触发均线止损
  775. if (direction == 'long' and today_price < adjusted_ma_value) or \
  776. (direction == 'short' and today_price > adjusted_ma_value):
  777. log.info(f"触发均线跟踪止盈 {security} {direction}, 止损均线: {close_line}, "
  778. f"均线值: {ma_value:.2f}, 调整后: {adjusted_ma_value:.2f}, "
  779. f"当前价: {today_price:.2f}, 持仓天数: {holding_days}")
  780. close_position(context, security, direction)
  781. return True
  782. else:
  783. log.debug(f"未触发均线跟踪止盈 {security} {direction}, 止损均线: {close_line}, "
  784. f"均线值: {ma_value:.2f}, 调整后: {adjusted_ma_value:.2f}, "
  785. f"当前价: {today_price:.2f}, 持仓天数: {holding_days}")
  786. return False
  787. ############################ 核心辅助函数 ###################################
  788. def calculate_ma_values(data, periods):
  789. """计算均线值
  790. Args:
  791. data: DataFrame,包含'close'列的历史数据(最后一行是最新的数据)
  792. periods: list,均线周期列表,如[5, 10, 20, 30]
  793. Returns:
  794. dict: {'MA5': value, 'MA10': value, 'MA20': value, 'MA30': value}
  795. 返回最后一行(最新日期)的各周期均线值
  796. """
  797. ma_values = {}
  798. for period in periods:
  799. if len(data) >= period:
  800. # 计算最后period天的均线值
  801. ma_values[f'MA{period}'] = data['close'].iloc[-period:].mean()
  802. else:
  803. ma_values[f'MA{period}'] = None
  804. return ma_values
  805. def calculate_ma_cross_score(open_price, current_price, ma_values, direction):
  806. """根据开盘价与当前价统计多周期均线穿越得分
  807. 返回: (总得分, 得分详情列表)
  808. 得分详情: [{'period': 5, 'delta': 1}, {'period': 10, 'delta': 1}, ...]
  809. """
  810. if not ma_values:
  811. return 0, []
  812. assert direction in ('long', 'short')
  813. score = 0
  814. score_details = []
  815. periods = getattr(g, 'ma_cross_periods', g.ma_periods)
  816. for period in periods:
  817. key = f'MA{period}'
  818. ma_value = ma_values.get(key)
  819. if ma_value is None:
  820. continue
  821. cross_up = open_price < ma_value and current_price > ma_value
  822. cross_down = open_price > ma_value and current_price < ma_value
  823. if not (cross_up or cross_down):
  824. continue
  825. if direction == 'long':
  826. delta = 1 if cross_up else -1
  827. else:
  828. delta = -1 if cross_up else 1
  829. score += delta
  830. score_details.append({'period': period, 'delta': delta})
  831. log.debug(
  832. f" 均线穿越[{key}] - 开盘 {open_price:.2f}, 当前 {current_price:.2f}, "
  833. f"均线 {ma_value:.2f}, 方向 {direction}, 增量 {delta}, 当前得分 {score}"
  834. )
  835. return score, score_details
  836. def calculate_ma_proximity_counts(data, periods, lookback_days):
  837. """统计近 lookback_days 天收盘价贴近各均线的次数"""
  838. proximity_counts = {f'MA{period}': 0 for period in periods}
  839. if len(data) < lookback_days:
  840. return proximity_counts
  841. closes = data['close'].iloc[-lookback_days:]
  842. ma_series = {
  843. period: data['close'].rolling(window=period).mean().iloc[-lookback_days:]
  844. for period in periods
  845. }
  846. for idx, close_price in enumerate(closes):
  847. min_diff = None
  848. closest_period = None
  849. for period in periods:
  850. ma_value = ma_series[period].iloc[idx]
  851. if pd.isna(ma_value):
  852. continue
  853. diff = abs(close_price - ma_value)
  854. if min_diff is None or diff < min_diff:
  855. min_diff = diff
  856. closest_period = period
  857. if closest_period is not None:
  858. proximity_counts[f'MA{closest_period}'] += 1
  859. return proximity_counts
  860. def calculate_extreme_trend_days(data, periods, lookback_days):
  861. """统计过去 lookback_days 天收盘价相对所有均线的极端趋势天数"""
  862. if len(data) < lookback_days:
  863. return 0, 0
  864. recent_closes = data['close'].iloc[-lookback_days:]
  865. ma_series = {
  866. period: data['close'].rolling(window=period).mean().iloc[-lookback_days:]
  867. for period in periods
  868. }
  869. above_count = 0
  870. below_count = 0
  871. for idx, close_price in enumerate(recent_closes):
  872. ma_values = []
  873. valid = True
  874. for period in periods:
  875. ma_value = ma_series[period].iloc[idx]
  876. if pd.isna(ma_value):
  877. valid = False
  878. break
  879. ma_values.append(ma_value)
  880. if not valid or not ma_values:
  881. continue
  882. if all(close_price > ma_value for ma_value in ma_values):
  883. above_count += 1
  884. elif all(close_price < ma_value for ma_value in ma_values):
  885. below_count += 1
  886. return above_count, below_count
  887. def check_ma5_distribution_filter(data, lookback_days, direction, min_ratio):
  888. """检查近 lookback_days 天收盘价相对于MA5的分布情况"""
  889. stats = {
  890. 'lookback_days': lookback_days,
  891. 'valid_days': 0,
  892. 'qualified_days': 0,
  893. 'required_days': max(0, math.ceil(lookback_days * min_ratio))
  894. }
  895. if lookback_days <= 0:
  896. return True, stats
  897. if len(data) < max(lookback_days, 5):
  898. return False, stats
  899. recent_closes = data['close'].iloc[-lookback_days:]
  900. ma5_series = data['close'].rolling(window=5).mean().iloc[-lookback_days:]
  901. for close_price, ma5_value in zip(recent_closes, ma5_series):
  902. log.debug(f"close_price: {close_price}, ma5_value: {ma5_value}")
  903. if pd.isna(ma5_value):
  904. continue
  905. stats['valid_days'] += 1
  906. if direction == 'long' and close_price < ma5_value:
  907. stats['qualified_days'] += 1
  908. elif direction == 'short' and close_price > ma5_value:
  909. stats['qualified_days'] += 1
  910. if stats['valid_days'] < lookback_days:
  911. return False, stats
  912. return stats['qualified_days'] >= stats['required_days'], stats
  913. def check_open_gap_filter(symbol, dominant_future, direction, yesterday_close, today_open, current_trading_day):
  914. """开盘价差过滤辅助函数
  915. 根据当前策略模式(`g.ma_gap_strategy_mode`)和对应阈值检查开盘价差是否符合方向要求。
  916. Args:
  917. symbol: 品种代码(如 'AO')
  918. dominant_future: 主力合约代码(如 'AO2502.XSGE')
  919. direction: 方向,'long' 或 'short'
  920. yesterday_close: 前一交易日收盘价
  921. today_open: 当日开盘价
  922. current_trading_day: 当前期货交易日
  923. Returns:
  924. bool: True 表示通过开盘价差过滤,False 表示未通过(并已加入排除缓存)
  925. """
  926. open_gap_ratio = (today_open - yesterday_close) / yesterday_close
  927. log.info(
  928. f" 开盘价差检查: 昨收 {yesterday_close:.2f}, 今开 {today_open:.2f}, "
  929. f"价差比例 {open_gap_ratio:.2%}"
  930. )
  931. gap_check_passed = False
  932. if g.ma_gap_strategy_mode == 1:
  933. # 方案1:多头检查上跳,空头检查下跳
  934. if direction == 'long' and open_gap_ratio >= g.ma_open_gap_threshold:
  935. log.info(
  936. f" {symbol}({dominant_future}) ✓ 方案1多头开盘价差检查通过 "
  937. f"({open_gap_ratio:.2%} >= {g.ma_open_gap_threshold:.2%})"
  938. )
  939. gap_check_passed = True
  940. elif direction == 'short' and open_gap_ratio <= -g.ma_open_gap_threshold:
  941. log.info(
  942. f" {symbol}({dominant_future}) ✓ 方案1空头开盘价差检查通过 "
  943. f"({open_gap_ratio:.2%} <= {-g.ma_open_gap_threshold:.2%})"
  944. )
  945. gap_check_passed = True
  946. elif g.ma_gap_strategy_mode == 2 or g.ma_gap_strategy_mode == 3:
  947. # 方案2和方案3:多头检查下跳,空头检查上跳
  948. if direction == 'long' and open_gap_ratio <= -g.ma_open_gap_threshold2:
  949. log.info(
  950. f" {symbol}({dominant_future}) ✓ 方案{g.ma_gap_strategy_mode}多头开盘价差检查通过 "
  951. f"({open_gap_ratio:.2%} <= {-g.ma_open_gap_threshold2:.2%})"
  952. )
  953. gap_check_passed = True
  954. elif direction == 'short' and open_gap_ratio >= g.ma_open_gap_threshold2:
  955. log.info(
  956. f" {symbol}({dominant_future}) ✓ 方案{g.ma_gap_strategy_mode}空头开盘价差检查通过 "
  957. f"({open_gap_ratio:.2%} >= {g.ma_open_gap_threshold2:.2%})"
  958. )
  959. gap_check_passed = True
  960. if not gap_check_passed:
  961. add_to_excluded_contracts(dominant_future, 'open_gap', current_trading_day)
  962. return False
  963. return True
  964. def check_ma_pattern(ma_values, direction):
  965. """检查均线排列模式是否符合方向要求
  966. Args:
  967. ma_values: dict,包含MA5, MA10, MA20, MA30的均线值
  968. direction: str,'long'或'short'
  969. Returns:
  970. bool: 是否符合均线排列要求
  971. """
  972. ma5 = ma_values['MA5']
  973. ma10 = ma_values['MA10']
  974. ma20 = ma_values['MA20']
  975. ma30 = ma_values['MA30']
  976. if direction == 'long':
  977. # 多头模式:MA30 <= MA20 <= MA10 <= MA5 或 MA30 <= MA20 <= MA5 <= MA10
  978. # 或者:MA20 <= MA30 <= MA10 <= MA5 或 MA20 <= MA30 <= MA5 <= MA10
  979. pattern1 = (ma30 <= ma20 <= ma10 <= ma5)
  980. pattern2 = (ma30 <= ma20 <= ma5 <= ma10)
  981. pattern3 = (ma20 <= ma30 <= ma10 <= ma5)
  982. pattern4 = (ma20 <= ma30 <= ma5 <= ma10)
  983. return pattern1 or pattern2 or pattern3 or pattern4
  984. elif direction == 'short':
  985. # 空头模式:MA10 <= MA5 <= MA20 <= MA30 或 MA5 <= MA10 <= MA20 <= MA30
  986. # 或者:MA10 <= MA5 <= MA30 <= MA20 或 MA5 <= MA10 <= MA30 <= MA20
  987. pattern1 = (ma10 <= ma5 <= ma20 <= ma30)
  988. pattern2 = (ma5 <= ma10 <= ma20 <= ma30)
  989. pattern3 = (ma10 <= ma5 <= ma30 <= ma20)
  990. pattern4 = (ma5 <= ma10 <= ma30 <= ma20)
  991. return pattern1 or pattern2 or pattern3 or pattern4
  992. else:
  993. return False
  994. def check_historical_ma_pattern_consistency(historical_data, direction, lookback_days, consistency_threshold):
  995. """检查历史均线模式的一致性
  996. Args:
  997. historical_data: DataFrame,包含足够天数的历史数据
  998. direction: str,'long'或'short'
  999. lookback_days: int,检查过去多少天
  1000. consistency_threshold: float,一致性阈值(0-1之间)
  1001. Returns:
  1002. tuple: (bool, float) - (是否通过一致性检查, 实际一致性比例)
  1003. """
  1004. if len(historical_data) < max(g.ma_periods) + lookback_days:
  1005. # 历史数据不足
  1006. return False, 0.0
  1007. match_count = 0
  1008. total_count = lookback_days
  1009. # log.debug(f"历史均线模式一致性检查: {direction}, 检查过去{lookback_days}天的数据")
  1010. # log.debug(f"历史数据: {historical_data}")
  1011. # 检查过去lookback_days天的均线模式
  1012. for i in range(lookback_days):
  1013. # 获取倒数第(i+1)天的数据(i=0时是昨天,i=1时是前天,依此类推)
  1014. end_idx = -(i + 1)
  1015. # 获取这一天的具体日期
  1016. date = historical_data.index[end_idx].date()
  1017. # 获取到该天(包括该天)为止的所有数据
  1018. if i == 0:
  1019. data_slice = historical_data
  1020. else:
  1021. data_slice = historical_data.iloc[:-i]
  1022. # 计算该天的均线值
  1023. # log.debug(f"对于倒数第{i+1}天,end_idx: {end_idx},日期: {date},计算均线值: {data_slice}")
  1024. ma_values = calculate_ma_values(data_slice, g.ma_periods)
  1025. # log.debug(f"end_idx: {end_idx},日期: {date},倒数第{i+1}天的均线值: {ma_values}")
  1026. # 检查是否符合模式
  1027. if check_ma_pattern(ma_values, direction):
  1028. match_count += 1
  1029. # log.debug(f"日期: {date},对于倒数第{i+1}天,历史均线模式一致性检查: {direction} 符合模式")
  1030. # else:
  1031. # log.debug(f"日期: {date},对于倒数第{i+1}天,历史均线模式一致性检查: {direction} 不符合模式")
  1032. consistency_ratio = match_count / total_count
  1033. passed = consistency_ratio >= consistency_threshold
  1034. return passed, consistency_ratio
  1035. ############################ 交易执行函数 ###################################
  1036. def open_position(context, security, target_hands, direction, single_hand_margin,
  1037. single_hand_exceeds_limit, reason='',
  1038. crossed_ma_details=None, entry_snapshot=None):
  1039. """开仓并可选地在成交后校验保证金
  1040. Args:
  1041. single_hand_exceeds_limit: bool,当单手保证金超过最大限制时为True
  1042. """
  1043. try:
  1044. # 记录交易前的可用资金
  1045. cash_before = context.portfolio.available_cash
  1046. # 根据单手保证金情况选择下单方式
  1047. use_value_order = (not single_hand_exceeds_limit) and getattr(g, 'max_margin_per_position', 0) > 0
  1048. if use_value_order:
  1049. target_value = g.max_margin_per_position
  1050. order = order_target_value(security, target_value, side=direction)
  1051. log.debug(f"使用order_target_value下单: {security} {direction} 目标价值 {target_value}")
  1052. else:
  1053. order = order_target(security, target_hands, side=direction)
  1054. log.debug(f"使用order_target按手数下单: {security} {direction} 目标手数 {target_hands}")
  1055. log.debug(f"order: {order}")
  1056. # 检查订单状态,如果为'new'说明当晚没有夜盘
  1057. if order is not None:
  1058. order_status = str(order.status).lower()
  1059. if order_status == 'new':
  1060. # 取消订单
  1061. cancel_order(order)
  1062. current_trading_day = get_current_trading_day(context.current_dt)
  1063. g.night_session_blocked = True
  1064. g.night_session_blocked_trading_day = current_trading_day
  1065. log.warning(f"订单状态为'new',说明{current_trading_day}当晚没有夜盘,已取消订单: {security} {direction} {target_hands}手,并禁止当晚所有操作")
  1066. return False
  1067. if order is not None and order.filled > 0:
  1068. # 记录交易后的可用资金
  1069. cash_after = context.portfolio.available_cash
  1070. # 计算实际资金变化
  1071. cash_change = cash_before - cash_after
  1072. # 计算保证金变化
  1073. margin_change = single_hand_margin * target_hands
  1074. # 获取订单价格和数量
  1075. order_price = order.avg_cost if order.avg_cost else order.price
  1076. order_amount = order.filled
  1077. # 记录当日交易
  1078. underlying_symbol = security.split('.')[0][:-4]
  1079. g.today_trades.append({
  1080. 'security': security, # 合约代码
  1081. 'underlying_symbol': underlying_symbol, # 标的代码
  1082. 'direction': direction, # 方向
  1083. 'order_amount': order_amount, # 订单数量
  1084. 'order_price': order_price, # 订单价格
  1085. 'cash_change': cash_change, # 资金变化
  1086. 'margin_change': margin_change, # 保证金
  1087. 'time': context.current_dt # 时间
  1088. })
  1089. # 记录交易信息
  1090. entry_trading_day = get_current_trading_day(context.current_dt)
  1091. # 处理穿越均线信息
  1092. crossed_ma_lines = []
  1093. if crossed_ma_details:
  1094. crossed_ma_lines = [f"MA{detail['period']}" for detail in crossed_ma_details if detail.get('delta', 0) > 0]
  1095. snapshot_copy = None
  1096. if entry_snapshot:
  1097. snapshot_copy = {
  1098. 'yesterday_close': entry_snapshot.get('yesterday_close'),
  1099. 'today_open': entry_snapshot.get('today_open'),
  1100. 'ma_values': (entry_snapshot.get('ma_values') or {}).copy(),
  1101. 'avg_5day_change': entry_snapshot.get('avg_5day_change')
  1102. }
  1103. g.trade_history[security] = {
  1104. 'entry_price': order_price,
  1105. 'target_hands': target_hands,
  1106. 'actual_hands': order_amount,
  1107. 'actual_margin': margin_change,
  1108. 'direction': direction,
  1109. 'entry_time': context.current_dt,
  1110. 'entry_trading_day': entry_trading_day,
  1111. 'crossed_ma_lines': crossed_ma_lines, # 记录穿越的均线
  1112. 'entry_snapshot': snapshot_copy
  1113. }
  1114. ma_trailing_enabled = True
  1115. ma_values_at_entry = calculate_realtime_ma_values(security, [5])
  1116. ma5_value = ma_values_at_entry.get('ma5')
  1117. if ma5_value is not None:
  1118. if direction == 'long' and order_price < ma5_value:
  1119. ma_trailing_enabled = False
  1120. log.info(f"禁用均线跟踪止盈: {security} {direction}, 开仓价 {order_price:.2f} < MA5 {ma5_value:.2f}")
  1121. elif direction == 'short' and order_price > ma5_value:
  1122. ma_trailing_enabled = False
  1123. log.info(f"禁用均线跟踪止盈: {security} {direction}, 开仓价 {order_price:.2f} > MA5 {ma5_value:.2f}")
  1124. g.trade_history[security]['ma_trailing_enabled'] = ma_trailing_enabled
  1125. crossed_ma_str = ', '.join(crossed_ma_lines) if crossed_ma_lines else '无'
  1126. log.info(f"开仓成功: {security} {direction} {order_amount}手 @{order_price:.2f}, "
  1127. f"保证金: {margin_change:.0f}, 资金变化: {cash_change:.0f}, 原因: {reason}, "
  1128. f"穿越均线: {crossed_ma_str}")
  1129. return True
  1130. except Exception as e:
  1131. log.warning(f"开仓失败 {security}: {str(e)}")
  1132. return False
  1133. def close_position(context, security, direction):
  1134. """平仓"""
  1135. try:
  1136. # 使用order_target平仓到0手
  1137. order = order_target(security, 0, side=direction)
  1138. if order is not None and order.filled > 0:
  1139. underlying_symbol = security.split('.')[0][:-4]
  1140. # 记录当日交易(平仓)
  1141. g.today_trades.append({
  1142. 'security': security,
  1143. 'underlying_symbol': underlying_symbol,
  1144. 'direction': direction,
  1145. 'order_amount': -order.filled,
  1146. 'order_price': order.avg_cost if order.avg_cost else order.price,
  1147. 'cash_change': 0,
  1148. 'time': context.current_dt
  1149. })
  1150. log.info(f"平仓成功: {underlying_symbol} {direction} {order.filled}手")
  1151. # 从交易历史中移除
  1152. if security in g.trade_history:
  1153. del g.trade_history[security]
  1154. log.debug(f"从交易历史中移除: {security}")
  1155. else:
  1156. log.info(f"平仓失败: {security} {direction} {order.filled}手")
  1157. return True
  1158. except Exception as e:
  1159. log.warning(f"平仓失败 {security}: {str(e)}")
  1160. return False
  1161. ############################ 辅助函数 ###################################
  1162. def get_futures_config(underlying_symbol, config_key=None, default_value=None):
  1163. """获取期货品种配置信息的辅助函数"""
  1164. if underlying_symbol not in g.futures_config:
  1165. if config_key and default_value is not None:
  1166. return default_value
  1167. return {}
  1168. if config_key is None:
  1169. return g.futures_config[underlying_symbol]
  1170. return g.futures_config[underlying_symbol].get(config_key, default_value)
  1171. def get_margin_rate(underlying_symbol, direction, default_rate=0.10):
  1172. """获取保证金比例的辅助函数"""
  1173. return g.futures_config.get(underlying_symbol, {}).get('margin_rate', {}).get(direction, default_rate)
  1174. def get_multiplier(underlying_symbol, default_multiplier=10):
  1175. """获取合约乘数的辅助函数"""
  1176. return g.futures_config.get(underlying_symbol, {}).get('multiplier', default_multiplier)
  1177. def is_financial_underlying(underlying_symbol):
  1178. """判断标的是否属于金融期货"""
  1179. if not underlying_symbol:
  1180. return False
  1181. return bool(get_futures_config(underlying_symbol, 'is_financial', False))
  1182. def has_financial_positions(context):
  1183. """检查当前持仓中是否包含金融期货"""
  1184. subportfolios = getattr(context, 'subportfolios', [])
  1185. if not subportfolios:
  1186. return False
  1187. subportfolio = subportfolios[0]
  1188. positions = list(getattr(subportfolio, 'long_positions', {}).values()) + \
  1189. list(getattr(subportfolio, 'short_positions', {}).values())
  1190. for position in positions:
  1191. security = getattr(position, 'security', '')
  1192. if not security:
  1193. continue
  1194. underlying = security.split('.')[0]
  1195. if not underlying:
  1196. continue
  1197. symbol = underlying[:-4] if len(underlying) > 4 else underlying
  1198. if is_financial_underlying(symbol):
  1199. return True
  1200. return False
  1201. def add_to_excluded_contracts(dominant_future, reason, current_trading_day):
  1202. """将合约添加到排除缓存"""
  1203. g.excluded_contracts[dominant_future] = {
  1204. 'reason': reason,
  1205. 'trading_day': current_trading_day
  1206. }
  1207. def has_reached_trading_start(current_dt, trading_start_time_str, has_night_session=False):
  1208. """判断当前是否已到达合约允许交易的起始时间"""
  1209. if not trading_start_time_str:
  1210. return True
  1211. try:
  1212. hour, minute = [int(part) for part in trading_start_time_str.split(':')[:2]]
  1213. except Exception:
  1214. return True
  1215. start_time = time(hour, minute)
  1216. current_time = current_dt.time()
  1217. if has_night_session:
  1218. if current_time >= start_time:
  1219. return True
  1220. if current_time < time(12, 0):
  1221. return True
  1222. if time(8, 30) <= current_time <= time(15, 30):
  1223. return True
  1224. return False
  1225. if current_time < start_time:
  1226. return False
  1227. if current_time >= time(20, 0):
  1228. return False
  1229. return True
  1230. def calculate_target_hands(context, security, direction, max_margin_override=None):
  1231. """计算目标开仓手数
  1232. Returns:
  1233. tuple: (target_hands, single_hand_margin, single_hand_exceeds_limit)
  1234. """
  1235. current_price = get_current_data()[security].last_price
  1236. underlying_symbol = security.split('.')[0][:-4]
  1237. # 使用保证金比例
  1238. margin_rate = get_margin_rate(underlying_symbol, direction)
  1239. multiplier = get_multiplier(underlying_symbol)
  1240. # 计算单手保证金
  1241. single_hand_margin = current_price * multiplier * margin_rate
  1242. log.debug(f"计算单手保证金: {current_price:.2f} * {multiplier:.2f} * {margin_rate:.2f} = {single_hand_margin:.2f}")
  1243. # 还要考虑可用资金限制,先计算账户总资金(可用资金 + 已占用保证金)
  1244. total_value = context.portfolio.total_value
  1245. reserved_cash = total_value * g.reserve_percentage # 按比例预留的现金
  1246. available_cash = max(total_value - reserved_cash, 0)
  1247. # log.debug(f"账户总资金: {total_value:.0f}, 预留现金: {reserved_cash:.0f}, 可用资金: {available_cash:.0f}")
  1248. # 如果当前没有持有金融期货,则额外预留指定金额,避免被其他品种占用
  1249. if not has_financial_positions(context):
  1250. reserve_amount = getattr(g, 'financial_reserve_amount', 0)
  1251. log.debug(f"金融期货预留资金: {reserve_amount:.0f}")
  1252. if reserve_amount > 0:
  1253. available_cash = max(available_cash - reserve_amount, 0)
  1254. log.debug(f"金融期货预留资金扣除后可用资金: {available_cash:.0f}")
  1255. # 根据单个标的最大持仓保证金限制计算开仓数量
  1256. max_margin = max_margin_override if max_margin_override is not None else g.max_margin_per_position
  1257. single_hand_exceeds_limit = single_hand_margin > max_margin
  1258. if not single_hand_exceeds_limit:
  1259. # 如果单手保证金不超过最大限制,计算最大可开仓手数
  1260. max_hands = int(max_margin / single_hand_margin)
  1261. max_hands_by_cash = int(available_cash / single_hand_margin)
  1262. # 取两者较小值
  1263. actual_hands = min(max_hands, max_hands_by_cash)
  1264. # 确保至少开1手
  1265. actual_hands = max(1, actual_hands)
  1266. log.info(f"单手保证金: {single_hand_margin:.0f}, 目标开仓手数: {actual_hands}")
  1267. return actual_hands, single_hand_margin, single_hand_exceeds_limit
  1268. else:
  1269. # 如果单手保证金超过最大限制,默认开仓1手
  1270. actual_hands = 1
  1271. log.info(f"单手保证金: {single_hand_margin:.0f} 超过最大限制: {max_margin}, 默认开仓1手")
  1272. return actual_hands, single_hand_margin, single_hand_exceeds_limit
  1273. def check_symbol_prefix_match(symbol, context, hold_symbols):
  1274. """检查是否有相似的持仓品种"""
  1275. log.debug(f"检查持仓")
  1276. symbol_prefix = symbol[:-9]
  1277. long_positions = context.subportfolios[0].long_positions
  1278. short_positions = context.subportfolios[0].short_positions
  1279. log.debug(f"long_positions: {long_positions}, short_positions: {short_positions}")
  1280. for hold_symbol in hold_symbols:
  1281. hold_symbol_prefix = hold_symbol[:-9] if len(hold_symbol) > 9 else hold_symbol
  1282. if symbol_prefix == hold_symbol_prefix:
  1283. return True
  1284. return False
  1285. def calculate_average_daily_change_rate(security, days=30):
  1286. """计算日均变化率"""
  1287. historical_data = attribute_history(security, days + 1, '1d', ['close'])
  1288. daily_change_rates = abs(historical_data['close'].pct_change()).iloc[1:]
  1289. return daily_change_rates.mean()
  1290. def calculate_realtime_ma_values(security, ma_periods):
  1291. """计算包含当前价格的实时均线值"""
  1292. if not ma_periods:
  1293. return {}
  1294. max_period = max(ma_periods)
  1295. history_days = max_period + MA_ADDITIONAL_HISTORY_DAYS
  1296. historical_data = attribute_history(security, history_days, '1d', ['close'])
  1297. today_price = get_current_data()[security].last_price
  1298. close_prices = historical_data['close'].tolist() + [today_price]
  1299. ma_values = {}
  1300. for period in ma_periods:
  1301. if len(close_prices) >= period:
  1302. ma_values[f'ma{period}'] = sum(close_prices[-period:]) / period
  1303. else:
  1304. ma_values[f'ma{period}'] = None
  1305. return ma_values
  1306. def calculate_recent_average_change(security, days=5):
  1307. """计算最近days天日间收盘涨幅的平均值"""
  1308. if days <= 0:
  1309. return None
  1310. try:
  1311. history = attribute_history(security, days + 1, '1d', ['close'])
  1312. except Exception as e:
  1313. log.warning(f"{security} 获取历史数据失败(均值涨幅计算): {str(e)}")
  1314. return None
  1315. closes = history['close']
  1316. if len(closes) < days + 1:
  1317. return None
  1318. pct_changes = closes.pct_change().dropna().abs() # 取绝对值,更关心涨跌幅度而不是方向
  1319. if len(pct_changes) < days:
  1320. return None
  1321. return pct_changes.iloc[-days:].mean()
  1322. def calculate_ma_compaction_from_values(ma_values, periods=(5, 10, 20)):
  1323. """基于给定的均线数值计算聚合度(标准差/均值)"""
  1324. if not ma_values:
  1325. return None
  1326. values = []
  1327. for period in periods:
  1328. key = f'MA{period}'
  1329. value = ma_values.get(key)
  1330. if value is None:
  1331. return None
  1332. values.append(value)
  1333. mean_val = np.mean(values)
  1334. if mean_val == 0:
  1335. return None
  1336. std_val = np.std(values, ddof=0)
  1337. return std_val / mean_val
  1338. def determine_margin_limit_from_compaction(compaction_value):
  1339. """根据均线聚合度返回对应的最大持仓保证金上限"""
  1340. base_limit = getattr(g, 'base_max_margin_per_position', getattr(g, 'max_margin_per_position', 30000))
  1341. if compaction_value is None:
  1342. return base_limit
  1343. thresholds = getattr(g, 'ma_compaction_thresholds', MA_COMPACTION_THRESHOLDS)
  1344. tight_threshold = thresholds.get('tight', MA_COMPACTION_THRESHOLDS['tight'])
  1345. balanced_threshold = thresholds.get('balanced', MA_COMPACTION_THRESHOLDS['balanced'])
  1346. loose_threshold = thresholds.get('loose', MA_COMPACTION_THRESHOLDS['loose'])
  1347. if compaction_value <= tight_threshold:
  1348. return 40000
  1349. if compaction_value <= balanced_threshold:
  1350. return 30000
  1351. if compaction_value <= loose_threshold:
  1352. return 20000
  1353. return None
  1354. def adjust_max_margin_per_position(compaction_value):
  1355. """根据聚合度动态调整 g.max_margin_per_position,并返回新的上限"""
  1356. margin_limit = determine_margin_limit_from_compaction(compaction_value)
  1357. if margin_limit is None:
  1358. return None
  1359. g.max_margin_per_position = margin_limit
  1360. return margin_limit
  1361. def format_entry_details(entry_snapshot):
  1362. """格式化记录到CSV的价格、均线及扩展信息"""
  1363. if not entry_snapshot:
  1364. return ''
  1365. def fmt(value):
  1366. return f"{value:.2f}" if value is not None else "NA"
  1367. ma_values = entry_snapshot.get('ma_values') or {}
  1368. snapshot_parts = [
  1369. f"prev_close:{fmt(entry_snapshot.get('yesterday_close'))}",
  1370. f"open:{fmt(entry_snapshot.get('today_open'))}"
  1371. ]
  1372. for period in [5, 10, 20, 30, 60]:
  1373. key_upper = f"MA{period}"
  1374. key_lower = key_upper.lower()
  1375. value = ma_values.get(key_upper)
  1376. if value is None:
  1377. value = ma_values.get(key_lower)
  1378. snapshot_parts.append(f"{key_upper}:{fmt(value)}")
  1379. avg_change = entry_snapshot.get('avg_5day_change')
  1380. if avg_change is not None:
  1381. snapshot_parts.append(f"AVG5:{avg_change:.4%}")
  1382. else:
  1383. snapshot_parts.append("AVG5:NA")
  1384. return '|'.join(snapshot_parts)
  1385. def save_today_new_positions_to_csv(context):
  1386. """将当天新增的持仓记录追加保存到CSV文件"""
  1387. log.info(f"保存当天新增的持仓记录到CSV文件")
  1388. if not g.trade_history:
  1389. return
  1390. current_trading_day = normalize_trade_day_value(get_current_trading_day(context.current_dt))
  1391. # 筛选当天新开仓的记录
  1392. today_new_positions = []
  1393. for security, trade_info in g.trade_history.items():
  1394. entry_trading_day = normalize_trade_day_value(trade_info.get('entry_trading_day'))
  1395. if entry_trading_day == current_trading_day:
  1396. underlying_symbol = security.split('.')[0][:-4]
  1397. crossed_ma_lines = trade_info.get('crossed_ma_lines', [])
  1398. # 使用分号分隔多条均线,避免CSV格式问题
  1399. crossed_ma_str = ';'.join(crossed_ma_lines) if crossed_ma_lines else ''
  1400. details = format_entry_details(trade_info.get('entry_snapshot'))
  1401. today_new_positions.append({
  1402. 'security': security,
  1403. 'underlying_symbol': underlying_symbol,
  1404. 'direction': trade_info['direction'],
  1405. 'entry_price': trade_info['entry_price'],
  1406. 'actual_hands': trade_info['actual_hands'],
  1407. 'actual_margin': trade_info['actual_margin'],
  1408. 'entry_time': trade_info['entry_time'].strftime('%H:%M:%S'), # 只保留时间
  1409. 'entry_trading_day': str(entry_trading_day),
  1410. 'crossed_ma_lines': crossed_ma_str,
  1411. 'details': details
  1412. })
  1413. # 如果没有当天新增的记录,直接返回
  1414. if not today_new_positions:
  1415. log.debug("当天无新增持仓记录,跳过保存")
  1416. return
  1417. try:
  1418. filename = 'trade_history.csv'
  1419. # 尝试读取现有文件
  1420. existing_content = ''
  1421. file_exists = False
  1422. try:
  1423. existing_content_bytes = read_file(filename)
  1424. if existing_content_bytes:
  1425. existing_content = existing_content_bytes.decode('utf-8')
  1426. file_exists = True
  1427. except:
  1428. pass
  1429. # 准备CSV内容
  1430. csv_lines = []
  1431. # 如果文件不存在,添加表头
  1432. if not file_exists:
  1433. header = 'security,underlying_symbol,direction,entry_price,actual_hands,actual_margin,entry_time,entry_trading_day,crossed_ma_lines,details'
  1434. csv_lines.append(header)
  1435. # 添加新记录
  1436. for pos in today_new_positions:
  1437. line = f"{pos['security']},{pos['underlying_symbol']},{pos['direction']},{pos['entry_price']:.2f},{pos['actual_hands']},{pos['actual_margin']:.2f},{pos['entry_time']},{pos['entry_trading_day']},{pos['crossed_ma_lines']},{pos['details']}"
  1438. csv_lines.append(line)
  1439. # 合并内容:现有内容 + 新内容
  1440. new_content = '\n'.join(csv_lines)
  1441. if file_exists:
  1442. final_content = existing_content.rstrip('\n') + '\n' + new_content
  1443. else:
  1444. final_content = new_content
  1445. # 写入文件 - 将字符串编码为bytes以符合JoinQuant API要求
  1446. write_file(filename, final_content.encode('utf-8'))
  1447. log.info(f"已追加{len(today_new_positions)}条当天新增持仓记录到文件: {filename}")
  1448. except Exception as e:
  1449. log.warning(f"保存持仓记录到CSV文件时出错: {str(e)}")
  1450. def sync_trade_history_with_positions(context):
  1451. """同步g.trade_history与实际持仓,清理已平仓但记录未删除的持仓"""
  1452. if not g.trade_history:
  1453. return
  1454. subportfolio = context.subportfolios[0]
  1455. actual_positions = set(subportfolio.long_positions.keys()) | set(subportfolio.short_positions.keys())
  1456. # 找出g.trade_history中有记录但实际已平仓的合约
  1457. stale_records = []
  1458. for security in g.trade_history.keys():
  1459. if security not in actual_positions:
  1460. stale_records.append(security)
  1461. # 清理这些过期记录
  1462. if stale_records:
  1463. log.info("=" * 60)
  1464. log.info("发现持仓记录与实际持仓不同步,进行清理:")
  1465. for security in stale_records:
  1466. trade_info = g.trade_history[security]
  1467. underlying_symbol = security.split('.')[0][:-4]
  1468. log.info(f" 清理过期记录: {underlying_symbol}({security}) {trade_info['direction']}, "
  1469. f"成本价: {trade_info['entry_price']:.2f}, "
  1470. f"入场时间: {trade_info['entry_time']}")
  1471. del g.trade_history[security]
  1472. log.info(f"共清理 {len(stale_records)} 条过期持仓记录")
  1473. log.info("=" * 60)
  1474. def after_market_close(context):
  1475. """收盘后运行函数"""
  1476. log.info(str('函数运行时间(after_market_close):'+str(context.current_dt.time())))
  1477. # 保存当天新增的持仓记录到CSV文件
  1478. save_today_new_positions_to_csv(context)
  1479. # 同步检查:清理g.trade_history中已平仓但记录未删除的持仓
  1480. sync_trade_history_with_positions(context)
  1481. # 清空候选列表(每天重新检查)
  1482. g.daily_ma_candidates = {}
  1483. # 清空排除缓存(每天重新检查)
  1484. excluded_count = len(g.excluded_contracts)
  1485. if excluded_count > 0:
  1486. log.info(f"清空排除缓存,共 {excluded_count} 个合约")
  1487. g.excluded_contracts = {}
  1488. # 重置夜盘禁止操作标志
  1489. if g.night_session_blocked:
  1490. log.info(f"重置夜盘禁止操作标志")
  1491. g.night_session_blocked = False
  1492. g.night_session_blocked_trading_day = None
  1493. # 只有当天有交易时才打印统计信息
  1494. if g.today_trades:
  1495. print_daily_trading_summary(context)
  1496. # 清空当日交易记录
  1497. g.today_trades = []
  1498. log.info('##############################################################')
  1499. def print_daily_trading_summary(context):
  1500. """打印当日交易汇总"""
  1501. if not g.today_trades:
  1502. return
  1503. log.info("\n=== 当日交易汇总 ===")
  1504. total_margin = 0
  1505. for trade in g.today_trades:
  1506. if trade['order_amount'] > 0: # 开仓
  1507. log.info(f"开仓 {trade['underlying_symbol']} {trade['direction']} {trade['order_amount']}手 "
  1508. f"价格:{trade['order_price']:.2f} 保证金:{trade['cash_change']:.0f}")
  1509. total_margin += trade['cash_change']
  1510. else: # 平仓
  1511. log.info(f"平仓 {trade['underlying_symbol']} {trade['direction']} {abs(trade['order_amount'])}手 "
  1512. f"价格:{trade['order_price']:.2f}")
  1513. log.info(f"当日保证金占用: {total_margin:.0f}")
  1514. log.info("==================\n")
  1515. ########################## 自动移仓换月函数 #################################
  1516. def position_auto_switch(context, pindex=0, switch_func=None, callback=None):
  1517. """期货自动移仓换月"""
  1518. import re
  1519. subportfolio = context.subportfolios[pindex]
  1520. symbols = set(subportfolio.long_positions.keys()) | set(subportfolio.short_positions.keys())
  1521. switch_result = []
  1522. for symbol in symbols:
  1523. match = re.match(r"(?P<underlying_symbol>[A-Z]{1,})", symbol)
  1524. if not match:
  1525. raise ValueError("未知期货标的: {}".format(symbol))
  1526. else:
  1527. underlying_symbol = match.groupdict()["underlying_symbol"]
  1528. trading_start = get_futures_config(underlying_symbol, 'trading_start_time', None)
  1529. has_night_session = get_futures_config(underlying_symbol, 'has_night_session', False)
  1530. # log.debug(f"移仓换月: {symbol}, 交易开始时间: {trading_start}, 夜盘: {has_night_session}")
  1531. if trading_start and not has_reached_trading_start(context.current_dt, trading_start, has_night_session):
  1532. # log.info("{} 当前时间 {} 未到达交易开始时间 {} (夜盘:{} ),跳过移仓".format(
  1533. # symbol,
  1534. # context.current_dt.strftime('%H:%M:%S'),
  1535. # trading_start,
  1536. # has_night_session
  1537. # ))
  1538. continue
  1539. dominant = get_dominant_future(underlying_symbol)
  1540. cur = get_current_data()
  1541. symbol_last_price = cur[symbol].last_price
  1542. dominant_last_price = cur[dominant].last_price
  1543. if dominant > symbol:
  1544. for positions_ in (subportfolio.long_positions, subportfolio.short_positions):
  1545. if symbol not in positions_.keys():
  1546. continue
  1547. else :
  1548. p = positions_[symbol]
  1549. if switch_func is not None:
  1550. switch_func(context, pindex, p, dominant)
  1551. else:
  1552. amount = p.total_amount
  1553. # 跌停不能开空和平多,涨停不能开多和平空
  1554. if p.side == "long":
  1555. symbol_low_limit = cur[symbol].low_limit
  1556. dominant_high_limit = cur[dominant].high_limit
  1557. if symbol_last_price <= symbol_low_limit:
  1558. log.warning("标的{}跌停,无法平仓。移仓换月取消。".format(symbol))
  1559. continue
  1560. elif dominant_last_price >= dominant_high_limit:
  1561. log.warning("标的{}涨停,无法开仓。移仓换月取消。".format(dominant))
  1562. continue
  1563. else:
  1564. log.info("进行移仓换月: ({0},long) -> ({1},long)".format(symbol, dominant))
  1565. order_old = order_target(symbol, 0, side='long')
  1566. if order_old != None and order_old.filled > 0:
  1567. order_new = order_target(dominant, amount, side='long')
  1568. if order_new != None and order_new.filled > 0:
  1569. switch_result.append({"before": symbol, "after": dominant, "side": "long"})
  1570. # 换月成功,更新交易记录
  1571. if symbol in g.trade_history:
  1572. # 复制旧的交易记录作为基础
  1573. old_entry_price = g.trade_history[symbol]['entry_price']
  1574. g.trade_history[dominant] = g.trade_history[symbol].copy()
  1575. # 更新成本价为新合约的实际开仓价
  1576. new_entry_price = None
  1577. if order_new.avg_cost and order_new.avg_cost > 0:
  1578. new_entry_price = order_new.avg_cost
  1579. g.trade_history[dominant]['entry_price'] = order_new.avg_cost
  1580. elif order_new.price and order_new.price > 0:
  1581. new_entry_price = order_new.price
  1582. g.trade_history[dominant]['entry_price'] = order_new.price
  1583. else:
  1584. # 如果订单价格无效,使用当前价格作为成本价
  1585. new_entry_price = dominant_last_price
  1586. g.trade_history[dominant]['entry_price'] = dominant_last_price
  1587. # 更新入场时间
  1588. g.trade_history[dominant]['entry_time'] = context.current_dt
  1589. # 更新入场交易日
  1590. g.trade_history[dominant]['entry_trading_day'] = get_current_trading_day(context.current_dt)
  1591. # 删除旧合约的交易记录
  1592. del g.trade_history[symbol]
  1593. log.info(f"移仓换月成本价更新: {symbol} -> {dominant}, "
  1594. f"旧成本价: {old_entry_price:.2f}, 新成本价: {new_entry_price:.2f}")
  1595. else:
  1596. log.warning("标的{}交易失败,无法开仓。移仓换月失败。".format(dominant))
  1597. if p.side == "short":
  1598. symbol_high_limit = cur[symbol].high_limit
  1599. dominant_low_limit = cur[dominant].low_limit
  1600. if symbol_last_price >= symbol_high_limit:
  1601. log.warning("标的{}涨停,无法平仓。移仓换月取消。".format(symbol))
  1602. continue
  1603. elif dominant_last_price <= dominant_low_limit:
  1604. log.warning("标的{}跌停,无法开仓。移仓换月取消。".format(dominant))
  1605. continue
  1606. else:
  1607. log.info("进行移仓换月: ({0},short) -> ({1},short)".format(symbol, dominant))
  1608. order_old = order_target(symbol, 0, side='short')
  1609. if order_old != None and order_old.filled > 0:
  1610. order_new = order_target(dominant, amount, side='short')
  1611. if order_new != None and order_new.filled > 0:
  1612. switch_result.append({"before": symbol, "after": dominant, "side": "short"})
  1613. # 换月成功,更新交易记录
  1614. if symbol in g.trade_history:
  1615. # 复制旧的交易记录作为基础
  1616. old_entry_price = g.trade_history[symbol]['entry_price']
  1617. g.trade_history[dominant] = g.trade_history[symbol].copy()
  1618. # 更新成本价为新合约的实际开仓价
  1619. new_entry_price = None
  1620. if order_new.avg_cost and order_new.avg_cost > 0:
  1621. new_entry_price = order_new.avg_cost
  1622. g.trade_history[dominant]['entry_price'] = order_new.avg_cost
  1623. elif order_new.price and order_new.price > 0:
  1624. new_entry_price = order_new.price
  1625. g.trade_history[dominant]['entry_price'] = order_new.price
  1626. else:
  1627. # 如果订单价格无效,使用当前价格作为成本价
  1628. new_entry_price = dominant_last_price
  1629. g.trade_history[dominant]['entry_price'] = dominant_last_price
  1630. # 更新入场时间
  1631. g.trade_history[dominant]['entry_time'] = context.current_dt
  1632. # 更新入场交易日
  1633. g.trade_history[dominant]['entry_trading_day'] = get_current_trading_day(context.current_dt)
  1634. # 删除旧合约的交易记录
  1635. del g.trade_history[symbol]
  1636. log.info(f"移仓换月成本价更新: {symbol} -> {dominant}, "
  1637. f"旧成本价: {old_entry_price:.2f}, 新成本价: {new_entry_price:.2f}")
  1638. else:
  1639. log.warning("标的{}交易失败,无法开仓。移仓换月失败。".format(dominant))
  1640. if callback:
  1641. callback(context, pindex, p, dominant)
  1642. return switch_result