Вы находитесь на странице: 1из 6

Breakout Bulletin - February 2004

TheBreakoutBulletin
ThefollowingarticlewasoriginallypublishedintheFebruary2004issueofTheBreakoutBulletin.

Page 1 of 6

NearestNeighborAnalysis
Inpastnewsletterarticles,I'veborrowedconceptsfromstatistics,suchasserialdependencyandsignificance testing,anddemonstratedtheirapplicationtotrading.ThismonthI'mgoingtointroduceanotherideafrom statistics.Thenearestneighborconceptcanbefoundinnonlinearmodeling,chaosresearch,andstatistical patternrecognition.Whilethesetopicscanbecomplex,thebasicideaofnearestneighboranalysisisnot difficulttounderstand. Inonerespect,thenearestneighborconceptisadataanalysistechnique.It'sawaytofocusananalysisonthe datathatareclosestinsomesensetotheconditionsofinterest.Instatisticalpatternrecognition,forexample, theobjectiveistodesigna"classifier,"whichdiscriminatesbetweenthepatternsyou'retryingtoidentifyandall otherpatternsencounteredbytheclassifier.Theclassifierisobtainedbyfittingaprobabilitydensityfunctionto asetofdatasamples.The"knearestneighbor"techniqueestimatestheprobabilitydensityfunctionforthe classifierpointbypointusingthekdatasamplesnearesttoeachpoint[seereference1].Thealternativewould betouseamathematicalequationtorepresenttheclassifier.Ifanequationwereavailable,theclassifierwould befitsimultaneouslytoallavailabledatasamples,ratherthanjusttheksamplesnearesttheevaluationpoint. Socalled"nonparametric"methods,likeknearestneighbor,aretypicallyusedwhenaparametricmethod--an equation--isnotavailable.Intradingthemarkets,viableparametricmethodsarerare. Inasimplesense,thenearestneighborapproachisanalogoustoanN-periodmovingaverage.Themoving averageiscalculatedoneachbarusingtheNbarsnearesttothecurrentbar.Similarly,theknearestneighbor estimateiscalculatedfromthek"neighbors"nearesttothepointofinterest.Nearestneighborestimatesare localestimates,likeasimplemovingaverage,centeredaroundthepointofinterestandrecalculatedusing differentneighborsateveryotherpointofinterest.Oneadvantageofthislocalizationapproachfortradingis thatittendstoadapttheresultstocurrentmarketconditions. Thereareseveralwaysinwhichnearestneighboranalysiscanbeappliedtotrading,includingthefollowing: 1. Usethenearestneighborestimateasapriceprediction,andincorporatethepredictionintoatrading system. 2. Usethenearestneighborpricepredictionasanindicatororguidetotrading. 3. Useanearestneighborsearchtoincreasethecomputationalefficiencyofdataintensivemarketmodeling methods,suchasneuralnetworksorkernelregression;seereference2. 4. Optimizeatradingsystem'sparametersovertheNnearestneighborstoadapttheparametervaluesto currentmarketconditions. 5. Optimizeasystem'spositionsizingparametersovertheNnearestneighborstoadaptthepositionsizing tocurrentmarketconditions. I'lldevelopidea#1below,butbeforeIdothat,Iwanttobrieflydiscussthelasttwoideas.Theusualapproach totailorsystemparametervaluestocurrentmarketconditionsistouse"walk-forward"optimization.Theidea ofwalk-forwardoptimizationisthatyouoptimizetheparametervaluesonapastsegmentofmarketdata,then testthesystemforwardintimeondatafollowingtheoptimizationsegment.Youevaluatethesystembasedon howwellitperformsonthetestdata,notthedataitwasoptimizedon.Theprocesscanberepeatedbymoving theoptimizationandtestsegmentsforwardintime.Thepremiseofwalk-forwardoptimizationisthattherecent pastisabetterfoundationforselectingsystemparametervaluesthanthedistantpast.Thehopeisthatthe parametervalueschosenontheoptimizationsegmentwillbewellsuitedtothemarketconditionsthat immediatelyfollow.Theproblemwiththisapproachisthatwhenmarketconditionschange--say,frombull markettobearmarket--youmayfindyourselfoptimizingononesetofmarketconditionswhiletradinga completelydifferentsetofconditions.Inthiscase,there'snogoodreasontoexpectthatthewalk-forward resultswillbesimilartotheoptimizedresults. Nearestneighboranalysisprovidesanalternativetothisapproach.Insteadofoptimizingoveralldatainthe recentpast,youcanselectasamplingoftradesoveralongerperiodoftimebasedonhowclosethetradesare inthenearestneighborsensetothecurrentmarketconditions.Forexample,chooseasetofindicators,suchas momentum,ADX,movingaverages,etc.Evaluatetheindicatorsonthecurrentbar.Thenevaluatetheindicators onthebarprecedingeachpasttrade.SelecttheN(say,30orso)tradeswithindicatorvaluesclosesttothose onthecurrentbar.Optimizethesystemparametervaluesoverthese30trades.Astheparametervaluesare variedduringtheoptimization,differenttradesmaybeproduced,sothe30tradeschosentoevaluatethe systemmaychangefromoptimizationiterationtoiteration. Oncetheoptimalparametervaluesarefound,theprofit/lossfromthenexttradecanberecorded.Asinwalk forwardoptimization,thistradewillbeout-of-sample,meaningitisgeneratedwithoutthebenefitofhindsight.

