TrendFalseChange_v002.py 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  1. # 导入函数库
  2. from jqdata import *
  3. from jqdata import finance
  4. import pandas as pd
  5. from datetime import date, datetime, timedelta
  6. from myTT import MA
  7. import re
  8. # 该版本重点是要实现记录下的2024-12-28以后的11个买入
  9. # 设置以便完整打印 DataFrame
  10. pd.set_option('display.max_rows', None) # 设置显示最大行数为无限制
  11. pd.set_option('display.max_columns', None) # 设置显示最大列数为无限制
  12. pd.set_option('display.width', None) # 设置打印宽度为无限制
  13. pd.set_option('display.max_colwidth', 20) # 设置列最大宽度为无限制
  14. ## 初始化函数,设定基准等等
  15. def initialize(context):
  16. # 设定沪深300作为基准
  17. set_benchmark('000300.XSHG')
  18. # 开启动态复权模式(真实价格)
  19. set_option('use_real_price', True)
  20. # 过滤掉order系列API产生的比error级别低的log
  21. # log.set_level('order', 'error')
  22. # 输出内容到日志 log.info()
  23. log.info('初始函数开始运行且全局只运行一次')
  24. ### 期货相关设定 ###
  25. # 设定账户为金融账户
  26. set_subportfolios([SubPortfolioConfig(cash=context.portfolio.starting_cash, type='index_futures')])
  27. # 期货类每笔交易时的手续费是: 买入时万分之0.23,卖出时万分之0.23,平今仓为万分之23
  28. set_order_cost(OrderCost(open_commission=0.000023, close_commission=0.000023,close_today_commission=0.0023), type='index_futures')
  29. # 设定保证金比例
  30. # set_option('futures_margin_rate', 0.15)
  31. # 设置期货交易的滑点
  32. set_slippage(StepRelatedSlippage(2))
  33. # 运行函数(reference_security为运行时间的参考标的;传入的标的只做种类区分,因此传入'IF8888.CCFX'或'IH1602.CCFX'是一样的)
  34. # 注意: before_open/open/close/after_close等相对时间不可用于有夜盘的交易品种,有夜盘的交易品种请指定绝对时间(如9: 30)
  35. #初始化全局变量
  36. g.usage_percentage = 0.8 # 最大持股数量
  37. # 定义默认的保证金比例
  38. g.default_margin_rates = {
  39. 'long': {'A': 0.07, 'AG': 0.04, 'AL': 0.05, 'AO': 0.05, 'AP': 0.08, 'AU': 0.04, 'B': 0.05,
  40. 'BC': 0.13, 'BR': 0.07, 'BU': 0.04, 'C': 0.07, 'CF': 0.05, 'CJ': 0.07, 'CS': 0.07,
  41. 'CU': 0.05, 'CY': 0.05, 'EB': 0.12, 'EC': 0.12, 'EG': 0.05, 'FG': 0.05, 'FU': 0.08,
  42. 'HC': 0.04, 'I': 0.1, 'J': 0.22, 'JD': 0.08, 'JM': 0.22,
  43. 'L': 0.07, 'LC': 0.05, 'LH':0.1, 'LR': 0.05, 'LU': 0.15, 'M': 0.07, 'MA': 0.05, 'NI': 0.05, 'NR': 0.13, 'OI': 0.05,
  44. 'P': 0.05, 'PB': 0.05, 'PF': 0.1, 'PG': 0.05, 'PK': 0.05,
  45. 'PP': 0.07, 'RB': 0.05, 'RI': 0.05, 'RM': 0.05, 'RU': 0.05,
  46. 'SA': 0.05, 'SC': 0.12, 'SF': 0.05, 'SH': 0.05, 'SI': 0.13, 'SM': 0.05, 'SN': 0.05, 'SP': 0.1, 'SR': 0.05,
  47. 'SS': 0.05, 'TA': 0.05, 'UR': 0.09, 'V': 0.07,
  48. 'Y': 0.05, 'ZC': 0.05, 'ZN': 0.05},
  49. 'short': {'A': 0.07, 'AG': 0.04, 'AL': 0.05, 'AO': 0.05, 'AP': 0.08, 'AU': 0.04, 'B': 0.05,
  50. 'BC': 0.13, 'BR': 0.07, 'BU': 0.04, 'C': 0.07, 'CF': 0.05, 'CJ': 0.07, 'CS': 0.07,
  51. 'CU': 0.05, 'CY': 0.05, 'EB': 0.12, 'EC': 0.12, 'EG': 0.05, 'FG': 0.05, 'FU': 0.08,
  52. 'HC': 0.04, 'I': 0.1, 'J': 0.22, 'JD': 0.08, 'JM': 0.22,
  53. 'L': 0.07, 'LC': 0.05, 'LH':0.1, 'LR': 0.05, 'LU': 0.15, 'M': 0.07, 'MA': 0.05, 'NI': 0.05, 'NR': 0.13, 'OI': 0.05,
  54. 'P': 0.05, 'PB': 0.05, 'PF': 0.1, 'PG': 0.05, 'PK': 0.05,
  55. 'PP': 0.07, 'RB': 0.05, 'RI': 0.05, 'RM': 0.05, 'RU': 0.05,
  56. 'SA': 0.05, 'SC': 0.12, 'SF': 0.05, 'SH': 0.05, 'SI': 0.13, 'SM': 0.05, 'SN': 0.05, 'SP': 0.1, 'SR': 0.05,
  57. 'SS': 0.05, 'TA': 0.05, 'UR': 0.09, 'V': 0.07,
  58. 'Y': 0.05, 'ZC': 0.05, 'ZN': 0.05}
  59. }
  60. g.multiplier = {
  61. 'A': 10, 'AG': 15, 'AL': 5, 'AO': 20, 'AP': 10, 'AU': 1000, 'B': 10,
  62. 'BC': 5, 'BR': 5, 'BU': 10, 'C': 10, 'CF': 5, 'CJ': 5, 'CS': 10,
  63. 'CU': 5, 'CY': 5, 'EB': 5, 'EC': 50, 'EG': 10, 'FG': 20, 'FU': 10,
  64. 'HC': 10, 'I': 100, 'J': 60, 'JD': 5, 'JM': 100,
  65. 'L': 5, 'LC': 1, 'LH':16, 'LR': 0.05, 'LU': 10, 'M': 10, 'MA': 10, 'NI': 1, 'NR': 10, 'OI': 10,
  66. 'P': 10, 'PB': 5, 'PF': 5, 'PG': 20, 'PK': 5,
  67. 'PP': 5, 'RB': 10, 'RI': 0.05, 'RM': 10, 'RU': 10,
  68. 'SA': 20, 'SC': 1000, 'SF': 5, 'SH': 30, 'SI': 5, 'SM': 5, 'SN': 1, 'SP': 10, 'SR': 10,
  69. 'SS': 5, 'TA': 5, 'UR': 20, 'V': 5,
  70. 'Y': 10, 'ZC': 0.05, 'ZN': 5} # 假设所有期货合约的乘数为固定值,如果不同合约有不同的乘数,需要进一步调整
  71. g.default_days = 10 # 判断趋势线的最小天数,某条K线连续这个天数大于收盘价则该K线为压力线;小于则是支撑线
  72. g.continuous_days_length = 5 # 破趋势后的观察天数
  73. g.change_direction_days = 5 # 检查是某个天数范围内有多少次均线穿过
  74. g.crossed_symbols_history = {} # 用于存储过去几天的穿越信息
  75. g.trade_history = {} # 初始化交易记录
  76. g.change_fail_history = {} # 初始化换月建仓失败的记录
  77. # 开盘时运行
  78. # run_daily(market_open, time='21:01', reference_security='IF8888.CCFX')
  79. # run_daily(market_open, time='09:01', reference_security='IF8888.CCFX')
  80. g.high_low_ma_relations = {}
  81. # 临时止损检查
  82. run_daily(loss_control, time='21:05:00', reference_security='IF8888.CCFX')
  83. run_daily(loss_control, time='21:35:00', reference_security='IF8888.CCFX')
  84. run_daily(loss_control, time='22:05:00', reference_security='IF8888.CCFX')
  85. run_daily(loss_control, time='22:35:00', reference_security='IF8888.CCFX')
  86. run_daily(loss_control, time='09:05:00', reference_security='IF8888.CCFX')
  87. run_daily(loss_control, time='09:35:00', reference_security='IF8888.CCFX')
  88. run_daily(loss_control, time='10:05:00', reference_security='IF8888.CCFX')
  89. run_daily(loss_control, time='10:35:00', reference_security='IF8888.CCFX')
  90. run_daily(loss_control, time='11:05:00', reference_security='IF8888.CCFX')
  91. run_daily(loss_control, time='13:05:00', reference_security='IF8888.CCFX')
  92. run_daily(loss_control, time='13:35:00', reference_security='IF8888.CCFX')
  93. run_daily(loss_control, time='14:05:00', reference_security='IF8888.CCFX')
  94. run_daily(loss_control, time='14:35:00', reference_security='IF8888.CCFX')
  95. # 收盘前运行
  96. run_daily(before_market_close, time='14:55', reference_security='IF8888.CCFX')
  97. ############################ 主程序中执行函数 ###################################
  98. #0-0 开盘前运行函数
  99. def market_open(context):
  100. # 输出运行时间
  101. # log.info('函数运行时间(before_market_open): '+str(context.current_dt.time()))
  102. # 给微信发送消息(添加模拟交易,并绑定微信生效)
  103. # send_message('美好的一天~')
  104. # 获取当下该品种有夜盘和无夜盘主力期货合约
  105. potential_night_list = ['AG', 'AL', 'AU', 'BU', 'CU', 'FU', 'HC', 'NI', 'PB', 'RB', 'RU', 'SN', 'ZN', 'SP', 'SS', \
  106. 'CF', 'CY', 'FG', 'JR', 'LR', 'MA', 'PM', 'RI', 'RM', 'OI', 'SR', 'TA', 'ZC', 'WH', \
  107. 'WT', 'SA', 'PF', 'A', 'B', 'BB', 'C', 'CS', 'I', 'J', 'JM', 'L', 'M', 'P', 'PP', \
  108. 'V', 'Y', 'EG', 'EB', 'PG']
  109. potential_day_list = ['SF', 'SM', 'UR', 'AP', 'CJ', 'PK', 'FB', 'JD', 'LH', 'SI', 'LC']
  110. # potential_night_list = ['OI']
  111. # potential_day_list = ['UR']
  112. if str(context.current_dt.time())[:2] == '21':
  113. potential_icon_list = potential_night_list
  114. else:
  115. potential_icon_list = potential_day_list
  116. potential_future_list = []
  117. for i in potential_icon_list:
  118. domaint_future = get_dominant_future(i)
  119. if len(domaint_future) > 0:
  120. potential_future_list.append(domaint_future)
  121. # c_data = get_current_data()
  122. target_list = []
  123. for future in potential_future_list:
  124. y_data = attribute_history(future, 2, '1m', ['close'])
  125. yesterday_close = y_data.iloc[0,0]
  126. # c_data = attribute_history(future, 1, '1m', ['open'])
  127. today_open = y_data.iloc[-1,0]
  128. # print(future, y_data)
  129. open_change_rate = (today_open - yesterday_close)/yesterday_close
  130. if open_change_rate >= 0.03:
  131. target_list.append(future)
  132. # print('future: ', future, today_open, yesterday_close, open_change_rate)
  133. # print(today_open, yesterday_close, open_change_rate)
  134. # print(y_data)
  135. if len(target_list) >= 5:
  136. value = context.portfolio.cash * g.usage_percentage / len(target_list)
  137. for future in target_list:
  138. open_position(future, value, 'short')
  139. elif len(target_list) > 0 and len(target_list) < 5:
  140. value = context.portfolio.cash * g.usage_percentage / 5
  141. for future in target_list:
  142. open_position(future, value, 'short')
  143. #0-1 判断是否买入函数
  144. def before_market_close(context):
  145. print("-"*20 + "New day ending!" + "-"*20)
  146. if len(g.trade_history) > 0:
  147. print(f'in the main - g.trade_history: {g.trade_history}, g.change_fail_history: {g.change_fail_history}')
  148. # loss_control(context)
  149. trend_symbols, daily_data_info = check_trend(context)
  150. print_list_elements("in the main - trend_symbols", trend_symbols)
  151. crossed_symbols = check_shadow_cross(context, trend_symbols, daily_data_info)
  152. print_list_elements("in the main - crossed_symbols", crossed_symbols)
  153. buy_symbols = check_buy_condition(context, crossed_symbols)
  154. print_list_elements("in the main - buy_symbols", buy_symbols)
  155. subportfolio = context.subportfolios[0]
  156. hold_symbols = set(subportfolio.long_positions.keys()) | set(subportfolio.short_positions.keys())
  157. # print(f'hold_symbols: {hold_symbols}')
  158. for symbol, line_label, line_type, direction in buy_symbols:
  159. if check_symbol_prefix_match(symbol, hold_symbols):
  160. print(f'Have similar symbol in hold_symbols: {hold_symbols} and symbol: {symbol}, stop the deal')
  161. else:
  162. print(f'No similar symbol in hold_symbols: {hold_symbols} and symbol: {symbol}, continue the deal')
  163. value_to_invest = calculate_order_value(context, symbol, direction)
  164. open_position(symbol, value_to_invest, direction, line_label)
  165. #0-2 止盈止损
  166. def loss_control(context):
  167. print("-" * 20 + "loss control" + "-" * 20)
  168. # 先检查是否有正常的夜盘
  169. now = context.current_dt.time()
  170. now_hour = now.hour
  171. if now_hour >= 21:
  172. test_future = get_dominant_future("A")
  173. test_data = attribute_history(test_future, 1, '1m', 'close')
  174. test_hour = test_data.index[0].hour
  175. if test_hour <= 15:
  176. print(f'最近的数据小时为: {test_hour},该夜盘不存在,直接停止')
  177. return
  178. # 先把换月买入失败的重新买入放在这里
  179. if len(g.change_fail_history) > 0:
  180. print(f'检查换月失败的{g.change_fail_history}')
  181. for symbol in g.change_fail_history.keys():
  182. direction = g.change_fail_history[symbol]['direction']
  183. print(f'检查换月失败的direction: {direction}')
  184. value_to_invest = calculate_order_value(context, symbol, direction)
  185. open_position(symbol, value_to_invest, g.trade_history[symbol]["direction"], g.trade_history[symbol]["line_label"])
  186. # 检查损失
  187. target_time = datetime.strptime('14:55:00', '%H:%M:%S').time()
  188. potential_future_list = get_potential_future_list(context)
  189. # print(f'check g.trade_history: {g.trade_history}, potential_future_list: {potential_future_list}')
  190. # print(f'current_hour: {current_hour}')
  191. # 遍历所有持仓
  192. long_positions_dict = context.portfolio.long_positions
  193. for position in list(long_positions_dict.values()):
  194. # print("security:{0}, position:{1}, value:{2}, time:{3},latest price:{4},cost:{5}".format(position.security, position.total_amount, position.value, position.init_time, position.price, position.acc_avg_cost))
  195. # 检查是否达到损失限制
  196. if position.security in potential_future_list:
  197. # print(f'for long position, {position.security} in the potential_future_list: {potential_future_list}')
  198. if check_loss_for_close(context, position, 'long'):
  199. continue # 如果已平仓,则跳过后续检查
  200. # 检查基于动态跟踪线的平仓条件
  201. if now > target_time:
  202. # print("Now {now} is after 14:55:00")
  203. check_ma_for_close(context, position, 0.01, 4)
  204. else:
  205. # print("Now {now} is before or equal to 14:55:00")
  206. check_ma_for_close(context, position, 0.003, 4)
  207. # print(type(context.subportfolios[0].short_positions))
  208. short_positions_dict = context.portfolio.short_positions
  209. for position in list(short_positions_dict.values()):
  210. # print("security:{0}, position:{1}, value:{2}, time:{3},latest price:{4},cost:{5}".format(position.security, position.total_amount, position.value, position.init_time, position.price, position.acc_avg_cost))
  211. # 检查是否达到损失限制
  212. if position.security in potential_future_list:
  213. # print(f'for short position, {position.security} in the potential_future_list: {potential_future_list}')
  214. if check_loss_for_close(context, position, 'short'):
  215. continue # 如果已平仓,则跳过后续检查
  216. # 检查基于动态跟踪线的平仓条件
  217. if now >= target_time:
  218. # print("Now {now} is after 14:55:00")
  219. check_ma_for_close(context, position, 0.01, 4)
  220. else:
  221. # print("Now {now} is before or equal to 14:55:00")
  222. check_ma_for_close(context, position, 0.003, 4)
  223. #0-3 收盘后运行函数
  224. def after_market_close(context):
  225. log.info(str('函数运行时间(after_market_close):'+str(context.current_dt.time())))
  226. # 得到当天所有成交记录
  227. trades = get_trades()
  228. for _trade in trades.values():
  229. log.info('成交记录: '+str(_trade))
  230. log.info('A day ends')
  231. log.info('##############################################################')
  232. ############################ 买卖函数 ###################################
  233. #1-0 交易模块-自定义下单
  234. #报单成功返回报单(不代表一定会成交),否则返回None,应用于
  235. def order_target_value_(security, value, direction):
  236. if value == 0:
  237. log.debug("Selling out %s" % (security))
  238. else:
  239. log.debug("Order %s to value %f" % (security, value))
  240. # 如果股票停牌,创建报单会失败,order_target_value 返回None
  241. # 如果股票涨跌停,创建报单会成功,order_target_value 返回Order,但是报单会取消
  242. # 部成部撤的报单,聚宽状态是已撤,此时成交量>0,可通过成交量判断是否有成交
  243. return order_target_value(security, value, side=direction)
  244. #1-1 交易模块-开仓
  245. #买入指定价值的证券,报单成功并成交(包括全部成交或部分成交,此时成交量大于0)返回True,报单失败或者报单成功但被取消(此时成交量等于0),返回False
  246. def open_position(security, value, direction, line_label):
  247. order = order_target_value_(security, value, direction)
  248. # print(f'order: {order}') # 查看订单的信息
  249. if order != None and order.filled > 0:
  250. print(f'Make an order of {security} with {line_label} and {direction}')
  251. g.trade_history[security] = {
  252. 'entry_price': get_current_data()[security].last_price,
  253. 'position_value': value,
  254. 'direction': direction,
  255. 'line_label': line_label,
  256. 'finish_time': order.finish_time
  257. }
  258. return True
  259. return False
  260. #1-2 交易模块-平仓
  261. #卖出指定持仓,报单成功并全部成交返回True,报单失败或者报单成功但被取消(此时成交量等于0),或者报单非全部成交,返回False
  262. def close_position(position, direction):
  263. security = position.security
  264. order = order_target_value_(security, 0, direction) # 可能会因停牌失败
  265. if order != None:
  266. if order.status == OrderStatus.held and order.filled == order.amount:
  267. # g.sold_future[security] = 0 # 避免连续重复买入
  268. # 如果成功平仓,从交易历史中移除该标的
  269. if security in g.trade_history:
  270. del g.trade_history[security]
  271. # print(f'after deal, g.trade_history: {g.trade_history}')
  272. return True
  273. return False
  274. #1-3 根据标的情况开仓
  275. def open_position_by_margin(context, security, direction):
  276. value_to_invest = calculate_order_value(context, security, direction)
  277. order_value(security, value_to_invest, side=direction, pindex=0)
  278. ############################ 判断买卖条件的函数 ###################################
  279. #2-0 找到可以交易的目标期货标的主连
  280. def get_potential_future_list(context):
  281. # potential_night_list = ['AG', 'AL', 'AU', 'BU', 'CU', 'FU', 'HC', 'NI', 'NR', 'PB', 'RB', 'RU', 'SN', 'ZN', 'SP', 'SS', \
  282. # 'CF', 'CY', 'FG', 'LR', 'MA', 'PM', 'RI', 'RM', 'OI', 'SC', 'SH','SR', 'TA', 'ZC', \
  283. # 'SA', 'PF', 'A', 'AO', 'B', 'BC', 'BR', 'C', 'CS', 'I', 'J', 'JM', 'L', 'M', 'P', 'PP', \
  284. # 'V', 'Y', 'EG', 'EB', 'PG']
  285. # potential_day_list = ['EC', 'SF', 'SM', 'UR', 'AP', 'CJ', 'PK', 'JD', 'LH', 'SI', 'LC']
  286. # potential_night_list = ['RM']
  287. # potential_day_list = ['']
  288. potential_night_list = ['NI', 'CF', 'PF', 'Y', 'M', 'B', 'SN', 'RM']
  289. potential_day_list = ['JD', 'UR']
  290. if str(context.current_dt.time())[:2] in ('21', '22'):
  291. potential_icon_list = potential_night_list
  292. else:
  293. potential_icon_list = potential_day_list + potential_night_list
  294. # print(f'potential_icon_list: {potential_icon_list}')
  295. potential_future_list = []
  296. for i in potential_icon_list:
  297. dominant_future = get_dominant_future(i)
  298. # print(f'i: {i}, dominant_future: {dominant_future}')
  299. if len(dominant_future) > 0:
  300. potential_future_list.append(dominant_future)
  301. return potential_future_list
  302. #2-1 检查品类的主连是否满足形成趋势的条件
  303. def check_trend(context):
  304. print("-" * 20 + "Check trends" + "-" * 20)
  305. trend_symbols = []
  306. daily_data_info = {} # 用于保存每日数据信息
  307. # 获取可以交易的所有标的主连
  308. potential_future_list = get_potential_future_list(context)
  309. # print(f'potential_future_list: {potential_future_list}')
  310. potential_future_list = [item for item in potential_future_list if item not in g.trade_history.keys()] # 移除已有持仓的标的
  311. # print(f'potential_future_list: {potential_future_list}')
  312. # 针对所有标的需要的基础数据
  313. ma_crosses_dict = {} # 用于存储某个symbol过去一段时间均线交叉的数量
  314. for symbol in potential_future_list:
  315. # 获取50天的收盘价数据
  316. close_data = attribute_history(symbol, 50, '1d', ['close', 'high', 'low', 'open'], df=True)
  317. close_series = close_data['close']
  318. # print(f'Check if future: {symbol} - {close_series}')
  319. # 计算5日、10日、20日和30日移动平均线
  320. ma5 = close_series.rolling(window=5).mean()
  321. ma10 = close_series.rolling(window=10).mean()
  322. ma20 = close_series.rolling(window=20).mean()
  323. ma30 = close_series.rolling(window=30).mean()
  324. trend_info = {'symbol': symbol, 'trend_lines': []}
  325. daily_data = [] # 存储50日的数据
  326. # 初始化连续天数计数器
  327. continuous_days = {
  328. 'above_ma5': [0] * g.continuous_days_length,
  329. 'below_ma5': [0] * g.continuous_days_length,
  330. 'above_ma10': [0] * g.continuous_days_length,
  331. 'below_ma10': [0] * g.continuous_days_length,
  332. 'above_ma20': [0] * g.continuous_days_length,
  333. 'below_ma20': [0] * g.continuous_days_length,
  334. 'above_ma30': [0] * g.continuous_days_length,
  335. 'below_ma30': [0] * g.continuous_days_length
  336. }
  337. # 获取连续在某一个均线上或者下的天数
  338. for i in range(len(close_series)):
  339. # 针对每一天更新连续天数计数器
  340. symbol_prefix = symbol[:-9]
  341. if symbol_prefix == 'RM':
  342. # log.debug(f'start updating trend details for {symbol}')
  343. update_continuous_days(continuous_days, close_series[i], ma5[i], ma10[i], ma20[i], ma30[i], g.continuous_days_length, True)
  344. else:
  345. update_continuous_days(continuous_days, close_series[i], ma5[i], ma10[i], ma20[i], ma30[i], g.continuous_days_length)
  346. # 收集每日数据
  347. day_data = {
  348. 'date': close_data.index[i].date(),
  349. 'close': close_series[i],
  350. 'high': close_data['high'][i],
  351. 'low': close_data['low'][i],
  352. 'open': close_data['open'][i],
  353. 'ma5': ma5[i],
  354. 'ma10': ma10[i],
  355. 'ma20': ma20[i],
  356. 'ma30': ma30[i],
  357. 'continuous_above_ma5': continuous_days['above_ma5'],
  358. 'continuous_below_ma5': continuous_days['below_ma5'],
  359. 'continuous_above_ma10': continuous_days['above_ma10'],
  360. 'continuous_below_ma10': continuous_days['below_ma10'],
  361. 'continuous_above_ma20': continuous_days['above_ma20'],
  362. 'continuous_below_ma20': continuous_days['below_ma20'],
  363. 'continuous_above_ma30': continuous_days['above_ma30'],
  364. 'continuous_below_ma30': continuous_days['below_ma30']
  365. }
  366. daily_data.append(day_data)
  367. # 在处理完所有数据后检查是否有特殊的趋势变化情况出现
  368. # adjust_for_special_condition(daily_data, g.continuous_days_length)
  369. daily_data_info[symbol] = daily_data
  370. # print(f'checking daily data for {symbol}')#: {daily_data}')
  371. # 检查过去一定天数内均线的相交次数,如果太多不会被认定为形成趋势
  372. # print(f'calculate {symbol}') # ma cross numbers with daily_data_info: {daily_data_info[symbol]}')
  373. ma_crosses_dict[symbol] = count_ma_crosses(daily_data_info[symbol], g.change_direction_days)
  374. # 检查哪些均线会形成什么类型的趋势
  375. for ma_type in ['ma5', 'ma10', 'ma20', 'ma30']:
  376. above_days = continuous_days[f'above_{ma_type}']
  377. below_days = continuous_days[f'below_{ma_type}']
  378. # print(f"Checking {ma_type} for {symbol}, above_days: {above_days}, below_days: {below_days}, g.default_days: {g.default_days}")
  379. above_condition_result = [day >= g.default_days for day in above_days]
  380. below_condition_result = [day >= g.default_days for day in below_days]
  381. # print(f"above_condition_result: {above_condition_result}, below_condition_result: {below_condition_result}")
  382. above_condition = any(above_condition_result)
  383. below_condition = any(below_condition_result)
  384. # print(f"above_condition: {above_condition}, below_condition: {below_condition}")
  385. if above_condition:
  386. # print(f"Adding {ma_type} support for {symbol}")
  387. trend_info['trend_lines'].append((ma_type, 'support'))
  388. if below_condition:
  389. # print(f"Adding {ma_type} resistance for {symbol}")
  390. trend_info['trend_lines'].append((ma_type, 'resistance'))
  391. if trend_info['trend_lines']:
  392. trend_symbols.append(trend_info)
  393. # 去除掉在一段时间内均线过于频繁交叉的对象
  394. valid_trend_symbols = []
  395. # print(f'before checking special circumstances, trend_symbols: {trend_symbols}')
  396. for trend_info in trend_symbols:
  397. # print(f'find trend_info: {trend_info["symbol"]}')
  398. symbol = trend_info["symbol"]
  399. ma_crosses = ma_crosses_dict[symbol]
  400. # log.debug(f'symbol: {symbol}, ma_crosses: {ma_crosses}')
  401. if ma_crosses <= 3:
  402. valid_trend_symbols.append(trend_info)
  403. # print(f'valid_trend_symbols: {valid_trend_symbols}')
  404. # 使用有效的趋势信息更新 trend_symbols
  405. trend_symbols = valid_trend_symbols
  406. # 创建一个集合来存储所有出现在 trend_info 中的标的
  407. trend_symbols_set = set([info['symbol'] for info in trend_symbols])
  408. log.debug(f"trend_symbols: {trend_symbols}, trend_symbols_set: {trend_symbols_set}, valid_trend_symbols: {valid_trend_symbols}")
  409. # 打印40日的数据和连续天数信息
  410. # print(f"trend_symbols: {trend_symbols}, trend_symbols_set: {trend_symbols_set}") #, daily_data_info:{daily_data_info}")
  411. for symbol, daily_data in daily_data_info.items():
  412. # 只打印出现在 trend_info 中的标的的数据
  413. if symbol in trend_symbols_set:
  414. # 查找与当前 symbol 对应的 trend_info
  415. current_trend_info = next((item for item in trend_symbols if item['symbol'] == symbol), None)
  416. # 打印 symbol 和其趋势信息
  417. trend_lines_str = ', '.join([f"{line[0]}: {line[1]}" for line in current_trend_info['trend_lines']]) if current_trend_info else "无趋势"
  418. # print(f"{symbol} 的趋势情况为: {trend_lines_str},满足条件的每日数据:")
  419. # print(f"{symbol} 的趋势情况为: ,满足条件的每日数据:")
  420. # print(f"daily_data: {daily_data[-1]}")
  421. # for data in daily_data:
  422. # print(f" 日期: {data['date']}, 收盘价: {data['close']}, MA5: {data['ma5']}, MA10: {data['ma10']}, MA20: {data['ma20']}, \
  423. # 连续高于MA5: {data['continuous_above_ma5']}, 连续低于MA5: {data['continuous_below_ma5']}, \
  424. # 连续高于MA10: {data['continuous_above_ma10']}, 连续低于MA10: {data['continuous_below_ma10']}, \
  425. # 连续高于MA20: {data['continuous_above_ma20']}, 连续低于MA20: {data['continuous_below_ma20']}")
  426. # continue
  427. return valid_trend_symbols, daily_data_info
  428. #2-2 检查行程趋势的主连的趋势是否发生变化
  429. def check_shadow_cross(context, trend_symbols, daily_data_info):
  430. print("-" * 20 + "Check shadow cross" + "-" * 20)
  431. # 1. 检查并进行换月
  432. current_day = context.current_dt.date() # 获取当前日期字符串
  433. # print(f'check current_day: {current_day}')
  434. switch_result = position_auto_switch(context)
  435. if switch_result:
  436. print(f'switch_result: {switch_result}, g.trade_history: {g.trade_history}, g.change_fail_history: {g.change_fail_history}')
  437. temp_crossed_symbols = {}
  438. today_crossed_symbols = {}
  439. # 1. 检查并清理过时的穿越信息
  440. # for symbol in list(g.crossed_symbols_history.keys()):
  441. # 保留最近 g.continuous_days_length 天的数据
  442. # print(f'crossed_symbols_history: {g.crossed_symbols_history}')
  443. # g.crossed_symbols_history[symbol] = {date: crosses for date, crosses in g.crossed_symbols_history[symbol].items() if (context.current_dt - datetime.strptime(date, '%Y-%m-%d')).days <= g.continuous_days_length}
  444. # if not g.crossed_symbols_history[symbol]:
  445. # del g.crossed_symbols_history[symbol] # 如果没有数据,删除该标的的记录
  446. # 2. 根据当天的数据更新g.crossed_symbols_history
  447. for trend_info in trend_symbols:
  448. symbol = trend_info['symbol']
  449. # print(f'Checking data for {symbol}')
  450. # 获取今天的最新数据
  451. # 计算并更新今天的完整数据
  452. today_daily_data_info = daily_data_info.copy()
  453. latest_data_today = get_latest_trading_data(symbol, context)
  454. print(f'{symbol} latest_data_today: {latest_data_today}')
  455. # 计算并更新今天的完整数据
  456. latest_data = calculate_continuous_data(latest_data_today, today_daily_data_info, symbol, g.continuous_days_length)
  457. # 根据昨天的价格判断有没有发生突破
  458. yesterday_data = daily_data_info[symbol][-1]
  459. print(f'find {symbol} yesterday_data: {yesterday_data}')
  460. # 临时存储当前标的的穿越信息
  461. symbol_crosses = []
  462. for line_label, line_type in trend_info['trend_lines']:
  463. print(f'line_label in change: {line_label}')
  464. yesterday_day = yesterday_data['date']
  465. close = yesterday_data['close']
  466. high = yesterday_data['high']
  467. low = yesterday_data['low']
  468. ma_value = yesterday_data[line_label]
  469. close_ma_difference = (close - ma_value) / ma_value
  470. # 删除指定的键
  471. yesterday_data_copy = yesterday_data.copy()
  472. # print(f'check yesterday_data_copy before changing {yesterday_data_copy}')
  473. keys_to_remove = ['continuous_above_ma5', 'continuous_below_ma5', 'continuous_above_ma10', 'continuous_below_ma10', 'continuous_above_ma20', 'continuous_below_ma20', 'continuous_above_ma30', 'continuous_below_ma30']
  474. for key in keys_to_remove:
  475. yesterday_data_copy.pop(key, None) # 使用pop方法删除键,如果键不存在则不做任何操作
  476. # print(f'check yesterday_data_copy after changing {yesterday_data_copy}')
  477. # 判断上一个交易日完整的数据中是否穿越
  478. if (line_type == 'support' and low < ma_value and close_ma_difference <= 0.0005) or (line_type == 'resistance' and high > ma_value and close_ma_difference >= -0.0005):
  479. symbol_crosses.append({
  480. 'date': yesterday_day,
  481. 'symbol': symbol,
  482. 'line_label': line_label,
  483. 'line_type': line_type,
  484. 'direction': 'long' if line_type == 'support' else 'short',
  485. 'latest_data': yesterday_data_copy
  486. })
  487. print(f"{symbol} crossed {line_label} ({line_type}) with ma_value: {ma_value} and close: {close} and high:{high} and low: {low} and close_ma_difference: {close_ma_difference}, latest_data: {yesterday_data_copy}")
  488. # 从符合条件的趋势线中选择数字较大的(即优先保留ma10而不是ma5)
  489. # print(f'check symbol_crosses: {symbol_crosses}')
  490. if symbol_crosses:
  491. # today_crossed_symbols.append(symbol) # 记录一下g.crossed_symbols_history已经更新过的symbol
  492. # 如果有穿越,选择数字较大的趋势
  493. best_cross = max(symbol_crosses, key=lambda x: int(''.join(filter(str.isdigit, x['line_label']))))
  494. temp_crossed_symbols[symbol] = best_cross
  495. # 3.1 针对没有更新过的g.crossed_symbols_history的symbol进行更新
  496. # print(f'check temp_crossed_symbols: {temp_crossed_symbols}, g.crossed_symbols_history: {g.crossed_symbols_history}')
  497. if temp_crossed_symbols:
  498. # 首先是针对今天出现趋势突破的标的进行历史记录的更新
  499. for symbol, best_cross in temp_crossed_symbols.items():
  500. # print(f'更新历史symbol: {symbol}, best_cross: {best_cross}')
  501. update_crossed_symbols_history(context, best_cross)
  502. print(f'针对也在今天出现突破的{symbol}完成g.crossed_symbols_history的更新,{g.crossed_symbols_history[symbol]}')
  503. #3.2 要针对那些不在今天出现突破的标的但是还在历史趋势里的进行更新,首先将两个字典的键转换成集合
  504. keys_temp_crossed_symbols = set(temp_crossed_symbols.keys())
  505. keys_g_crossed_symbols_history = set(g.crossed_symbols_history.keys())
  506. #3.2.1 找出在g.crossed_symbols_history中但不在temp_crossed_symbols中的键
  507. difference_keys = keys_g_crossed_symbols_history - keys_temp_crossed_symbols
  508. print(f"Keys that are in g.crossed_symbols_history: {keys_g_crossed_symbols_history} but not in temp_crossed_symbols: {keys_g_crossed_symbols_history}; {difference_keys}")
  509. for symbol in difference_keys:
  510. update_record_for_existing_symbol(symbol, 'new_symbol')
  511. #3.2.2 找出在在g.crossed_symbols_history和temp_crossed_symbols中都有的key,但是temp里的line_label要小
  512. keys_in_both = keys_temp_crossed_symbols & keys_g_crossed_symbols_history
  513. for symbol in keys_in_both:
  514. print(f'{symbol}, {keys_temp_crossed_symbols}, {keys_g_crossed_symbols_history}, temp_crossed_symbols: {temp_crossed_symbols}')
  515. temp_line_label = temp_crossed_symbols[symbol]['line_label']
  516. g_line_label = g.crossed_symbols_history[symbol][0]['line_label']
  517. temp_line_num = int(temp_line_label.replace('ma', ''))
  518. g_line_num = int(g_line_label.replace('ma', ''))
  519. # 检查是否需要更新
  520. if g_line_num > temp_line_num:
  521. update_record_for_existing_symbol(symbol, 'old_symbol')
  522. #4. 针对出现趋势变化的检查均线顺序、破趋势天数、收盘价和均线的最高点关系
  523. for symbol, records in g.crossed_symbols_history.items():
  524. latest_record = max(records, key=lambda x: x['date'])
  525. print(f'symbol: {symbol}, records: {records}, latest_record: {latest_record}')
  526. # 检查均线得满足一定的条件,以及破趋势的天数和其中收盘价和均线的最高差别
  527. relation_check = check_ma_relations(context, symbol, False)
  528. duration_check, ma_close = check_cross_details(context, symbol, False)
  529. # 只有均线顺序、破趋势天数、收盘价和均线的最高点关系满足条件,才会被加入购买真正的突破趋势清单
  530. if relation_check and duration_check and ma_close:
  531. print(f'{symbol}满足均线顺序、破趋势天数、收盘价和均线的最高点关系满足条件,才会被加入购买真正的突破趋势清单这三项条件,被视为合格标的')
  532. today_crossed_symbols[symbol] = [latest_record]
  533. print(f'today_crossed_symbols: {today_crossed_symbols}')
  534. latest_records = {}
  535. # print(f'最后获取还在历史记录里的g.crossed_symbols_history: {g.crossed_symbols_history}')
  536. for symbol, records in g.crossed_symbols_history.items():
  537. # 找到日期最大的记录
  538. latest_record = max(records, key=lambda x: x['date'])
  539. # 将找到的记录添加到新字典中
  540. latest_records[symbol] = [latest_record]
  541. # print(f'latest_records: {len(latest_records)} & {latest_records}, g.crossed_symbols_history: {len(g.crossed_symbols_history)} & {g.crossed_symbols_history}')
  542. return [item for sublist in today_crossed_symbols.values() for item in sublist]
  543. #2-3 检查趋势发生变化的主连是否满足买入条件
  544. def check_buy_condition(context, crossed_symbols, shadow_body_ratio_threshold=0.5):
  545. print("-" * 20 + "Check buy condition" + "-" * 20)
  546. buy_symbols = []
  547. update_margin_rate(context)
  548. for crossed_symbol in crossed_symbols:
  549. # print(f'crossed_symbol: {crossed_symbol}')
  550. symbol = crossed_symbol['symbol']
  551. line_label = crossed_symbol['line_label']
  552. line_type = crossed_symbol['line_type']
  553. histroical_data = calculate_daily_data(context, 50, symbol)
  554. latest_data_today = histroical_data.iloc[-1]
  555. latest_data = {
  556. 'date': latest_data_today.name, # 使用索引的 name 属性获取日期
  557. 'close': latest_data_today['close'],
  558. 'high': latest_data_today['high'],
  559. 'low': latest_data_today['low'],
  560. 'open': latest_data_today['open'],
  561. 'ma5': latest_data_today['ma5'],
  562. 'ma10': latest_data_today['ma10'],
  563. 'ma20': latest_data_today['ma20'],
  564. 'ma30': latest_data_today['ma30']
  565. }
  566. # 计算并更新今天的完整数据
  567. # latest_data = crossed_symbol['latest_data']
  568. print(f'check latest_data_today: {latest_data_today}, latest_data: {latest_data}')
  569. # 获取突破趋势天数,用来判断突破趋势天数和突破均线的关系
  570. all_records = g.crossed_symbols_history[symbol]
  571. if not all_records:
  572. debug_print(f"No records found for {symbol}.")
  573. return False, False
  574. first_day = all_records[0]['date']
  575. today = context.current_dt.date()
  576. all_days = get_trade_days(first_day, today)
  577. cross_length = len(all_days)
  578. ma_lines_in_range, ma_labels_in_range = count_ma_lines_in_range(symbol, latest_data)
  579. diff_days_lines = cross_length - ma_lines_in_range
  580. print(f'check diff_days_lines: {diff_days_lines}, cross_length: {cross_length}, latest_data: {latest_data}')
  581. # 直接使用 latest_data,无需再次更新 MA 关系
  582. ma_value = latest_data[line_label]
  583. close_price = latest_data['close']
  584. today_in_change = (latest_data['close'] - latest_data['open'])/latest_data['open']
  585. print(f'symbol: {symbol}, close_price: {close_price}, 1.005: {1.005*ma_value}, 1.02: {1.02*ma_value}, 0.98: {0.98*ma_value}, 0.995: {0.995*ma_value}, today_in_change: {today_in_change}')
  586. if line_label != "ma5":
  587. if line_type == 'support' and 1.005*ma_value <= close_price <= 1.02*ma_value and today_in_change >= -0.002:
  588. buy_symbols.append((symbol, line_label, 'support', 'long'))
  589. elif line_type == 'resistance' and 0.98*ma_value <= close_price <= 0.995*ma_value and today_in_change <= 0.002:
  590. buy_symbols.append((symbol, line_label, 'resistance', 'short'))
  591. return buy_symbols
  592. #2-4 根据均线止损
  593. def check_ma_for_close(context, position, offset_ratio, days_for_adjustment):
  594. # print("-" * 20 + "MA closing check" + "-" * 20)
  595. security = position.security
  596. if security not in g.trade_history:
  597. # print(f'security: {security} not in trade history')
  598. return False
  599. trade_info = g.trade_history.get(position.security, {})
  600. finish_time = trade_info.get('finish_time')
  601. line_label = trade_info.get('line_label')
  602. # 获取所有交易日并计算持仓的交易日数
  603. all_trade_days = get_all_trade_days()
  604. holding_days = 0
  605. if finish_time:
  606. finish_date = finish_time.date()
  607. current_date = context.current_dt.date()
  608. holding_days = sum((finish_date <= d <= current_date) for d in all_trade_days)
  609. # 计算变化率
  610. today_price = get_current_data()[position.security].last_price
  611. avg_daily_change_rate = calculate_average_daily_change_rate(position.security)
  612. historical_data = attribute_history(position.security, 1, '1d', ['close'])
  613. yesterday_close = historical_data['close'].iloc[-1]
  614. today_change_rate = abs((today_price - yesterday_close) / yesterday_close)
  615. # 检查是否符合直接使用 ma5 和 ma10 规则的条件
  616. close_line = None
  617. if today_change_rate >= 1.5 * avg_daily_change_rate:
  618. close_line = 'ma5'
  619. elif holding_days <= days_for_adjustment:
  620. close_line = line_label
  621. else:
  622. close_line = 'ma5' if today_change_rate >= 1.2 * avg_daily_change_rate else 'ma10'
  623. # 计算 MA 值并进行平仓判断
  624. ma_values = calculate_ma_values(position.security, [5, 10, 20, 30])
  625. adjusted_ma_value = ma_values[close_line] * (1 + offset_ratio if position.side == 'short' else 1 - offset_ratio)
  626. # print(f'security: {position.security}, close_line: {close_line}, today_price: {today_price}, ma_price: {ma_values[close_line]}, check_point: {adjusted_ma_value}, offset_ratio: {offset_ratio}')
  627. if (position.side == 'long' and today_price < adjusted_ma_value) or \
  628. (position.side == 'short' and today_price > adjusted_ma_value):
  629. # print(f"Closing position in {position.security} based on {close_line} with position_side: {position.side} and adjusted_ma_value: {adjusted_ma_value}")
  630. close_position(position, g.trade_history[position.security]['direction'])
  631. return True
  632. return False
  633. #2-5 根据最大止损线进行止损
  634. def check_loss_for_close(context, position, direction, initial_loss_limit=-4000, loss_increment_per_day=200):
  635. print("-" * 20 + "Last line control" + "-" * 20)
  636. trade_info = g.trade_history.get(position.security, {})
  637. finish_time = trade_info.get('finish_time')
  638. # 获取所有交易日并计算持仓的交易日数
  639. all_trade_days = get_all_trade_days()
  640. holding_days = 0
  641. if finish_time:
  642. finish_date = finish_time.date()
  643. current_date = context.current_dt.date()
  644. holding_days = sum((finish_date <= d <= current_date) for d in all_trade_days)
  645. # 调整损失限制
  646. adjusted_loss_limit = initial_loss_limit + holding_days * loss_increment_per_day
  647. # print(f"Adjusted Loss Limit for {position.security}: {adjusted_loss_limit}")
  648. # print(f'position: {position}, direction: {direction}')
  649. if direction == 'long':
  650. current_price = position.price
  651. multiplier = position.value/(position.total_amount * position.price)
  652. revenue = multiplier * (position.price - position.acc_avg_cost) * position.total_amount
  653. # print(f"security: {position.security}, current_price: {current_price}, cost: {position.acc_avg_cost}, multiplier: {multiplier}, revenue: {revenue}")
  654. if revenue < adjusted_loss_limit:
  655. close_position(position, g.trade_history[position.security]['direction'])
  656. return True
  657. elif direction == 'short':
  658. current_price = position.price
  659. multiplier = position.value/(position.total_amount * position.price)
  660. revenue = multiplier * (position.acc_avg_cost - position.price) * position.total_amount
  661. # print(f"security: {position.security}, current_price: {current_price}, cost: {position.acc_avg_cost}, multiplier: {multiplier}, revenue: {revenue}")
  662. if revenue < adjusted_loss_limit:
  663. close_position(position, g.trade_history[position.security]['direction'])
  664. return True
  665. return False
  666. ############################ 一些辅助函数 ###################################
  667. #3-0 计算可以用于交易的金额
  668. def calculate_order_value(context, security, direction):
  669. # 获取最新的合约价格
  670. current_price = get_current_data()[security].last_price
  671. # 获取合约的品种(从合约代码中提取)
  672. underlying_symbol = security.split('.')[0][:-4] # 例如从 'IF1412.CCFX' 提取 'IF'
  673. # 获取保证金比例
  674. print(f'检查保证金比例{security}, direction: {direction}')
  675. margin_rate = g.default_margin_rates.get(direction, {}).get(underlying_symbol, 0.10) # 默认值作为后备
  676. # 获取合约乘数
  677. multiplier = g.multiplier.get(underlying_symbol, 10)
  678. # 计算单手保证金
  679. single_hand_margin = current_price * multiplier * margin_rate
  680. # 根据单手保证金决定购买手数
  681. if single_hand_margin <= 20000:
  682. total_margin = 20000
  683. else:
  684. total_margin = single_hand_margin
  685. print(f'calculate margin for {security} multiplier: {multiplier}, margin_rate: {margin_rate}, single_hand_margin: {single_hand_margin}, total_margin: {total_margin}')
  686. return total_margin
  687. #3-1 更新收盘价和4条ma均线之间的关系
  688. def update_high_low_ma_relations(symbol, high, low, ma5, ma10, ma20, ma30):
  689. if symbol not in g.high_low_ma_relations:
  690. g.high_low_ma_relations[symbol] = []
  691. today_relations = {
  692. 'ma5': {'low_below': low <= 0.997*ma5, 'high_above': high >= 1.003*ma5},
  693. 'ma10': {'low_below': low <= 0.997*ma10, 'high_above': high >= 1.003*ma10},
  694. 'ma20': {'low_below': low <= 0.997*ma20, 'high_above': high >= 1.003*ma20},
  695. 'ma30': {'low_below': low <= 0.997*ma30, 'high_above': high >= 1.003*ma30}
  696. }
  697. # 保持最近三天的数据
  698. if len(g.high_low_ma_relations[symbol]) >= 3:
  699. g.high_low_ma_relations[symbol].pop(0)
  700. g.high_low_ma_relations[symbol].append(today_relations)
  701. #3-2 更新收盘价是否够购买条件
  702. def check_ma_buy_conditions(symbol, latest_data, line_label, line_type):
  703. relations = g.high_low_ma_relations.get(symbol, [])
  704. print(f'checking {symbol}, relations: {relations}, line_label: {line_label}, line_type: {line_type}')
  705. if not relations:
  706. return False
  707. # 通过line_label确定是对ma5, ma10还是ma20进行判断
  708. ma_value = latest_data[line_label]
  709. close_price = latest_data['close']
  710. # 检查今天的关系是否满足特定条件
  711. for today_relations in relations:
  712. if line_type == 'support' and today_relations[line_label]['low_below'] and 1.005*ma_value <= close_price < 1.02*ma_value:
  713. return True
  714. elif line_type == 'resistance' and today_relations[line_label]['high_above'] and 0.98*ma_value < close_price <= 0.995*ma_value:
  715. return True
  716. return False
  717. #更新和均线关系连续天数的方法
  718. def update_continuous_days(continuous_days, close, ma5, ma10, ma20, ma30, length, message=False):
  719. # log.warning(f"before update_continuous_days: {continuous_days}")
  720. # 更新连续天数逻辑
  721. for key in ['above_ma5', 'below_ma5', 'above_ma10', 'below_ma10', 'above_ma20', 'below_ma20', 'above_ma30', 'below_ma30']:
  722. condition_met = False
  723. if ((key == 'above_ma5' and close > ma5) or
  724. (key == 'below_ma5' and close < ma5) or
  725. (key == 'above_ma10' and close > ma10) or
  726. (key == 'below_ma10' and close < ma10) or
  727. (key == 'above_ma20' and close > ma20) or
  728. (key == 'below_ma20' and close < ma20) or
  729. (key == 'above_ma30' and close > ma30) or
  730. (key == 'below_ma30' and close < ma30)):
  731. condition_met = True
  732. # 基本更新
  733. if condition_met:
  734. continuous_days[key].insert(0, continuous_days[key][0] + 1)
  735. else:
  736. continuous_days[key].insert(0, 0)
  737. continuous_days[key] = continuous_days[key][:length]
  738. # log.warning(f"before dealing ma30: {continuous_days}")
  739. # 特殊处理针对ma30
  740. for ma_key in ['above_ma30', 'below_ma30']:
  741. max_days = max(continuous_days[ma_key]) > 10
  742. first_day_positive = continuous_days[ma_key][0] > 0
  743. zeros_count = continuous_days[ma_key].count(0) <= 2
  744. message = False
  745. if message:
  746. log.debug(f'check {ma_key}: max_days-{max_days}, first_day_positive-{first_day_positive}, zeros_count-{zeros_count}')
  747. if ma_key == 'below_ma30' and close > ma30 or ma_key == 'above_ma30' and close < ma30:
  748. max_ratio = abs((close - ma30) / ma30)
  749. # 检查是否满足特殊处理的条件
  750. if max_days and first_day_positive and zeros_count and max_ratio <= 0.015:
  751. # log.debug(f"Meet the condition: {ma_key} for {continuous_days[ma_key]}")
  752. # 恢复继续计数的情况
  753. if ma_key == 'below_ma30':
  754. continuous_days['below_ma30'] = [sum(x > 0 for x in continuous_days['below_ma30'][:i+1]) for i in range(length)]
  755. continuous_days['above_ma30'] = [0] * length
  756. elif ma_key == 'above_ma30':
  757. continuous_days['above_ma30'] = [sum(x > 0 for x in continuous_days['above_ma30'][:i+1]) for i in range(length)]
  758. continuous_days['below_ma30'] = [0] * length
  759. # log.warning(f"by the end of update_continuous_days: {continuous_days}")
  760. #3-4 更新crossed_symbol
  761. def create_crossed_symbol_info(symbol, line_label, line_type, latest_data):
  762. """Helper function to create crossed symbol info dictionary"""
  763. return {
  764. 'symbol': symbol,
  765. 'line_label': line_label,
  766. 'line_type': line_type,
  767. 'direction': 'long' if line_type == 'support' else 'short',
  768. 'latest_data': latest_data,
  769. 'historical_condition_tracker': {
  770. 'low_below_ma10': False,
  771. 'low_below_ma20': False,
  772. 'low_below_ma30': False,
  773. 'high_above_ma10': False,
  774. 'high_above_ma20': False,
  775. 'high_above_ma30': False,
  776. 'close_above_threshold': False,
  777. 'close_below_threshold': False
  778. }
  779. }
  780. #3-5 获取今天的交易数据
  781. def get_latest_trading_data(symbol, context):
  782. # 获取今天日期
  783. today = context.current_dt.date()
  784. # 获取昨天的日期和夜盘开始时间(假设从21:00开始)
  785. yesterday = today - timedelta(days=1)
  786. night_session_start_date = get_night_session_start_date(today)
  787. night_session_start = datetime.combine(night_session_start_date, datetime.strptime("21:00", "%H:%M").time())
  788. night_session_start = pd.to_datetime(night_session_start)
  789. # print(f'night_session_start: {night_session_start}, {type(night_session_start)}')
  790. # 获取分钟级别的数据
  791. minute_data = attribute_history(symbol, 600, '1m', ['close', 'high', 'low', 'open'], df=True)
  792. # 打印数据类型和索引的前几个值
  793. # print("minute_data earliest index:", minute_data.index.min())
  794. # print("minute_data latest index:", minute_data.index.max())
  795. # 保留属于当前交易日的数据,包括夜盘
  796. # print("Is night_session_start in minute_data index range:",
  797. # night_session_start >= minute_data.index.min() and night_session_start <= minute_data.index.max())
  798. latest_data = minute_data[minute_data.index >= night_session_start]
  799. # print("latest_data index after filtering first few values:", latest_data.index[:5])
  800. # print("latest_data index after filtering last few values:", latest_data.index[-5:])
  801. # 计算截至当前时刻的 close, high, low, open, ma5, ma10, ma20
  802. close = latest_data['close'][-1]
  803. high = latest_data['high'].max()
  804. low = latest_data['low'].min()
  805. open_price = latest_data['open'][0]
  806. # ma5 = latest_data['close'].rolling(window=5).mean()[-1]
  807. # ma10 = latest_data['close'].rolling(window=10).mean()[-1]
  808. # ma20 = latest_data['close'].rolling(window=20).mean()[-1]
  809. # print(f'symbol: {symbol}, close: {close}, high: {high}, low: {low}, open: {open_price}')#, ma5: {ma5}, ma10: {ma10}, ma20: {ma20}')
  810. return {'close': close, 'high': high, 'low': low, 'open': open_price}#, 'ma5': ma5, 'ma10': ma10, 'ma20': ma20}
  811. #3-6 获取今天的交易时间
  812. def get_night_session_start_date(current_date):
  813. # 如果今天是周一(weekday() == 0),夜盘开始于上一个周五
  814. if current_date.weekday() == 0:
  815. return current_date - timedelta(days=3)
  816. else:
  817. return current_date - timedelta(days=1)
  818. #3-7 将当日的数据融入之前的数据里
  819. def calculate_continuous_data(today_data, today_daily_data_info, symbol, continuous_days_length):
  820. # 先计算今天的近似MA,获取包括今天在内的近日数据,至少需要 20 天数据来计算 MA20
  821. # print(f'for calculate_continuous_data, today_daily_data_info: {today_daily_data_info}')
  822. recent_data = today_daily_data_info[symbol][-29:] + [today_data]
  823. # print(f'under calculate_continuous_data, recent_data: {recent_data}')
  824. # print(f'today_daily_data_info[symbol]: {today_daily_data_info[symbol]}')
  825. # print(f'original today_data: {today_data}')
  826. # 计算 MA 值
  827. df_recent = pd.DataFrame(recent_data)
  828. # print(f'df_recent: {df_recent}')
  829. today_data['ma5'] = df_recent['close'].rolling(window=5).mean().iloc[-1]
  830. today_data['ma10'] = df_recent['close'].rolling(window=10).mean().iloc[-1]
  831. today_data['ma20'] = df_recent['close'].rolling(window=20).mean().iloc[-1]
  832. today_data['ma30'] = df_recent['close'].rolling(window=30).mean().iloc[-1]
  833. # print(f'new today_data: {today_data}')
  834. # 获取昨天的数据
  835. last_day_data = today_daily_data_info[symbol][-1]
  836. # print(f'checking calculate_continuous_data: symbol-{symbol}')
  837. # print(f'df_recent: {df_recent.info()}')
  838. # print(f'last_day_data: {last_day_data}')
  839. # 初始化今天的连续天数数据
  840. new_continuous_data = {
  841. 'above_ma5': last_day_data['continuous_above_ma5'].copy(),
  842. 'below_ma5': last_day_data['continuous_below_ma5'].copy(),
  843. 'above_ma10': last_day_data['continuous_above_ma10'].copy(),
  844. 'below_ma10': last_day_data['continuous_below_ma10'].copy(),
  845. 'above_ma20': last_day_data['continuous_above_ma20'].copy(),
  846. 'below_ma20': last_day_data['continuous_below_ma20'].copy(),
  847. 'above_ma30': last_day_data['continuous_above_ma30'].copy(),
  848. 'below_ma30': last_day_data['continuous_below_ma30'].copy()
  849. }
  850. # print(f'new_continuous_data: {new_continuous_data}')
  851. # 更新今天的连续天数数据
  852. for ma_label in ['ma5', 'ma10', 'ma20', 'ma30']:
  853. # print(f"today close: {today_data['close']}, today ma_label: {today_data[ma_label]}")
  854. if today_data['close'] > today_data[ma_label]:
  855. new_continuous_data[f'above_{ma_label}'].insert(0, new_continuous_data[f'above_{ma_label}'][0] + 1)
  856. new_continuous_data[f'below_{ma_label}'].insert(0, 0)
  857. elif today_data['close'] < today_data[ma_label]:
  858. new_continuous_data[f'below_{ma_label}'].insert(0, new_continuous_data[f'below_{ma_label}'][0] + 1)
  859. new_continuous_data[f'above_{ma_label}'].insert(0, 0)
  860. else:
  861. # 如果收盘价等于MA值,则重置连续天数
  862. new_continuous_data[f'above_{ma_label}'].insert(0, 0)
  863. new_continuous_data[f'below_{ma_label}'].insert(0, 0)
  864. # 确保长度不超过 continuous_days_length
  865. for key in new_continuous_data:
  866. if len(new_continuous_data[key]) > continuous_days_length:
  867. new_continuous_data[key] = new_continuous_data[key][:continuous_days_length]
  868. # 构建今天的完整数据并返回
  869. today_full_data = {**today_data, **{
  870. 'continuous_above_ma5': new_continuous_data['above_ma5'],
  871. 'continuous_below_ma5': new_continuous_data['below_ma5'],
  872. 'continuous_above_ma10': new_continuous_data['above_ma10'],
  873. 'continuous_below_ma10': new_continuous_data['below_ma10'],
  874. 'continuous_above_ma20': new_continuous_data['above_ma20'],
  875. 'continuous_below_ma20': new_continuous_data['below_ma20'],
  876. 'continuous_above_ma30': new_continuous_data['above_ma30'],
  877. 'continuous_below_ma30': new_continuous_data['below_ma30']
  878. }}
  879. # print(f'today_full_data: {today_full_data}')
  880. return today_full_data
  881. #3-8 计算日均变化率
  882. def calculate_average_daily_change_rate(security, days=30):
  883. historical_data = attribute_history(security, days + 1, '1d', ['close']) # 获取额外一天的数据以便计算变化率
  884. daily_change_rates = abs(historical_data['close'].pct_change()).iloc[1:] # 排除第一条数据
  885. avg_daily_change_rate = daily_change_rates.mean()
  886. # print(f"Average Daily Change Rate for {security}: {avg_daily_change_rate}")
  887. return avg_daily_change_rate
  888. #3-9 计算所需MA数据
  889. def calculate_ma_values(security, ma_periods):
  890. historical_data = attribute_history(security, max(ma_periods), '1d', ['close'])
  891. today_price = get_current_data()[security].last_price
  892. close_prices = historical_data['close'].tolist() + [today_price]
  893. # print(f'check ma prices: security-{security}, close_prices-{close_prices}')
  894. ma_values = {f'ma{period}': np.mean(close_prices[-period:]) for period in ma_periods}
  895. return ma_values
  896. #3-10 判断过去一段时间不包括当天4条ma均线在过去几天有多少次交叉
  897. def count_ma_crosses(future_data, days, if_details=False):
  898. recent_data = future_data[-days:]
  899. # print(f'future_data: {future_data}, recent_data: {recent_data}')
  900. ma5 = [day['ma5'] for day in recent_data]
  901. ma10 = [day['ma10'] for day in recent_data]
  902. ma20 = [day['ma20'] for day in recent_data]
  903. ma30 = [day['ma30'] for day in recent_data]
  904. # print(f'ma5: {ma5}, ma10: {ma10}, ma20: {ma20}, ma30: {ma30}')
  905. cross_5_10 = sum([1 for i in range(1, len(ma5)) if ((ma5[-i] > ma10[-i] and ma5[-i-1] < ma10[-i-1]) or
  906. (ma5[-i] < ma10[-i] and ma5[-i-1] > ma10[-i-1]))])
  907. cross_5_20 = sum([1 for i in range(1, len(ma5)) if ((ma5[-i] > ma20[-i] and ma5[-i-1] < ma20[-i-1]) or
  908. (ma5[-i] < ma20[-i] and ma5[-i-1] > ma20[-i-1]))])
  909. cross_5_30 = sum([1 for i in range(1, len(ma5)) if ((ma5[-i] > ma30[-i] and ma5[-i-1] < ma30[-i-1]) or
  910. (ma5[-i] < ma30[-i] and ma5[-i-1] > ma30[-i-1]))])
  911. cross_10_20 = sum([1 for i in range(1, len(ma10)) if ((ma10[-i] > ma20[-i] and ma10[-i-1] < ma20[-i-1]) or
  912. (ma10[-i] < ma20[-i] and ma10[-i-1] > ma20[-i-1]))])
  913. cross_10_30 = sum([1 for i in range(1, len(ma10)) if ((ma10[-i] > ma30[-i] and ma10[-i-1] < ma30[-i-1]) or
  914. (ma10[-i] < ma30[-i] and ma10[-i-1] > ma30[-i-1]))])
  915. cross_20_30 = sum([1 for i in range(1, len(ma20)) if ((ma20[-i] > ma30[-i] and ma20[-i-1] < ma30[-i-1]) or
  916. (ma20[-i] < ma30[-i] and ma20[-i-1] > ma30[-i-1]))])
  917. crosses = cross_5_10 + cross_5_20 + cross_5_30 + cross_10_20 + cross_10_30 + cross_20_30
  918. # log.debug(f'crosses: {crosses}, cross_5_10: {cross_5_10}, cross_5_20: {cross_5_20}, cross_5_30: {cross_5_30}, cross_10_20: {cross_10_20}, cross_10_30: {cross_10_30}, cross_20_30: {cross_20_30}')
  919. if if_details:
  920. check_ma_cross_details(recent_data, days)
  921. return crosses
  922. #3-11 查看具体均线交叉的日期和价格
  923. def check_ma_cross_details(recent_data, days):
  924. # 初始化交叉信息列表
  925. cross_info = []
  926. # 遍历days天的数据
  927. for i in range(1, days):
  928. # 检查MA线的交叉
  929. current_date = recent_data[-i]['date']
  930. previous_date = recent_data[-i - 1]['date']
  931. if (ma5[-i] > ma10[-i] and ma5[-i-1] < ma10[-i-1]) or (ma5[-i] < ma10[-i] and ma5[-i-1] > ma10[-i-1]):
  932. cross_info.append({
  933. 'Date': previous_date, # 前一天的日期
  934. 'MA5': ma5[-i], # MA5的值
  935. 'MA10': ma10[-i], # MA10的值
  936. 'Date_Next': current_date, # 当天的日期
  937. 'MA5_Next': ma5[-i-1],
  938. 'MA10_Next': ma10[-i-1],
  939. 'Cross_Type': 'MA5_MA10'
  940. })
  941. if (ma5[-i] > ma20[-i] and ma5[-i-1] < ma20[-i-1]) or (ma5[-i] < ma20[-i] and ma5[-i-1] > ma20[-i-1]):
  942. cross_info.append({
  943. 'Date': previous_date, # 前一天的日期
  944. 'MA5': ma5[-i], # MA5的值
  945. 'MA20': ma20[-i], # MA20的值
  946. 'Date_Next': current_date, # 当天的日期
  947. 'MA5_Next': ma5[-i-1],
  948. 'MA20_Next': ma20[-i-1],
  949. 'Cross_Type': 'MA5_MA20'
  950. })
  951. if (ma5[-i] > ma30[-i] and ma5[-i-1] < ma30[-i-1]) or (ma5[-i] < ma30[-i] and ma5[-i-1] > ma30[-i-1]):
  952. cross_info.append({
  953. 'Date': previous_date, # 前一天的日期
  954. 'MA5': ma5[-i], # MA5的值
  955. 'MA30': ma30[-i], # MA30的值
  956. 'Date_Next': current_date, # 当天的日期
  957. 'MA5_Next': ma5[-i-1],
  958. 'MA30_Next': ma30[-i-1],
  959. 'Cross_Type': 'MA5_MA30'
  960. })
  961. # 检查MA10与其他MA线的交叉
  962. if (ma10[-i] > ma20[-i] and ma10[-i-1] < ma20[-i-1]) or (ma10[-i] < ma20[-i] and ma10[-i-1] > ma20[-i-1]):
  963. cross_info.append({
  964. 'Date': previous_date, # 前一天的日期
  965. 'MA10': ma10[-i], # MA10的值
  966. 'MA20': ma20[-i], # MA20的值
  967. 'Date_Next': current_date, # 当天的日期
  968. 'MA10_Next': ma10[-i-1],
  969. 'MA20_Next': ma20[-i-1],
  970. 'Cross_Type': 'MA10_MA20'
  971. })
  972. if (ma10[-i] > ma30[-i] and ma10[-i-1] < ma30[-i-1]) or (ma10[-i] < ma30[-i] and ma10[-i-1] > ma30[-i-1]):
  973. cross_info.append({
  974. 'Date': previous_date, # 前一天的日期
  975. 'MA10': ma10[-i], # MA10的值
  976. 'MA30': ma30[-i], # MA30的值
  977. 'Date_Next': current_date, # 当天的日期
  978. 'MA10_Next': ma10[-i-1],
  979. 'MA30_Next': ma30[-i-1],
  980. 'Cross_Type': 'MA10_MA30'
  981. })
  982. if (ma20[-i] > ma30[-i] and ma20[-i-1] < ma30[-i-1]) or (ma20[-i] < ma30[-i] and ma20[-i-1] > ma30[-i-1]):
  983. cross_info.append({
  984. 'Date': previous_date, # 前一天的日期
  985. 'MA20': ma10[-i], # MA20的值
  986. 'MA30': ma30[-i], # MA30的值
  987. 'Date_Next': current_date, # 当天的日期
  988. 'MA20_Next': ma20[-i-1],
  989. 'MA30_Next': ma30[-i-1],
  990. 'Cross_Type': 'MA10_MA30'
  991. })
  992. # 将交叉信息转换为DataFrame
  993. cross_info_df = pd.DataFrame(cross_info)
  994. # 打印交叉信息
  995. # print(f'crosses: {crosses}, cross_info: {cross_info}, cross_info_df: {cross_info_df}')
  996. #3-12 查看一下保证金的情况
  997. def update_margin_rate(context):
  998. today = context.current_dt.date()
  999. margin_rate_df = finance.run_query(query(finance.FUT_MARGIN).filter(finance.FUT_MARGIN.day == '2022-12-22'))
  1000. # print(f'today date: {today}, margin_rate_df: {margin_rate_df}')
  1001. #3-13 检查趋势是否有特殊情况
  1002. def adjust_for_special_condition(daily_data, continuous_days_length):
  1003. # 为每个均线条件检查是否满足特殊情况
  1004. print("Daily Data:", daily_data)
  1005. for key in ['above_ma5', 'below_ma5', 'above_ma10', 'below_ma10', 'above_ma20', 'below_ma20', 'above_ma30', 'below_ma30']:
  1006. for i in range(len(daily_data) - continuous_days_length + 1):
  1007. print("Processing Date:", daily_data[i]['date'])
  1008. # 提取特定范围内的数据
  1009. subset = daily_data[i:i + continuous_days_length]
  1010. # 检查最大连续天数是否大于等于10
  1011. max_continuous_days = max([day[f'continuous_{key}'][0] for day in subset])
  1012. if max_continuous_days >= 10:
  1013. # 检查连续天数为0的天数
  1014. days_with_zero = [day for day in subset if day[f'continuous_{key}'][0] == 0]
  1015. if len(days_with_zero) <= 3:
  1016. # 检查收盘价与均线价格之间的比值是否在-0.005到0.005之间
  1017. valid_ratio_days = all(
  1018. -0.005 <= (day['close'] - day[key.split('_')[1]]) / day[key.split('_')[1]] <= 0.005
  1019. for day in days_with_zero
  1020. )
  1021. if valid_ratio_days:
  1022. # 如果满足特殊情况,根据需要调整逻辑
  1023. pass # 在这里实现具体的调整逻辑
  1024. #3-14 更新突破的列表
  1025. def update_crossed_symbols_history(context, new_record):
  1026. symbol = new_record['symbol']
  1027. print(f'更新突破的历史列表,这次是{symbol}')
  1028. date = new_record['date']
  1029. line_label = new_record['line_label']
  1030. line_type = new_record['line_type']
  1031. # 检查历史穿越记录里是否有相同的标的或者同一族的标的
  1032. if len(g.crossed_symbols_history) == 0:
  1033. print('g.crossed_symbols_history为空')
  1034. # 这里也要改,要增加该记录
  1035. g.crossed_symbols_history[symbol] = [new_record]
  1036. return
  1037. if symbol in g.crossed_symbols_history:
  1038. pass
  1039. else:
  1040. similar_symbol = next((s for s in g.crossed_symbols_history if s.split('.')[0] == symbol.split('.')[0]), None)
  1041. if similar_symbol:
  1042. pass
  1043. else:
  1044. # 这里也要改,要增加该记录
  1045. print(f'g.crossed_symbols_history没有{symbol},所以要增加')
  1046. g.crossed_symbols_history[symbol] = [new_record]
  1047. return
  1048. # 情况1:检查是否存在一模一样的symbol
  1049. if symbol in g.crossed_symbols_history:
  1050. existing_records = g.crossed_symbols_history[symbol]
  1051. print(f'temp里现有{symbol}对应的记录: {existing_records}')
  1052. for record in existing_records:
  1053. print(f'开始逐条处理{symbol}的记录: {record}和最新的记录{new_record}')
  1054. # 情况1.1:检查line_label和line_type是否一致
  1055. if record['line_label'] == line_label and record['line_type'] == line_type:
  1056. # 情况1.1.2:检查date是否一样
  1057. if record['date'] == date:
  1058. # 情况1.1.1:更新相同date的数据
  1059. print(f'情况1.1.1 查到相同标的{symbol}、趋势线{line_label} & {line_type}、日期{date}的记录{g.crossed_symbols_history[symbol]},删除日期日期相同的那一条,并添加最新的')
  1060. record.update(new_record)
  1061. print(f'更新之后的记录{record}')
  1062. return
  1063. # 情况1.1.2.1: 添加新记录
  1064. else:
  1065. print(f'情况1.1.2 查到相同标的{symbol}、趋势线{line_label} & {line_type}、不同日期{date}的记录,直接添加新纪录。g.crossed_symbols_history[symbol]: {g.crossed_symbols_history[symbol]}')
  1066. g.crossed_symbols_history[symbol].append(new_record)
  1067. print(f'更新之后的记录{g.crossed_symbols_history[symbol]}')
  1068. break
  1069. else:
  1070. # 情况1.1.1: 比较line_label,只保留数字较大的
  1071. print(f"情况1.2.1 查到相同标的{symbol},不同趋势线{line_label} & {line_type}和{record['line_label']} & {record['line_type']},更新所有原始记录: {g.crossed_symbols_history[symbol]},添加新纪录")
  1072. max_label_record = max(existing_records, key=lambda x: int(''.join(filter(str.isdigit, x['line_label']))))
  1073. max_label_number = int(''.join(filter(str.isdigit, max_label_record['line_label'])))
  1074. new_label_number = int(''.join(filter(str.isdigit, line_label)))
  1075. print(f'max_label_record: {max_label_record}, max_label_number: {max_label_number}, new_label_number: {new_label_number}')
  1076. if new_label_number > max_label_number:
  1077. # 创建一个新的列表来保存所有记录的日期
  1078. dates_to_save = [record['date'] for record in existing_records]
  1079. print(f'dates_to_save: {dates_to_save}')
  1080. # 根据上述的dates_to_save更新新的趋势线的历史数据,首先天数不能超过3
  1081. if len(dates_to_save) + 1 <=3:
  1082. close_data = attribute_history(symbol, 50, '1d', ['close', 'high', 'low', 'open'], df=True)
  1083. close_series = close_data['close']
  1084. # print(f'Check if future: {symbol} - {close_series}')
  1085. # 计算5日、10日、20日和30日移动平均线
  1086. ma5 = close_series.rolling(window=5).mean()
  1087. ma10 = close_series.rolling(window=10).mean()
  1088. ma20 = close_series.rolling(window=20).mean()
  1089. ma30 = close_series.rolling(window=30).mean()
  1090. print(f'updating data for new lines MA{new_label_number}')
  1091. # 更新为数字较大的记录
  1092. # for record in existing_records:
  1093. # # 替换 existing_records 中每个记录的 'line_label' 和 'line_type'
  1094. # record['line_label'] = new_record['line_label']
  1095. # record['line_type'] = new_record['line_type']
  1096. existing_records.clear()
  1097. print(f'去除原有{max_label_record}的记录后: {existing_records}')
  1098. existing_records.append(new_record)
  1099. print(f'增加最新的记录:{g.crossed_symbols_history[symbol]}')
  1100. return
  1101. else:
  1102. # 情况1.2:检查是否有字母相同的symbol
  1103. similar_symbol = next((s for s in g.crossed_symbols_history if s.split('.')[0] == symbol.split('.')[0]), None)
  1104. if similar_symbol:
  1105. print(f'情况1.2 有字母相同的symbol,similar_symbol: {similar_symbol}, g.crossed_symbols_history: {g.crossed_symbols_history}')
  1106. # 情况1.2.2:更新为最新的symbol,并继承原始symbol的数据
  1107. g.crossed_symbols_history[symbol] = g.crossed_symbols_history.pop(similar_symbol)
  1108. print(f'更新完之后g.crossed_symbols_history: {g.crossed_symbols_history}')
  1109. # 递归调用以重新处理新记录
  1110. return update_crossed_symbols_history(g.crossed_symbols_history, new_record)
  1111. else:
  1112. # 情况1.2.1:直接添加新记录
  1113. g.crossed_symbols_history[symbol] = [new_record]
  1114. print(f'没有查询到该标的{symbol}的相关记录,满足情况1.2.1,增加新纪录{new_record}')
  1115. return
  1116. #3-15 检查MA均线之间的关系
  1117. def check_ma_relations(context, symbol, debug=False):
  1118. def debug_print(message):
  1119. if debug:
  1120. print(message)
  1121. debug_print(f'检查均线之间的关系')
  1122. relation_check = False
  1123. latest_record = g.crossed_symbols_history[symbol][-1]
  1124. debug_print(f'latest_record: {latest_record}')
  1125. line_label = latest_record['line_label'].lower() # 确保大小写一致
  1126. line_type = latest_record['line_type']
  1127. MA_values = latest_record['latest_data']
  1128. MA5 = MA_values['ma5']
  1129. MA10 = MA_values['ma10']
  1130. MA20 = MA_values['ma20']
  1131. MA30 = MA_values['ma30']
  1132. debug_print(f'line_label: {line_label}, line_type: {line_type}, MA5: {MA5}, MA10: {MA10}, MA20: {MA20}, MA30: {MA30}')
  1133. conditions = {
  1134. ('ma10', 'resistance'): MA5 > MA10 > MA20 > MA30,
  1135. ('ma10', 'support'): MA30 > MA20 > MA10 > MA5,
  1136. ('ma20', 'resistance'): (MA30 >= MA20 * 0.999 and MA5 >= MA10 * 0.999) or (MA30 > MA20 > MA10 > MA5),
  1137. ('ma20', 'support'): (MA20 >= MA30 * 0.999 and MA10 >= MA5 * 0.999) or (MA5 > MA10 > MA20 > MA30),
  1138. ('ma30', 'resistance'): (MA30 >= MA20 * 0.999 and MA5 >= MA10 * 0.999) or (MA30 > MA20 > MA10 > MA5),
  1139. ('ma30', 'support'): (MA20 >= MA30 * 0.999 and MA10 >= MA5 * 0.999) or (MA5 > MA10 > MA20 > MA30),
  1140. }
  1141. relation_check = conditions.get((line_label, line_type), False)
  1142. condition_status = "满足" if relation_check else "不满足"
  1143. debug_print(f'均线为{line_label} & {line_type},{condition_status}MA指定的条件,MA5: {MA5}, MA10: {MA10}, MA20: {MA20}, MA30: {MA30}')
  1144. return relation_check
  1145. #3-16 检查破趋势的天数和其中收盘价和均线的最高差别
  1146. def check_cross_details(context, symbol, debug=False):
  1147. def debug_print(message):
  1148. if debug:
  1149. print(message)
  1150. all_records = g.crossed_symbols_history[symbol]
  1151. if not all_records:
  1152. debug_print(f"No records found for {symbol}.")
  1153. return False, False
  1154. # 检查破趋势的天数
  1155. first_day = all_records[0]['date']
  1156. today = context.current_dt.date()
  1157. all_days = get_trade_days(first_day, today)
  1158. duration_length = len(all_days)
  1159. cross_duration = duration_length <= 6
  1160. debug_print(f"[检查破趋势的天数] first_day: {first_day}, today: {today}, duration_length: {duration_length}, cross_duration: {cross_duration}")
  1161. # 检查破趋势范围内收盘价和均价差距的情况
  1162. ma_close = False
  1163. for record in all_records:
  1164. line_label = record['line_label']
  1165. ma_price = record['latest_data'][line_label]
  1166. close = record['latest_data']['close']
  1167. ma_close_rate = abs((close - ma_price) / ma_price)
  1168. if ma_close_rate <= 0.02:
  1169. ma_close = True
  1170. debug_print(f"[检查破趋势内收盘价和均线的关系] record: {record}, ma_price: {ma_price}, close: {close}, ma_close_rate: {ma_close_rate}, ma_close: {ma_close}")
  1171. return cross_duration, ma_close
  1172. #3-17 计算开盘到收盘之间穿过几条均线
  1173. def count_ma_lines_in_range(symbol, latest_data):
  1174. # 获取开盘和收盘价格
  1175. open_price = latest_data['open']
  1176. close_price = latest_data['close']
  1177. # 确定价格区间的上限和下限
  1178. lower_bound = min(open_price, close_price)
  1179. upper_bound = max(open_price, close_price)
  1180. # 计算哪些MA线在开盘和收盘价格之间
  1181. ma_lines_in_range = 0
  1182. ma_labels_in_range = []
  1183. for key, value in latest_data.items():
  1184. if 'ma' in key and lower_bound <= value <= upper_bound:
  1185. ma_lines_in_range += 1
  1186. ma_labels_in_range.append(key)
  1187. print(f"在{symbol}的{lower_bound}到{upper_bound}之间,有{ma_lines_in_range}条MA线:{', '.join(ma_labels_in_range)}")
  1188. return ma_lines_in_range, ma_labels_in_range
  1189. #3-18 更新不在今天出现或者今天出现但是趋势线不如历史大的记录更新最新一天的数据
  1190. def update_record_for_existing_symbol(symbol, case):
  1191. # 假设 attribute_history, g.crossed_symbols_history 等都已正确定义和初始化
  1192. # 根据case参数,执行特定的处理逻辑
  1193. if case == 'difference_keys':
  1194. print(f"处理差异键情况的{symbol}")
  1195. # 这里是处理差异键情况的逻辑...
  1196. elif case == 'keys_in_both':
  1197. print(f"处理line_label差异的{symbol}")
  1198. # 获取symbol的最新记录
  1199. historical_record = max(g.crossed_symbols_history[symbol], key=lambda x: x['date'])
  1200. historical_record_copy = historical_record.copy()
  1201. # 获取过去50天的数据
  1202. close_data = attribute_history(symbol, 50, '1d', ['close', 'high', 'low', 'open'], df=True)
  1203. close_series = close_data['close']
  1204. # 计算移动平均线
  1205. ma5 = close_series.rolling(window=5).mean().iloc[-1]
  1206. ma10 = close_series.rolling(window=10).mean().iloc[-1]
  1207. ma20 = close_series.rolling(window=20).mean().iloc[-1]
  1208. ma30 = close_series.rolling(window=30).mean().iloc[-1]
  1209. yesterday_data = close_data.iloc[-1]
  1210. # 更新记录
  1211. historical_record_copy['date'] = yesterday_data.name.date() # 假设'name'是一个datetime对象
  1212. historical_record_copy['latest_data'] = {
  1213. 'date': yesterday_data.name.date(),
  1214. 'close': yesterday_data['close'],
  1215. 'high': yesterday_data['high'],
  1216. 'low': yesterday_data['low'],
  1217. 'open': yesterday_data['open'],
  1218. 'ma5': ma5,
  1219. 'ma10': ma10,
  1220. 'ma20': ma20,
  1221. 'ma30': ma30,
  1222. }
  1223. # 将更新后的记录添加到g.crossed_symbols_history[symbol]
  1224. g.crossed_symbols_history[symbol].append(historical_record_copy)
  1225. # print(f'根据{case}更新{symbol}的g.crossed_symbols_history[symbol]: {g.crossed_symbols_history[symbol]}, historical_record_copy: {historical_record_copy}')
  1226. #3-19 检查有没有相似元素
  1227. def check_symbol_prefix_match(symbol, hold_symbols):
  1228. # 去掉右侧9个字符得到symbol的左侧部分
  1229. symbol_prefix = symbol[:-9]
  1230. # 遍历hold_symbols中的每个元素
  1231. for hold_symbol in hold_symbols:
  1232. # 对每个元素同样去掉右侧9个字符
  1233. hold_symbol_prefix = hold_symbol[:-9]
  1234. # 检查左侧部分是否一致
  1235. if symbol_prefix == hold_symbol_prefix:
  1236. return True # 找到一致的情况,返回True
  1237. return False # 遍历完成,没有找到一致的情况,返回False
  1238. #3-20 计算过去一段时间的日内以及均线数据
  1239. def calculate_daily_data(context, day, symbol, combine_today=True):
  1240. """
  1241. 计算指定股票的移动平均线并返回一个包含这些数据的DataFrame。
  1242. """
  1243. # 使用attribute_history函数获取过去50天的交易数据
  1244. historical_data = attribute_history(symbol, day, '1d', ['close', 'high', 'low', 'open'], df=True)
  1245. if combine_today:
  1246. # 使用get_latest_trading_data函数获取最新的交易数据
  1247. latest_data_today = get_latest_trading_data(symbol, context)
  1248. print(f'check latest_data_today under buy check: {latest_data_today}')
  1249. # 将最新的交易数据转换成DataFrame并追加到historical_data中
  1250. latest_data_df = pd.DataFrame([latest_data_today], index=[context.current_dt.date()])
  1251. full_data = pd.concat([historical_data, latest_data_df])
  1252. else:
  1253. full_data = historical_data
  1254. # 计算移动平均线并将其添加到DataFrame中
  1255. full_data['ma5'] = full_data['close'].rolling(window=5).mean()
  1256. full_data['ma10'] = full_data['close'].rolling(window=10).mean()
  1257. full_data['ma20'] = full_data['close'].rolling(window=20).mean()
  1258. full_data['ma30'] = full_data['close'].rolling(window=30).mean()
  1259. return full_data
  1260. #3-21 辅助打印list
  1261. def print_list_elements(title, elements):
  1262. """
  1263. 打印列表中的元素,每个元素占一行。
  1264. :param title: 打印前显示的标题
  1265. :param elements: 要打印的元素列表
  1266. """
  1267. print(f"{title}:")
  1268. for item in elements:
  1269. print(item)
  1270. ########################## 自动移仓换月函数 #################################
  1271. def position_auto_switch(context,pindex=0,switch_func=None, callback=None):
  1272. """
  1273. 期货自动移仓换月。默认使用市价单进行开平仓。
  1274. :param context: 上下文对象
  1275. :param pindex: 子仓对象
  1276. :param switch_func: 用户自定义的移仓换月函数.
  1277. 函数原型必须满足: func(context, pindex, previous_dominant_future_position, current_dominant_future_symbol)
  1278. :param callback: 移仓换月完成后的回调函数。
  1279. 函数原型必须满足: func(context, pindex, previous_dominant_future_position, current_dominant_future_symbol)
  1280. :return: 发生移仓换月的标的。类型为列表。
  1281. """
  1282. import re
  1283. subportfolio = context.subportfolios[pindex]
  1284. symbols = set(subportfolio.long_positions.keys()) | set(subportfolio.short_positions.keys())
  1285. switch_result = []
  1286. for symbol in symbols:
  1287. match = re.match(r"(?P<underlying_symbol>[A-Z]{1,})", symbol)
  1288. if not match:
  1289. # raise ValueError("未知期货标的: {}".format(symbol))
  1290. raise ValueError("Unknow target: {}".format(symbol))
  1291. else:
  1292. dominant = get_dominant_future(match.groupdict()["underlying_symbol"])
  1293. cur = get_current_data()
  1294. symbol_last_price = cur[symbol].last_price
  1295. dominant_last_price = cur[dominant].last_price
  1296. print(f'current_hold_symbol: {symbol}, current_main_symbol: {dominant}')
  1297. if dominant > symbol:
  1298. for positions_ in (subportfolio.long_positions, subportfolio.short_positions):
  1299. if symbol not in positions_.keys():
  1300. continue
  1301. else :
  1302. p = positions_[symbol]
  1303. if switch_func is not None:
  1304. switch_func(context, pindex, p, dominant)
  1305. else:
  1306. amount = p.total_amount
  1307. # 跌停不能开空和平多,涨停不能开多和平空。
  1308. if p.side == "long":
  1309. symbol_low_limit = cur[symbol].low_limit
  1310. dominant_high_limit = cur[dominant].high_limit
  1311. if symbol_last_price <= symbol_low_limit:
  1312. # log.warning("标的{}跌停,无法平仓。移仓换月取消。".format(symbol))
  1313. log.warning("Can't close {} position due to the limit up. Cancelling the exchange.".format(symbol))
  1314. continue
  1315. elif dominant_last_price >= dominant_high_limit:
  1316. # log.warning("标的{}涨停,无法开仓。移仓换月取消。".format(symbol))
  1317. log.warning("Can't close {} position due to the limit down. Cancelling the exchange.".format(symbol))
  1318. continue
  1319. else:
  1320. # log.info("进行移仓换月: ({0},long) -> ({1},long)".format(symbol, dominant))
  1321. log.info("Start the exchange: ({0},long) -> ({1},long)".format(symbol, dominant))
  1322. order_old = order_target(symbol,0,side='long')
  1323. if order_old != None and order_old.filled > 0:
  1324. order_new = order_target(dominant,amount,side='long')
  1325. if order_new != None and order_new.filled >0:
  1326. switch_result.append({"before": symbol, "after":dominant, "side": "long"})
  1327. # 换月中的买卖都成功了,则增加新的记录去掉旧的记录
  1328. g.trade_history[dominant] = g.trade_history[symbol]
  1329. del g.trade_history[symbol]
  1330. else:
  1331. # log.warning("标的{}交易失败,无法开仓。移仓换月取消。".format(domaint))
  1332. log.warning("Trade of {} failed, no new positions. Cancelling the exchange.".format(dominant))
  1333. # 换月中的买成功了,卖失败了,则在换月记录里增加新的记录,在交易记录里去掉旧的记录
  1334. print(f'换月多头失败{dominant}, {g.trade_history[symbol]}')
  1335. g.change_fail_history[domaint] = g.trade_history[symbol]
  1336. del g.trade_history[symbol]
  1337. if callback:
  1338. callback(context, pindex, p, dominant)
  1339. if p.side == "short":
  1340. symbol_high_limit = cur[symbol].high_limit
  1341. dominant_low_limit = cur[dominant].low_limit
  1342. if symbol_last_price >= symbol_high_limit:
  1343. # log.warning("标的{}涨停,无法平仓。移仓换月取消。".format(symbol))
  1344. log.warning("Can't close {} position due to the limit up. Cancelling the exchange.".format(symbol))
  1345. continue
  1346. elif dominant_last_price <= dominant_low_limit:
  1347. # log.warning("标的{}跌停,无法开仓。移仓换月取消。".format(symbol))
  1348. log.warning("Can't close {} position due to the limit down. Cancelling the exchange.".format(symbol))
  1349. continue
  1350. else:
  1351. # log.info("进行移仓换月: ({0},short) -> ({1},short)".format(symbol, dominant))
  1352. log.info("Start the exchange: ({0},short) -> ({1},short)".format(symbol, dominant))
  1353. order_old = order_target(symbol,0,side='short')
  1354. if order_old != None and order_old.filled > 0:
  1355. print(f'换月做空{dominant},数量位{amount}')
  1356. order_new = order_target(dominant,amount,side='short')
  1357. if order_new != None and order_new.filled >0:
  1358. switch_result.append({"before": symbol, "after":dominant, "side": "short"})
  1359. # 换月中的买卖都成功了,则增加新的记录去掉旧的记录
  1360. g.trade_history[dominant] = g.trade_history[symbol]
  1361. del g.trade_history[symbol]
  1362. else:
  1363. # log.warning("标的{}交易失败,无法开仓。移仓换月取消。".format(dominant))
  1364. log.warning("Trade of {} failed, no new positions. Cancelling the exchange.".format(dominant))
  1365. # 换月中的买成功了,卖失败了,则在换月记录里增加新的记录,在交易记录里去掉旧的记录
  1366. print(f'换月空头失败{dominant}, {g.trade_history[symbol]}')
  1367. g.change_fail_history[dominant] = g.trade_history[symbol]
  1368. print(f'失败记录{g.change_fail_history}')
  1369. del g.trade_history[symbol]
  1370. # order_target(symbol,0,side='short')
  1371. # order_target(dominant,amount,side='short')
  1372. # switch_result.append({"before": symbol, "after": dominant, "side": "short"})
  1373. if callback:
  1374. callback(context, pindex, p, dominant)
  1375. return switch_result