【宽客策略】海龟系统源码分享

0
回复
6009
查看
[复制链接]

4

主题

1

回帖

30

积分

新手上路

积分
30
来源: 2020-3-11 08:56:31 显示全部楼层 |阅读模式
金字塔软件自带有海龟系统,不过那个写的太复杂不利于理解。后来去理思路的时候发现其实原本海龟设计没有很高深,关键就是在数量仓位上利用波动幅度atr来进行构建。
        出入场条件就根据唐奇安通道的高低位,属于典型的趋势跟踪策略,在期货上前几年收益还是很惊人的,不过近几年就不甚理想。
        这里给出一个我自己整理逻辑后的模板范例

金字塔版本
  1. TRR:=MAX(MAX((HIGH-LOW),ABS(REF(CLOSE,1)-HIGH)),ABS(REF(CLOSE,1)-LOW));//真实波幅
  2. ATR:=ref(MA(TRR,20),1); //波幅的20日均线
  3. unit:(asset*0.01)/(MULTIPLIER*atr);

  4. INPUT:X(20,1,100,5);
  5. X周期高点:=REF(HHV(H,X),1);//X是参数,自行调整
  6. X周期低点:=REF(LLV(L,X),1);

  7. //记录建仓的atr
  8. variable:entry=0;
  9. //记录交易次数
  10. variable:num=0;
  11. //入场条件:
  12. 开多条件:=High>=X周期高点 and barpos>20;
  13. 开空条件:=Low<=X周期低点 and barpos>20;

  14. //建立头寸
  15. if 开多条件 and holding=0 then
  16. begin
  17. buy(1,unit,marketr);
  18. entry:=atr;
  19. num:=1;
  20. end

  21. if 开空条件 and holding=0 then
  22. begin
  23. buyshort(1,unit,marketr);
  24. entry:=atr;
  25. num:=1;
  26. end


  27. //每盈利0.5个atr加仓,最多加4次
  28. if holding>0 and high>enterprice+0.5*entry and num<4 then
  29. begin
  30. buy(1,unit,marketr);
  31. num:=num+1;
  32. end

  33. if holding<0 and low<enterprice-0.5*entry and num<4 then
  34. begin
  35. buyshort(1,unit,marketr);
  36. num:=num+1;
  37. end

  38. //统计出场和止损的次数
  39. variable:n1=0,n2=0;

  40. //止损2个atr
  41. if holding>0 and low<enterprice-2*entry and holding>0 then
  42. begin
  43. sell(1,holding,marketr);
  44. n1:=n1+1;
  45. end
  46. if holding<0 and high>enterprice+2*entry and holding<0 then
  47. begin
  48. sellshort(1,holding,marketr);
  49. n1:=n1+1;
  50. end

  51. //破短期高低位,平仓出场
  52. INPUT:Y(10,1,100,5);
  53. Y周期高点:=REF(HHV(H,10),1);
  54. Y周期低点:=REF(LLV(L,10),1);
  55. if low<Y周期低点 and holding>0 then
  56. begin
  57. sell(1,holding,marketr);
  58. n2:=n2+1;
  59. end
  60. if high>Y周期高点 and holding<0 then
  61. begin
  62. sellshort(1,holding,marketr);
  63. n2:=n2+1;
  64. end
复制代码
Python版本

  1. # 本Python代码主要用于策略交易
  2. # 可以自己import我们平台支持的第三方python模块,比如pandas、numpy等。
  3. from PythonApi import *
  4. import numpy as np
  5. import talib
  6. import math


  7. #  在这个方法中编写任何的初始化逻辑。context对象将会在你的算法策略的任何方法之间做传递。--(必须实现)
  8. def init(context):
  9.     #入场周期
  10.     context.X = 20
  11.     #出场周期
  12.     context.Y = 10
  13.     #记录建仓的atr
  14.     context.entry = 0
  15.     #记录交易次数
  16.     context.num = 0
  17.     #交易标的
  18.     context.s = context.run_info.base_book_id
  19.     #记录上次开仓价
  20.     context.enterprice = 0


  21. # 你选择的品种的数据更新将会触发此段逻辑,例如日或分钟历史数据切片或者是实时数据切片更新。--(必须实现)
  22. def handle_bar(context):
  23.     close = history_bars(context.s,context.X+2,'self','close',include_now=True)
  24.     high = history_bars(context.s,context.X+2,'self','high',include_now=True)
  25.     low = history_bars(context.s,context.X+2,'self','low',include_now=True)  
  26.     if len(close) == context.X+2:
  27.         tr = talib.TRANGE(high,low,close)
  28.         atr = talib.SMA(tr[1:],context.X)
  29.         unit = int((get_account(6)*0.01) / (atr[-2] * get_dynainf(context.s,209)))
  30.         #X天的高低点(不包含当天)
  31.         X周期高点 = high[:-1].max()
  32.         X周期低点 = low[:-1].min()
  33.       
  34.         #建立头寸,根据唐奇安通道创新高入场,关键点就是利用波动atr计算仓位数量,portfolio用来进行仓位的控制
  35.         portfolio=get_portfolio (context.s, 2)
  36.         if high[-1]>=X周期高点 and portfolio.buy_quantity==0 and portfolio.sell_quantity==0:
  37.             buy_open(context.s, "Market",0 ,unit,serial_id = 1)
  38.             context.entry = atr[-2]
  39.             context.num = 1
  40.             context.enterprice = close[-1]
  41.         if low[-1]<=X周期低点 and portfolio.sell_quantity==0 and portfolio.buy_quantity==0:
  42.             sell_open(context.s, "Market",0 ,unit,serial_id = 2)
  43.             context.entry = atr[-2]
  44.             context.num = 1
  45.             context.enterprice = close[-1]
  46.            
  47.         #加仓,最高价比上次开仓价多0.5个atr(盈利加仓)
  48.         if portfolio.sell_quantity ==0 and portfolio.buy_quantity>0 and high[-1]>context.enterprice + 0.5*context.entry and context.num<4:
  49.             buy_open(context.s, "Market",0 ,unit,serial_id = 3)
  50.             context.num+=1
  51.             context.enterprice = close[-1]
  52.         if portfolio.buy_quantity==0 and portfolio.sell_quantity>0 and low[-1]<context.enterprice - 0.5*context.entry and context.num<4:
  53.             sell_open(context.s, "Market",0 ,unit,serial_id = 4)
  54.             context.num+=1
  55.             context.enterprice = close[-1]
  56.            
  57.         #出场,跌破短周期低点平多
  58.         Y周期高点 = high[-context.Y-1:-1].max()
  59.         Y周期低点 = low[-context.Y-1:-1].min()
  60.         if portfolio.buy_quantity>0 and low[-1] < Y周期低点:
  61.             sell_close(context.s,"Market",0,portfolio.buy_quantity,serial_id = 5)
  62.         if portfolio.sell_quantity>0 and high[-1] > Y周期高点:
  63.             buy_close(context.s,"Market",0,portfolio.sell_quantity,serial_id = 6)
  64.            
  65.         #止损,亏损幅度超过开仓2个atr幅度止损
  66.         if portfolio.buy_quantity>0 and low[-1] < context.enterprice - 2*context.entry:
  67.             sell_close(context.s,"Market",0,portfolio.buy_quantity,serial_id = 7)
  68.         if portfolio.sell_quantity>0 and high[-1] > context.enterprice + 2*context.entry:
  69.             buy_close(context.s,"Market",0,portfolio.sell_quantity,serial_id = 8)
  70.    
复制代码




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 免费注册
关注微信