: | : | :期货程序化 | :期货程序化研究 | :期货量化学习 | :期货量化 |
返回列表 发帖

[Python源码] 鳄鱼法则交易系统源码(Python源码)

[Python源码] 鳄鱼法则交易系统源码(Python源码)

源码如下:
  1. import numpy as np
  2. def initialize(context):
  3.     g.up_price = 0 #向上碎形最高价
  4.     g.low_price = 0 #向下碎形最低价
  5.     g.up_fractal_exists = False #判断有效向上碎形
  6.     g.down_fractal_exists = False #判断有效向下碎形
  7.     g.AO_index = [0] #存放连续的AO指标数据
  8.     g.cal_AC_index = [] #计算AC指标中转存储
  9.     g.AC_index = [0] #存放连续的AC指标数据
  10.     g.amount = 0 #满仓仓位
  11.     g.stock = ['160119.XSHE']
  12.     set_benchmark('160119.XSHE')

  13. #判断 向上 或 向下 碎形
  14. def is_fractal(stock,direction):
  15.     hist = history(5,'1d',direction,[stock],df = False)
  16.     if direction == 'high'\
  17.     and hist[stock][2] > hist[stock][0]\
  18.     and hist[stock][2] > hist[stock][1]\
  19.     and hist[stock][2] > hist[stock][3]\
  20.     and hist[stock][2] > hist[stock][4]:
  21.         g.up_price = hist[stock][2]
  22.         return True
  23.     elif direction == 'low'\
  24.     and hist[stock][2] < hist[stock][0]\
  25.     and hist[stock][2] < hist[stock][1]\
  26.     and hist[stock][2] < hist[stock][3]\
  27.     and hist[stock][2] < hist[stock][4]:
  28.         g.low_price = hist[stock][2]
  29.         return True
  30.     return False

  31. #通过比较碎形与红线位置,判断碎形是否有效
  32. def is_effective_fractal(stock, direction):
  33.     if is_fractal(stock,direction):
  34.         hist = history(13,'1d','close',[stock],df = False)
  35.         red_line = hist[stock][:-5].mean()
  36.         close_price = hist[stock][-1]
  37.         if direction == 'high':
  38.             if close_price > red_line:
  39.                 g.up_fractal_exists = True
  40.             else:
  41.                 g.up_fractal_exists = False
  42.         elif direction == 'low':
  43.             if close_price < red_line:
  44.                 g.down_fractal_exists = True
  45.             else:
  46.                 g.down_fractal_exists = False

  47. #N日内最高价格的N日线
  48. def nday_high_point(stock,n):
  49.     hist = history(2*n,'1d','high',[stock],df = False)[stock]
  50.     high_point = []
  51.     for i in range(n):
  52.         high_point.append(max(hist[-5-i:-1-i]))
  53.     return np.array(high_point).mean()

  54. #N日内最低价格的N日线
  55. def nday_low_point(stock,n):
  56.     hist = history(2*n,'1d','low',[stock],df = False)[stock]
  57.     low_point = []
  58.     for i in range(n):
  59.         low_point.append(max(hist[-5-i:-1-i]))
  60.     return np.array(low_point).mean()

  61. #AO=5日内(最高-最低)/2的5日移动平均-34日内(最高-最低)/2的34日移动平均
  62. def AO_index(stock):
  63.     g.AO_index.append(nday_high_point(stock,5)/2 + nday_low_point(stock,5)/2\
  64.                       - nday_high_point(stock,34)/2 - nday_low_point(stock,34)/2)
  65.     return g.AO_index[-1]

  66. #AO-AO的5日平均值的5日平均
  67. def AC_index(stock):
  68.     AO_index(stock)
  69.     if len(g.AO_index) >= 5:
  70.         g.cal_AC_index.append(g.AO_index[-1] - np.array(g.AO_index[-5:]).mean())
  71.         if len(g.cal_AC_index) >=5:
  72.             g.AC_index.append(np.array(g.cal_AC_index[-5:]).mean())

  73. #判断序列n日上行
  74. def is_up_going(alist,n):
  75.     if len(alist) < n:
  76.         return False
  77.     for i in range(n-1):
  78.         if alist[-(1+i)] <= alist[-(2+i)]:
  79.             return False
  80.     return True

  81. #判断序列n日下行
  82. def is_down_going(alist,n):
  83.     if len(alist) < n:
  84.         return False
  85.     for i in range(n-1):
  86.         if alist[-(1+i)] >= alist[-(2+i)]:
  87.             return False
  88.     return True

  89. #碎形被突破
  90. def active_fractal(stock,direction):
  91.     close_price = history(1,'1d','close',[stock],df=False)[stock][0]
  92.     if direction == 'up' and close_price > g.up_price:
  93.         return True
  94.     elif direction == 'down' and close_price < g.low_price:
  95.         return True

  96. #进场,初始仓位50%
  97. def set_initial_position(stock,context):
  98.     close_price = history(1,'1d','close',[stock],df=False)[stock][0]
  99.     g.amount = context.portfolio.cash/close_price
  100.     order(stock, g.amount*0.8)
  101.     log.info("buying %s 股数为 %s"%(stock,g.amount*0.7))
  102.     g.down_fractal_exists = False

  103. #卖出
  104. def sell_all_stock(stock,context):
  105.     order_target(stock,0)
  106.     log.info("selling %s"%stock)
  107.     g.up_fractal_exists = False

  108. #加仓
  109. def adjust_position(stock,context,position):
  110.     order(stock,g.amount*position)
  111.     log.info("adjust position buying %s 股数为 %s"%(stock,g.amount*position))

  112. def handle_data(context,data):
  113.     stock = g.stock[0]
  114.     #计算AO,AC指标
  115.     AC_index(stock)
  116.     #止损
  117.     #空仓时,寻找机会入场
  118.     if context.portfolio.positions[stock].amount == 0:
  119.         #计算向上碎形
  120.         is_effective_fractal(stock,'high')
  121.         #有效向上碎形存在,并被突破,买入
  122.         if g.up_fractal_exists and active_fractal(stock,'up'):
  123.             set_initial_position(stock,context)
  124.     #有持仓时,加仓或离场
  125.     else:
  126.         close_price = history(13,'1d','close',[stock],df=False)
  127.         red_line = close_price[stock][:-5].mean()
  128.         #计算向下碎形
  129.         is_effective_fractal(stock,'low')
  130.         #出场条件1:有效向下碎形存在,并被突破,卖出
  131.         if g.down_fractal_exists and active_fractal(stock,'down'):
  132.             sell_all_stock(stock,context)
  133.             return
  134.         #出场条件2:AC
  135.         #加仓10%:AO,AC同时5日上行,且收盘价走高
  136.         if is_up_going(g.AO_index,5)\
  137.         and is_up_going(g.AC_index,3)\
  138.         and is_up_going(close_price[stock],2):
  139.             adjust_position(stock,context,0.1)
  140.         #减仓10%:AO,AC同时3日下行,且收盘价走低
  141.         if is_down_going(g.AO_index,5)\
  142.         and is_down_going(g.AC_index,3)\
  143.         and is_down_going(close_price[stock],2):
  144.             adjust_position(stock,context,-0.1)
  145.     record(AOindex = g.AO_index[-1])
  146.     record(ACindex = g.AC_index[-1])
