Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
132 views
36 pages
Technical Indicators and GRU-LSTM To Predict Stock Price
Technical Indicators and GRU-LSTM to Predict Stock price
Uploaded by
FabioSantos
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save Technical Indicators and GRU-LSTM to Predict Stock... For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
132 views
36 pages
Technical Indicators and GRU-LSTM To Predict Stock Price
Technical Indicators and GRU-LSTM to Predict Stock price
Uploaded by
FabioSantos
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save Technical Indicators and GRU-LSTM to Predict Stock... For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save Technical Indicators and GRU-LSTM to Predict Stock... For Later
You are on page 1
/ 36
Search
Fullscreen
sanz “enc nati and GAUL STAs Pree! Sk pe by Sat Maka Tamme Oat sence S48KFollowers - Editors’ Picks Features Explore Contribute About You have 2free member-only stories lft this month. Sign up for Medium and get an extra one Technical Indicators and GRU/LSTM to Predict Stock price Predictive model using multivariate time-Series and Python Code @ servers nov. 208 -sominend»“enc nati and GAUL STAs Pree! Sk pe by Sat Maka Tamms Oat sence Image by Sart Matt RCHNICAL indicators are available at large numbers and used mostly by stock traders. Most indicators have user-defined variables which allow traders to adapt key inputs such as look-back period which us how much historical data will be used to form the calculations to suit their needs. We will use some of the indicators to create features in the existing data set. Applying these features, we will see if we can predict the future price of a particular stock, ‘We will use Gated Recurrent Unit (GRU) and Long Short-Term Memory (LSTM) of recurrent units to compare their performance. LSTM is well established on sequence-based tasks with long-term dependencies, and GRU is a new addition in the field of machine learning which is an onitwatnancenn omtaecapaictalindcaoreatgsim-rnnuratntesminaSZec 208swanaeet “woman ang RULE T Pe Bah py Sta Toe Bt Src improvised version of Recurrent Neural Network(RNN). | found this article is quite interesting to know more about these two network architecture. ‘There are some similarities and differences between GRU and LSTM. Similarities + Both the network have additive component from tto t + 1; new content is added on the top of existing content. + Both addresses vanishing and exploding gradient issue + The update gate in GRU and forget gate in LSTM takes the linear sum between the existing state and the newly computed state Differences ‘* GRU has two gates, reset and update gates compared to LSTM has three gates, input, forget and output. GRU does not have an output gate like LSTM. Update gate in GRU does the work of input and forget gate of LSTM. onitwatnancenn omtaecapaictalindcaoreatgsim-rnnuratntesminaSZec 8sr Poe Sk en Seta ost Saree ‘+ GRU is computationally more efficient considering fewer parameters and need less data to generalize. + LSTM maintains an internal memory state cell, while GRU does not have a separate memory cell ‘+ GRU does not have any mechanism to control the degree to which its state or memory content is exposed, but exposes the whole state or memory content each time. LSTM can control how much memory content it wants to expose. You may visit this article to get more theoretical ideas about GRU and LSTM network architecture. I will explain here both feature creation and applying RNN with a simplified example. # Import Gold data in panda and passed to a variable name “df” df = pd.read_csv("CIPLA.lS.csv", parse_dates= True) dF nead(), Date Open High Low Close Adj Close Volume © ‘1ai2000 1i2e00003 THB.Se7IGr 110.0300 T1e5eTOOT 69.207605 2632500 4 1142000 122-958909 122.960800 14 400002 117.220001 6a 783824 a77608.0 2 ‘Ws2000 117919008 THT.oTeWE 109 190007 T14.804001 GA-s28KG5 4724280 3 ‘62000 114720001 114.800003 107 819996 198.468002 64555061. 4135380 onitwatnancenn orton aiectalindcaoreatgi-rnnratntesmin Zech 8GAULT Pree Sek [Sr Mat | Tome Da Seance 4 11772000 108.590¢08 111.192001 99.792000 99.904001 5¢.998638 9917000 dF 1snul().sum() bate e ‘open a igh a tow 6 Close ol adj Close 61 volume 6 type: Inter Af = df.Fillna(nethod ='pad") # filling the missing values with previous ones ‘dF isnui().sum() ate ‘open igh Close adj Close Volume type: intes print(‘There are {} number of days in the dataset.’ .format(df.shape[0])) ‘There are 4967 number of days in the dataset. ———_ — -—______, on tommasooncenen cor torweainganihe rien gr kr —ierameoneyaiet 6001 003s 0000 0020 2005 “enc nati and CRUST Pree! Stk pe by Sat Maka Tamms Oat sence oot oon on oe a6 1 008 00s oon om com> mo moO tor oom dataset.describe() # overall statistics Open igh low Close eount 4957-00000 mean 206193487 std 195.107408 ‘min 49.090002 4967.000000 4967.000000 4967-00000 3t0.104928 301.489249 305.600409 197.105247 192772437 194.813300 so.9se0v 4entege —50.180000 on 290900 oa annem? 40R a5nonn onitwatnancenn mtseoatn ications mrtmaranimesmnasZeetce‘moot “ec nat and GAUL STs roe! Sk pe fy Sal Maka Tamms Cat sence ‘50% 299.500000 287.000000 277.0808 282.200012 7% 488.000000 472.500000 460.900009 467.224007, max 744950012 752.e49976 790250000 749 se9a76 Thave created 2 copies of data (tek_ind_1 & tek_ind_2) to add columns of different sets of Technical Indicators. Amport copy data = dataset ‘tek_ind 1 = copy.deepcopy(data) ‘tek_ind_2 = copy-deepcopy(data) tek_ind_1 dataframe ‘tek ind_a[ "dally return’] = tek ind 2.close.pet_charige()-Fi10a(0) tek ind_1[ "cum daily return’] ~ (1 # tek ind a[ daily. return’ ])-cumprod() ‘tek ind_1[-0-1"] ~ tok ind_A.tigh ~ dataset, Low tek ind_a ['¢-0"] = tek ind a.close - tek ind 2.open tek ind a["oday *'] = tek ind_a,close,shift(1) rolLing(window = 10) .mean().fillna(o) ‘tekLind_1[ soday m'] = tek ind_1.close,shife(s).relLing(uindow = se) .mean() filna(0) tok ind a[*200day 0°] = tek ind 4.Closo.shife(1).rolLinglindow = 200)-mean()-FLna(0) Monitwatnaancenn omtanoap atelier intmulrantesminaSZec08 18“enc nati and GAUL STs roe! Stk pe fy Sat Maka Tamme Oat Sserce ‘tek_ind_2[‘rsi"] = talib.nsz(tek_ind_a.close.values, tineperiod = 14) aL MILLR(tek_id_1. igh. values, ‘ek ind Low.values, ‘tend Lsclovervalues, 18) tek ind A Wilians 28°] 4 create 7 ond 21 days Moving Average ‘sek ing_t[°ne7"] - tak dnd_2.close, rolling(window-”).meen().F1Ina(0) ‘tok ind_i[°mo21"] = tek_ind_1.Close.rolLing(vindow-21).mean() fiLna(0) 4 creating mo ‘ek ind_1[ ena. 26°] = tek ind_1.Close,cum(spane26) mean) -321n9(0) ‘ek ind_a[‘ena_12'] = tek ind 1.close.eum(spanci2) mean) #511na(0) ‘ekLind_a[mned] = (tek ind ifm. 12") ~ tek ind 3["eaa_26"]) 1 cresting toioger sods erat of dps at Str eatin efor otic pr fr Bling caution Tolling sen = tek Sn 3cloverellngende) men) Felling st = tok nd isclneselingfinae 000) ‘a slting ran") = rolling manta) {ek ind. AM hgh] = (rolling mean (ellis * wo. sta) .#1nb( {etcind al teow] = (Polling - (Pollingstd* we 9 Sta) Alla) fecina atau’) = tek ind clove em(com.).men() {elLind2( sorte’) = te ing. 2clove = tek. ind.schndie) plt.figure(Figsize=(12, 8)) Monitwatuncenn omtanoapaitalindcaor-ag ornamentswanaeet ‘enc ao ng RUST Pee Sk py a | Tn Dla Sr pit.piot(tek and 1] "Close" J, 1abel=" Actual") plt_plot(tek_ind_a[‘20day MA"], label="16day Ma") plt.plot(tek_ind_1["seday ma], label plt_plot(tek_ind_1['200day ma}, labe pt. legend(1oc="best") ttek_ind 1. info() onitwatnancenn orton aiectralindcoreagi-rnnarantesminaZeecGAULT Pree Se [Sr Mat | Tomes Daa Seance
RangeIndex: 4967 entries, 0 to 4966 data columns (total 22 columns): ‘open 4967 non-null High 4967 non-null Low 4967 non-null close 4967 non-null daily_return 4967 non-null cum daily return 4967 non-null we 4967 non-null co 4967 non-null loday mA 4967 non-null seday MA 4967 non-null 2oaday mA 4967 non-null Psi 4953 non-null Willians XR 4954 non-null na? 4967 non-null maz 4967 non-null ena_26 4967 non-null ema_12 4967 non-null macd 4967 non-null bb_high 4967 non-null bb_low 4967 non-null ema 4967 non-null ‘momentum 4967 non-null types: floatea(22) memory usage: 852.8 KB tek_ind_2 dataframe onitwatnancenn omtaecapaictalindcaoreatgsim-rnnuratntesminaSZec floates Floates floates Floates Floates Floatea floatee Floates Floates floatea floates Floates Floatea floates floates Floates floatea floates Floates floatea Floates Floates‘moot “enc nati and GAUL STAs Pree! Sk pe by Sat Maka Tamme Oat sence Calculation of Stochastic Oscillator (%K and %D) det son(at, tet aah) = tok ind 2clas") - tek In 2f'tow High Jrllsngineon=n cot {ta rong, corer tnt a[ eno | eoisegeon =) 2s, aerate) / sin())) * 190 mise nea) seein Stok(tek 2, 4) (cura D tek Sa. futtnaey scin eal CCI = (typical price - ma) / (0.015 * mean deviation) * typical price = (high + low + close) /3 + p= number of periods (20 commonly used) + ma = moving average ‘* moving average = typical price / p * mean deviation = (typical price —MA) /p Calculations of Ichimoku Cloud onitwatnancenn omtaecapaictalindcaoreatgsim-rnnuratntesminaSZec se‘enc ht and GAULT Pres ik pe + Turning Line Highest High + Lowest Low ) / 2, for the past 9 days * Standard Line = (Highest High + Lowest Low ) / 2, for the past 26 days + Leading Span 1 = (Standard Line + Turning Line ) / 2, plotted 26 days ahead of today + Leading Span 2 = ( Highest High + Lowest Low ) / 2, for the past 52 days, plotted 26 days ahead of today * Cloud = Shaded Area between Span 1 and Span 2 fr roe [(Close "cove nperids a) / (IOS petads ogs)} * 188 fee Ina aR] = (C0 ea close’) ~ nek od aco’ shitty) 7 (ese int af cloee Lanecan)ytoe thin. = tok ind 3.) fescind a nonctun']- tk ind af'clore')- tk ind af ‘cows etiFe) tecieee~ tm Ind 2.450060) p= (te stat Wigh') + Ue ed 3 LOH) + CA tnd 2Eese1) 3 Povey m= itp ma) 20 eine 20 CEr 1 = ep-ay 7605 * a) ‘ calulatin of Tepe apnea having erage fF 1.0R > GUM SOR(@R)) + HA(CWCOM)) Tews aac) 62 * (Clase - 2) rewire) onitwatnncenn ortanoay inane nt grrr temiconitwatnancenn omtaecapaictalindcaoreatgsim-rnnuratntesminaSZec Gusto Pre Se [Sr Mat | Tomes Da Seance ec ine tein aint) kind af‘ten'] = (2¢ tok ind afm") = 2 tek nda ema'] © tind fa") (ek ind 2am] ¢ tek ind afew] * teelnd arom") ‘Turing Line igh = tee ind. 2[ iy" T.notngQind, contra) soe) Io = sok il 3[ La]. Lingtwindana,cortreralsa) ni) ‘ek ind 2f turog ii} = (high + Jo) 22 26 high = te 2nd 2 ¥0'].oldingodnden-2c,certer-Flse) tax( o-law = gekcind Low |.ral ingle 2jconker Flea)-min0) ‘skLind af stand Line] = (pu gh « pat-2ow) / 2 ain 3 Tekin epnd!) ~ (Ce Sn turng Dine} tak And af tan Bin) J 2. shlf8(26) fated 3 china, sant’) = (tale ind 2[ turing Line") + tke ind 2[“etangordino"]) 2)-eRiFE(2=) 4 veaing span 2 52) high = tek ind 2 Ng Jsoddingandanes2,conterFalse omoxQ) 52 low = tek ina Low] rldtngeindwnn,centertalse).minO fokcind aL Sehinolyspon'] = ((2_high + 982 Lon) 2) si RC) 4 the most current closing price plotted 2 tine periats banat (etteral) ‘ek ink aL chow apa |= tek fn 2 Cone’ T-shiF 22) 422 according 22 overtopedia Fourier transformation For data that is known to have seasonal, or daily patterns we'd like to use Fourier analysis to make predictions. 1's like a combination of extrapolation and de-noising. We want to repeat the observed data over multiple periods.‘moot “enc nati and GAUL STAs Pree! Sk pe by Sat Maka Tamme Oat sence and also want to find patterns by finding the dominant frequency components in the observed data. We will compute the Fourier transformation and the spectral density of the signal, + The first step is to compute the FFT of the signal using the fft() function + Once the FFT has been obtained, we need to take the square of its, absolute value in order to get the power spectral density (PSD) Close fft = np.FFt.fFt(np.asarray (dataset. Close. tolist())) fft_df = pd.DataFrane({'Fft' :close_fft}) fFtLdF[ ‘absolute’ ] = fft_df['#¥t' ]-apply(lambda x: np.abs(x)) fft_df['angle'] = fft_df['fft']-apply(lanbda x: np.angle(x)) plt-fipare(Figsize=(1s, 7), dpisi90) Eee Last = nprasarray( tft af #1 ]-tolist)) for nue in Dy 2 109) FE Latiles opi copy (HFC lst); FFE st ate num_t-nu pltcplot(ap-¢et-i#se(e#t List-eta), Jabel="raurier iransearw with () companents* foraat(rum )) pit. plot(tek ind clove, Jatel= teal) pltodavel (D.y5") puts ylavel (mi BIEL LItle(‘ciPua (close) stock peices & Founten transfor") BIE Togsns) Bits show0) onitowatnancenn ortaecaatetalindcoreagsrtnuaratntesmin Ze 1036‘moot “enc nat and CRUST roe! Stk py Sat Maka Tamme Oat sence If we compare with 10,50,200 day MA plot, we see here that Fourier transformation smoothing the data better by de-noising. ‘tek_ind_2. info()
Rangelndex: 4967 entries, @ to 4966 data columns (total 18 columns): pen 4967 non-null floatea High 4967 non-null floats4 Low 4967 non-null floatsa Close 2967 non-null floats stok 4967 non-null floatea stod 4967 non-null floatsa onitwatnaancenn omtancopaiectralindcaor-nt gs irnnarantesnin ateGAULT Pre Sek p[y Sr Mat | Tomes Da Seance Momentum 4957 non-null floatsa cer 4967 non-null floatea ema 4967 non-null floatea tema 4957 non-null floatsa turning line 4959 non-null floates standard_line 4942 non-null floates ichimoku"spant 4916 non-null floatea ichimoku_span2 4890 non-null floates chikou_span 4945 non-null floatea absolute 4967 non-null floatea angle 4967 non-null floatsa types: floatea(1s) memory usage: 698.6 KB Data preparation for RNN ‘tek_ind_a.coluans ndex({‘open', "high", ‘Low’, "close", ‘daily return’, ‘cun daily return’, HL," "C0", “doday Ma", "Soday MA’, “zooday Ma", "rsi', “williams sR‘, maz") ‘maza', ‘ema_26","‘ena_12", ‘acd’, ‘db high’, "bb_low', ‘ona’, “momentum” ], types’ object") «copy deepcopy(tek ind 1) copy deepcopy(tek_ind_2) print(*Total dataset has () samples, and () features.” .format(a.shape[o], a.shape[])) onitwatnncenn orton aiectalicaoreagrrnnaratntesmin Zeexude Sis ey Total dataset has 4967 samples, and 22 Features. acinfo()
Rangelndex: 4967 entries, 0 to 4065, Data columns (total 22 columns): open 967 igh aser Low 2967 Close 2967 daily return 2907 cun.daily return 4967 aL 2967 soday Wa 2967 Soday ma 967 2oaday HA 2267 hiTLans 28 954 maz 4967 eno? 2967 imacd 2967 bo high “er below 2967 oma 4967 ment 2967 types: Floatsa(22) memory usage: 853.8 Ke ‘on-nuld, on-nuld on-nulL pon-nuld on-null enn fon-nuld en-nld on-nuld on-nuld fon-null on-nuld fon-null enn en-null en-nuld fon-null nnd on-nuld on-nld pon-null en-au floatea floated Floatea ‘floates Aosta ‘fleseea Aloatea flostea Floatéa ‘floated Floated foatea Aloatea flostea Floatee floatea Floatea floatea Hosted Flnatea Floats flostea a = a.fillna(a) # removing NaN from coLums a.info() onitwatnancenn omtaecapaictalindcaoreatgsim-rnnuratntesminaSZecexude Si pe 1S a. shape (4967, 22) values = a.values # ensure all data is float values = values.astype(’ Float22') values array({[112.8 116.588, 110.408 . 115.588 J, (aise "12.96 yaaa, o , hea, (17.92, 4117.92 108.2 pry 113.804 J}, [478.95 5 478.95, 462.25 5 .+.y 404.06226, ast. |, (aes, y'ara.7 400.35 oss, 403.02627, aos.05 |, [467-3 474.2, 462.4, «4 402.82706, 258.2, j], atype=Fioat22) 1 print("Min:", np.min(values) ) print(*Max:", np.max(values) ) Min: -100.0 Fax: 756.7664 1 values =pd.pataFrane(values) onitwatnancenn omtaecapaictalindcaoreatgsim-rnnuratntesminaSZec 116.588 117.062 115.49877, 465.81265, 466.5002 , 468.3012,‘moot “enc nati and GAUL STAs Pree! Sk pe by Sat Maka Tamme Oat sence ‘We see that data points varies from -100 to 756.77; we need to scale and standardize the data for RNN network. This will be done once we split the train/test data set. Now, let’s define a function to create the time series data set. [have specified a look back interval (60 time steps) and the predicted column. RNN works based on time steps. If we are making 60 time steps which means that, for making future forecast our RNN will observe previous 60 time steps and every time it will predict the output, it will check previous 60 time steps. So, we need to create the data structure accordingly. def ts (a, look back = 60, pred_col = 4): £ = a-copy() ‘ange(1, len(t)+2) t= teilocl:-Look’ back, #] teset_index('id’, inplace =True) pred_value = a.copy() pred_value = pred value.iloc{look back:, pred col] pred value.colunns = ["Pred” pred_value = pd.DataFrame(pred_value) pred_value["id"] = range(1, Len(pred_value)+1) pred_value.set_index(‘id", inplace = True) Final_df= pd.concat([t, pred_value], axi: 1) return final_df onitwatnancenn omtaecapaictalindcaoreatgsim-rnnuratntesminaSZec 086GAULT Pree Sek sy Sr Mat | Tomes Da Seance arr_df = ts(values, 60,4) arr_dF.fillna(o, inplace rue) arr_dF.columns = ['vi(t-60)", *v2(t-60)', ‘v3(t-60)", "va(t-60)", 'v3(t-60)', ‘vo(t-60)", ‘v7(t-60)", “va(t-60)', 'vo(t-60)', "vi0(t-60)", "vi1(t-60)", "vi2(t-60)"s 'v13(t-00)", "v14(t-60)", “v15(t-60)", ‘v16(t-60)", vI7(¢-60)", "v18(t-60)", “vi9(t-60)", "v20(t-60)", 'v2i(t-60)", “v22(t-60)", “vICt)"] 10 print(arr_df.head(2)) vA(t-60) va(t-60) _v3(t-60) vai(t-60) v2a(t-60) —_vatt) i ee 1 112,800003 116.587997 110.403999 11. 116,587997 115.587997 -0.048071 2 4221959900 172950009 114.a00002 1. 117.061996 116.220001 0.034573 2 1u7la1e9 117,91999@ 199.190097 1! 41S.408772 112.404e01 @.020241 [4 rows x 23 columns] Above is first 4 rows of the transformed data set. We can see the 22 input variables (input series) and the 1 output variable (Close Price). If we check the new shape of data, we can see the difference of 60 time steps. 1 print(arr_df.describe()) vi(t-62) —v2(t-60) ...—_v22(t-60) vac) count 49@7.220000 4907.000000 .., 4907.000000 4907.000000 mean 304,257782 308.236847 ... 302.685883 0.001152 std 195.572800197.573212 .., 195198288 0.059529, onitwatnaancenn omtanoapaitalidcor-agsrnnuramtresminaSZec® 08min -48,980002 50.959999 ... 49,1800 25% 105.320000 107.199997 .., 103.868000 50% 276.000000 © 282.200012 ... 273.660004 75% —-445.425003 450,599991 .., 443.574997 max -744,950012 752.8497... 738.599976 [8 rows x 23 columns] 1 arr_df.shape (4907, 23) Here we split the data-set into train and test sets. Then splits the train and test sets into input and output variables. Finally, the inputs are reshaped GAULT Pree Sek [Sra | Tomes Daa Seance -0.795261 -0.009259 2.00000 0.010135 3.861170 into the 3D format expected by LSTMs, namely [samples, time-steps, features]. We will fit the model on 90% of the data, then evaluate it on the remaining data. val = arr_df.values train_sample =int(len(a)*9.8) train = val[: train_sample, test = val[train_sample:, :] print(train.shape, test. shape) (3073. 23) (93a, 22) onitwatnaancenn omtancopaiectalindcaoreatgrerhularatn tne sminaSZeec # split into train and test sets1 train GRU sToPreseSe ps [Sra | Tomes Da Seance pd.patarrane(train) 2 test = pd.DataFrame(test) Train/test split Sequence of values are crucial with time-series data. So, we have split the data in train and test test using a systematic approach as below. Also, normalization is required to fit the data for neural network architecture learning. 1% y = train, test 2 ton sklearn. preprocessing inport Minvaxscaler 3 scaler = Hinaxscaler() 4x scalor.fit transform) 5 print (x) 6 print(’\n") 7 print(x. shape) [2.00287242 0,09250183 0.09558799 ... 0.09640107 [o.10745189 0.19258018 0.10141853 ... 0.09709395 [e.1e02i956 0.09520957 0.0928215 ... 0.ese0e99, [o.aoe6oe46 0.83337855 0.8128715 ... 0.8351234 onoworhtscencncrawomning.nthical iors bintlaramineomineSZee38 2.09632485 0,06072869] 0.09724117 0.05039677] 0.09373678 0.05579232] 0.8440718 0.819263 ]swanaeet ‘cnc ao ng RUST Pe Sk ps ty Sea |Tnm Dala Sr [w.uspausa v.asesviae wrecideos -- Uruceadoue OreLsa0be, B.eLz00es | [0,8120165 @,80210584 0.129445... 0,81777245 0.80882484 0, 80993587] } (x10, 23) Shaping data for LSTM Here I have taken a look back period of 60 days where the model will look through last 60 time steps to predict future price, ‘Shaping train data 1. & shaping data from neural. network 2 xsrain =f] 3 yitrain = [] 4 for 4 in range(oo, x.shapefe]): 5 | xtrain.append(x[i-00:3]) 6 8 ° y_train.append(x[i,0}) igo print (x train) print(‘\n") 10 print(y_train) u print() [array([[0.08287242, 0.09350183, 0.09558759, ..., 9,09640107, 0.09632845, 0.06072868], [o.10745189, 0.10259018, 0.20241853, ..., 0.09700395, 0.09724117, 0.05039077], [0:10023956, 0.09529957, @.09289125, ..., @.09480888, 0.00373678, 0.055792321, onitwatnancenn ortega indore rnnaratntesmin Ze‘moot cause rae Sek pe 1S [e-d6s13948, 0,06245994, 006523306, ..., @.05037507, 0.0627368 , 0.0282226 |, [o.06380%67, 0.061g0004, 0.06524078, ..., 0.06161238, 0.06241768, 0.02054903], [0:05075165, 0.06063529, 0.05126433, ..., 0.05836065, 0.05696384, 0.01951583]], dtype-float32)] [0.05072269] [array({[0.09287242, 0,09350183, 0.09558799, ..., 9.09640107, 0,09632845, 0.06072868),, [o.10745189, 0.10258018, 0.20241853, ..., 0.09700395, 0.09728117, 0.050396771, [9:10021956, 0.09520957, 0.00382125, ..., 0.094z0888, 0.09373678, 0.05579232],, 1 import numpy as_np 2 Xtrain, y train = np.array(x train), np.array(y train) 2 print(x_train.shape, y_train.shape) (3913, 60, 23) (3913,) LSTM model ‘We now create the LSTM model with 3 LSTM layers and one Dense layer. I have defined the LSTM with 75 neurons in the first hidden layer, 30 neuron onitwatnancenn omtaecapaictalindcaoreatgsim-rnnuratntesminaSZec 208utr rede Sock py SMa Tomas Da Sarce in the 2nd and 3rd layers; 1 neuron in the output layer for predicting price. The input shape will be 60 time step with 22 features. ‘The model is compiled with Mean Absolute Error (MAE) loss function and the efficient Adam version of stochastic gradient descent. create model rodel_lstn ~ £f.keras.Sequantial() nodel_stn.2d4(tF-keras.layers.isin(units = 75, return sequences = True, | lnput_shape = (train. shapes], train shepe[2]))) rodel_1stn.add(tF.keras. layers. usth(units = 30,netumn. sequences wodel_stu-add( tf keras.layers.usTH(units = 20, ture sequences odel_1stu.add( tf. keras- layers.Dense(units = 1)) 5 model_Astn-canptle(losso"aae’, eptimlzer='aan’) 10 togel stn. sumary() del: "sequential Terr) ape S~SCi ra ine (sm) (Wore, 68, 75) sere esis Cre, OSC E ‘ Trainable garans! 49,772 on-trainable parame: ® onitwatnancenn omtaecapaictalindcaoreatgsim-rnnuratntesminaSZec 6‘moot “enc nati and GAUL STAs Pree! Sk pe by Sat Maka Tamme Oat sence Prepare test data Once the model is fit on training data, we want to see the skill of the model on out-of-sample data. Likewise we did in training set, we will prepare the test set and we need to pick up look-back value from training data. The look-back is the number of previous time steps to use as input variables to predict the next time period (60 in this case). look_back = train.tail(60) data = look back.append(test) print (data) inputs = scaler. transform(data) inputs 1 shaping data from neural. network 2 Xtest = [] 3 ytest = [] 4 for i in ronge(60, inputs.shape[o]): 5X test -append(inputs[i-c0:i]) © y_test.-append(inputs[,0]) 7 Pico: 8 | printQctest) onitwatnancenn omtanca atrial rrnnarantresminaSZec 08exude Sis pe 1S ° prant(-\n") 10 print(y_test) a print() [array ([[0.75038385, 0.7414838 , 0.75509155, ..., 0.7532028 , 0.7458734 , 0.810653 ], [e.74134344, '0.75010335, 0.7566965 , ..., 0.7624983 , 0.761013 , 8.792429 ], [o.75425833, '0.7528816 , 0.75888515, . (0,7905635 ], ©.7590191 , 0.752105 [e-80060846, 0.93337055, 0.812715 , ..., 0.8351234 , @.8aao7i8 , 0.819263 |, [2.836831 ,'0.82639736, 0.8230852 , ..., 0.82810906, 0.813464 , eceiz08e3 ], [e-siz0iss ,’0.so210584, 0.8120885 , ..., 0.81777245, .808s2484, ,80993587]], dtype-float32)] [o.#106523] [array({[0.75036385, 0.7414838 , 0.75509155, ..., 0.753202 , 0.7458734 , 0.8106533 J, {0.78134344,”0.75010335, 0.7565905 , ..., 0.762983 , 0.7619013 , 0.792429 j, [0.75425833,'0.7526816 , 0.75868515, ..., 0.7590191 , 0.752105 , 1 x test, y test = np.array(x test), np.array(y test) 2 printQ_test.shape, y_test.shape) (924, 60, 23) (924,) onitwatnancenn omtaecapaictalindcaoreatgsim-rnnuratntesminaSZec m8onitwatnancenn orton aictralindcaoreatgsi-rnnaratnte smite exude Sisk ey ‘The model is fitted with 20 training epochs with a batch size of 32. 1 Fit network 2 history. Istm = model_Isim.Fit(x train, y_ train, 2 epochs = 20, 4 beteh size = 22, 5 validation data 6 shuffle-False) Epoch 1/26 2 1 1 ¥ x 1 Epoch 9/20 wa3/a2s [ Epoch 10/20 23/123 [ x Epoch 13/20 3/23 [ Epoch 12/20 33/123 [ Epoch 13/20 w/a L Epoch 14/20 was/i2s Epoch 15/20 x test, sgns/step ~ loss hans/step ~ Toss: aams/step ~ loss sams/step ~ loss: aams/step - loss: sans/step ~ loss: szms/step ~ Loss 15 ins/step - loss sans/step ~ Loss sans/step ~ loss. 4s uns/step - Loss: 15 tns/step ~ Loss 4s tams/step ~ Loss 1s 1ims/step - loss: y.test), 0.0609 o.1018 0.0839 + 9.0708 0.0598 0.0863 0.0555 0.0574 + 0.0898 0.0825 val_oss: val toss: val_toss: val_toss: val_toss: val_oss: val_losst val_toss: val_losst val toss: val toss: val toss: val_toss: val toss: 0.2502 0.2340 2087 east 0.1688 0.0569 esas eae oan + ons‘enc inst and GAUL ETN ret ik py at Tonnes Dat Sore GRU GRU network is created with same parameters and configuration as LSTM. steer «tear goal) ‘Siege teasers, return sence «rat, pusher trae shape}, ain. sapl)9) Setar sit seme cnatn rene) ‘Sie ao thera Lessee) ele copa etna mre a, BF oar rr a ae Fit network 2 gru history = model_gru.fit(x train, y train, epochs = 20, batch size = 64, a validation data = (x'test, y_test), shuffle-False) |] - 25 250s/step ~ loss: 0.0566 - val_loss: 0.1555 |] = 26 tans/stap - loss: o.0901 - val Joss: 0.2612 |] = 45 1ans/step - loss: 0.0515 - val loss: 0.0218GRU Pree Se po[ySr Mat | Tome Data Seance 62/62 | 1S laws/step ~ loss: 0.0526 - val toss: ©2014 Epoch e2/ez = as aans/step - loss: @.0251 - val_loss: 9.0145 Epoch 62/62 45 tans/step ~ loss: 0.0255 - val_loss: 0.1160 Epoch 62/62 45 tns/step ~ loss: 0.0780 - val loss: 0.0258 Epoch 62/02 45 14ns/step loss: 0.0116 - val loss: 0.1269 pach 62/62 35 14ms/step - losst 0.0235 - val_loss: 0.0340 Epoch 62/62 415 tns/step ~ loss: 0.0199 - 0.0993 Epoch 62/62 45 taus/step ~ loss: 9.0718 - val loss: 0.0752 Epoch 2/02 35 1ans/step ~ loss: 0.0176 - val loss: 0.1015 Epoch 62/62 = as a4ns/step - loss: 0.0422 - val_loss: 0.0648 Epoch 2/62 = 15 aams/step - loss: 0.0256 - val_loss: 0.0812 Epoch 62/62 45 tns/step - loss: 9.0828 - val loss: 0.0168 Epoch 62/62 45 tuns/step - loss: 0.0275 - val_loss: 0.0757 Epoch Here, both LSTM and GRU had the same architecture but the number of parameters in LSTM is 49, 771 whereas GRU in GRU is 37,741 Total params: 49,771 Trainable parans: 49,771 Non-trainable params: @ onitwatnancenn omtaecapaictalindcaoreatgsim-rnnuratntesminaSZecGAULT Pree Se ps[ySrt Mat | Tomes Da Seance Total params: 37,741 trainable params: 37,741 Non-trainable params: @ As we already discussed the differences, GRU with two gates compared to LSTM that has three gates. GRU has fewer parameters it is computationally more efficient than LSTM. Diagnostic Plots ‘The training history of both LSTM & GRU models are used to diagnose the behavior of the models. I have created a single plot for the ease of convenience. plt.figure(Figsize-(10, 6), dpi-100) plt.plot (history _Istm.history['loss'], Labels'LSTM train’, color="red") plt.plot(history_Istu-history[ 'val_loss'], label="LsT™H test’, color= ‘green") plt.plot(gru_history.history['loss*], label="oRU train’, color="brown") plt.plot(gruhistory.history[ ‘val loss], label='oRu test", color="blue") plt.xlabel("epochs") pit.ylabel("oss") plt.legend() plt.title( ‘Training and validation loss") plt.show() onitwatnancenn omtaecapaictalindcaoreatgsim-rnnuratntesminaSZec se“enc nati and GAUL STs Pree! Sk pe by Sat Maka Tamms Cat sence ‘aining and Validation oss ‘We can see that, in both cases the training losses is considerably lower than validation losses indicating models may have under-fitting the training data. The performance may be improved by increasing the epochs which can be experimented. LSTM Prediction onitwatnancenn omtaecapaictalindcaoreatgsim-rnnuratntesminaSZec 6This gives us the scale down values of prediction; we inverse the scale and need to bring these values to normal scale. Let us find out the scaling level of our data. exude Sisk ey 1 scaler.scale_ array([e.00142499, 0.00142472, 0.00145909, 0.00145049, 0.214767, 0.16911027, 0.01628664, 9.00942507, 2.00138611, 0.00143893, @.00140408, 0.010247, 0.01 » @,00137255, 0.0012019 0.00152457, 0,00150257, 0.141036 , 0.00132141, 0.00133131, @,00146176, 0.00145049], dtype=float32) Here, the Ist value is the Open price; to bring this to normal scale, we divide this value by 1 as shown below. 1 normal_scale = 1/0.00143499 2 normal_scale 696.8689677279981 y_pred = y pred * normal_scale onitwatnancenn omtaecapaictalindcaoreatgsim-rnnuratntesminaSZec sa‘moot “enc nati and GAULT Pree! Sk pe by Sat Maka Tamms Oat sence y_test = y test * normal_scale Evaluate Model Now we can forecast for the entire test data-set combining the forecast with the test data-set and invert the scaling. ith forecasts and actual values in their original scale, we can calculate an error score ( Root Mean Squared Error — RMSE) for the model. RMSE gives error in the same units as the variable itself. ‘The accuracy can be calculated by taking the average of observed values and predicted values and dividing them with each other. 1 mean_y test = y test.mean() 2 mean_y pred = y_pred.mean() 2 print(mean_y_test, mean_y pred) 524.4402 599..86346 1 accuracy = round((mean_y test / mean_y pred)*109,2) 2 accuracy 87.43 onitwatnancenn omtaecapaictalindcaoreatgsim-rnnuratntesminaSZec se‘moot “enc nati and GAUL STAs Pree! Sk pe by Sat Maka Tamme Oat sence We can perform the similar exercise on GRU model. Moreover, performance of the models can be improved by adding more time variants e.g. adding Weeks (day of weeks, week no.), Months (month no.) and Year Columns, time lags etc. and also adjusting the hyper- parameters and epochs. There are several types of motivation and data analysis available for time series which are appropriate for different purposes and etc. Conclusion The key difference between a GRU and an LSTM is that a GRU has two gates (reset and update gates) whereas an LSTM has three gates (namely input, output and forget gates). GRU network is simpler and thus easier to modify, for example adding new gates in case of ad ional input to the network. It’s just less code in general. However, if sequence is large or accuracy is very critical, would recommend LSTM Ican be connected here. Sign up for The Daily Pick Monitwatuaanc enn omtancapaiectralindcaoreagsirnnuarantesminaSZec 16exude Sisk ey 8 Toward Data Seance Hands-on real-world examples, research, tutorials, and cutting-edge techniques
You might also like
Data Aggregation
PDF
No ratings yet
Data Aggregation
68 pages
PD 2 Preview
PDF
0% (3)
PD 2 Preview
32 pages
The RSI Delta Indicator. Enhancing Momentum Trading
PDF
No ratings yet
The RSI Delta Indicator. Enhancing Momentum Trading
21 pages
Creating A Modified Fisher Transformation For Profitable Trading.
PDF
No ratings yet
Creating A Modified Fisher Transformation For Profitable Trading.
21 pages
Importing High Quality Tick Data On MetaTrader 4 & 5
PDF
No ratings yet
Importing High Quality Tick Data On MetaTrader 4 & 5
34 pages
How To Export Data From Quant Data Manager and Import To Metatrader 5
PDF
No ratings yet
How To Export Data From Quant Data Manager and Import To Metatrader 5
18 pages
Python Programming123uo00es0440
PDF
No ratings yet
Python Programming123uo00es0440
405 pages
Test Strategy in MetaTrader 4 With Tick Precision
PDF
0% (1)
Test Strategy in MetaTrader 4 With Tick Precision
15 pages
Libro Nuevo ML
PDF
No ratings yet
Libro Nuevo ML
577 pages
Trading Strategy - Technical Analysis With Python TA-Lib
PDF
No ratings yet
Trading Strategy - Technical Analysis With Python TA-Lib
12 pages
Panda Python
PDF
100% (1)
Panda Python
398 pages
Python Scikit Learn PDF
PDF
No ratings yet
Python Scikit Learn PDF
113 pages
Py Spark
PDF
No ratings yet
Py Spark
427 pages
Py Spark
PDF
No ratings yet
Py Spark
427 pages
The Manual: Openair
PDF
No ratings yet
The Manual: Openair
224 pages
CDO User Guide: Uwe Schulzweida - MPI For Meteorology
PDF
No ratings yet
CDO User Guide: Uwe Schulzweida - MPI For Meteorology
222 pages
OpenAir Manual PDF
PDF
No ratings yet
OpenAir Manual PDF
287 pages
Deep Down About d2l
PDF
No ratings yet
Deep Down About d2l
922 pages
CDO User Guide New PDF
PDF
No ratings yet
CDO User Guide New PDF
216 pages
Hidden Divergence - Chamane's Guidelines
PDF
100% (1)
Hidden Divergence - Chamane's Guidelines
13 pages
OpenAir Manual
PDF
No ratings yet
OpenAir Manual
287 pages
Dive Into Deep Learning
PDF
No ratings yet
Dive Into Deep Learning
972 pages
Dive Into Deep Learning
PDF
No ratings yet
Dive Into Deep Learning
883 pages
d2l en PDF
PDF
No ratings yet
d2l en PDF
996 pages
StatisticsMachineLearningPythonDraft PDF
PDF
100% (1)
StatisticsMachineLearningPythonDraft PDF
223 pages
Pyspark PDF
PDF
100% (1)
Pyspark PDF
397 pages
Using Machine Learning To Locate Support and Resistance Lines For Stocks
PDF
No ratings yet
Using Machine Learning To Locate Support and Resistance Lines For Stocks
14 pages
Dive Into Deep Learning
PDF
No ratings yet
Dive Into Deep Learning
894 pages
Heiken-Ashi Trading - The Full Guide in Python
PDF
100% (2)
Heiken-Ashi Trading - The Full Guide in Python
14 pages
Gap Trading. An Introduction & Back-Test in Python
PDF
No ratings yet
Gap Trading. An Introduction & Back-Test in Python
15 pages
Algorithmic Trading Models - Breakouts
PDF
No ratings yet
Algorithmic Trading Models - Breakouts
10 pages
Estimate Support and Resistance of A Stock With Python
PDF
No ratings yet
Estimate Support and Resistance of A Stock With Python
18 pages
Using Machine Learning To Locate Support and Resistance Lines For Stocks - by Suhail Saqan - The Startup - Jan, 2021 - Medium
PDF
No ratings yet
Using Machine Learning To Locate Support and Resistance Lines For Stocks - by Suhail Saqan - The Startup - Jan, 2021 - Medium
14 pages
The Augmented Bollinger Bands
PDF
No ratings yet
The Augmented Bollinger Bands
23 pages
Orange 3
PDF
100% (1)
Orange 3
46 pages
Pandas: Powerful Python Data Analysis Toolkit: Release 0.7.1
PDF
No ratings yet
Pandas: Powerful Python Data Analysis Toolkit: Release 0.7.1
283 pages
POA - Tracker
PDF
No ratings yet
POA - Tracker
60 pages
How To Trade Forex Using Roboforex Strategyquant Software
PDF
No ratings yet
How To Trade Forex Using Roboforex Strategyquant Software
44 pages
Time Series Forecasting With 2D Convolutions
PDF
No ratings yet
Time Series Forecasting With 2D Convolutions
33 pages
How I Created A Bitcoin Trading Algorithm Using Sentiment Analysis With A 29% Return
PDF
No ratings yet
How I Created A Bitcoin Trading Algorithm Using Sentiment Analysis With A 29% Return
10 pages
Machine Learning With Python/Scikit-Learn: - Application To The Estimation of Occupancy and Human Activities
PDF
No ratings yet
Machine Learning With Python/Scikit-Learn: - Application To The Estimation of Occupancy and Human Activities
113 pages
Building A Stock Option Valuation Model With Python Part II
PDF
No ratings yet
Building A Stock Option Valuation Model With Python Part II
18 pages
Cdo-1 9 6
PDF
No ratings yet
Cdo-1 9 6
215 pages
Building A Stock Option Valuation Model With Python Part I
PDF
No ratings yet
Building A Stock Option Valuation Model With Python Part I
17 pages
A Review of Reinforcement Learning For Financial Time Series Prediction and Portfolio Optimization
PDF
No ratings yet
A Review of Reinforcement Learning For Financial Time Series Prediction and Portfolio Optimization
38 pages
Timetk Functions
PDF
No ratings yet
Timetk Functions
185 pages
Null
PDF
No ratings yet
Null
25 pages
Group7 Report
PDF
No ratings yet
Group7 Report
10 pages
RNN LSTM Example Implementations With Keras TensorFlow
PDF
No ratings yet
RNN LSTM Example Implementations With Keras TensorFlow
20 pages
Regression Linaire Python Tome I
PDF
No ratings yet
Regression Linaire Python Tome I
9 pages
Ip Project File
PDF
No ratings yet
Ip Project File
50 pages
Zero To Deep Learning
PDF
100% (4)
Zero To Deep Learning
753 pages
Python For DS Cheat Sheet
PDF
100% (2)
Python For DS Cheat Sheet
6 pages
Mastering Advanced Time Series Techniques - by Sadrach Pierre, Ph.D. - DataDrivenInvestor
PDF
No ratings yet
Mastering Advanced Time Series Techniques - by Sadrach Pierre, Ph.D. - DataDrivenInvestor
10 pages
Data Wrangling With Python and Pandas
PDF
No ratings yet
Data Wrangling With Python and Pandas
7 pages
Learning Apache Spark With Python: Wenqiang Feng
PDF
No ratings yet
Learning Apache Spark With Python: Wenqiang Feng
8 pages
Zero To Deep Learning With Keras and Tensorflow Compress
PDF
No ratings yet
Zero To Deep Learning With Keras and Tensorflow Compress
769 pages
TOBo ML
PDF
No ratings yet
TOBo ML
135 pages
Pandas Cheat Sheet - Python For Data Science
PDF
No ratings yet
Pandas Cheat Sheet - Python For Data Science
5 pages
DWDW Practical Writeup (4-4-24)
PDF
No ratings yet
DWDW Practical Writeup (4-4-24)
9 pages
2324 BigData Lab3
PDF
No ratings yet
2324 BigData Lab3
6 pages
Project Report
PDF
No ratings yet
Project Report
37 pages
Bitcoin Prise Using LSTM - Ipynb - Colab
PDF
No ratings yet
Bitcoin Prise Using LSTM - Ipynb - Colab
49 pages
Project ML Code
PDF
No ratings yet
Project ML Code
132 pages
Exercise - 6: DS203-2024-S1 Problem1:: Statistics
PDF
No ratings yet
Exercise - 6: DS203-2024-S1 Problem1:: Statistics
10 pages
MLbook Extract
PDF
No ratings yet
MLbook Extract
14 pages
Ameya Yamini Linear Regression Doc
PDF
No ratings yet
Ameya Yamini Linear Regression Doc
15 pages
STL
PDF
No ratings yet
STL
2 pages
Effective Pandas Patterns For Data Manipulation Treading On Python Matt Harrison Independently Published 2021
PDF
No ratings yet
Effective Pandas Patterns For Data Manipulation Treading On Python Matt Harrison Independently Published 2021
40 pages
Pandas Notes
PDF
No ratings yet
Pandas Notes
27 pages
Data Analysis Using R and Python
PDF
No ratings yet
Data Analysis Using R and Python
99 pages
OpenAir Manual
PDF
No ratings yet
OpenAir Manual
230 pages
Data Handling Module
PDF
No ratings yet
Data Handling Module
10 pages