海龟交易法则的代码中买进条件如何改成均线突破买入?

Discussion in 'Wealth-Lab Developer' started by installman, Oct 19, 2009.

  1. 一下是本论坛搜到的原版海龟法则的策略代码。其中买进部分如何改成根据均线突破买入?
    比如价格突破60日均线开始买入。

    //本策略使用.NET框架System命名空间中的Math类,WL5本身不再提供有关数学,字符串,文件操作方法,分别将以.NET框架Math,StreamReader, StreamWrite,String等类来代替。
    using System;
    //本策略使用.NET框架System.Drawing命名空间中相关的类。WL5部分绘制方法的颜色和字体参数需要此命名空间。
    using System.Drawing;
    //本策略使用WealthLab命名空间中相关的类,WealthLab命名空间包含WL5本身提供发展策略所需类(每个成员的详细说明参考WealthScript QuickRef文档)如Alert Object,Bars Object,ChartPane Object,DataSeries Object,FundamentalItem Object,Position Object,SymbolInfo Object和WealthScript基类以及其它高级开发的基类等等。
    using WealthLab;
    //WL5本身提供了包含超过100多个技术指标,使用指标需要包含此命名空间。
    using WealthLab.Indicators;

    namespace WealthLab.Strategies
    {
    //WL5中每个策略定义为类并且都从WealthScript基类继承所有方法与属性,WealthScript基类定义了WealthScript QuickRef参考文档罗列的Common Signals,Cosmetic Chart,Data Access,Fundamental Data,Math,Position Management,System,Time Frames,Trading类别中的所有方法与属性和虚方法Execute。
    public class MyStrategy : WealthScript
    {
    double N,L1, S1, L2, S2, L3, S3, L4, S4, SE, LE, LS, SS, FU, FD, Tick ;

    //WL5每个策略需要重定义继承来自WealthScript基类虚方法Execute,它是策略的主体也是执行点。
    protected override void Execute()
    {
    int ChannelUp, ChannelDn, LongExit, ShrtExit, FailsafeUp, FailsafeDn;
    double BrktProf;
    int LeadBars, ATRParam, LstPsExBar;
    bool cond20;
    //时间参数设置 
    ChannelUp = 20 ;
    ChannelDn = 20 ;
    LongExit = 10 ;
    ShrtExit = 10 ;
    FailsafeUp = 55 ;
    FailsafeDn = 55 ;
    ATRParam = 20 ;
    //绘制止损与退出点 
    PlotStops();
    Tick = Bars.SymbolInfo.Tick;
    //绘制通道指标
    PlotSeries(PricePane, Highest.Series(High, ChannelUp) >> 1, Color.Green, LineStyle.Solid, 1);
    PlotSeries(PricePane, Lowest.Series(Low, ChannelDn ) >> 1, Color.Red, LineStyle.Dotted, 1);
    PlotSeries(PricePane, Highest.Series(High, FailsafeUp) >> 1, Color.Blue, LineStyle.Dotted, 1);
    PlotSeries(PricePane, Lowest.Series(Low, FailsafeDn) >> 1, Color.Fuchsia, LineStyle.Solid, 1);

    LeadBars =Math.Max(FailsafeUp, FailsafeDn);
    //WL5中每个交易系统或者策略都有一个主体循环
    for(int bar = LeadBars; bar < Bars.Count; bar++)
    {

    //计算系统一多空退出价
    SE = Highest.Series(High, ShrtExit)[bar]+ Tick ;
    LE = Lowest.Series(Low, LongExit)[bar]-Tick ;
    N = ATR.Series(Bars,ATRParam)[bar];
    //头次交易,交易次数为零时的进场
    if (Positions.Count == 0 )
    {
    L1 = Highest.Series(High, ChannelUp)[bar];
    S1 = Lowest.Series(Low, ChannelDn)[bar];
    L2 = L1 + 0.5 * N ;
    S2 = S1 - 0.5 * N ;
    L3 = L1 + 1.0 * N ;
    S3 = S1 - 1.0 * N ;
    L4 = L1 + 1.5 * N ;
    S4 = S1 - 1.5 * N ;
    BuyAtStop( bar+1,L1, "L1" );
    //ShortAtStop( bar+1,S1, "S1");
    }
    else//Positions.Count的else
    //加仓
    {
    if (ActivePositions.Count==3 )
    {
    BuyAtStop( bar+1,L4, "L4" );
    //ShortAtStop( bar+1,S4, "S4");

    }
    if(ActivePositions.Count==2 )
    {
    BuyAtStop( bar+1,L3, "L3" );
    //ShortAtStop( bar+1,S3, "S3");

    }
    if (ActivePositions.Count==1 )
    {
    BuyAtStop( bar+1,L2, "L2" );
    //ShortAtStop( bar+1,S2, "S2");

    }
    //进场
    if (ActivePositions.Count==0 )
    {
    cond20 = false ;
    BrktProf = 0 ;
    LstPsExBar = LastPosition.ExitBar;
    //收集最近一次突破的利润(进场+加仓的合计),
    foreach( Position p in Positions )
    if (p.ExitBar == LstPsExBar )
    BrktProf = BrktProf + p.NetProfit;

    //亏损使用20日进场信号,否则在55日突破时进场
    if (BrktProf < 0 )
    cond20 = true ;

    if (cond20 == true)
    {

    //计算20日的进场价
    L1 = Highest.Series(High, ChannelUp)[bar];
    S1 = Lowest.Series(Low, ChannelDn)[bar];
    //设置20日的码仓价
    L2 = L1 + 0.5 * N ;
    S2 = S1 - 0.5 * N ;
    L3 = L1 + 1.0 * N ;
    S3 = S1 - 1.0 * N ;
    L4 = L1 + 1.5 * N ;
    S4 = S1 - 1.5 * N ;
    BuyAtStop( bar+1,L1, "L1" );
    //ShortAtStop( bar+1,S1, "S1");

    }


    else//cond20的else
    {

    //计算55日的进场价
    FU = Highest.Series(High, FailsafeUp)[bar];
    FD = Lowest.Series(Low, FailsafeDn)[bar];
    //设置55日的码仓价
    L2 = FU + 0.5 * N ;
    S2 = FD - 0.5 * N ;
    L3 = FU + 1.0 * N ;
    S3 = FD - 1.0 * N ;
    L4 = FU + 1.5 * N ;
    S4 = FD - 1.5 * N ;
    BuyAtStop( bar+1,FU, "FU" );
    //ShortAtStop( bar+1,FD, "FD");

    }//结束cond20的else
    }//结束ActivePositions.Count==0

    }//结束Positions.Count的else
    //设置头寸止损价格
    //WL5新特征:也可用for循环搭配ActivePositions形式,直接以活动头寸介入,运行效率更高,下同
    foreach( Position p in Positions )
    {
    if ( p.Active )
    if(p.PositionType==PositionType.Long)
    LS=p.EntryPrice-2*N;

    else
    SS=p.EntryPrice+2*N;
    }
    //头寸止损与退出条件
    foreach( Position p in Positions )
    {
    if ( p.Active )
    if(p.PositionType==PositionType.Long)
    SellAtStop( bar+1,p, LS, "LongStop");
    //else
    //CoverAtStop(bar+1, p, SS, "ShortStop");
    if(p.PositionType==PositionType.Long)
    SellAtStop( bar+1, p, LE, "LongExit");
    //else
    //CoverAtStop( bar+1, p, SE, "ShortExit");
    }

    }//结束系统主循环
    }

    }
    }
     
  2. 哈哈,依葫芦画瓢,也不难啊。
    //本策略使用.NET框架System命名空间中的Math类,WL5本身不再提供有关数学,字符串,文件操作方法,分别将以.NET框架Math,StreamReader, StreamWrite,String等类来代替。
    using System;
    //本策略使用.NET框架System.Drawing命名空间中相关的类。WL5部分绘制方法的颜色和字体参数需要此命名空间。
    using System.Drawing;
    //本策略使用WealthLab命名空间中相关的类,WealthLab命名空间包含WL5本身提供发展策略所需类(每个成员的详细说明参考WealthScript QuickRef文档)如Alert Object,Bars Object,ChartPane Object,DataSeries Object,FundamentalItem Object,Position Object,SymbolInfo Object和WealthScript基类以及其它高级开发的基类等等。
    using WealthLab;
    //WL5本身提供了包含超过100多个技术指标,使用指标需要包含此命名空间。
    using WealthLab.Indicators;

    namespace WealthLab.Strategies
    {
    //WL5中每个策略定义为类并且都从WealthScript基类继承所有方法与属性,WealthScript基类定义了WealthScript QuickRef参考文档罗列的Common Signals,Cosmetic Chart,Data Access,Fundamental Data,Math,Position Management,System,Time Frames,Trading类别中的所有方法与属性和虚方法Execute。
    public class MyStrategy : WealthScript
    {
    StrategyParameter strategyParameter1;

    public MyStrategy()
    {
    strategyParameter1 = CreateParameter("Parameter 1", 60, 5, 120, 1);
    }

    double N,L1, L2, L3, L4, LE, LS, FU, Tick ;

    //WL5每个策略需要重定义继承来自WealthScript基类虚方法Execute,它是策略的主体也是执行点。
    protected override void Execute()
    {
    int ChannelUp, LongExit, LongTerm;
    double BrktProf;
    int LeadBars, ATRParam, LstPsExBar;
    bool cond20;
    //时间参数设置 
    LongExit = 10 ;
    LongTerm = strategyParameter1.ValueInt ;
    ATRParam = 20 ;
    //绘制止损与退出点 
    PlotStops();
    Tick = Bars.SymbolInfo.Tick;
    //绘制通道指标
    PlotSeries(PricePane,SMA.Series(Close,LongTerm),Color.Blue,LineStyle.Solid,2);

    SMA smaFast = SMA.Series(Close, LongTerm);

    //WL5中每个交易系统或者策略都有一个主体循环
    for(int bar = LongTerm+1; bar < Bars.Count; bar++)
    {
    //计算系统一多头退出价,10日新低退出?
    LE = SMA.Series(Close, LongTerm)[bar]-Tick ;
    //计算波动幅度
    N = ATR.Series(Bars,ATRParam)[bar];
    //头次交易,交易次数为零时的进场
    if (Positions.Count == 0 )
    {
    L1 = SMA.Series(Close, LongTerm)[bar];
    L2 = L1 + 0.5 * N ;
    L3 = L1 + 1.0 * N ;
    L4 = L1 + 1.5 * N ;
    BuyAtStop( bar+1,L1, "L1" );
    }
    else//Positions.Count的else
    //加仓
    {
    if (ActivePositions.Count==3 )
    {
    BuyAtStop( bar+1,L4, "L4" );
    }
    if(ActivePositions.Count==2 )
    {
    BuyAtStop( bar+1,L3, "L3" );
    }
    if (ActivePositions.Count==1 )
    {
    BuyAtStop( bar+1,L2, "L2" );
    }
    //进场
    if (ActivePositions.Count==0 )
    {
    cond20 = false ;
    BrktProf = 0 ;
    LstPsExBar = LastPosition.ExitBar;
    //收集最近一次突破的利润(进场+加仓的合计),
    foreach( Position p in Positions )
    if (p.ExitBar == LstPsExBar )
    BrktProf = BrktProf + p.NetProfit;

    //亏损使用20日进场信号,否则在55日突破时进场
    if (BrktProf < 0 )
    cond20 = true ;

    if (cond20 == true)
    {
    //计算20日的进场价
    L1 = SMA.Series(Close, LongTerm)[bar];
    //设置20日的码仓价
    L2 = L1 + 0.5 * N ;
    L3 = L1 + 1.0 * N ;
    L4 = L1 + 1.5 * N ;
    BuyAtStop( bar+1,L1, "L1" );
    }

    else//cond20的else
    {
    //计算55日的进场价
    FU = SMA.Series(Close, LongTerm)[bar];
    //设置55日的码仓价
    L2 = FU + 0.5 * N ;
    L3 = FU + 1.0 * N ;
    L4 = FU + 1.5 * N ;
    BuyAtStop( bar+1,FU, "FU" );
    }//结束cond20的else
    }//结束ActivePositions.Count==0

    }//结束Positions.Count的else
    //设置头寸止损价格
    //WL5新特征:也可用for循环搭配ActivePositions形式,直接以活动头寸介入,运行效率更高,下同
    foreach( Position p in Positions )
    {
    if ( p.Active )
    if(p.PositionType==PositionType.Long)
    LS=p.EntryPrice-2*N;
    }
    //头寸止损与退出条件
    foreach( Position p in Positions )
    {
    if ( p.Active )
    if(p.PositionType==PositionType.Long)
    SellAtStop( bar+1,p, LS, "LongStop");
    if(p.PositionType==PositionType.Long)
    SellAtStop( bar+1, p, LE, "LongExit");
    }
    }//结束系统主循环
    }
    }
    }
     
  3. 仔细看有问题,出现当天买当天买的情况了。