复制代码

论坛官方微信、群(期货热点、量化探讨、开户与绑定实盘)
 
期货论坛 - 版权/免责声明   1.本站发布源码(包括函数、指标、策略等)均属开放源码,用意在于让使用者学习程序化语法撰写,使用者可以任意修改语法內容并调整参数。仅限用于个人学习使用,请勿转载、滥用,严禁私自连接实盘账户交易
  2.本站发布资讯(包括文章、视频、历史记录、教材、评论、资讯、交易方案等)均系转载自网络主流媒体,内容仅为作者当日个人观点,本网转载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。本网不对该类信息或数据做任何保证。不对您构成任何投资建议,不能依靠信息而取代自身独立判断,不对因使用本篇文章所诉信息或观点等导致的损失承担任何责任。
  3.本站发布资源(包括书籍、杂志、文档、软件等)均从互联网搜索而来,仅供个人免费交流学习,不可用作商业用途,本站不对显示的内容承担任何责任。请在下载后24小时内删除。如果喜欢,请购买正版,谢谢合作!
  4.龙听期货论坛原创文章属本网版权作品,转载须注明来源“龙听期货论坛”,违者本网将保留追究其相关法律责任的权力。本论坛除发布原创文章外,亦致力于优秀财经文章的交流分享,部分文章推送时若未能及时与原作者取得联系并涉及版权问题时,请及时联系删除。联系方式:http://www.qhlt.cn/thread-262-1-1.html
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

下载 学习 谢谢大大

TOP

谢谢楼主的无私奉献

TOP

学习了

TOP

返回列表