|
金字塔软件自带有海龟系统,不过那个写的太复杂不利于理解。后来去理思路的时候发现其实原本海龟设计没有很高深,关键就是在数量仓位上利用波动幅度atr来进行构建。
出入场条件就根据唐奇安通道的高低位,属于典型的趋势跟踪策略,在期货上前几年收益还是很惊人的,不过近几年就不甚理想。
这里给出一个我自己整理逻辑后的模板范例
金字塔版本
- TRR:=MAX(MAX((HIGH-LOW),ABS(REF(CLOSE,1)-HIGH)),ABS(REF(CLOSE,1)-LOW));//真实波幅
- ATR:=ref(MA(TRR,20),1); //波幅的20日均线
- unit:(asset*0.01)/(MULTIPLIER*atr);
- INPUT:X(20,1,100,5);
- X周期高点:=REF(HHV(H,X),1);//X是参数,自行调整
- X周期低点:=REF(LLV(L,X),1);
- //记录建仓的atr
- variable:entry=0;
- //记录交易次数
- variable:num=0;
- //入场条件:
- 开多条件:=High>=X周期高点 and barpos>20;
- 开空条件:=Low<=X周期低点 and barpos>20;
- //建立头寸
- if 开多条件 and holding=0 then
- begin
- buy(1,unit,marketr);
- entry:=atr;
- num:=1;
- end
- if 开空条件 and holding=0 then
- begin
- buyshort(1,unit,marketr);
- entry:=atr;
- num:=1;
- end
- //每盈利0.5个atr加仓,最多加4次
- if holding>0 and high>enterprice+0.5*entry and num<4 then
- begin
- buy(1,unit,marketr);
- num:=num+1;
- end
- if holding<0 and low<enterprice-0.5*entry and num<4 then
- begin
- buyshort(1,unit,marketr);
- num:=num+1;
- end
- //统计出场和止损的次数
- variable:n1=0,n2=0;
- //止损2个atr
- if holding>0 and low<enterprice-2*entry and holding>0 then
- begin
- sell(1,holding,marketr);
- n1:=n1+1;
- end
- if holding<0 and high>enterprice+2*entry and holding<0 then
- begin
- sellshort(1,holding,marketr);
- n1:=n1+1;
- end
- //破短期高低位,平仓出场
- INPUT:Y(10,1,100,5);
- Y周期高点:=REF(HHV(H,10),1);
- Y周期低点:=REF(LLV(L,10),1);
- if low<Y周期低点 and holding>0 then
- begin
- sell(1,holding,marketr);
- n2:=n2+1;
- end
- if high>Y周期高点 and holding<0 then
- begin
- sellshort(1,holding,marketr);
- n2:=n2+1;
- end
复制代码 Python版本
- # 本Python代码主要用于策略交易
- # 可以自己import我们平台支持的第三方python模块,比如pandas、numpy等。
- from PythonApi import *
- import numpy as np
- import talib
- import math
- # 在这个方法中编写任何的初始化逻辑。context对象将会在你的算法策略的任何方法之间做传递。--(必须实现)
- def init(context):
- #入场周期
- context.X = 20
- #出场周期
- context.Y = 10
- #记录建仓的atr
- context.entry = 0
- #记录交易次数
- context.num = 0
- #交易标的
- context.s = context.run_info.base_book_id
- #记录上次开仓价
- context.enterprice = 0
- # 你选择的品种的数据更新将会触发此段逻辑,例如日或分钟历史数据切片或者是实时数据切片更新。--(必须实现)
- def handle_bar(context):
- close = history_bars(context.s,context.X+2,'self','close',include_now=True)
- high = history_bars(context.s,context.X+2,'self','high',include_now=True)
- low = history_bars(context.s,context.X+2,'self','low',include_now=True)
- if len(close) == context.X+2:
- tr = talib.TRANGE(high,low,close)
- atr = talib.SMA(tr[1:],context.X)
- unit = int((get_account(6)*0.01) / (atr[-2] * get_dynainf(context.s,209)))
- #X天的高低点(不包含当天)
- X周期高点 = high[:-1].max()
- X周期低点 = low[:-1].min()
-
- #建立头寸,根据唐奇安通道创新高入场,关键点就是利用波动atr计算仓位数量,portfolio用来进行仓位的控制
- portfolio=get_portfolio (context.s, 2)
- if high[-1]>=X周期高点 and portfolio.buy_quantity==0 and portfolio.sell_quantity==0:
- buy_open(context.s, "Market",0 ,unit,serial_id = 1)
- context.entry = atr[-2]
- context.num = 1
- context.enterprice = close[-1]
- if low[-1]<=X周期低点 and portfolio.sell_quantity==0 and portfolio.buy_quantity==0:
- sell_open(context.s, "Market",0 ,unit,serial_id = 2)
- context.entry = atr[-2]
- context.num = 1
- context.enterprice = close[-1]
-
- #加仓,最高价比上次开仓价多0.5个atr(盈利加仓)
- if portfolio.sell_quantity ==0 and portfolio.buy_quantity>0 and high[-1]>context.enterprice + 0.5*context.entry and context.num<4:
- buy_open(context.s, "Market",0 ,unit,serial_id = 3)
- context.num+=1
- context.enterprice = close[-1]
- if portfolio.buy_quantity==0 and portfolio.sell_quantity>0 and low[-1]<context.enterprice - 0.5*context.entry and context.num<4:
- sell_open(context.s, "Market",0 ,unit,serial_id = 4)
- context.num+=1
- context.enterprice = close[-1]
-
- #出场,跌破短周期低点平多
- Y周期高点 = high[-context.Y-1:-1].max()
- Y周期低点 = low[-context.Y-1:-1].min()
- if portfolio.buy_quantity>0 and low[-1] < Y周期低点:
- sell_close(context.s,"Market",0,portfolio.buy_quantity,serial_id = 5)
- if portfolio.sell_quantity>0 and high[-1] > Y周期高点:
- buy_close(context.s,"Market",0,portfolio.sell_quantity,serial_id = 6)
-
- #止损,亏损幅度超过开仓2个atr幅度止损
- if portfolio.buy_quantity>0 and low[-1] < context.enterprice - 2*context.entry:
- sell_close(context.s,"Market",0,portfolio.buy_quantity,serial_id = 7)
- if portfolio.sell_quantity>0 and high[-1] > context.enterprice + 2*context.entry:
- buy_close(context.s,"Market",0,portfolio.sell_quantity,serial_id = 8)
-
复制代码
|
|