turtle weathlab 5 代码分享

Discussion in 'Wealth-Lab Developer' started by cbi_luoy, Jun 7, 2010.

  1. 通过测试无问题。在此想感谢对海洋论坛做过贡献的所有朋友。看到坛主hylt对weathlab的介绍才开始学习wld的。在TS上花了很多时间,搞到现在rina的组合测试还没有通过。一看weathlab觉得比较适合自己(java程序员出身),语言比easylanguage严谨多了。祝大家少走弯路。

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Drawing;
    using WealthLab;
    using WealthLab.Indicators;

    namespace WealthLab.Strategies
    {
    public class MyStrategy : WealthScript
    {
    double N;//art N.
    double L1, S1, L2, S2, L3, S3, L4, S4;//20 day,entry price.
    double SE, LE, LS, SS;//20 day,short exit,long exit,long stoploss,short stoploss.
    double FU, FD;//55 day,entry price.
    double Tick ;//point like MetaTrader.
    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);
    //back test all bars.
    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
    {
    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 )
    {
    BrktProf = 0 ;
    LstPsExBar = LastPosition.ExitBar;
    foreach( Position p in Positions )
    if (p.ExitBar == LstPsExBar )
    BrktProf += p.NetProfit;

    cond20 = (BrktProf < 0 );
    if (cond20 == true)
    {
    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
    {
    FU = Highest.Series(High, FailsafeUp)[bar];
    FD = Lowest.Series(Low, FailsafeDn)[bar];
    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"); }
    }
    }

    for(int p = ActivePositions.Count - 1; p >= 0; p--)
    {
    Position pos = ActivePositions[p];
    if(pos.PositionType==PositionType.Long)
    {
    LS=pos.EntryPrice-2*N;
    if( !SellAtStop( bar+1,pos, LS, "LongStop") )
    SellAtStop( bar+1, pos, LE, "LongExit");
    }
    else
    {
    SS=pos.EntryPrice+2*N;
    if( !CoverAtStop(bar+1, pos, SS, "ShortStop") )
    CoverAtStop( bar+1, pos, SE, "ShortExit");
    }
    }//end of exit for.
    }//end of the first for.
    }//end of Execute.
    }//end of public class.
    }//end of namespace.
     
  2. thanks 4 sharing!
     
  3. thank you very much
     
  4. 谢谢,我也是java程序员出身,不过我也学过C#。