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 }
{ 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;
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;