A NEURAL NET FOR STOCK TREND PREDICTOR · Thus, neural network can be a suitable approach for it....
Transcript of A NEURAL NET FOR STOCK TREND PREDICTOR · Thus, neural network can be a suitable approach for it....
ANEURALNETFORSTOCKTRENDPREDICTOR
CS297Report
Presentedto
Dr.ChrisPollett
DepartmentofComputerScience
SanJoseStateUniversity
InPartialFulfillment
oftheRequirementsfortheClass
CS297
By
SonalKabra
Dec2016
NeuralNetStockTrendPredictor 2
TABLE OF CONTENTS
INTRODUCTION................................................................................................................................... 3
DELIVERABLE 1................................................................................................................................... 5
DELIVERABLE 2................................................................................................................................... 9
DELIVERABLE 3...................................................................................................................................12
DELIVERABLE 4…...............................................................................................................................15
CONCLUSION .......................................................................................................................................16
REFERENCES .......................................................................................................................................17
NeuralNetStockTrendPredictor 3
INTRODUCTION
The“NeuralNetStockTrendPredictor”isanapproachtodevelopingastocktrend
predictor.ThisreportdescribespreliminaryworktowardsmyCS297project.Stock
predictionusingcomputersisalsoknownasalgorithmictrading(AT)orautomatedtrading.
Sincethemid-2000s,nearlyallthefinancialtradesareexecutedviacomputers.Muchofthat
tradingoccursalgorithmically,withcomputersexecutingpurchasesandsalesinresponse
toamarket.About66percentofallequitytransactionsintheUnitedStatesnowaredone
viahighfrequencyoralgorithmictrading.[1]
Algorithmictradingistheprocessofusingprogrammedcomputerstofollowa
definedsetofinstructionsforplacingatradeinorder.Thishelpstogenerateprofitsata
speedandfrequencythatisimpossibleforahumantrader.Theseinstructionscanbebased
onvolume,timing,price,oranymathematicalmodel.Algorithmictradinghelpstotakeout
thehumanemotionalimpactontrading.Thus,increasingtheprofitopportunitiesforthe
trader.[1]
Forexample,ifacomputerisprovidedwithallthehistoricalstockdata,itcanlearn
andanalyzethepatternsinthedataandcanpredictthefuturevalueforastockbasedonits
findings.
IamusingQuandlforpoolingallthehistoricalstockdata,andYahoo-Financefor
realtimestockdata.Bothwebsitesprovidefinancialandeconomicaldata.Thestockdata
pooledfromtherecontains5featuresasshowninthefollowingfigure:
Therefore,theobjectiveofthisprojectistoanalyzethedataandgeneratefeatures
thathelppredictstockprices.Besidesthegeneratedfeaturestheaimistobuildacomputer
programmablemodelusinganeuralnetandvariousmachine-learningtechniques.Neural
NeuralNetStockTrendPredictor 4
netisaninformation-processingparadigm.Theconceptisinspiredfromthewaybiological
nervoussystems,suchasthebrain,processinformation.
ThefollowingarethedeliverablesthatIhaveimplementedthissemesterto
understandtheessenceandtheworkingofalgorithmictradingsystem.
InDeliverable1,Icreatedapresentationcoveringthebasicsofanalgorithmic
tradingsystemandcalculatedsomebasictechnicalindicatorsfordataanalysis.In
deliverable2,Idevelopedaneuralnetworktopredictthestartingmonthforgivendata
series.Moredetailsonthiswillbediscussedunderthesectiontitled‘Deliverable2.’Inthe
nextdeliverable,i.e.Deliverable3,Idevelopedaprogramtopredicttheclosingpricefora
stockusingtheDecisiontreeregressionanalysisandKnearestneighborregression
analysis.InDeliverable4,Idevelopedaprogramthatpredictsthereturnsofaportfolioof
stock.
NeuralNetStockTrendPredictor 5
DELIVERABLE1Inordertomovetowardsimplementation,itwillbeusefultounderstandthebasic
ofalgorithmictrading.Theprocessofalgorithmictradingisasshowninfigure1.[2]
Figure1:AlgorithmicTradingProcess[2]
Thetradingprocessisdividedintofivestagesasfollows:
1.Dataaccessingandcleaning:
Algorithmictradingobtainsdatafromvarioussourcesandcleansthedataaccording
tothealgorithmicrequirements.Thedatacanberawdata,cleaneddataoranalyzeddata.
2.Pre-tradeanalysis:
Inthisstage,basedoninputteddata,theATanalyzesthevariouspropertiesofthe
financialinstrumentsinordertoidentifytradingopportunities.Thepropertiescanbevalue
ofacompanyorsentimentanalysisforacompany.Theanalysiscanbe:
FundamentalAnalysis:Inthisthefactors,whichmightaffecttheinstrument’svaluesuch
astheeconomicstateofacountry,orpricetoearningsratioofacompanyareanalyzed.
TechnicalAnalysis:Ittriestopredictfuturepricemovementsofaninstrument.Thisis
basedonitspricehistory,varioustradinghistorieslikevolumetradedpreviously.Thus,
tryingtoidentifythetradingpatterns.
QuantitativeAnalysis:Thisisamathematicalandstatisticalanalysisofaninstrument.It
describestherandomnessofthepriceofaninstrument.Thisismainlyusefulinperforming
riskmanagement.
NeuralNetStockTrendPredictor 6
3.Tradingsignalgeneration:
Inthisstage,itgeneratestheportfolioofinstrumentsbasedonthepreviousstage
analysisresultinginwhattotrade,howmuchtotradeandwhentotrade.
4.Tradeexecution:
Inthisstage,thetrademayormaynotbeexecutedviaautomationbecauseofthe
constrainingfactorssuchasliquidityofmarketandsizeoforder.Thus,ittriestoexecute
thetradeinsuchawaythatimpactonmarketandriskoftimingisminimized.
5.Post-tradeanalysis:
Inthisstage,itanalyzestheresultsofthetrade.Itcalculatesthedifferencebetween
thepricewhenabuy/selldecisionwasmadeandthefinalexecutionprice.Thisisimportant
becausebasedonthisanalysisandresultstheabovemodelsarereadjustedforgenerating
moreprofit.
AfterlearningATprocess,Iunderstoodthattechnicalanalysisiscrucialinanalyzing
howthestockmarketoraparticularstockismoving.Forthe297partoftheproject,Ihave
implementedthefollowingtechnicalindicators:
DailyReturns:ThisisthepercentagedifferenceofAdjustedClosePriceofi-thday
comparedto(i-1)-thday:
IhavecalculatedthedailyreturnsontheadjustedclosingpriceofApple’s(Stocksymbol:
AAPL)stockdata.Ihavepooledhistoricalstockdatafrom01-01-2014to09-12-2016from
YahooFinance.Tocalculatethis,Ihaveusedthepct_change()functionofPandas,aPython
librarymodule.Pandasisadataanalysislibrary.Itoffersdatastructuresandoperationsfor
manipulatingnumericaltablesandtimeseries.pct_change()isapercentchangeovera
periodoftimet.
Thisgivesusthepercentchangeoveragivennumberofperiods.Theoutputcanbeseenin
thefollowingscreenshot.Thes_returnsindicatethestockreturnsbasedonadjacentclose
(s_adjclose)valueoftheApplestock.
NeuralNetStockTrendPredictor 7
Volatility:Volatilityisthestandarddeviationofthereturnsofthestock.Itgivesusasense
ofhowmuchthestockreturnsfluctuateandhowriskyitisincomparisontotheoverall
marketreturn.
Iamcalculatingvolatilitybyusingthecovariancematrix.Thefirstelementofamatrixwill
giveusthevolatility.Thus,itgivesasenseofhowriskythestockiscomparedtomarket
return.
Covariancematrix:Covarianceindicatestheleveltowhichtwovariablesvarytogether.
Theformulatocalculatecovariancematrixis:
Ihaveusedcov()functionofNumpylibrary.Numpyisafundamentalpackageforscientific
computing.
Beta:Betaofastockisameasureoftherelativeriskofthestockwithrespecttothemarket.
Itindicateswhetherthestockismoreorlessvolatilethanthemarket.
Ifthebetavalueisgreaterthan1meansthatthestockreturnsamplifythemarketreturns
onboththeupsideanddownside.Whenthebetavalueisless,then1meansthatthestock
returnsaresubduedincomparisontothemarketreturns.Ihaveusedthefollowing
formulatocalculatethevalueofBeta.
NeuralNetStockTrendPredictor 8
Momentum:Momentumisameasureofthepastreturnsoveracertainperiod.For
example,the1-yearmomentumwillbethe1-yearreturnofthestock.Itmeasurestherate
oftheriseorfallinstockprices.Itisaveryusefulindicatorofthestrengthsandweaknesses
ofastock.Theformulaforcalculatingmomentumisasfollows.
Alpha:Alphaofastockgivesyouameasureoftheexcessreturnwithrespecttothemarket.
Positivealphasforastockorportfoliogivesyouasenseofhowwellyourstock
outperformedamarket.Alphaisahistoricalmeasureofthestockreturnsoninvestment
comparedtotheriskadjustedexpectedreturn.
RESULT:
TheoutputofDeliverable1foralltheabove-mentionedtechnicalindicatorsisasfollows.
NeuralNetStockTrendPredictor 9
DELIVERABLE2 Forthisdeliverable,Ihavebuiltaneuralnetwork,whichcantellthestartingmonth
ofagivenlabeleddataseries.Forexample,givenaseriesofsalesnumberofiPhonelabeled
eachwithmonthforanalyzing,oneshouldbeabletotellwhatcouldbethemonthofthe
firstsalespointinagivensimilardataset(size=10)withoutlabels.
Myneuralnetworkinthisdeliverablewillbedoingthesamething.Forthis
problem,Iusedanemploymentdatasetofgoodsproducingindustry.Idownloadedthis
datasetfromtheQuandlwebsite.Thedatasetcontainstwocolumnsofdatesand
employmentvalues.Theneuralnetwilltellusthemonthofthefirstemploymentdatavalue
ofgiventestset.
Aneuralnetworkisaprogrammingparadigmthatenablesacomputertolearnfrom
observationaldata.Itcanrecognizecomplexpatternsveryeasily.Usually,aneuralnetwork
getsalargenumberofinputandexpectedoutputforthatinput.Itthensearchesforthe
relationsbetweeninputandoutput.Oncethesepatternsareidentifiedthentheneural
networkcanproduceoutputonanyadditionalgivendataset.
Instockmarket,alotofinformationisproducedinashortperiodoftime.The
analysisandunderstandingofsuchavastdatainlimitedtimeisnotfeasibleforhumans.
Thus,neuralnetworkcanbeasuitableapproachforit.Totestthis,Iambuildinga
convolutionalneuralnetworkforasmalldataset.
Convolutionalneuralnetworkisafeedforwardneuralnetwork.Itisinspiredby
howthebrainprocessesvisualinputbydividingthesignalsintosmallerareascalled
receptivefields.Similarly,Convolutionallayersexploitthespatialrelationshipbetween
groupsofdatapointsandgeneratethefeaturemapbasedonthem[6].Theselayerstakethe
smallsizeofinput,multiplyitwithweightsandaddbiases.Thisiscalledasanactivation
function.TheConvolutionalneuralnetworkusesitfromrawinputdatatooutputclass
values.
Fordevelopingsuchaconvolutionalneuralnet,IamusingKerasPythonlibrary,
whichisbuiltontopoftheTensorflowlibrary.Tensorflowisanopensourcesoftware
libraryfornumericalcomputationusingdataflowgraphs.Kerasisahigh-levelneural
networklibrarythatfacilitatesfastandeasyprototypingforbuildinganeuralnetwork.
Theemploymentdatasetisaone-dimensionalinputvector.Therearetotalof924
datapointsinadataset.Ipreprocessedthedatatoreducethevaluestorangefrom0to1
anddivideditintoatestsetandatrainset.AsIampredictingthemonth,mytargetvariable
NeuralNetStockTrendPredictor 10
isavaluefrom1to12.Totransformitintoabinarymatrix,Iusedonehotencodingonthe
targetvariable.Thebasicarchitectureformyconvolutionalneuralnetisasshowninthe
followingfigure:
Figure2:CNNarchitecture
Myfirstlayerofneuralnetisaconvolutionallayer.Thislayertakestheinputinthe
shapeof(12,1).Fromthisitgenerates18featuremapsofstride1.Thefeaturelengthisof
size3.Iusedrectifieractivationfunction.Afterthis,Iusedamaxpoollayerwiththepool
sizeof2andfollowingthis,Iagainusedaconvolutionallayer.Thisgenerates6-featuremap
ofsize2.Finally,Iusedadropoutlayerfollowedbyadenselayertogeneratetheoutput.
Theoutputlayercontains12classes.Thedenselayerusedasoftmaxactivationfunctionto
outputprobability-likepredictionsforeachclass.
NeuralNetStockTrendPredictor 11
RESULT:
Theoutputforthisconvolutionalneuralnetworkisasfollows.
Forthisproblem,Itrainedmyneuralnetworkfor300epochs.Inmachine-learning
parlance,anepochisacompletepassthroughagivendataset.Thatis,bytheendofone
epoch,myneuralnetwork,inthiscase-aconvolutionalnet,willhavebeenexposedto
everyrecordtoexamplewithinthedatasetonce.AfterthatIachieved75%accuracy.The
numbersinthelastarrayindicatetheoutputclassificationforastartingmonth.The
expectedoutputwas[8,7,6,5,4,3,2,1,12,11,10,9].
Toevaluatewhethermyneuralnetisperformingwellinthisproblem,Idecidedtotestthe
sameproblemwith3people.Theresultiscompiledinthefollowingtable.
PredictedResult ActualResult
Person1 [10,9] [9,8]
Person2 [7,8] [5,4]
Person3 [10,9] [12,11]
Table1:HumanExperiment
AsshownintheTable1,theclosestanswergivenhasadifferenceofonemonthinactual
resultsandpredictedresults.Thus,theresultsgivenbytheneuralnetarecertainlymore
accuratethanhumans.
NeuralNetStockTrendPredictor 12
DELIVERABLE3 Thethirddeliverablewastodevelopaprogramtopredicttheclosingpriceofa
stockusingthehistoricaldataofthesamestock.Forexample,stockdataforCISCOSystems
isgivenfromtheyear2010tilltoday.Basedonthisdata,aprogramhastopredictwhatwill
betheclosingpriceofCISCOtoday.
Instockmarketprediction,wepredictthepriceofaparticularstock.Aspriceisa
continuousvariable,theregressionmethodworksbetter.Becauseifwecomparetostock
trends,theexactincrementinstockindexmayprovidemoreinformationforbuildinga
predictionmodel.
Andsimilarly,ingeneralhumanstendtodosimilartrading.Theytrytobuyorsell
thestockbasedonthepastperformanceofthestock.Peoplechangetheirstrategyonly
whenthereissomemajorcurrentnewsthatinfluencesthestockmarket.
Forthisproblem,IusedtheDecisionTreeandKNearestNeighbors(KNN)machine
learningtechniques,becausetheycancapturethepastdataandmakeareliableprediction
basedonit.Thus,decisiontreesandKNNregressiontechniquesaregoodchoices.
Decisiontreessplitthedataintosmallgroupsbasedonmaximizinginformation
gain.Thus,thesegroupscapturethepastdataandpreciselypredictdatathataresimilarto
thepastdata.K-nearestneighborspredicttheresultusingk-nearestdatapointsfromthe
pastdata.Hence,italsocancapturethepastdataandassumesimilardatapointswillhave
similarresults.
Forimplementingtheaboveapproach,IusedScikit-learnPythonlibrary.Scikit-
learnisaspeciallibraryformachinelearning.Itprovidessimpleandefficienttoolsfordata
mining.
Initially,IqueriedthestockdatafromQuandlbasedonthereadtickersymboland
userinputteddates.Iusedonly‘Open,’‘High,’and‘Volume’featuresoftheretrieveddataset.
Ialsoaskedtheusertoinputthepredictiondateforwhichtheywanttheprediction.
Iftherearenogivenpredictiondates,thenquerythemostrecentdatafromYahoo
Finance.Idividedtheaboveretrieveddatasetusingcross-validationtocreateatrainand
testdatasetbysplittingthedatain75:25ratio.
IusedensemblebaggingregressormethodforbothdecisiontreeandKNN
regressor.Abaggingregressorisamethodofcombiningmultiplepredictorsandthenusing
anaveragingorvotingmethodtogetthevalue.
NeuralNetStockTrendPredictor 13
Therefore,tofindthebestparameters,Iusedagridsearchmethod.Usingthose
parameterstrainthedataandcalculatetherootmeansquarederrorwiththetestdataand
predictthevalueforthetestdata.
RESULTS:
1.UsingDecisionTree:
FollowingistheoutputfortheFacebookstockpriceusingDecisionTree.Here
predictiondateswerenotprovided.DataistrainedfordatafromJan2012tillDec2016.
TheactualclosingpricesareFB:120.57,CSCO:30.63,AAPL:115.82
NeuralNetStockTrendPredictor 14
2.UsingKNN:
The following screen shot shows the predicted and actual values for stocks of
Facebook,CiscoandApple.Herepredictiondateisprovided.ThedatasetisfromJan.2016
tillDec.2016.
NeuralNetStockTrendPredictor 15
DELIVERABLE4 Forthisdeliverable,Ipredictedreturnportfolioofastock.Whenapersoninvestsin
more than one stock then it creates the portfolio of a stock. People buy, sell, or hold the
portfoliobasedonprofitorlossgeneratedfromit.Inthisdeliverable,Icalculatedprofitor
lossofaportfolio.ThisdeliverableisanextensionofDeliverable3.
Icalculatedreturnofstockbasedonpredictedclosepriceforindividualstock,and
later,summedupallthereturnsofastock.Iftheresultingvalueispositive,thenportfoliois
generatingprofit.
RESULT:
The portfolio of a stock contains Facebook, Cisco and Apple shares. Here, suppose I am
investingonDec.01,2016,thenmypredictedreturnofportfolioforDec.12isaround7%
asfollows.
NeuralNetStockTrendPredictor 16
CONCLUSIONIhavecompletedthebasicimplementationpartofmyproject.Iamnowawareof
whatpartsIneedtoemphasizemoreforCS298.
DuringthespanofCS297,Istartedbygaininganunderstandingofhowthestock
predictionsystemworks,andwhataretheimportantfeaturesthataffectthetrendingofa
stock.Stockpredictionisnotastraightproblem.Therearevariouspatternsinthedataand
allthefeaturesofapredictionmodeldependoneachother.
InCS297,Iexploredallthesedependenciesofdatabyusingdifferenttypesof
approacheslikeneuralnet,decisiontreeandk-nearestneighbors.Ialsoperformed
technicalanalysesondatasetinordertomeasurethereturn,riskofinvesting,and
performanceofstockfactors.
InCS298,Iwillmergethesefeatureswithotherapproachestoimprovetheir
predictioncapabilities.Iwillalsoaddthefundamentalanalysismodule,whichwillperform
theanalysisofacompanyforwhichwewanttopredictthestockprice.Forthat,Ineedto
studyindepththevariouseconomicindicators.Iwillalsoexploretheothermachine
learningtechniquesforstockprediction.
NeuralNetStockTrendPredictor 17
REFERENCES1. Readmore:BasicsofAlgorithmicTrading:ConceptsandExamples|Investopedia
http://www.investopedia.com/articles/active-trading/101014/basics-algorithmic-
trading-concepts-and-examples.asp#ixzz4PA8nb8Kq
2. Philip,T.,etall,algorithmicTradingReview
3. BarryJohnson-AlgorithmicTrading&DMA
4. PerryKaufman-TradingSystemsandMethods(5thed)(2013)