欢迎使用金字塔普通技术服务论坛,您可以在相关区域发表技术支持贴。
我司技术服务人员将优先处理 VIP客服论坛 服务贴,普通区问题处理速度慢,请耐心等待。谢谢您对我们的支持与理解。


金字塔客服中心 - 专业程序化交易软件提供商金字塔软件交易策略发布专区 → 海龟系统

   

欢迎使用金字塔普通技术服务论坛,您可以在相关区域发表技术支持贴。
我司技术服务人员将优先处理 VIP客服论坛 服务贴,普通区问题处理速度慢,请耐心等待。谢谢您对我们的支持与理解。    


  共有6265人关注过本帖树形打印复制链接

主题:海龟系统

帅哥哟,离线,有人找我吗?
yukizzc
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:21598 积分:0 威望:0 精华:1 注册:2010/7/31 16:35:30
海龟系统  发帖心情 Post By:2020/2/10 14:29:24 [只看该作者]

软件自带有海龟系统,不过那个写的太复杂不利于理解。后来去理思路的时候发现其实原本海龟设计没有很高深,关键就是在数量仓位上利用波动幅度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


 回到顶部
帅哥哟,离线,有人找我吗?
yukizzc
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:21598 积分:0 威望:0 精华:1 注册:2010/7/31 16:35:30
  发帖心情 Post By:2020/2/10 14:31:25 [只看该作者]

二楼,再给出一个python版本的。不过建议理解思路的话看一楼pel的,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:
        #atr的计算参考这个帖子http://www.weistock.com/bbs/dispbbs.asp?boardid=10&Id=173300
        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)
   
       


 回到顶部
帅哥哟,离线,有人找我吗?
yzhybw
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:黑侠 帖子:659 积分:0 威望:0 精华:0 注册:2015/2/5 10:08:43
  发帖心情 Post By:2020/4/3 14:18:30 [只看该作者]

谢谢老师回复,我先试试。


 回到顶部
帅哥哟,离线,有人找我吗?
yzhybw
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:黑侠 帖子:659 积分:0 威望:0 精华:0 注册:2015/2/5 10:08:43
  发帖心情 Post By:2020/4/3 14:34:59 [只看该作者]

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

 

 

这个策略还是有的复杂,对股票买卖来说,我喜欢用一定资金全仓进出,想让老师改一个满仓买卖的策略,有空改一下吧,谢谢。


 回到顶部
帅哥哟,离线,有人找我吗?
yukizzc
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:21598 积分:0 威望:0 精华:1 注册:2010/7/31 16:35:30
  发帖心情 Post By:2020/4/24 9:59:19 [只看该作者]

改成后台的,加仓允许当根k重复加仓的模式,不支持回测无法在回测中得到所谓盘中那种效果

 

 

 

TRR:=MAX(MAX((HIGH-LOW),ABS(REF(CLOSE,1)-HIGH)),ABS(REF(CLOSE,1)-LOW));//真实波幅
ATR:=ref(MA(TRR,20),1); //波幅的20日均线
unit:(tasset*0.01)/(MULTIPLIER*atr);

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

//记录建仓的atr

GLOBALVARIABLE:entry=0;
//记录交易次数a
GLOBALVARIABLE:num=0;
//入场条件:
开多条件:=High>=X周期高点 and barpos>20;
开空条件:=Low<=X周期低点 and barpos>20;

//建立头寸
if 开多条件 and tholding=0 then
begin
 tbuy(1,unit,mkt);
 entry:=atr;
 num:=1;
end

if 开空条件 and tholding=0 then
begin
 tbuyshort(1,unit,mkt);
 entry:=atr;
 num:=1;
end


//每盈利0.5个atr加仓,最多加4次
if tbuyholding(1)>0 and high>tenterprice+0.5*entry and num<4 and tsellholding(1)=0 then
begin
 tbuy(1,unit,mkt),ALLOWREPEAT;
 num:=num+1;
end

if tsellholding(1)>0 and low<tenterprice-0.5*entry and num<4 and tbuyholding(1)=0 then
begin
 tbuyshort(1,unit,mkt),ALLOWREPEAT;
 num:=num+1;
end

 

//止损2个atr
if tbuyholding(1)>0 and low<tenterprice-2*entry  then
begin
 tsell(1,tbuyholding(1),mkt);
end
if tsellholding(1)>0 and high>tenterprice+2*entry  then
begin
 tsellshort(1,tsellholding(1),mkt);
end

//破短期高低位,平仓出场
INPUT:Y(10,1,100,5);
Y周期高点:=REF(HHV(H,10),1);
Y周期低点:=REF(LLV(L,10),1);
if low<Y周期低点 and tbuyholding(1)>0 then
begin
 tsell(1,tbuyholding(1),mkt);
end
if high>Y周期高点 and tsellholding(1)>0 then
begin
 tsellshort(1,tsellholding(1),mkt);
end

[此贴子已经被作者于2020/4/24 15:19:01编辑过]

 回到顶部