mhtml:file://D:\DOWNLOADS\00_M_ Bryant\Nearest Neighbor Analysis.mht

9/16/10

Breakout Bulletin - February 2004

Page 2 of 6

Aftereachtradeiscomplete,theprocesscanberepeatedforeachnewtrade.Thismeansthatnewparameter valueswillbegeneratedforeachtrade.Ifthepurposeistoevaluateatradingsystem,youwouldusetheoutof-sampletraderesultstoevaluatethesystem,ratherthantheoptimizedresultsontheN nearestneighbors.Whilethisprocedureismorecomplexthantraditionalwalkforwardoptimization,thenearest neighborapproachmayprovideamorereliablewaytodeterminesystemparametervaluesthatadaptto currentmarketconditions. Thesameideacanbeappliedtopositionsizing.Let'ssayyou'reusingafixedfractionalpositionsizing technique,inwhichapercentageoftheaccountequityisriskedoneachtrade.Typically,onewoulduseall availabletradeprofit/lossdatatocomputethebestfixedfractionforfuturetrading.However,insteadofusing alltrades,whynotusetheNnearestneighbors;i.e.,theNtradesthatareclosesttocurrentmarketconditions asdeterminedbyasetofindicatorvalues?Theprocedureisthesameasdescribedabove,exceptthatyou're optimizingonthefixedfraction(orwhateverpositionsizingparameteryouwant)ratherthanonthesystem parametervalues.Inthisway,youadaptthefixedfractiontocurrentmarketconditions. ANearestNeighborTradingSystem Imayhavemoretosayonthesetechniquesinalaternewsletter,butfornow,Iwanttodemonstratehow nearestneighborpredictioncanbeusedtodevelopatradingsystem.Thebasicideaistodefineapatternusing tradingindicatorsandfindtheNpatternsthatareclosesttothepatternonthecurrentbar.Youthendecideto buyorsellbasedonwhethertheaveragemarketmovefollowingtheNpatternsisupordown[reference3].For example,considerthefollowingmomentumindicators: I1=(C-C[1])/I1Max I2=(C-C[2])/I2Max I3=(C-C[3])/I3Max I4=(C-C[5])/I4Max whereCistheclosingpriceonthecurrentbar,C[i]isthecloseibarsago,andI1Max-I4Maxarethemaximum valuesofI1-I4.Themomentumvaluesarenormalizedbythemaximumvaluessothateachindicatorwillbein therange[-1,+1].ThefirststepistoevaluateI1-I4onthecurrentbar.Theindicatorsarethenevaluatedon eachpriorbar,andthefollowingerrorvaluesarecalculated: Ei=(I1-I1[i])^2+(I2-I2[i])^2+(I3-I3[i])^2+(I4-I4[i])^2 whereEiistheerroronthebaribarsago,andI1[i]-I4[i]aretheindicatorvaluesibarsago.TheNnearest neighborsarethei's(thatis,thebars)thatprovidethesmallesterrorvalues,Ei. Asaresult,weendupwithasetofNbarswithindicatorpatternssimilartothepatternwehaveonthecurrent bar.ThefinalstepistoseewhichwaythemarketmovedfollowingeachoftheNpatterns.If,forexample,the averagemovefollowingtheNpatternsis10pointsupintwodays,thenwemightwanttobuythemarketand holdfortwodaysinexpectationofasimilarmove. IwroteasimplesystembasedonthisideaandtesteditontheE-miniS&P500futures.TheEasyLanguagecode forthesystemisshownbelow. { N-NPredict("NearestNeighborPrediction") ThissystemsearchesfortheNnearestneighborstoapatternconsisting ofasetofindicatorvalues.TheaveragemarketmovefollowingtheNpatterns istakenastheprediction. Iftheaveragepredictionisup,thesystembuysandexitstwodayslater.If theaveragepredictionisdown,thesystemsellsandcoverstwodayslater. Thesystemalsowritesoutthepredictiononeachbarforthenextseveraldays. Copyright2004BreakoutFutures www.BreakoutFutures.com mrb@BreakoutFutures.com } Input:NNN(50),{Numberofnearestneighborsusedinprediction} NPredict(5),{Numberofdaysforwardtopredict} FName("C:\NNResults1.csv");{filenametowriteresultsto} Array:Errors[500,2](0),{Errorofpatternsrelativetocurrentbar} NNBars[50](0),{Barnumbersofnearestneighbors} CPredict[20](0);{Predictionofclose1to20daysinfuture} Var:Ind1(0),{Valueofindicator1} Ind2(0),{Valueofindicator2} Ind3(0),{Valueofindicator3} Ind4(0),{Valueofindicator4} I1Max(0.01),{Maxvalueofindicator1} I2Max(0.01),{Maxvalueofindicator2} I3Max(0.01),{Maxvalueofindicator3} I4Max(0.01),{Maxvalueofindicator4} SumC(0),{Sumofpredictedcloses}

