MAPatternStrategy_v001.py 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. # 导入函数库
  2. from jqdata import *
  3. from jqdata import finance
  4. import pandas as pd
  5. import numpy as np
  6. from datetime import date, datetime, timedelta, time
  7. import re
  8. # 均线形态交易策略 v001
  9. # 基于均线走势(前提条件)+ K线形态(开盘价差、当天价差)的期货交易策略
  10. #
  11. # 核心逻辑:
  12. # 1. 开盘时检查均线走势(MA30<=MA20<=MA10<=MA5为多头,反之为空头)
  13. # 2. 检查开盘价差是否符合方向要求(多头>=0.5%,空头<=-0.5%)
  14. # 3. 14:35和14:55检查当天价差(多头>0,空头<0),满足条件则开仓
  15. # 4. 应用固定止损和动态追踪止盈
  16. # 5. 自动换月移仓
  17. # 设置以便完整打印 DataFrame
  18. pd.set_option('display.max_rows', None)
  19. pd.set_option('display.max_columns', None)
  20. pd.set_option('display.width', None)
  21. pd.set_option('display.max_colwidth', 20)
  22. ## 初始化函数,设定基准等等
  23. def initialize(context):
  24. # 设定沪深300作为基准
  25. set_benchmark('000300.XSHG')
  26. # 开启动态复权模式(真实价格)
  27. set_option('use_real_price', True)
  28. # 输出内容到日志
  29. log.info('=' * 60)
  30. log.info('均线形态交易策略 v001 初始化开始')
  31. log.info('策略类型: 均线走势 + K线形态')
  32. log.info('=' * 60)
  33. ### 期货相关设定 ###
  34. # 设定账户为金融账户
  35. set_subportfolios([SubPortfolioConfig(cash=context.portfolio.starting_cash, type='index_futures')])
  36. # 期货类每笔交易时的手续费是: 买入时万分之0.23,卖出时万分之0.23,平今仓为万分之23
  37. set_order_cost(OrderCost(open_commission=0.000023, close_commission=0.000023, close_today_commission=0.0023), type='index_futures')
  38. # 设置期货交易的滑点
  39. set_slippage(StepRelatedSlippage(2))
  40. # 初始化全局变量
  41. g.usage_percentage = 0.8 # 最大资金使用比例
  42. g.max_margin_per_position = 20000 # 单个标的最大持仓保证金(元)
  43. # 均线策略参数
  44. g.ma_periods = [5, 10, 20, 30] # 均线周期
  45. g.ma_historical_days = 60 # 获取历史数据天数(确保足够计算MA30)
  46. g.ma_open_gap_threshold = 0.002 # 方案1开盘价差阈值(0.2%)
  47. g.ma_pattern_lookback_days = 10 # 历史均线模式一致性检查的天数
  48. g.ma_pattern_consistency_threshold = 0.8 # 历史均线模式一致性阈值(80%)
  49. g.check_intraday_spread = False # 是否检查日内价差(True: 检查, False: 跳过)
  50. g.ma_proximity_min_threshold = 8 # MA5与MA10贴近计数和的最低阈值
  51. # 均线价差策略方案选择
  52. g.ma_gap_strategy_mode = 2 # 策略模式选择(1: 原方案, 2: 新方案)
  53. g.ma_open_gap_threshold2 = 0.002 # 方案2开盘价差阈值(0.2%)
  54. g.ma_intraday_threshold_scheme2 = 0.005 # 方案2日内变化阈值(0.5%)
  55. # 止损止盈策略参数
  56. g.fixed_stop_loss_rate = 0.01 # 固定止损比率(1%)
  57. g.ma_offset_ratio_normal = 0.003 # 均线跟踪止盈常规偏移量(0.3%)
  58. g.ma_offset_ratio_close = 0.01 # 均线跟踪止盈收盘前偏移量(1%)
  59. g.days_for_adjustment = 4 # 持仓天数调整阈值
  60. # 输出策略参数
  61. log.info("均线形态策略参数:")
  62. log.info(f" 均线周期: {g.ma_periods}")
  63. log.info(f" 策略模式: 方案{g.ma_gap_strategy_mode}")
  64. log.info(f" 方案1开盘价差阈值: {g.ma_open_gap_threshold:.1%}")
  65. log.info(f" 方案2开盘价差阈值: {g.ma_open_gap_threshold2:.1%}")
  66. log.info(f" 方案2日内变化阈值: {g.ma_intraday_threshold_scheme2:.1%}")
  67. log.info(f" 历史均线模式检查天数: {g.ma_pattern_lookback_days}天")
  68. log.info(f" 历史均线模式一致性阈值: {g.ma_pattern_consistency_threshold:.1%}")
  69. log.info(f" 均线贴近计数阈值: {g.ma_proximity_min_threshold}")
  70. log.info(f" 是否检查日内价差: {g.check_intraday_spread}")
  71. log.info(f" 固定止损: {g.fixed_stop_loss_rate:.1%}")
  72. log.info(f" 均线跟踪止盈常规偏移: {g.ma_offset_ratio_normal:.1%}")
  73. log.info(f" 均线跟踪止盈收盘前偏移: {g.ma_offset_ratio_close:.1%}")
  74. log.info(f" 持仓天数调整阈值: {g.days_for_adjustment}天")
  75. # 期货品种完整配置字典
  76. g.futures_config = {
  77. # 贵金属
  78. 'AU': {'has_night_session': True, 'margin_rate': {'long': 0.14, 'short': 0.14}, 'multiplier': 1000, 'trading_start_time': '21:00'},
  79. 'AG': {'has_night_session': True, 'margin_rate': {'long': 0.14, 'short': 0.14}, 'multiplier': 15, 'trading_start_time': '21:00'},
  80. # 有色金属
  81. 'CU': {'has_night_session': True, 'margin_rate': {'long': 0.09, 'short': 0.09}, 'multiplier': 5, 'trading_start_time': '21:00'},
  82. 'AL': {'has_night_session': True, 'margin_rate': {'long': 0.09, 'short': 0.09}, 'multiplier': 5, 'trading_start_time': '21:00'},
  83. 'ZN': {'has_night_session': True, 'margin_rate': {'long': 0.09, 'short': 0.09}, 'multiplier': 5, 'trading_start_time': '21:00'},
  84. 'PB': {'has_night_session': True, 'margin_rate': {'long': 0.09, 'short': 0.09}, 'multiplier': 5, 'trading_start_time': '21:00'},
  85. 'NI': {'has_night_session': True, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 1, 'trading_start_time': '21:00'},
  86. 'SN': {'has_night_session': True, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 1, 'trading_start_time': '21:00'},
  87. 'SS': {'has_night_session': True, 'margin_rate': {'long': 0.07, 'short': 0.07}, 'multiplier': 5, 'trading_start_time': '21:00'},
  88. # 黑色系
  89. 'RB': {'has_night_session': True, 'margin_rate': {'long': 0.07, 'short': 0.07}, 'multiplier': 10, 'trading_start_time': '21:00'},
  90. 'HC': {'has_night_session': True, 'margin_rate': {'long': 0.07, 'short': 0.07}, 'multiplier': 10, 'trading_start_time': '21:00'},
  91. 'I': {'has_night_session': True, 'margin_rate': {'long': 0.1, 'short': 0.1}, 'multiplier': 100, 'trading_start_time': '21:00'},
  92. 'JM': {'has_night_session': True, 'margin_rate': {'long': 0.22, 'short': 0.22}, 'multiplier': 100, 'trading_start_time': '21:00'},
  93. 'J': {'has_night_session': True, 'margin_rate': {'long': 0.22, 'short': 0.22}, 'multiplier': 60, 'trading_start_time': '21:00'},
  94. # 能源化工
  95. 'SP': {'has_night_session': True, 'margin_rate': {'long': 0.1, 'short': 0.1}, 'multiplier': 10, 'trading_start_time': '21:00'},
  96. 'FU': {'has_night_session': True, 'margin_rate': {'long': 0.08, 'short': 0.08}, 'multiplier': 10, 'trading_start_time': '21:00'},
  97. 'BU': {'has_night_session': True, 'margin_rate': {'long': 0.04, 'short': 0.04}, 'multiplier': 10, 'trading_start_time': '21:00'},
  98. 'RU': {'has_night_session': True, 'margin_rate': {'long': 0.05, 'short': 0.05}, 'multiplier': 10, 'trading_start_time': '21:00'},
  99. 'BR': {'has_night_session': True, 'margin_rate': {'long': 0.07, 'short': 0.07}, 'multiplier': 5, 'trading_start_time': '21:00'},
  100. 'SC': {'has_night_session': True, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 1000, 'trading_start_time': '21:00'},
  101. 'NR': {'has_night_session': True, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 10, 'trading_start_time': '21:00'},
  102. 'LU': {'has_night_session': True, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 10, 'trading_start_time': '21:00'},
  103. 'LC': {'has_night_session': False, 'margin_rate': {'long': 0.1, 'short': 0.1}, 'multiplier': 1, 'trading_start_time': '09:00'},
  104. # 化工
  105. 'FG': {'has_night_session': True, 'margin_rate': {'long': 0.05, 'short': 0.05}, 'multiplier': 20, 'trading_start_time': '21:00'},
  106. 'TA': {'has_night_session': True, 'margin_rate': {'long': 0.05, 'short': 0.05}, 'multiplier': 5, 'trading_start_time': '21:00'},
  107. 'MA': {'has_night_session': True, 'margin_rate': {'long': 0.05, 'short': 0.05}, 'multiplier': 10, 'trading_start_time': '21:00'},
  108. 'SA': {'has_night_session': True, 'margin_rate': {'long': 0.05, 'short': 0.05}, 'multiplier': 20, 'trading_start_time': '21:00'},
  109. 'L': {'has_night_session': True, 'margin_rate': {'long': 0.07, 'short': 0.07}, 'multiplier': 5, 'trading_start_time': '21:00'},
  110. 'V': {'has_night_session': True, 'margin_rate': {'long': 0.07, 'short': 0.07}, 'multiplier': 5, 'trading_start_time': '21:00'},
  111. 'EG': {'has_night_session': True, 'margin_rate': {'long': 0.05, 'short': 0.05}, 'multiplier': 10, 'trading_start_time': '21:00'},
  112. 'PP': {'has_night_session': True, 'margin_rate': {'long': 0.07, 'short': 0.07}, 'multiplier': 5, 'trading_start_time': '21:00'},
  113. 'EB': {'has_night_session': True, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 5, 'trading_start_time': '21:00'},
  114. 'PG': {'has_night_session': True, 'margin_rate': {'long': 0.05, 'short': 0.05}, 'multiplier': 20, 'trading_start_time': '21:00'},
  115. # 农产品
  116. 'RM': {'has_night_session': True, 'margin_rate': {'long': 0.05, 'short': 0.05}, 'multiplier': 10, 'trading_start_time': '21:00'},
  117. 'OI': {'has_night_session': True, 'margin_rate': {'long': 0.05, 'short': 0.05}, 'multiplier': 10, 'trading_start_time': '21:00'},
  118. 'CF': {'has_night_session': True, 'margin_rate': {'long': 0.05, 'short': 0.05}, 'multiplier': 5, 'trading_start_time': '21:00'},
  119. 'SR': {'has_night_session': True, 'margin_rate': {'long': 0.05, 'short': 0.05}, 'multiplier': 10, 'trading_start_time': '21:00'},
  120. 'PF': {'has_night_session': True, 'margin_rate': {'long': 0.1, 'short': 0.1}, 'multiplier': 5, 'trading_start_time': '21:00'},
  121. 'C': {'has_night_session': True, 'margin_rate': {'long': 0.07, 'short': 0.07}, 'multiplier': 10, 'trading_start_time': '21:00'},
  122. 'CS': {'has_night_session': True, 'margin_rate': {'long': 0.07, 'short': 0.07}, 'multiplier': 10, 'trading_start_time': '21:00'},
  123. 'CY': {'has_night_session': True, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 5, 'trading_start_time': '21:00'},
  124. 'A': {'has_night_session': True, 'margin_rate': {'long': 0.07, 'short': 0.07}, 'multiplier': 10, 'trading_start_time': '21:00'},
  125. 'B': {'has_night_session': True, 'margin_rate': {'long': 0.05, 'short': 0.05}, 'multiplier': 10, 'trading_start_time': '21:00'},
  126. 'M': {'has_night_session': True, 'margin_rate': {'long': 0.07, 'short': 0.07}, 'multiplier': 10, 'trading_start_time': '21:00'},
  127. 'Y': {'has_night_session': True, 'margin_rate': {'long': 0.05, 'short': 0.05}, 'multiplier': 10, 'trading_start_time': '21:00'},
  128. 'P': {'has_night_session': True, 'margin_rate': {'long': 0.05, 'short': 0.05}, 'multiplier': 10, 'trading_start_time': '21:00'},
  129. # 无夜盘品种
  130. 'IF': {'has_night_session': False, 'margin_rate': {'long': 0.08, 'short': 0.08}, 'multiplier': 300, 'trading_start_time': '09:30'},
  131. 'IH': {'has_night_session': False, 'margin_rate': {'long': 0.08, 'short': 0.08}, 'multiplier': 300, 'trading_start_time': '09:30'},
  132. 'IC': {'has_night_session': False, 'margin_rate': {'long': 0.08, 'short': 0.08}, 'multiplier': 200, 'trading_start_time': '09:30'},
  133. 'IM': {'has_night_session': False, 'margin_rate': {'long': 0.08, 'short': 0.08}, 'multiplier': 200, 'trading_start_time': '09:30'},
  134. 'AP': {'has_night_session': False, 'margin_rate': {'long': 0.08, 'short': 0.08}, 'multiplier': 10, 'trading_start_time': '09:00'},
  135. 'CJ': {'has_night_session': False, 'margin_rate': {'long': 0.09, 'short': 0.09}, 'multiplier': 5, 'trading_start_time': '09:00'},
  136. 'PK': {'has_night_session': False, 'margin_rate': {'long': 0.05, 'short': 0.05}, 'multiplier': 5, 'trading_start_time': '09:00'},
  137. 'JD': {'has_night_session': False, 'margin_rate': {'long': 0.07, 'short': 0.07}, 'multiplier': 10, 'trading_start_time': '09:00'},
  138. 'LH': {'has_night_session': False, 'margin_rate': {'long': 0.1, 'short': 0.1}, 'multiplier': 16, 'trading_start_time': '09:00'}
  139. }
  140. # 策略品种选择策略配置
  141. # 方案1:全品种策略 - 考虑所有配置的期货品种
  142. g.strategy_focus_symbols = ['IC', 'LH'] # 空列表表示考虑所有品种
  143. # 方案2:精选品种策略 - 只交易流动性较好的特定品种(如需使用请取消下行注释)
  144. # g.strategy_focus_symbols = ['RM', 'CJ', 'CY', 'JD', 'L', 'LC', 'SF', 'SI']
  145. log.info(f"品种选择策略: {'全品种策略(覆盖所有配置品种)' if not g.strategy_focus_symbols else '精选品种策略(' + str(len(g.strategy_focus_symbols)) + '个品种)'}")
  146. # 交易记录和数据存储
  147. g.trade_history = {} # 持仓记录 {symbol: {'entry_price': xxx, 'direction': xxx, ...}}
  148. g.daily_ma_candidates = {} # 通过均线和开盘价差检查的候选品种 {symbol: {'direction': 'long'/'short', 'open_price': xxx, ...}}
  149. g.today_trades = [] # 当日交易记录
  150. g.excluded_contracts = {} # 每日排除的合约缓存 {dominant_future: {'reason': 'ma_trend'/'open_gap', 'trading_day': xxx}}
  151. g.ma_checked_underlyings = {} # 记录各品种在交易日的均线检查状态 {symbol: trading_day}
  152. g.last_ma_trading_day = None # 最近一次均线检查所属交易日
  153. # 定时任务设置
  154. # 夜盘开始(21:05) - 均线和开盘价差检查
  155. run_daily(check_ma_trend_and_open_gap, time='21:05:00', reference_security='IF1808.CCFX')
  156. # 日盘开始 - 均线和开盘价差检查
  157. run_daily(check_ma_trend_and_open_gap, time='09:05:00', reference_security='IF1808.CCFX')
  158. run_daily(check_ma_trend_and_open_gap, time='09:35:00', reference_security='IF1808.CCFX')
  159. # 盘中价差检查和开仓(14:35和14:55)
  160. run_daily(check_intraday_price_diff, time='14:35:00', reference_security='IF1808.CCFX')
  161. run_daily(check_intraday_price_diff, time='14:55:00', reference_security='IF1808.CCFX')
  162. # 夜盘止损止盈检查
  163. run_daily(check_stop_loss_profit, time='21:05:00', reference_security='IF1808.CCFX')
  164. run_daily(check_stop_loss_profit, time='21:35:00', reference_security='IF1808.CCFX')
  165. run_daily(check_stop_loss_profit, time='22:05:00', reference_security='IF1808.CCFX')
  166. run_daily(check_stop_loss_profit, time='22:35:00', reference_security='IF1808.CCFX')
  167. # 日盘止损止盈检查
  168. run_daily(check_stop_loss_profit, time='09:05:00', reference_security='IF1808.CCFX')
  169. run_daily(check_stop_loss_profit, time='09:35:00', reference_security='IF1808.CCFX')
  170. run_daily(check_stop_loss_profit, time='10:05:00', reference_security='IF1808.CCFX')
  171. run_daily(check_stop_loss_profit, time='10:35:00', reference_security='IF1808.CCFX')
  172. run_daily(check_stop_loss_profit, time='11:05:00', reference_security='IF1808.CCFX')
  173. run_daily(check_stop_loss_profit, time='11:25:00', reference_security='IF1808.CCFX')
  174. run_daily(check_stop_loss_profit, time='13:35:00', reference_security='IF1808.CCFX')
  175. run_daily(check_stop_loss_profit, time='14:05:00', reference_security='IF1808.CCFX')
  176. run_daily(check_stop_loss_profit, time='14:35:00', reference_security='IF1808.CCFX')
  177. run_daily(check_stop_loss_profit, time='14:55:00', reference_security='IF1808.CCFX')
  178. # 收盘后
  179. run_daily(after_market_close, time='15:30:00', reference_security='IF1808.CCFX')
  180. log.info('=' * 60)
  181. ############################ 主程序执行函数 ###################################
  182. def get_current_trading_day(current_dt):
  183. """根据当前时间推断对应的期货交易日"""
  184. current_date = current_dt.date()
  185. current_time = current_dt.time()
  186. trade_days = get_trade_days(end_date=current_date, count=1)
  187. if trade_days and trade_days[0] == current_date:
  188. trading_day = current_date
  189. else:
  190. next_days = get_trade_days(start_date=current_date, count=1)
  191. trading_day = next_days[0] if next_days else current_date
  192. if current_time >= time(20, 59):
  193. next_trade_days = get_trade_days(start_date=trading_day, count=2)
  194. if len(next_trade_days) >= 2:
  195. return next_trade_days[1]
  196. if len(next_trade_days) == 1:
  197. return next_trade_days[0]
  198. return trading_day
  199. def normalize_trade_day_value(value):
  200. """将交易日对象统一转换为 datetime.date"""
  201. if isinstance(value, date) and not isinstance(value, datetime):
  202. return value
  203. if isinstance(value, datetime):
  204. return value.date()
  205. if hasattr(value, 'to_pydatetime'):
  206. return value.to_pydatetime().date()
  207. try:
  208. return pd.Timestamp(value).date()
  209. except Exception:
  210. return value
  211. def check_ma_trend_and_open_gap(context):
  212. """阶段一:开盘时均线走势和开盘价差检查(一天一次)"""
  213. log.info("=" * 60)
  214. current_trading_day = get_current_trading_day(context.current_dt)
  215. log.info(f"执行均线走势和开盘价差检查 - 时间: {context.current_dt}, 交易日: {current_trading_day}")
  216. log.info("=" * 60)
  217. # 先检查换月移仓
  218. position_auto_switch(context)
  219. # 检查是否进入新交易日,必要时清空缓存
  220. if g.last_ma_trading_day != current_trading_day:
  221. if g.excluded_contracts:
  222. log.info(f"交易日切换至 {current_trading_day},清空上一交易日的排除缓存")
  223. g.excluded_contracts = {}
  224. g.ma_checked_underlyings = {}
  225. g.last_ma_trading_day = current_trading_day
  226. # 获取当前时间
  227. current_time = str(context.current_dt.time())[:5] # HH:MM格式
  228. # 筛选可交易品种(根据交易开始时间判断)
  229. focus_symbols = g.strategy_focus_symbols if g.strategy_focus_symbols else list(g.futures_config.keys())
  230. tradable_symbols = []
  231. # 根据当前时间确定可交易的时段
  232. # 21:05 -> 仅接受21:00开盘的合约
  233. # 09:05 -> 接受09:00或21:00开盘的合约
  234. # 09:35 -> 接受所有时段(21:00, 09:00, 09:30)的合约
  235. for symbol in focus_symbols:
  236. trading_start_time = get_futures_config(symbol, 'trading_start_time', '09:05')
  237. should_trade = False
  238. if current_time == '21:05':
  239. # 夜盘开盘:仅接受21:00开盘的品种
  240. should_trade = trading_start_time.startswith('21:00')
  241. elif current_time == '09:05':
  242. # 日盘早盘:接受21:00和09:00开盘的品种
  243. should_trade = trading_start_time.startswith('21:00') or trading_start_time.startswith('09:00')
  244. elif current_time == '09:35':
  245. # 日盘晚开:接受所有品种(21:00, 09:00, 09:30)
  246. should_trade = True
  247. if should_trade:
  248. tradable_symbols.append(symbol)
  249. if not tradable_symbols:
  250. log.info(f"当前时间 {current_time} 无品种开盘,跳过检查")
  251. return
  252. log.info(f"当前时间 {current_time} 开盘品种: {tradable_symbols}")
  253. # 对每个品种执行均线和开盘价差检查
  254. for symbol in tradable_symbols:
  255. if g.ma_checked_underlyings.get(symbol) == current_trading_day:
  256. log.info(f"{symbol} 已在交易日 {current_trading_day} 完成均线检查,跳过本次执行")
  257. continue
  258. try:
  259. g.ma_checked_underlyings[symbol] = current_trading_day
  260. # 获取主力合约
  261. dominant_future = get_dominant_future(symbol)
  262. # log.debug(f"{symbol} 主力合约: {dominant_future}")
  263. if not dominant_future:
  264. log.info(f"{symbol} 未找到主力合约,跳过")
  265. continue
  266. # 检查是否在排除缓存中(当日已检查过但不符合条件)
  267. if dominant_future in g.excluded_contracts:
  268. excluded_info = g.excluded_contracts[dominant_future]
  269. if excluded_info['trading_day'] == current_trading_day:
  270. # log.debug(f"{symbol} 在排除缓存中(原因: {excluded_info['reason']}),跳过")
  271. continue
  272. else:
  273. # 新的一天,从缓存中移除(会在after_market_close统一清理,这里也做兜底)
  274. del g.excluded_contracts[dominant_future]
  275. # 检查是否已有持仓
  276. if check_symbol_prefix_match(dominant_future, set(g.trade_history.keys())):
  277. log.info(f"{symbol} 已有持仓,跳过")
  278. continue
  279. # 获取历史数据(需要足够计算MA30)
  280. # 使用get_price获取数据,可以正确处理夜盘品种
  281. # 注意:historical_data最后一行是昨天的数据,不包含今天的数据
  282. historical_data = get_price(dominant_future, end_date=context.current_dt,
  283. frequency='1d', fields=['open', 'close', 'high', 'low'],
  284. count=g.ma_historical_days)
  285. if historical_data is None or len(historical_data) < max(g.ma_periods):
  286. log.info(f"{symbol} 历史数据不足,跳过")
  287. continue
  288. previous_trade_days = get_trade_days(end_date=current_trading_day, count=2)
  289. previous_trade_days = [normalize_trade_day_value(d) for d in previous_trade_days]
  290. previous_trading_day = None
  291. if len(previous_trade_days) >= 2:
  292. previous_trading_day = previous_trade_days[-2]
  293. elif len(previous_trade_days) == 1 and previous_trade_days[0] < current_trading_day:
  294. previous_trading_day = previous_trade_days[0]
  295. if previous_trading_day is None:
  296. log.info(f"{symbol} 无法确定前一交易日,跳过")
  297. continue
  298. historical_dates = historical_data.index.date
  299. match_indices = np.where(historical_dates == previous_trading_day)[0]
  300. if len(match_indices) == 0:
  301. earlier_indices = np.where(historical_dates < previous_trading_day)[0]
  302. if len(earlier_indices) == 0:
  303. log.info(f"{symbol} 历史数据缺少 {previous_trading_day} 之前的记录,跳过")
  304. continue
  305. match_indices = [earlier_indices[-1]]
  306. data_upto_yesterday = historical_data.iloc[:match_indices[-1] + 1]
  307. # log.debug(f"data_upto_yesterday: {data_upto_yesterday}")
  308. yesterday_data = data_upto_yesterday.iloc[-1]
  309. yesterday_close = yesterday_data['close']
  310. # 获取今天的开盘价(使用get_current_data API)
  311. current_data = get_current_data()[dominant_future]
  312. today_open = current_data.day_open
  313. # log.info(f" 历史数据时间范围: {historical_data.index[0]} 至 {historical_data.index[-1]}")
  314. # 计算昨天的均线值(使用截至前一交易日的数据)
  315. ma_values = calculate_ma_values(data_upto_yesterday, g.ma_periods)
  316. ma_proximity_counts = calculate_ma_proximity_counts(data_upto_yesterday, g.ma_periods, g.ma_pattern_lookback_days)
  317. log.info(f"{symbol}({dominant_future}) 均线检查:")
  318. # log.debug(f"yesterday_data: {yesterday_data}")
  319. # log.info(f" 昨收: {yesterday_close:.2f}, 今开: {today_open:.2f}")
  320. # log.info(f" 昨日均线 - MA5: {ma_values['MA5']:.2f}, MA10: {ma_values['MA10']:.2f}, "
  321. # f"MA20: {ma_values['MA20']:.2f}, MA30: {ma_values['MA30']:.2f}")
  322. log.info(f" 均线贴近统计: {ma_proximity_counts}")
  323. proximity_sum = ma_proximity_counts.get('MA5', 0) + ma_proximity_counts.get('MA10', 0)
  324. if proximity_sum < g.ma_proximity_min_threshold:
  325. log.info(f" {symbol}({dominant_future}) ✗ 均线贴近计数不足,MA5+MA10={proximity_sum} < {g.ma_proximity_min_threshold},跳过")
  326. g.excluded_contracts[dominant_future] = {
  327. 'reason': 'ma_proximity',
  328. 'trading_day': current_trading_day
  329. }
  330. continue
  331. # 判断均线走势(使用新的灵活模式检查)
  332. direction = None
  333. if check_ma_pattern(ma_values, 'long'):
  334. direction = 'long'
  335. # log.info(f" {symbol}({dominant_future}) 均线走势判断: 多头排列")
  336. elif check_ma_pattern(ma_values, 'short'):
  337. direction = 'short'
  338. # log.info(f" {symbol}({dominant_future}) 均线走势判断: 空头排列")
  339. else:
  340. # log.info(f" 均线走势判断: 不符合多头或空头排列,跳过")
  341. # 将不符合条件的合约加入排除缓存
  342. g.excluded_contracts[dominant_future] = {
  343. 'reason': 'ma_trend',
  344. 'trading_day': current_trading_day
  345. }
  346. continue
  347. # 检查历史均线模式一致性
  348. consistency_passed, consistency_ratio = check_historical_ma_pattern_consistency(
  349. historical_data, direction, g.ma_pattern_lookback_days, g.ma_pattern_consistency_threshold
  350. )
  351. if not consistency_passed:
  352. log.info(f" {symbol}({dominant_future}) ✗ 历史均线模式一致性不足 "
  353. f"({consistency_ratio:.1%} < {g.ma_pattern_consistency_threshold:.1%}),跳过")
  354. g.excluded_contracts[dominant_future] = {
  355. 'reason': 'ma_consistency',
  356. 'trading_day': current_trading_day
  357. }
  358. continue
  359. else:
  360. log.info(f" {symbol}({dominant_future}) ✓ 历史均线模式一致性检查通过 "
  361. f"({consistency_ratio:.1%} >= {g.ma_pattern_consistency_threshold:.1%})")
  362. # 计算开盘价差比例
  363. open_gap_ratio = (today_open - yesterday_close) / yesterday_close
  364. log.info(f" 开盘价差检查: 昨收 {yesterday_close:.2f}, 今开 {today_open:.2f}, "
  365. f"价差比例 {open_gap_ratio:.2%}")
  366. # 检查开盘价差是否符合方向要求
  367. gap_check_passed = False
  368. if g.ma_gap_strategy_mode == 1:
  369. # 方案1:多头检查上跳,空头检查下跳
  370. if direction == 'long' and open_gap_ratio >= g.ma_open_gap_threshold:
  371. log.info(f" {symbol}({dominant_future}) ✓ 方案1多头开盘价差检查通过 ({open_gap_ratio:.2%} >= {g.ma_open_gap_threshold:.2%})")
  372. gap_check_passed = True
  373. elif direction == 'short' and open_gap_ratio <= -g.ma_open_gap_threshold:
  374. log.info(f" {symbol}({dominant_future}) ✓ 方案1空头开盘价差检查通过 ({open_gap_ratio:.2%} <= {-g.ma_open_gap_threshold:.2%})")
  375. gap_check_passed = True
  376. elif g.ma_gap_strategy_mode == 2:
  377. # 方案2:多头检查下跳,空头检查上跳
  378. if direction == 'long' and open_gap_ratio <= -g.ma_open_gap_threshold2:
  379. log.info(f" {symbol}({dominant_future}) ✓ 方案2多头开盘价差检查通过 ({open_gap_ratio:.2%} <= {-g.ma_open_gap_threshold2:.2%})")
  380. gap_check_passed = True
  381. elif direction == 'short' and open_gap_ratio >= g.ma_open_gap_threshold2:
  382. log.info(f" {symbol}({dominant_future}) ✓ 方案2空头开盘价差检查通过 ({open_gap_ratio:.2%} >= {g.ma_open_gap_threshold2:.2%})")
  383. gap_check_passed = True
  384. if not gap_check_passed:
  385. # log.info(f" ✗ 开盘价差不符合方案{g.ma_gap_strategy_mode} {direction}方向要求,跳过")
  386. # 将不符合条件的合约加入排除缓存
  387. g.excluded_contracts[dominant_future] = {
  388. 'reason': 'open_gap',
  389. 'trading_day': current_trading_day
  390. }
  391. continue
  392. # 将通过检查的品种加入候选列表
  393. g.daily_ma_candidates[dominant_future] = {
  394. 'symbol': symbol,
  395. 'direction': direction,
  396. 'open_price': today_open,
  397. 'yesterday_close': yesterday_close,
  398. 'ma_values': ma_values
  399. }
  400. log.info(f" ✓✓ {symbol} 通过均线和开盘价差检查,加入候选列表")
  401. except Exception as e:
  402. g.ma_checked_underlyings.pop(symbol, None)
  403. log.warning(f"{symbol} 检查时出错: {str(e)}")
  404. continue
  405. log.info(f"候选列表更新完成,当前候选品种: {list(g.daily_ma_candidates.keys())}")
  406. log.info("=" * 60)
  407. def check_intraday_price_diff(context):
  408. """阶段二:盘中价差检查和开仓(14:35和14:55)"""
  409. log.info("=" * 60)
  410. log.info(f"执行当天价差检查和开仓逻辑 - 时间: {context.current_dt}")
  411. log.info("=" * 60)
  412. # 先检查换月移仓
  413. position_auto_switch(context)
  414. if not g.daily_ma_candidates:
  415. log.info("当前无候选品种,跳过")
  416. return
  417. log.info(f"候选品种数量: {len(g.daily_ma_candidates)}")
  418. # 遍历候选品种
  419. candidates_to_remove = []
  420. for dominant_future, candidate_info in g.daily_ma_candidates.items():
  421. try:
  422. symbol = candidate_info['symbol']
  423. direction = candidate_info['direction']
  424. open_price = candidate_info['open_price']
  425. # 再次检查是否已有持仓
  426. if check_symbol_prefix_match(dominant_future, set(g.trade_history.keys())):
  427. log.info(f"{symbol} 已有持仓,从候选列表移除")
  428. candidates_to_remove.append(dominant_future)
  429. continue
  430. # 获取当前价格
  431. current_data = get_current_data()[dominant_future]
  432. current_price = current_data.last_price
  433. # 计算当天价差
  434. intraday_diff = current_price - open_price
  435. intraday_diff_ratio = intraday_diff / open_price # 计算相对变化比例
  436. log.info(f"{symbol}({dominant_future}) 当天价差检查:")
  437. log.info(f" 方向: {direction}, 开盘价: {open_price:.2f}, 当前价: {current_price:.2f}, "
  438. f"当天价差: {intraday_diff:.2f}, 变化比例: {intraday_diff_ratio:.2%}")
  439. # 判断是否满足开仓条件
  440. should_open = False
  441. if g.ma_gap_strategy_mode == 1:
  442. # 方案1:根据参数决定是否检查日内价差
  443. if not g.check_intraday_spread:
  444. # 跳过日内价差检查,直接允许开仓
  445. log.info(f" 方案1跳过日内价差检查(check_intraday_spread=False)")
  446. should_open = True
  447. elif direction == 'long' and intraday_diff > 0:
  448. log.info(f" ✓ 方案1多头当天价差检查通过 ({intraday_diff:.2f} > 0)")
  449. should_open = True
  450. elif direction == 'short' and intraday_diff < 0:
  451. log.info(f" ✓ 方案1空头当天价差检查通过 ({intraday_diff:.2f} < 0)")
  452. should_open = True
  453. else:
  454. log.info(f" ✗ 方案1当天价差不符合{direction}方向要求")
  455. elif g.ma_gap_strategy_mode == 2:
  456. # 方案2:强制检查日内变化,使用专用阈值
  457. if direction == 'long' and intraday_diff_ratio >= g.ma_intraday_threshold_scheme2:
  458. log.info(f" ✓ 方案2多头日内变化检查通过 ({intraday_diff_ratio:.2%} >= {g.ma_intraday_threshold_scheme2:.2%})")
  459. should_open = True
  460. elif direction == 'short' and intraday_diff_ratio <= -g.ma_intraday_threshold_scheme2:
  461. log.info(f" ✓ 方案2空头日内变化检查通过 ({intraday_diff_ratio:.2%} <= {-g.ma_intraday_threshold_scheme2:.2%})")
  462. should_open = True
  463. else:
  464. log.info(f" ✗ 方案2日内变化不符合{direction}方向要求(阈值: ±{g.ma_intraday_threshold_scheme2:.2%})")
  465. if should_open:
  466. # 执行开仓
  467. log.info(f" 准备开仓: {symbol} {direction}")
  468. target_hands = calculate_target_hands(context, dominant_future, direction)
  469. if target_hands > 0:
  470. success = open_position(context, dominant_future, target_hands, direction,
  471. f'均线形态开仓')
  472. if success:
  473. log.info(f" ✓✓ {symbol} 开仓成功,从候选列表移除")
  474. candidates_to_remove.append(dominant_future)
  475. else:
  476. log.warning(f" ✗ {symbol} 开仓失败")
  477. else:
  478. log.warning(f" ✗ {symbol} 计算目标手数为0,跳过开仓")
  479. except Exception as e:
  480. log.warning(f"{dominant_future} 处理时出错: {str(e)}")
  481. continue
  482. # 从候选列表中移除已开仓的品种
  483. for future in candidates_to_remove:
  484. if future in g.daily_ma_candidates:
  485. del g.daily_ma_candidates[future]
  486. log.info(f"剩余候选品种: {list(g.daily_ma_candidates.keys())}")
  487. log.info("=" * 60)
  488. def check_stop_loss_profit(context):
  489. """阶段三:止损止盈检查(所有时间点)"""
  490. # 先检查换月移仓
  491. position_auto_switch(context)
  492. # 获取当前时间
  493. current_time = str(context.current_dt.time())[:2]
  494. # 判断是否为夜盘时间
  495. is_night_session = (current_time in ['21', '22', '23', '00', '01', '02'])
  496. # 遍历所有持仓进行止损止盈检查
  497. subportfolio = context.subportfolios[0]
  498. long_positions = list(subportfolio.long_positions.values())
  499. short_positions = list(subportfolio.short_positions.values())
  500. closed_count = 0
  501. skipped_count = 0
  502. for position in long_positions + short_positions:
  503. security = position.security
  504. underlying_symbol = security.split('.')[0][:-4]
  505. # 检查交易时间适配性
  506. has_night_session = get_futures_config(underlying_symbol, 'has_night_session', False)
  507. # 如果是夜盘时间,但品种不支持夜盘交易,则跳过
  508. if is_night_session and not has_night_session:
  509. skipped_count += 1
  510. continue
  511. # 执行止损止盈检查
  512. if check_position_stop_loss_profit(context, position):
  513. closed_count += 1
  514. if closed_count > 0:
  515. log.info(f"执行了 {closed_count} 次止损止盈")
  516. if skipped_count > 0:
  517. log.info(f"夜盘时间跳过 {skipped_count} 个日间品种的止损止盈检查")
  518. def check_position_stop_loss_profit(context, position):
  519. """检查单个持仓的止损止盈"""
  520. security = position.security
  521. if security not in g.trade_history:
  522. return False
  523. trade_info = g.trade_history[security]
  524. direction = trade_info['direction']
  525. entry_price = trade_info['entry_price']
  526. entry_time = trade_info['entry_time']
  527. current_price = position.price
  528. # 计算当前盈亏比率
  529. if direction == 'long':
  530. profit_rate = (current_price - entry_price) / entry_price
  531. else:
  532. profit_rate = (entry_price - current_price) / entry_price
  533. # 检查固定止损
  534. if profit_rate <= -g.fixed_stop_loss_rate:
  535. log.info(f"触发固定止损 {security} {direction}, 当前亏损率: {profit_rate:.3%}, "
  536. f"成本价: {entry_price:.2f}, 当前价格: {current_price:.2f}")
  537. close_position(context, security, direction)
  538. return True
  539. # 检查是否启用均线跟踪止盈
  540. if not trade_info.get('ma_trailing_enabled', True):
  541. return False
  542. # 检查均线跟踪止盈
  543. # 获取持仓天数
  544. entry_date = entry_time.date()
  545. current_date = context.current_dt.date()
  546. all_trade_days = get_all_trade_days()
  547. holding_days = sum((entry_date <= d <= current_date) for d in all_trade_days)
  548. # 计算变化率
  549. today_price = get_current_data()[security].last_price
  550. avg_daily_change_rate = calculate_average_daily_change_rate(security)
  551. historical_data = attribute_history(security, 1, '1d', ['close'])
  552. yesterday_close = historical_data['close'].iloc[-1]
  553. today_change_rate = abs((today_price - yesterday_close) / yesterday_close)
  554. # 根据时间判断使用的偏移量
  555. current_time = context.current_dt.time()
  556. target_time = datetime.strptime('14:55:00', '%H:%M:%S').time()
  557. if current_time > target_time:
  558. offset_ratio = g.ma_offset_ratio_close
  559. else:
  560. offset_ratio = g.ma_offset_ratio_normal
  561. # 选择止损均线
  562. close_line = None
  563. if today_change_rate >= 1.5 * avg_daily_change_rate:
  564. close_line = 'ma5' # 波动剧烈时用短周期
  565. elif holding_days <= g.days_for_adjustment:
  566. close_line = 'ma5' # 持仓初期用短周期
  567. else:
  568. close_line = 'ma5' if today_change_rate >= 1.2 * avg_daily_change_rate else 'ma10'
  569. # 计算实时均线值
  570. ma_values = calculate_realtime_ma_values(security, [5, 10])
  571. ma_value = ma_values[close_line]
  572. # 应用偏移量
  573. if direction == 'long':
  574. adjusted_ma_value = ma_value * (1 - offset_ratio)
  575. else:
  576. adjusted_ma_value = ma_value * (1 + offset_ratio)
  577. # 判断是否触发均线止损
  578. if (direction == 'long' and today_price < adjusted_ma_value) or \
  579. (direction == 'short' and today_price > adjusted_ma_value):
  580. log.info(f"触发均线跟踪止盈 {security} {direction}, 止损均线: {close_line}, "
  581. f"均线值: {ma_value:.2f}, 调整后: {adjusted_ma_value:.2f}, "
  582. f"当前价: {today_price:.2f}, 持仓天数: {holding_days}")
  583. close_position(context, security, direction)
  584. return True
  585. return False
  586. ############################ 核心辅助函数 ###################################
  587. def calculate_ma_values(data, periods):
  588. """计算均线值
  589. Args:
  590. data: DataFrame,包含'close'列的历史数据(最后一行是最新的数据)
  591. periods: list,均线周期列表,如[5, 10, 20, 30]
  592. Returns:
  593. dict: {'MA5': value, 'MA10': value, 'MA20': value, 'MA30': value}
  594. 返回最后一行(最新日期)的各周期均线值
  595. """
  596. ma_values = {}
  597. for period in periods:
  598. if len(data) >= period:
  599. # 计算最后period天的均线值
  600. ma_values[f'MA{period}'] = data['close'].iloc[-period:].mean()
  601. else:
  602. ma_values[f'MA{period}'] = None
  603. return ma_values
  604. def calculate_ma_proximity_counts(data, periods, lookback_days):
  605. """统计近 lookback_days 天收盘价贴近各均线的次数"""
  606. proximity_counts = {f'MA{period}': 0 for period in periods}
  607. if len(data) < lookback_days:
  608. return proximity_counts
  609. closes = data['close'].iloc[-lookback_days:]
  610. ma_series = {
  611. period: data['close'].rolling(window=period).mean().iloc[-lookback_days:]
  612. for period in periods
  613. }
  614. for idx, close_price in enumerate(closes):
  615. min_diff = None
  616. closest_period = None
  617. for period in periods:
  618. ma_value = ma_series[period].iloc[idx]
  619. if pd.isna(ma_value):
  620. continue
  621. diff = abs(close_price - ma_value)
  622. if min_diff is None or diff < min_diff:
  623. min_diff = diff
  624. closest_period = period
  625. if closest_period is not None:
  626. proximity_counts[f'MA{closest_period}'] += 1
  627. return proximity_counts
  628. def check_ma_pattern(ma_values, direction):
  629. """检查均线排列模式是否符合方向要求
  630. Args:
  631. ma_values: dict,包含MA5, MA10, MA20, MA30的均线值
  632. direction: str,'long'或'short'
  633. Returns:
  634. bool: 是否符合均线排列要求
  635. """
  636. ma5 = ma_values['MA5']
  637. ma10 = ma_values['MA10']
  638. ma20 = ma_values['MA20']
  639. ma30 = ma_values['MA30']
  640. if direction == 'long':
  641. # 多头模式:MA30 <= MA20 <= MA10 <= MA5 或 MA30 <= MA20 <= MA5 <= MA10
  642. pattern1 = (ma30 <= ma20 <= ma10 <= ma5)
  643. pattern2 = (ma30 <= ma20 <= ma5 <= ma10)
  644. return pattern1 or pattern2
  645. elif direction == 'short':
  646. # 空头模式:MA10 <= MA5 <= MA20 <= MA30 或 MA5 <= MA10 <= MA20 <= MA30
  647. pattern1 = (ma10 <= ma5 <= ma20 <= ma30)
  648. pattern2 = (ma5 <= ma10 <= ma20 <= ma30)
  649. return pattern1 or pattern2
  650. else:
  651. return False
  652. def check_historical_ma_pattern_consistency(historical_data, direction, lookback_days, consistency_threshold):
  653. """检查历史均线模式的一致性
  654. Args:
  655. historical_data: DataFrame,包含足够天数的历史数据
  656. direction: str,'long'或'short'
  657. lookback_days: int,检查过去多少天
  658. consistency_threshold: float,一致性阈值(0-1之间)
  659. Returns:
  660. tuple: (bool, float) - (是否通过一致性检查, 实际一致性比例)
  661. """
  662. if len(historical_data) < max(g.ma_periods) + lookback_days:
  663. # 历史数据不足
  664. return False, 0.0
  665. match_count = 0
  666. total_count = lookback_days
  667. # 检查过去lookback_days天的均线模式
  668. for i in range(lookback_days):
  669. # 获取倒数第(i+1)天的数据(i=0时是昨天,i=1时是前天,依此类推)
  670. end_idx = -(i + 1)
  671. if end_idx == -1:
  672. data_slice = historical_data
  673. else:
  674. data_slice = historical_data.iloc[:end_idx]
  675. # 计算该天的均线值
  676. ma_values = calculate_ma_values(data_slice, g.ma_periods)
  677. # 检查是否符合模式
  678. if check_ma_pattern(ma_values, direction):
  679. match_count += 1
  680. consistency_ratio = match_count / total_count
  681. passed = consistency_ratio >= consistency_threshold
  682. return passed, consistency_ratio
  683. ############################ 交易执行函数 ###################################
  684. def open_position(context, security, target_hands, direction, reason=''):
  685. """开仓"""
  686. try:
  687. # 记录交易前的可用资金
  688. cash_before = context.portfolio.available_cash
  689. # 使用order_target按手数开仓
  690. order = order_target(security, target_hands, side=direction)
  691. if order is not None and order.filled > 0:
  692. # 记录交易后的可用资金
  693. cash_after = context.portfolio.available_cash
  694. # 计算实际资金变化
  695. cash_change = cash_before - cash_after
  696. # 获取订单价格和数量
  697. order_price = order.avg_cost if order.avg_cost else order.price
  698. order_amount = order.filled
  699. # 记录当日交易
  700. underlying_symbol = security.split('.')[0][:-4]
  701. g.today_trades.append({
  702. 'security': security,
  703. 'underlying_symbol': underlying_symbol,
  704. 'direction': direction,
  705. 'order_amount': order_amount,
  706. 'order_price': order_price,
  707. 'cash_change': cash_change,
  708. 'time': context.current_dt
  709. })
  710. # 记录交易信息
  711. g.trade_history[security] = {
  712. 'entry_price': order_price,
  713. 'target_hands': target_hands,
  714. 'actual_hands': order_amount,
  715. 'actual_margin': cash_change,
  716. 'direction': direction,
  717. 'entry_time': context.current_dt
  718. }
  719. ma_trailing_enabled = True
  720. if direction == 'long':
  721. ma_values_at_entry = calculate_realtime_ma_values(security, [5])
  722. ma5_value = ma_values_at_entry.get('ma5')
  723. if ma5_value is not None and order_price < ma5_value:
  724. ma_trailing_enabled = False
  725. log.info(f"禁用均线跟踪止盈: {security} {direction}, 开仓价 {order_price:.2f} < MA5 {ma5_value:.2f}")
  726. g.trade_history[security]['ma_trailing_enabled'] = ma_trailing_enabled
  727. log.info(f"开仓成功: {security} {direction} {order_amount}手 @{order_price:.2f}, "
  728. f"保证金: {cash_change:.0f}, 原因: {reason}")
  729. return True
  730. except Exception as e:
  731. log.warning(f"开仓失败 {security}: {str(e)}")
  732. return False
  733. def close_position(context, security, direction):
  734. """平仓"""
  735. try:
  736. # 使用order_target平仓到0手
  737. order = order_target(security, 0, side=direction)
  738. if order is not None and order.filled > 0:
  739. underlying_symbol = security.split('.')[0][:-4]
  740. # 记录当日交易(平仓)
  741. g.today_trades.append({
  742. 'security': security,
  743. 'underlying_symbol': underlying_symbol,
  744. 'direction': direction,
  745. 'order_amount': -order.filled,
  746. 'order_price': order.avg_cost if order.avg_cost else order.price,
  747. 'cash_change': 0,
  748. 'time': context.current_dt
  749. })
  750. log.info(f"平仓成功: {underlying_symbol} {direction} {order.filled}手")
  751. # 从交易历史中移除
  752. if security in g.trade_history:
  753. del g.trade_history[security]
  754. return True
  755. except Exception as e:
  756. log.warning(f"平仓失败 {security}: {str(e)}")
  757. return False
  758. ############################ 辅助函数 ###################################
  759. def get_futures_config(underlying_symbol, config_key=None, default_value=None):
  760. """获取期货品种配置信息的辅助函数"""
  761. if underlying_symbol not in g.futures_config:
  762. if config_key and default_value is not None:
  763. return default_value
  764. return {}
  765. if config_key is None:
  766. return g.futures_config[underlying_symbol]
  767. return g.futures_config[underlying_symbol].get(config_key, default_value)
  768. def get_margin_rate(underlying_symbol, direction, default_rate=0.10):
  769. """获取保证金比例的辅助函数"""
  770. return g.futures_config.get(underlying_symbol, {}).get('margin_rate', {}).get(direction, default_rate)
  771. def get_multiplier(underlying_symbol, default_multiplier=10):
  772. """获取合约乘数的辅助函数"""
  773. return g.futures_config.get(underlying_symbol, {}).get('multiplier', default_multiplier)
  774. def calculate_target_hands(context, security, direction):
  775. """计算目标开仓手数"""
  776. current_price = get_current_data()[security].last_price
  777. underlying_symbol = security.split('.')[0][:-4]
  778. # 使用保证金比例
  779. margin_rate = get_margin_rate(underlying_symbol, direction)
  780. multiplier = get_multiplier(underlying_symbol)
  781. # 计算单手保证金
  782. single_hand_margin = current_price * multiplier * margin_rate
  783. # 还要考虑可用资金限制
  784. available_cash = context.portfolio.available_cash * g.usage_percentage
  785. # 根据单个标的最大持仓保证金限制计算开仓数量
  786. max_margin = g.max_margin_per_position
  787. if single_hand_margin <= max_margin:
  788. # 如果单手保证金不超过最大限制,计算最大可开仓手数
  789. max_hands = int(max_margin / single_hand_margin)
  790. max_hands_by_cash = int(available_cash / single_hand_margin)
  791. # 取两者较小值
  792. actual_hands = min(max_hands, max_hands_by_cash)
  793. # 确保至少开1手
  794. actual_hands = max(1, actual_hands)
  795. log.info(f"单手保证金: {single_hand_margin:.0f}, 目标开仓手数: {actual_hands}")
  796. return actual_hands
  797. else:
  798. # 如果单手保证金超过最大限制,默认开仓1手
  799. actual_hands = 1
  800. log.info(f"单手保证金: {single_hand_margin:.0f} 超过最大限制: {max_margin}, 默认开仓1手")
  801. return actual_hands
  802. def check_symbol_prefix_match(symbol, hold_symbols):
  803. """检查是否有相似的持仓品种"""
  804. symbol_prefix = symbol[:-9]
  805. for hold_symbol in hold_symbols:
  806. hold_symbol_prefix = hold_symbol[:-9] if len(hold_symbol) > 9 else hold_symbol
  807. if symbol_prefix == hold_symbol_prefix:
  808. return True
  809. return False
  810. def calculate_average_daily_change_rate(security, days=30):
  811. """计算日均变化率"""
  812. historical_data = attribute_history(security, days + 1, '1d', ['close'])
  813. daily_change_rates = abs(historical_data['close'].pct_change()).iloc[1:]
  814. return daily_change_rates.mean()
  815. def calculate_realtime_ma_values(security, ma_periods):
  816. """计算包含当前价格的实时均线值"""
  817. historical_data = attribute_history(security, max(ma_periods), '1d', ['close'])
  818. today_price = get_current_data()[security].last_price
  819. close_prices = historical_data['close'].tolist() + [today_price]
  820. ma_values = {f'ma{period}': sum(close_prices[-period:]) / period for period in ma_periods}
  821. return ma_values
  822. def after_market_close(context):
  823. """收盘后运行函数"""
  824. log.info(str('函数运行时间(after_market_close):'+str(context.current_dt.time())))
  825. # 清空候选列表(每天重新检查)
  826. g.daily_ma_candidates = {}
  827. # 清空排除缓存(每天重新检查)
  828. excluded_count = len(g.excluded_contracts)
  829. if excluded_count > 0:
  830. log.info(f"清空排除缓存,共 {excluded_count} 个合约")
  831. g.excluded_contracts = {}
  832. # 只有当天有交易时才打印统计信息
  833. if g.today_trades:
  834. print_daily_trading_summary(context)
  835. # 清空当日交易记录
  836. g.today_trades = []
  837. log.info('##############################################################')
  838. def print_daily_trading_summary(context):
  839. """打印当日交易汇总"""
  840. if not g.today_trades:
  841. return
  842. log.info("\n=== 当日交易汇总 ===")
  843. total_margin = 0
  844. for trade in g.today_trades:
  845. if trade['order_amount'] > 0: # 开仓
  846. log.info(f"开仓 {trade['underlying_symbol']} {trade['direction']} {trade['order_amount']}手 "
  847. f"价格:{trade['order_price']:.2f} 保证金:{trade['cash_change']:.0f}")
  848. total_margin += trade['cash_change']
  849. else: # 平仓
  850. log.info(f"平仓 {trade['underlying_symbol']} {trade['direction']} {abs(trade['order_amount'])}手 "
  851. f"价格:{trade['order_price']:.2f}")
  852. log.info(f"当日保证金占用: {total_margin:.0f}")
  853. log.info("==================\n")
  854. ########################## 自动移仓换月函数 #################################
  855. def position_auto_switch(context, pindex=0, switch_func=None, callback=None):
  856. """期货自动移仓换月"""
  857. import re
  858. subportfolio = context.subportfolios[pindex]
  859. symbols = set(subportfolio.long_positions.keys()) | set(subportfolio.short_positions.keys())
  860. switch_result = []
  861. for symbol in symbols:
  862. match = re.match(r"(?P<underlying_symbol>[A-Z]{1,})", symbol)
  863. if not match:
  864. raise ValueError("未知期货标的: {}".format(symbol))
  865. else:
  866. dominant = get_dominant_future(match.groupdict()["underlying_symbol"])
  867. cur = get_current_data()
  868. symbol_last_price = cur[symbol].last_price
  869. dominant_last_price = cur[dominant].last_price
  870. if dominant > symbol:
  871. for positions_ in (subportfolio.long_positions, subportfolio.short_positions):
  872. if symbol not in positions_.keys():
  873. continue
  874. else :
  875. p = positions_[symbol]
  876. if switch_func is not None:
  877. switch_func(context, pindex, p, dominant)
  878. else:
  879. amount = p.total_amount
  880. # 跌停不能开空和平多,涨停不能开多和平空
  881. if p.side == "long":
  882. symbol_low_limit = cur[symbol].low_limit
  883. dominant_high_limit = cur[dominant].high_limit
  884. if symbol_last_price <= symbol_low_limit:
  885. log.warning("标的{}跌停,无法平仓。移仓换月取消。".format(symbol))
  886. continue
  887. elif dominant_last_price >= dominant_high_limit:
  888. log.warning("标的{}涨停,无法开仓。移仓换月取消。".format(dominant))
  889. continue
  890. else:
  891. log.info("进行移仓换月: ({0},long) -> ({1},long)".format(symbol, dominant))
  892. order_old = order_target(symbol, 0, side='long')
  893. if order_old != None and order_old.filled > 0:
  894. order_new = order_target(dominant, amount, side='long')
  895. if order_new != None and order_new.filled > 0:
  896. switch_result.append({"before": symbol, "after": dominant, "side": "long"})
  897. # 换月成功,更新交易记录
  898. if symbol in g.trade_history:
  899. g.trade_history[dominant] = g.trade_history[symbol]
  900. del g.trade_history[symbol]
  901. else:
  902. log.warning("标的{}交易失败,无法开仓。移仓换月失败。".format(dominant))
  903. if p.side == "short":
  904. symbol_high_limit = cur[symbol].high_limit
  905. dominant_low_limit = cur[dominant].low_limit
  906. if symbol_last_price >= symbol_high_limit:
  907. log.warning("标的{}涨停,无法平仓。移仓换月取消。".format(symbol))
  908. continue
  909. elif dominant_last_price <= dominant_low_limit:
  910. log.warning("标的{}跌停,无法开仓。移仓换月取消。".format(dominant))
  911. continue
  912. else:
  913. log.info("进行移仓换月: ({0},short) -> ({1},short)".format(symbol, dominant))
  914. order_old = order_target(symbol, 0, side='short')
  915. if order_old != None and order_old.filled > 0:
  916. order_new = order_target(dominant, amount, side='short')
  917. if order_new != None and order_new.filled > 0:
  918. switch_result.append({"before": symbol, "after": dominant, "side": "short"})
  919. # 换月成功,更新交易记录
  920. if symbol in g.trade_history:
  921. g.trade_history[dominant] = g.trade_history[symbol]
  922. del g.trade_history[symbol]
  923. else:
  924. log.warning("标的{}交易失败,无法开仓。移仓换月失败。".format(dominant))
  925. if callback:
  926. callback(context, pindex, p, dominant)
  927. return switch_result