开拓者期货期权程序化系统交易论坛

标题: TS源码(据说很好) [打印本页]

作者: 舵手trader    时间: 2010-10-8 17:14:43     标题: TS源码(据说很好)

Input:  c1       (0),          { Coefficient of indicator 1 }
         c2       (0),          { Coefficient of indicator 2 }
         c3       (0),          { Coefficient of indicator 3 }
         c4       (0),          { Coefficient of indicator 4 }
         c5       (0),          { Coefficient of indicator 5 }
         c6       (0),          { Coefficient of indicator 6 }
         NMA1     (10),         { Length of moving average 1 }
         NMA2     (50),         { Length of moving average 2 }
         NMA3     (135),        { Length of moving average 3 }
         NATR     (27),        { Period for average true range }
         NRnge    (30),         { Period for computing average daily range }
         RngeFr   (0.6);        { Fraction of daily range }

Var:    SessLen  (0),          { Length in minutes of trading session }
         Time1Thrd(0),          { Time of end of 1st third of session }
         Time2Thrd(0),          { Time of end of 2nd third of session }
         NBarsDay (0),          { Number of bars per day }
         minTrend (0),          { minimum required daily change for "trend" }
         AveCO    (0),          { Average close - open }
         ACOPrior (0),          { Value of AveCO on prior day }
         DayRnge  (0),          { Most recent day's close - open }
         DRPrior  (0),          { Prior day's DayRnge }
         MovAve1  (0),          { Value of moving average 1 }
         MA1Prior (0),          { Value of moving average 1 on prior day }
         MovAve2  (0),          { Value of moving average 2 }
         MA2Prior (0),          { Value of moving average 2 on prior day }
         MovAve3  (0),          { Value of moving average 3 }
         MA3Prior (0),          { Value of moving average 3 on prior day }
         ATR      (0),          { Average true range }
         ATRPrior (0),          { Value of ATR on prior day }
         TRPrior  (0),          { Prior day's true range }
         PatVal   (0),          { Pattern number }
         PatCount (0),          { Number of occurances of pattern PatID }
         DayCount (0),          { Number of days examined }
         Cond1L   (False),      { Condition 1, up trend }
         Cond1S   (False),      { Condition 1, down trend }
         Cond2L   (False),      { Condition 2, up trend }
         Cond2S   (False),      { Condition 2, down trend }
         Cond3L   (False),      { Condition 3, up trend }
         Cond3S   (False),      { Condition 3, down trend }
         Cond4L   (False),      { Condition 4, up trend }
         Cond4S   (False),      { Condition 4, down trend }
         Cond5L   (False),      { Condition 5, up trend }
         Cond5S   (False),      { Condition 5, down trend }
         Cond6L   (False),      { Condition 6, up trend }
         Cond6S   (False),      { Condition 6, down trend }
         SumCondL (False),      { Summary of conditions for up trend }
         SumCondS (False),      { Summary of conditions for down trend }
         Score    (0),          { Accuracy of neural network prediction }
         FalsePos (0),          { Number of false positives: predicted pattern when not present }
         FalseNeg (0);          { Number of false negatives: didn't predict pattern when present }

Array: OThird[3](0),                   { Open prices for each third of day }
        HThird[3](0),                   { High prices for each third of day }
        LThird[3](99999999),            { Low prices for each third of day }
        CThird[3](0);                   { Close prices for each third of day }

{ Calculate indicator values }
ATR = Average(TrueRange, NATR);
MovAve1 = Average(C, NMA1);
MovAve2 = Average(C, NMA2);
MovAve3 = Average(C, NMA3);

{ Initialize some variables on 1st bar }
If BarNumber = 1 then Begin
    SessLen = AbsValue(TimeToMinutes(SessionEndTime(0,1)) - TimeToMinutes(SessionStartTime(0,1)));
    Time1Thrd = CalcTime(SessionStartTime(0,1), SessLen/3.);
    Time2Thrd = CalcTime(SessionStartTime(0,1), (2. * SessLen/3.));

    If BarInterval > 0 then
       NBarsDay = SessLen/BarInterval;
End;

{ Calculations on first bar of new day }
If date <> date[1] or BarNumber = 1 then Begin
       
    DayRnge = AbsValue(CloseD(1) - OpenD(1));   { range of prior day }

    { Calculate the minimum required move for a trend based on average daily range }
    AveCO = GetAveDayRange(NRnge);
    minTrend = RngeFr * AveCO;
    {Print("AveCO = ", AveCO, " minTrend = ", minTrend);}

    { Obtain O, H, L, C of each third of day }
    Value1 = GetDayThirds(Time1Thrd, Time2Thrd, NBarsDay, OThird, HThird, LThird, CThird);

End;

{ Initialize prior values of indicators }
If BarNumber = 1 then Begin
    MA1Prior = MovAve1;
    MA2Prior = MovAve2;
    MA3Prior = MovAve3;
    DRPrior = DayRnge;
    ACOPrior = AveCO;
    ATRPrior = ATR;
    TRPrior = TrueRange;
End;

{ Evaluate indicators to obtain prediction on prior day }
If date <> date[1] then Begin
       
    { Use indicator values from day prior to one we're trying to predict }
    MovAve1 = MA1Prior;
    MovAve2 = MA2Prior;
    MovAve3 = MA3Prior;
    ATR = ATRPrior;
       
    { Pattern on prior day; we want to predict this }
    PatVal = GetTrendPattern(OThird, HThird, LThird, CThird, minTrend);

    { See if we can predict pattern or absence of pattern }               
    DayCount = DayCount + 1;

    If AbsValue(PatVal) > 0 then
       PatCount = PatCount + 1;

    { Construct indicators and evaluate }
    Cond1L = TRUE;
    Cond1S = TRUE;
    If AbsValue(c1) > 0 then Begin
       Cond1L = c1 * (CloseD(2) - MovAve1) > 0;
       Cond1S = -c1 * (CloseD(2) - MovAve1) > 0;
    End;

    Cond2L = TRUE;
    Cond2S = TRUE;
    If AbsValue(c2) > 0 then Begin
       Cond2L = c2 * (CloseD(2) - MovAve2) > 0;
       Cond2S = -c2 * (CloseD(2) - MovAve2) > 0;
    End;

    Cond3L = TRUE;
    Cond3S = TRUE;
    If AbsValue(c3) > 0 then Begin
       Cond3L = c3 * (CloseD(2) - MovAve3) > 0;
       Cond3S = -c3 * (CloseD(2) - MovAve3) > 0;
    End;

    Cond4L = TRUE;
    Cond4S = TRUE;
    If AbsValue(c4) > 0 then Begin
       Cond4L = c4 * (DRPrior - ACOPrior) > 0;
           Cond4S = -c4 * (DRPrior - ACOPrior) > 0;
    End;

    Cond5L = TRUE;
    Cond5S = TRUE;
    If AbsValue(c5) > 0 then Begin
       Cond5L = c5 * (CloseD(2) - CloseD(3)) > 0;
       Cond5S = -c5 * (CloseD(2) - CloseD(3)) > 0;
    End;

        Cond6L = TRUE;
        Cond6S = TRUE;
    If AbsValue(c6) > 0 then Begin
      { Cond6L = c6 * (TRPrior - ATR) > 0;
           Cond6S = -c6 * (TRPrior - ATR) > 0; }
       Cond6L = c6 * (CloseD(2) - OpenD(2)) > 0;
       Cond6S = -c6 * (CloseD(2) - OpenD(2)) > 0;
    End;

    SumCondL = Cond1L and Cond2L and Cond3L and Cond4L and Cond5L and Cond6L;
    SumCondS = Cond1S and Cond2S and Cond3S and Cond4S and Cond5S and Cond6S;

    If PatVal > 0 then Begin              { Long pattern present so see if we predict its presence }
       If SumCondL then
          Score = Score + 1          { Correct prediction }
       Else
          FalseNeg = FalseNeg + 1;   { False negative }
        End
    Else If PatVal < 0 then Begin         { Short pattern present so see if we predict its presence }
       If SumCondS then
          Score = Score + 1          { Correct prediction }
       Else
          FalseNeg = FalseNeg + 1;   { False negative }
        End
    Else Begin                                 { Pattern not present, so see if we don't predict it }
           If SumCondL = False and SumCondS = False then
          Score = Score + 1          { Correct prediction }
       Else
          FalsePos = FalsePos + 1;   { False positive }
    End;
    {Print("Date: ", Date[1]:0:0, " Pattern: ", PatVal:0:0); }

    { Calculate indicator values at end of prior day }
    MA1Prior = MovAve1[1];
    MA2Prior = MovAve2[1];
    MA3Prior = MovAve3[1];
    ACOPrior = AveCO;
        DRPrior = DayRnge;
    TRPrior = TrueRange[1];
    ATRPrior = ATR[1];
End;

If LastBarOnChart then Begin
    If DayCount > 0 then
       Score = 100 * Score/DayCount;
    If PatCount > 0 then
       FalseNeg = 100 * FalseNeg/PatCount;
    If (DayCount - PatCount) > 0 then
       FalsePos = 100 * FalsePos/(DayCount - PatCount);
    Print("FindPrediction (c1 c2 c3 c4 c5 c6 Score FNeg FPos DayCount PatCount):,", c1:0:0, ",", c2:0:0, ",", c3:0:0,
              ",", c4:0:0, ",", c5:0:0, ",", c6:0:0, ",", Score:0:2, ",", FalseNeg:0:1, ",", FalsePos:0:1, ",", DayCount:0:0, ",",
              PatCount:0:0);
End;

请高手会改成TB的
作者: 草根。    时间: 2012-7-1 11:41:06

支持下。。。
作者: tufeiyige    时间: 2012-7-7 18:15:57

我是 没看明白, 高手 是不是 改成了TB 的 在 自己用哦
作者: 松鹏    时间: 2012-8-19 08:52:12

等牛了,现在还不太明白
作者: 程序化编辑    时间: 2012-8-19 18:14:37


作者: beixiao2005    时间: 2012-9-2 09:21:13

多谢分享

作者: 醒客老罗    时间: 2012-9-22 09:14:20

有人转成TB源码吗?

作者: flyfish    时间: 2012-9-23 18:51:48

明天研究一下
作者: flyfish    时间: 2012-9-25 11:12:04

放弃了,好多看不懂的地方
作者: fengyun    时间: 2012-10-8 15:01:12

比较复杂
作者: win5ms    时间: 2016-12-6 12:19:40

本帖最后由 win5ms 于 2021-9-9 13:23 编辑

[attach]40672[/attach]先在ts回测




欢迎光临 开拓者期货期权程序化系统交易论坛 (http://bbs.tb18.net/) Powered by Discuz! X2