mhtml:file://D:\DOWNLOADS\00_M_ Bryant\Nearest Neighbor Analysis.mht

9/16/10

Breakout Bulletin - February 2004

Page 3 of 6

MinErr(0),{Minimumerror} MAXBARS(500),{MaxdimensionofErrorarray} MAXNNN(50),{MaxdimensionofNNBarsarray} MAXPRED(20),{MaxdimensionofCPredictarray} NBars(0),{Numberofbarstolookback} NoMatch(True),{Logicalflagforsearching} ii(0),{loopcounter} jj(0),{loopcounter} kk(0),{loopcounter} StrOut("");{outputcharacterstring} {Initializefileonfirstbar} IfBarNumber=1thenBegin FileDelete(FName); StrOut="Date,Close,PredDay1,PredDay2,PredDay3,PredDay4,PredDay5"+NewLine; FileAppend(FName,StrOut); End; NBars=MinList(MaxBarsBack-20,MAXBARS); {Defineindicators} Ind1=C-C[1]; Ind2=C-C[2]; Ind3=C-C[3]; Ind4=C-C[5]; {Loopoverpriorbarstofindmaxindicatorvalues} forii=1toNBarsBegin ifInd1[ii]>I1Maxthen I1Max=Ind1[ii]; ifInd2[ii]>I2Maxthen I2Max=Ind2[ii]; ifInd3[ii]>I3Maxthen I3Max=Ind3[ii]; ifInd4[ii]>I4Maxthen I4Max=Ind4[ii]; End; {Calculateerrors} forii=1toNBarsBegin 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; {TakethesmallestNNNvaluesasthenearestneighbors} forii=0toMinList(NNN-1,MAXNNN-1)Begin MinErr=100*Errors[0,0]; forjj=0toNBars-1Begin ifErrors[jj,0]<MinErrthenBegin NoMatch=true; forkk=0toiiBegin ifErrors[jj,1]=NNBars[kk]then NoMatch=false; End; ifNoMatchthenBegin MinErr=Errors[jj,0]; NNBars[ii]=Errors[jj,1]; End; End; End; End; {CalculatepredictionfornextNPredictbars} forii=0toMinList(NPredict-1,MAXPRED-1)Begin SumC=0; forjj=0toMinList(NNN-1,MAXNNN-1)Begin if(NNBars[jj]-ii-1)>0then SumC=SumC+(C[NNBars[jj]-ii-1]-C[NNBars[jj]]); End; CPredict[ii]=C+(SumC/NNN); End; {Writeoutpredictions} StrOut=NumtoStr(Date,0)+","+NumToStr(C,2)+","+

