{ N-N Predict ("Nearest Neighbor Prediction") This system searches for the N nearest neighbors to a pattern consisting of a set of indicator values. The average market move following the N patterns is taken as the prediction. If the average prediction is up, the system buys and exits two days later. If the average prediction is down, the system sells and covers two days later. The system also writes out the prediction on each bar for the next several days. Copyright 2004 Breakout Future www.BreakoutFutures.com mrb@BreakoutFutures.com } Input: NNN (50), { Number of nearest neighbors used in prediction } NPredict (5), { Number of days forward to predict } FName ("C:\NNResults1.csv"); { file name to write results to } Array: Errors[500, 2](0), { Error of patterns relative to current bar } NNBars [50](0), { Bar numbers of nearest neighbors } CPredict [20](0); { Prediction of close 1 to 20 days in future } Var: Ind1 (0), { Value of indicator 1 } Ind2 (0), { Value of indicator 2 } Ind3 (0), { Value of indicator 3 } Ind4 (0), { Value of indicator 4 } I1Max (0.01), { Max value of indicator 1 } I2Max (0.01), { Max value of indicator 2 } I3Max (0.01), { Max value of indicator 3 } I4Max (0.01), { Max value of indicator 4 } SumC (0), { Sum of predicted closes } MinErr (0), { Minimum error } MAXBARS (500), { Max dimension of Error array } MAXNNN (50), { Max dimension of NNBars array } MAXPRED (20), { Max dimension of CPredict array } NBars (0), { Number of bars to look back } NoMatch (True), { Logical flag for searching } ii (0), { loop counter } jj (0), { loop counter } kk (0), { loop counter } StrOut (""); { output character string } { Initialize file on first bar } If BarNumber = 1 then Begin FileDelete(FName); StrOut = "Date, Close, PredDay1, PredDay2, PredDay3, PredDay4, PredDay5" + NewLine; FileAppend(FName, StrOut); End; NBars = MinList(MaxBarsBack - 20, MAXBARS); { Define indicators } Ind1 = C - C[1]; Ind2 = C - C[2]; Ind3 = C - C[3]; Ind4 = C - C[5]; { Loop over prior bars to find max indicator values } for ii = 1 to NBars Begin if Ind1[ii] > I1Max then I1Max = Ind1[ii]; if Ind2[ii] > I2Max then I2Max = Ind2[ii]; if Ind3[ii] > I3Max then I3Max = Ind3[ii]; if Ind4[ii] > I4Max then I4Max = Ind4[ii]; End; { Calculate errors } for ii = 1 to NBars Begin Errors[ii - 1, 0] = Square((Ind1 - Ind1[ii])/I1Max) + Square((Ind2 - Ind2[ii])/I2Max) + Square((Ind3 - Ind3[ii])/I3Max) + Square((Ind4 - Ind4[ii])/I4Max); Errors[ii - 1, 1] = ii - 1; End; { Take the smallest NNN values as the nearest neighbors } for ii = 0 to MinList(NNN - 1, MAXNNN - 1) Begin MinErr = 100 * Errors[0, 0]; for jj = 0 to NBars - 1 Begin if Errors[jj, 0] < MinErr then Begin NoMatch = true; for kk = 0 to ii Begin if Errors[jj, 1] = NNBars[kk] then NoMatch = false; End; if NoMatch then Begin MinErr = Errors[jj, 0]; NNBars[ii]= Errors[jj, 1]; End; End; End; End; { Calculate prediction for next NPredict bars } for ii = 0 to MinList(NPredict - 1, MAXPRED - 1) Begin SumC = 0; for jj = 0 to MinList(NNN - 1, MAXNNN - 1) Begin if (NNBars[jj] - ii - 1) > 0 then SumC = SumC + (C[NNBars[jj] - ii - 1] - C[NNBars[jj]]); End; CPredict[ii] = C + (SumC/NNN); End; { Write out predictions } StrOut = NumtoStr(Date, 0) + "," + NumToStr(C, 2) + "," + NumtoStr(CPredict[0], 2) + "," + NumToStr(CPredict[1], 2) + "," + NumtoStr(CPredict[2], 2) + "," + NumToStr(CPredict[3], 2) + "," + NumToStr(CPredict[4], 2) + NewLine; FileAppend(FName, StrOut); { Buy if predictions are up; sell if down } if CPredict[0] > C and CPredict[1] > C and CPredict[2] > C then Buy next bar at C limit; if CPredict[0] < C and CPredict[1] < C and CPredict[2] < C then Sell short next bar at C limit; if BarsSinceEntry = 2 then Begin Sell this bar on close; Buy to cover this bar on close; End;