mhtml:file://D:\DOWNLOADS\00_M_ Bryant\Nearest Neighbor Analysis.mht

9/16/10

Breakout Bulletin - February 2004

Page 4 of 6

NumtoStr(CPredict[0],2)+","+NumToStr(CPredict[1],2)+","+ NumtoStr(CPredict[2],2)+","+NumToStr(CPredict[3],2)+","+ NumToStr(CPredict[4],2)+NewLine; FileAppend(FName,StrOut); {Buyifpredictionsareup;sellifdown} ifCPredict[0]>CandCPredict[1]>CandCPredict[2]>Cthen BuynextbaratClimit; ifCPredict[0]<CandCPredict[1]<CandCPredict[2]<Cthen SellshortnextbaratClimit; ifBarsSinceEntry=2thenBegin Sellthisbaronclose; Buytocoverthisbaronclose; End; Fig.1.EasyLanguagecodefornearestneighborsystem,"N-NPredict". TheN-NPredictsystemusestheindicatorsI1-I4givenabove.Itcalculatestheseindicatorsoneachbarand storestheerrors,Ei,intheErrorsarray.Thefirstcolumnofthistwo-dimensionalarrayholdstheerrorvalueon eachbar;thesecondcolumnstoresthebarnumber.TheerrorarrayisthensearchedfortheNNNnearest neighbors(bars)withthesmallesterrors.ThepredictedclosingpriceinthefutureforthenextNPredictbarsis calculatedbyaveragingthepricechangesfollowingthenearestneighborsandaddingtheaveragepricechange tothecurrentclose.Forexample,iftheaveragepricechangetwodaysafterthenearestneighborsis+4points, andthecurrentcloseis1143.00,thenthepredictedclosetwodaysfromthecurrentclosewouldbe1147.00.A predictionismadeforeachbarfollowingthecurrentcloseuptoNPredictbars,althoughthepredictionsforonly thefirstfivebarsarewrittenouttothetextfile. Afterthepredictionsaremade,thetradesareplaced.Alongtradeisplacedifthepredictedcloseforeachofthe nextthreebarsishigherthanthecurrentclose.Ashorttradeisplaceifthepredictedcloseforeachofthenext threebarsislowerthanthecurrentclose.Theentryordersareplacedatthepriorcloseusingalimitorderto ensurethattheentrypriceisnoworsethanthepriorclose.Thetradeisexitedtwodayslaterontheclose.No protectivestoporderisused. Inordertomakesurethesystemhasenoughdatatoidentifyagoodsetofnearestneighbors,it'snecessaryto setthelookbacklength(MaxBarsBackinTradeStation)toalargenumber.Ifoundthatalookbacklengthof about150generatedgoodresultsusing50nearestneighbors.Withalargerlookbacklength,thesystemwillbe basingthepredictionsmoreonthepast,whereasashorterlengthmeansthesampleswillbetakenfrommore recentbars. Withoutincludinganycostsforslippageorcommissions,thefollowingresultswereobtainedover1000barson dailydata(symbol@ESinTradeStation7.2),endingon2/25/04: NetProfit:$39,968 ProfitFactor:1.37 No.Trades:277 %Profitable:57% Ave.Trade:$144 MaxDD:-$13,700 Theequitycurveisshowbelow.

mhtml:file://D:\DOWNLOADS\00_M_ Bryant\Nearest Neighbor Analysis.mht

9/16/10

Breakout Bulletin - February 2004

Page 5 of 6

Fig.2.Equitycurvefor"N-NPredict"systemontheE-miniS&P. TheN-NPredictsystemisasimplesystemdesignedtoillustratetheconceptofusingnearestneighbor predictionasthebasisforatradingsystem.Nonetheless,itworksreasonablywelloverdataincludingbothbull andbearmarkets.Also,onceasetofindicatorvaluesischosen,therearefewparametersthatrequire adjustment.Asmallnumberofparameterstendstoimplythatasystemislesslikelytobecurvefittothe market.Theonlyexplicitparameteristhenumberofnearestneighborsusedtocomputethepricepredictions.I foundthat50workedbetterthan40,whichwasbetterthan20or30.Ididn'ttrymorethan50nearest neighbors,butasthisnumberapproachesthelengthofthelookbackperiod,thepredictionwillbebasedona greaterandgreaterfractionofthelookbackperioduntilthepredictionisnothingmorethananaverageofall pricechangesinthelookbackperiod. Inadditiontothenumberofnearestneighbors,theperformanceofthissystemwilldependonthelookback length.Itmaybethattheratiobetweenthelookbacklengthandthenumberofnearestneighborsisimportant. WiththevaluesIchosehere,theratiois3:1.Ofcourse,theselectionofindicatorsforthenearestneighbor patternsisveryimportant.IchosesimplemomentumindicatorsforN-NPredict.Anyindicatorcouldbeused, includingmovingaverages,stochastics,ADX,Bollingerbands,etc.Infact,the"neuralnetwork"indicatorI developedinthelastnewsletterwouldbeaninterestingindicatortoincludeinthistypeofsystem. References: 1. KeinosukeFukunaga,IntroductiontoStatisticalPatternRecognition,2ndEd.,AcademicPressInc.,San Diego,CA,1990. 2. JohnR.Wolberg,ExpertTradingSystems,JohnWiley&Sons,Inc.,NewYork,2000. 3. HansUhlig,NearestNeighborPrediction,TechnicalAnalysisofStocks&Commodities,November2001, pp.56-61.

Correction
InDecember,IforgottoincludetheEasyLanguagecodeforthetanhfunction,whichwasusedintheneural networksystem.Hereitis: { tanhfunction. Returnanapproximationtothehyperbolictangentfunction.The approximationisgivenonp.219ofCRCStandardMathematical

mhtml:file://D:\DOWNLOADS\00_M_ Bryant\Nearest Neighbor Analysis.mht

9/16/10

Breakout Bulletin - February 2004


Tables,26thedition. MikeBryant BreakoutFutures www.BreakoutFutures.com Copyright2003 } Input:x(NumericSimple),{inputtofunction} NTerms(NumericSimple);{#termsinseries} Var:pi(3.1415926536), Sum(0), ii(0); Sum=0.; Forii=0toNTermsBegin Sum=Sum+1./(Power(((ii+0.5)*pi),2)+Power(x,2)); End; tanh=Sum*2*x; That'sallfornow.Goodluckwithyourtrading. MikeBryant BreakoutFutures

Page 6 of 6

mhtml:file://D:\DOWNLOADS\00_M_ Bryant\Nearest Neighbor Analysis.mht

9/16/10

Вам также может понравиться