forked from sumittttttt/Stock-market-prediction-and-screener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebapp.py
1140 lines (946 loc) · 41.4 KB
/
webapp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#webapp python script
#Streamlit webapp
#importing packages
import time
import pandas as pd
import numpy as np
import tensorflow as tf
import random as rn
import yfinance as yf
import streamlit as st
import datetime as dt
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from sklearn.preprocessing import MinMaxScaler
np.random.seed(1)
tf.random.set_seed(1)
rn.seed(1)
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM
from sklearn.metrics import mean_squared_error, mean_absolute_error,r2_score
from st_aggrid import AgGrid
from st_aggrid.grid_options_builder import GridOptionsBuilder
from finta import TA
import ta
import sqlite3
import talib
from patterns import candlestick_patterns
from functions import *
from millify import millify
from annotated_text import annotated_text
#connecting database
connection = sqlite3.connect('stock_price.db')
cursor = connection.cursor()
#streamlit page config
st.set_page_config(page_title="Predict Stocks", layout="wide", page_icon=":chart_with_upwards_trend:")
hide_menu_style ="""
<style>
footer{visibility:hidden;}
</style>
"""
st.markdown(hide_menu_style, unsafe_allow_html=True)
# getting symbols/tickers
csv = pd.read_csv('symbols.csv')
symbol = csv['Symbol'].tolist()
for i in range(0, len(symbol)):
symbol[i] = symbol[i] + ".JK"
#sidebar features
feature = st.sidebar.radio(
"Choose feature",
('Home','Fundamental Info','Technical Indicators','Screener','Next-Day Forecasting','Pattern Recognition'))
#feature 1 - Home
if(feature =="Home"):
#st.image("images/logo.png", width=460)
st.markdown(" ### Predict Stocks is the all in one financial website for the retail investors where retail investors can take a look at all Fundamental Information, Technical Indicators, Screeners, Pattern Recognition and Next-Day Forecasting of all the National Stock Exchange (NSE) listed stocks.")
st.markdown('#### Features of the Machine Learning based Web-app:')
st.markdown('##### - One can gain information of all the stocks that are listed on National Stock Exchange (NSE) (To Be Exact - 1773 Companies). ')
st.markdown('##### - Webapp have features like Fundamental Information, Technical Indicators, Screener, Pattern Recognition, Next-Day Forecasting (Machine Learning based).')
st.markdown('##### - Database ready.')
st.markdown('##### - Mobile responsive webapp.')
st.markdown('##### - Webapp comes with Custom Theming and Light and Dark mode.')
st.markdown('#### Details related to stock prices:')
st.markdown('##### - All the historical prices for last 10 years are taken from Yahoo Finance and then further pushed to SQLIte database.')
st.markdown('##### - Time period for stock prices is one day.')
st.markdown('#### Fundamental Information feature overview:')
st.markdown(' ##### - It contains all the information related to company. ')
st.markdown(' ##### - Historical prices of the company of last 10 years with candlestick and line chart.')
st.markdown(' ##### - One can download historical prices too.')
st.markdown(' ##### - All quarterly and annually Financial Results, Balance Sheet, Cash Flow and Splits & Dividends')
st.markdown('#### Technical Indicators feature overview:')
st.markdown(' ##### - It contains all the famous indicators that traders and investors use while investing.')
st.markdown(' ##### - More than 11 indicators are there.')
st.markdown('#### Technical Screener feature overview:')
st.markdown(' ##### - It contains all the important parameters or metrics that are related to company.')
st.markdown(' ##### - It also contains one more important feature, that is it give signals whether selected stock is breaking out or not. Traders do this manually by looking at candles.')
st.markdown(' ##### - With breaking out it also gives signals for consolidating or not.')
st.markdown('#### Pattern Recognition feature overview:')
st.markdown(' ##### - It is important feature of the webapp.')
st.markdown(' ##### - Traders do this manually by looking at candles.')
st.markdown(' ##### - We automate this thing by scanning all the candlestick patterns for the selected stock, then it generates signals whether it is bullish or bearish')
st.markdown('#### Next-Day Forecasting feature overview:')
st.markdown(' ##### - We can say it is a star feature of the webapp.')
st.markdown(' ##### - We have build the efficient Machine Learning model to predict the next day price.')
st.markdown(' ##### - Our model trained on past 5 years of historical data and while predicting it looks for past 2 months ton predict next-day price.')
if (feature == "Fundamental Info"):
st.title('Fundamental Information')
ticker = st.selectbox(
'Enter or Choose NSE listed Stock Symbol',
symbol)
stock = yf.Ticker(ticker)
#company info
info = stock.info
st.subheader(info['longName'])
st.markdown('**Sector**:' + info['sector'])
st.markdown('**Industry**: ' + info['industry'])
st.markdown('**Phone**: ' + info['phone'])
st.markdown(
'**Address**: ' + info['address1'] + ', ' + info['city'] + ', ' + info['zip'] + ', ' + info['country'])
st.markdown('**Website**: ' + info['website'])
with st.expander('See detailed business summary'):
st.write(info['longBusinessSummary'])
# closing price chart
st.subheader('Stocks Prices')
st.write("Enter period to check closing price of ", ticker)
#getting date input
min_value = dt.datetime.today() - dt.timedelta(10 * 365)
max_value = dt.datetime.today()
start_input = st.date_input(
'Enter starting date',
value=dt.datetime.today()- dt.timedelta(90),
min_value=min_value, max_value=max_value, help='Enter the starting date from which you have to look the price'
)
end_input = st.date_input(
'Enter last date',
value=dt.datetime.today(),
min_value=min_value, max_value=max_value, help='Enter the last date till which you have to look the price'
)
#retrieving data from database
cursor.execute("SELECT * from stock_price where symbols= (?) and Date > (?) and Date < (?)",
(ticker, start_input, end_input))
hist_price = pd.DataFrame(cursor.fetchall(),
columns=['Date', 'symbols', 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume'])
hist_price['Date'] = pd.to_datetime(hist_price['Date'])
@st.cache
def convert_df(df):
# IMPORTANT: Cache the conversion to prevent computation on every rerun
return df.to_csv().encode('utf-8')
historical_csv = convert_df(hist_price)
st.download_button(
label="Download historical data as CSV",
data=historical_csv,
file_name='historical_df.csv',
mime='text/csv',
)
#radio button ro switch between style
chart = st.radio(
"Choose Style",
('Candlestick', 'Line Chart'))
st.write('<style>div.row-widget.stRadio > div{flex-direction:row;}</style>', unsafe_allow_html=True)
if (chart == 'Line Chart'):
# line chart plot
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=hist_price['Date'],
y=hist_price['Adj Close'],
name='Closing price'
)
)
fig.update_layout(
title = {
'text': 'Stock Prices of ' + ticker,
'y': 0.9,
'x': 0.5,
'xanchor': 'center',
'yanchor': 'top'}, height = 600, template = 'gridon')
fig.update_yaxes(tickprefix='₹')
st.plotly_chart(fig, use_container_width=True)
if (chart == 'Candlestick'):
fig = go.Figure()
fig.add_trace(
go.Candlestick(
x=hist_price['Date'],
open=hist_price['Open'],
high=hist_price['High'],
low=hist_price['Low'],
close=hist_price['Close'],
name='OHLC'
)
)
fig.update_layout(
title = {
'text': 'Stock Prices of ' + ticker,
'y': 0.9,
'x': 0.5,
'xanchor': 'center',
'yanchor': 'top'}, height = 600, template = 'gridon')
fig.update_yaxes(tickprefix='₹')
st.plotly_chart(fig, use_container_width=True)
#quarterly results
st.subheader('Quarterly Result')
st.write('A quarterly result is a summary or collection of unaudited financial statements, such as balance sheets, income statements, and cash flow statements, issued by companies every quarter (three months).')
quarterly_results = stock.quarterly_financials
quarterly_results.columns = quarterly_results.columns.date
quarterly_results.dropna(axis=0, inplace=True)
quarterly_results = quarterly_results.astype('int64')
for i in quarterly_results.columns:
quarterly_results[i] = quarterly_results.apply(lambda x: "{:,}".format(x[i]), axis=1)
st.dataframe(quarterly_results.style.highlight_max(axis=1, color='lightgreen'),height=1000, width=2000)
#profit and loss
st.subheader('Profit & Loss')
st.write("A profit and loss (P&L) statement is a annually financial report that provides a summary of a company's revenue, expenses and profit.")
financials = stock.financials
financials.columns = financials.columns.date
financials.dropna(axis=0, inplace=True)
financials = financials.astype('int64')
for i in financials.columns:
financials[i] = financials.apply(lambda x: "{:,}".format(x[i]), axis=1)
st.dataframe(financials.style.highlight_max(axis=1,color='lightgreen'),height=1000)
#balance sheet
st.subheader('Balance Sheet')
st.write("A balance sheet is a financial statement that reports a company's assets, liabilities, and shareholder equity.")
balance = stock.balance_sheet
balance.columns = balance.columns.date
balance.dropna(axis=0, inplace=True)
balance = balance.astype('int64')
for i in balance.columns:
balance[i] = balance.apply(lambda x: "{:,}".format(x[i]), axis=1)
st.dataframe(balance.style.highlight_max(axis=1,color='lightgreen'),height=1000)
#cash flow
st.subheader('Cash Flows')
st.write("The term cash flow refers to the net amount of cash and cash equivalents being transferred in and out of a company.")
cf = stock.cashflow
cf.columns = cf.columns.date
cf.dropna(axis=0, inplace=True)
cf = cf.astype('int64')
for i in cf.columns:
cf[i] = cf.apply(lambda x: "{:,}".format(x[i]), axis=1)
st.dataframe(cf.style.highlight_max(axis=1,color='lightgreen'),height=1000)
#actions
st.subheader('Splits & Dividends')
st.write('')
actions = stock.actions
actions.index = actions.index.date
st.table(actions)
#gettig technical indicators
if (feature == 'Technical Indicators'):
st.title('Technical Indicators')
# creating dropdown
ticker = st.selectbox(
'Enter or Choose',
symbol)
# getting date input
min_value = dt.datetime.today() - dt.timedelta(10 * 365)
max_value = dt.datetime.today()
start_input = st.date_input(
'Enter starting date',
value=dt.datetime.today() - dt.timedelta(180),
min_value=min_value, max_value=max_value, help='Enter the starting date from which you have to look the price'
)
end_input = st.date_input(
'Enter last date',
value=dt.datetime.today(),
min_value=min_value, max_value=max_value, help='Enter the last date till which you have to look the price'
)
# retrieving data from database
cursor.execute("SELECT * from stock_price where symbols= (?) and Date > (?) and Date < (?)",
(ticker, start_input, end_input))
df = pd.DataFrame(cursor.fetchall(),
columns=['Date', 'symbols', 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume'])
df['Date'] = pd.to_datetime(df['Date'])
#graph template
temp_style = st.radio(
"Choose Template Style",
('ggplot2', 'seaborn', 'plotly_white','plotly_dark', 'gridon'))
st.write('<style>div.row-widget.stRadio > div{flex-direction:row;}</style>', unsafe_allow_html=True)
#OVERLAP STUDIES INDICATOR
st.header('OVERLAY INDICATORS')
st.write("Technical indicators that use the same scale as prices are plotted over the top of the prices on a stock chart. ")
st.write('#### Moving Average')
st.write(
"Moving averages (MA) are one of the most popular and often-used technical indicators in the financial markets. In simple word, a moving average is an indicator that shows the average value of a stock's price over a period (i.e. 10 days, 50 days, 200 days, etc) and is usually plotted along with the closing price.")
df_ma = calc_moving_average(df, 14)
df_ma = df_ma.reset_index()
figMA = go.Figure()
figMA.add_trace(
go.Scatter(
x=df_ma['Date'],
y=df_ma['Close'],
name="Prices"
)
)
figMA.add_trace(
go.Scatter(
x=df_ma['Date'],
y=df_ma['sma'],
name='SMA '
)
)
figMA.add_trace(
go.Scatter(
x=df_ma['Date'],
y=df_ma['ema'],
name='EMA '
)
)
figMA.update_layout(legend=dict(
orientation='h',
yanchor='bottom',
y=1,
xanchor='left',
x=0
))
figMA.update_layout(height=600, width=1000, title_text='Closing Price of Stock & Moving Average',
template=temp_style)
st.plotly_chart(figMA, use_container_width=True)
# hma
st.subheader('Hull Moving Average (HMA)')
st.write(
'The Hull Moving Average (HMA) is a directional trend indicator. It captures the current state of the market and uses recent price action to determine if conditions are bullish or bearish relative to historical data.')
df['HMA']= TA.HMA(df, 14)
fig_apo = go.Figure()
fig_apo.add_trace(
go.Scatter(
x=df['Date'],
y=df['HMA'],
name='HMA'
)
)
fig_apo.add_trace(
go.Scatter(
x=df['Date'],
y=df['Close'],
name='Close'
)
)
fig_apo.update_layout(legend=dict(
orientation='h',
yanchor='bottom',
y=1,
xanchor='left',
x=0)
, height=600, title_text='HMA', template=temp_style
)
st.plotly_chart(fig_apo, use_container_width=True)
#bollinger bands
st.subheader('Bollinger Bands')
st.write(
"Bollinger Bands are envelopes plotted at a standard deviation level above and below a simple moving average of the price. Because the distance of the bands is based on standard deviation, they adjust to volatility swings in the underlying price.")
#calculating bollinger bands
df_boll = calc_bollinger(df, 20)
df_boll = df_boll.reset_index()
figBoll = go.Figure()
figBoll.add_trace(
go.Scatter(
x=df_boll['Date'],
y=df_boll['bolu'],
name='Upper Band'
)
)
figBoll.add_trace(
go.Scatter(
x=df_boll['Date'],
y=df_boll['sma'],
name='SMA'
)
)
figBoll.add_trace(
go.Scatter(
x=df_boll['Date'],
y=df_boll['bold'],
name="Lower Band"
)
)
figBoll.add_trace(
go.Scatter(
x=df_boll['Date'],
y=df_boll['Close'],
name="closing price"
)
)
figBoll.update_layout(legend=dict(
orientation='h',
yanchor='bottom',
y=1,
xanchor='left',
x=0)
, height=600, title_text='Closing Price of Stock & Bollinger Band', template=temp_style
)
st.plotly_chart(figBoll, use_container_width=True)
#kama
st.subheader('KAMA Indicator')
st.write(" Kaufman’s Adaptive Moving Average (KAMA) is to identify the general trend of current market price action. Basically, when the KAMA indicator line is moving lower, it indicates the existence of a downtrend. On the other hand, when the KAMA line is moving higher, it shows an uptrend. ")
kama = ta.momentum.KAMAIndicator(df['Close'],20,2,20)
df['kama'] = kama.kama()
df = df.reset_index()
fig_kama = go.Figure()
fig_kama.add_trace(
go.Scatter(
x=df['Date'],
y=df['kama'],
name="KAMA"
)
)
fig_kama.add_trace(
go.Scatter(
x=df['Date'],
y=df['Close'],
name="Close"
)
)
fig_kama.update_layout(legend=dict(
orientation='h',
yanchor='bottom',
y=1,
xanchor='left',
x=0)
, height=600, title_text='KAMA Indicator', template=temp_style
)
st.plotly_chart(fig_kama, use_container_width=True)
## Momentum indiactors
st.header('Momentum Indicators')
st.write('Technical indicator which shows the trend direction and measures the pace of the price fluctuation by comparing current and past values.')
st.subheader('Average Directional Index (ADX)')
st.write(
"ADX stands for Average Directional Movement Index and can be used to help measure the overall strength of a trend. Indicator suggests that a strong trend is present when ADX is above 25 and no trend is present when below 20.")
df['ADX'] = ADX(df, 14)
fig_ADX = go.Figure()
fig_ADX.add_trace(
go.Scatter(
x=df['Date'],
y=df['ADX'],
name='Average Directional Index'
)
)
fig_ADX.update_layout(legend=dict(
orientation='h',
yanchor='bottom',
y=1,
xanchor='left',
x=0)
, height=600, title_text='Average Directional Index', template=temp_style
)
st.plotly_chart(fig_ADX, use_container_width=True)
# aroon
st.subheader('Aroon Indicator')
st.write(
"The Aroon indicator is a technical indicator that is used to identify trend changes in the price of an asset, as well as the strength of that trend. In essence, the indicator measures the time between highs and the time between lows over a time period.The indicator consists of the 'Aroon up' line, which measures the strength of the uptrend, and the 'Aroon down' line, which measures the strength of the downtrend.")
aroon = ta.trend.AroonIndicator(df['Close'], 14)
df['aroon_down'] = aroon.aroon_down()
df['aroon_indicator'] = aroon.aroon_indicator()
df['aroon_up'] = aroon.aroon_up()
data_aroon = df.reset_index()
fig_aroon = go.Figure()
fig_aroon.add_trace(
go.Scatter(
x=data_aroon['Date'],
y=data_aroon['aroon_down'],
name='Aroon Down'
)
)
fig_aroon.add_trace(
go.Scatter(
x=data_aroon['Date'],
y=data_aroon['aroon_up'],
name="Aroon up"
)
)
fig_aroon.update_layout(legend=dict(
orientation='h',
yanchor='bottom',
y=1,
xanchor='left',
x=0)
, height=600, title_text='Aroon Indicator', template=temp_style
)
st.plotly_chart(fig_aroon, use_container_width=True)
#plotting MACD
st.subheader("Moving Average Convergance Divergence (MACD) ")
st.write("Moving average convergence divergence (MACD) is a trend-following momentum indicator that shows the relationship between two moving averages of a stock. The MACD is calculated by subtracting the 26-period exponential moving average (EMA) from the 12-period EMA.")
df_macd = calc_macd(df)
df_macd = df_macd.reset_index()
figMACD = make_subplots(rows=2, cols=1,
shared_xaxes=True,
vertical_spacing=0.01)
figMACD.add_trace(
go.Scatter(
x=df_macd['Date'],
y=df_macd['Close'],
name="Prices"
)
)
figMACD.add_trace(
go.Scatter(
x=df_macd['Date'],
y=df_macd['ema12'],
name='EMA12 '
),
row=1, col=1
)
figMACD.add_trace(
go.Scatter(
x=df_macd['Date'],
y=df_macd['ema26'],
name='EMA26'
),
row=1, col=1
)
figMACD.add_trace(
go.Scatter(
x=df_macd['Date'],
y=df_macd['macd'],
name='MACD Line'
),
row=2, col=1
)
figMACD.add_trace(
go.Scatter(
x=df_macd['Date'],
y=df_macd['signal'],
name='Signal Line'
),
row=2, col=1
)
figMACD.update_layout(legend=dict(
orientation='h',
yanchor='bottom',
y=1,
xanchor='left',
x=0
),height=800,template=temp_style,title_text='Closing Price of Stock & MACD'
)
st.plotly_chart(figMACD, use_container_width=True)
#plotting RSI
st.subheader('Relative Strength Index (RSI)')
st.write("The relative strength index (RSI) is a momentum indicator used in technical analysis that measures the magnitude of recent price changes to evaluate overbought or oversold conditions in the price of a stock or other asset.Traditionally the RSI is considered overbought when above 70 and oversold when below 30.")
df_RSI = RSI(df,14)
df_RSI=df_RSI.reset_index()
fig_RSI = make_subplots(rows=2, cols=1,
shared_xaxes=True,
vertical_spacing=0.01)
fig_RSI.add_trace(
go.Scatter(
x=df_RSI['Date'],
y = df_RSI['Adj Close'],
name='Closing Prices'
),
row=1,col=1
)
fig_RSI.add_trace(
go.Scatter(
x=df_RSI['Date'],
y=df_RSI['RSI'],
name='RSI'
),
row=2,col=1
)
fig_RSI.update_layout(legend=dict(
orientation='h',
yanchor='bottom',
y=1,
xanchor='left',
x=0
),
height=800, width=1000, title_text="Closing Price of Stock & RSI",template=temp_style)
st.plotly_chart(fig_RSI, use_container_width=True)
# TRIX
st.subheader('TRIX Indicator')
st.write(
"The triple exponential average (TRIX) is a momentum indicator used by technical traders that shows the percentage change in a moving average that has been smoothed exponentially three times. "
)
trix = ta.trend.TRIXIndicator(df['Close'], 14)
df['trix'] = trix.trix()
data_trix = df.reset_index()
fig_trix = go.Figure()
fig_trix.add_trace(
go.Scatter(
x=data_trix['Date'],
y=data_trix['trix'],
name='TRIX'
)
)
fig_trix.update_layout(legend=dict(
orientation='h',
yanchor='bottom',
y=1,
xanchor='left',
x=0)
, height=600, title_text='TRIX Indicator', template=temp_style
)
st.plotly_chart(fig_trix, use_container_width=True)
#stc
st.subheader('Schaff Trend Cycle (STC)')
st.write("The Schaff trend cycle indicator is popular for a general trading strategy. The strategy suggests buying when it surges above 25 level and sell when the signal lines go below the 75 leve")
stc = TA.STC(df, 14)
fig_stc = go.Figure()
fig_stc.add_trace(
go.Scatter(
x=df['Date'],
y=stc,
name="Schaff Trend Cycle"
)
)
fig_stc.update_layout(legend=dict(
orientation='h',
yanchor='bottom',
y=1,
xanchor='left',
x=0)
, height=600, title_text='Schaff Trend Cycle', template=temp_style
)
st.plotly_chart(fig_stc, use_container_width=True)
#Volume indicators
st.header('Volume Indicators')
st.write('Trading volume is a measure of how much a given financial asset has traded in a period of time. For stocks, volume is measured in the number of shares traded.Volume indicators are mathematical formulas that are visually represented in the most commonly used charting platforms.'
)
#OBV
# plotting OBV
st.subheader('On Balance Volume (OBV)')
st.write(
"On-balance volume (OBV) is a technical trading momentum indicator that uses volume flow to predict changes in stock price.")
df['obv'] = OBV(df)
fig_OBV = go.Figure()
fig_OBV.add_trace(
go.Scatter(
x=df['Date'],
y=df['obv'],
name='On Balance Volume'
)
)
fig_OBV.update_layout(legend=dict(
orientation='h',
yanchor='bottom',
y=1,
xanchor='left',
x=0)
, height=600, title_text='On Balance Volume', template=temp_style
)
st.plotly_chart(fig_OBV, use_container_width=True)
#volatility indicators
st.header('Volatility Indicators')
st.write('The volatility indicator is a technical tool that measures how far security stretches away from its mean price, higher and lower. ')
# plotting ATR
st.subheader('Average True Range (ATR)')
st.write(
"Average True Range (ATR) is the average of true ranges over the specified period. ATR measures volatility, taking into account any gaps in the price movement.")
df_ATR = ATR(df, 20)
fig_ATR = go.Figure()
fig_ATR.add_trace(
go.Scatter(
x=df_ATR['Date'],
y=df_ATR['ATR'],
name='Average True Range'
)
)
fig_ATR.update_layout(legend=dict(
orientation='h',
yanchor='bottom',
y=1,
xanchor='left',
x=0)
, height=600, title_text='Average True Range', template=temp_style
)
st.plotly_chart(fig_ATR, use_container_width=True)
#short-term forecasting
if (feature == 'Next-Day Forecasting'):
st.subheader('Next-Day Forecasting with Long-Short Term Memory (LSTM)')
# creating sidebar
ticker = st.selectbox(
'Enter or Choose NSE listed Stock Symbol',
symbol, index=symbol.index('ITMG.JK'))
def my_LSTM(ticker):
try:
start = dt.datetime.today() - dt.timedelta(5*365)
end = dt.datetime.today()
df = yf.download(ticker,start,end)
df = df.reset_index()
df['Date'] = pd.to_datetime(df['Date']).dt.date
st.write('It will take some seconds to fit the model....')
data = df.sort_index(ascending=True, axis=0)
new_data = pd.DataFrame(index=range(0, len(df)), columns=['Date', 'Close'])
for i in range(0, len(data)):
new_data['Date'][i] = data['Date'][i]
new_data['Close'][i] = data['Close'][i]
# setting index
new_data.index = new_data.Date
new_data.drop('Date', axis=1, inplace=True)
# creating train and test sets
dataset = new_data.values
train = dataset[0:987, :]
valid = dataset[987:, :]
# converting dataset into x_train and y_train
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(dataset)
x_train, y_train = [], []
for i in range(60, len(train)):
x_train.append(scaled_data[i - 60:i, 0])
y_train.append(scaled_data[i, 0])
x_train, y_train = np.array(x_train), np.array(y_train)
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(x_train.shape[1], 1)))
model.add(LSTM(units=50))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(x_train, y_train, epochs=1, batch_size=1, verbose=2)
st.success('Model Fitted')
# predicting 246 values, using past 60 from the train data
inputs = new_data[len(new_data) - len(valid) - 60:].values
inputs = inputs.reshape(-1, 1)
inputs = scaler.transform(inputs)
X_test = []
for i in range(60, inputs.shape[0]):
X_test.append(inputs[i - 60:i, 0])
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
closing_price = model.predict(X_test)
closing_price = scaler.inverse_transform(closing_price)
# for plotting
train = data[:987]
valid = data[987:]
valid['Predictions'] = closing_price
st.write('#### Actual VS Predicted Prices')
fig_preds = go.Figure()
fig_preds.add_trace(
go.Scatter(
x=train['Date'],
y=train['Adj Close'],
name='Training data Closing price'
)
)
fig_preds.add_trace(
go.Scatter(
x=valid['Date'],
y=valid['Adj Close'],
name='Validation data Closing price'
)
)
fig_preds.add_trace(
go.Scatter(
x=valid['Date'],
y=valid['Predictions'],
name='Predicted Closing price'
)
)
fig_preds.update_layout(legend=dict(
orientation='h',
yanchor='bottom',
y=1,
xanchor='left',
x=0)
, height=600, title_text='Predictions on Validation Data', template='gridon'
)
st.plotly_chart(fig_preds, use_container_width=True)
# metrics
mae = mean_absolute_error(closing_price, valid['Adj Close'])
rmse = np.sqrt(mean_squared_error(closing_price, valid['Adj Close']))
accuracy = r2_score(closing_price, valid['Adj Close'])*100
with st.container():
st.write('#### Metrics')
col_11, col_22, col_33 = st.columns(3)
col_11.metric('Absolute error between predicted and actual value', round(mae,2))
col_22.metric('Root mean squared error between predicted and actual value', round(rmse,2))
# forecasting
real_data = [inputs[len(inputs) - 60:len(inputs + 1), 0]]
real_data = np.array(real_data)
real_data = np.reshape(real_data, (real_data.shape[0], real_data.shape[1], 1))
prediction = model.predict(real_data)
prediction = scaler.inverse_transform(prediction)
st.write('#### Next-Day Forecasting')
with st.container():
col_111, col_222, col_333 = st.columns(3)
col_111.metric(f'Closing Price Prediction of the next trading day for {ticker} is',f' ₹ {str(round(float(prediction),2))}')
except:
st.warning("Oops! you can't go ahead!!")
st.warning("The company you selected is listed newly...so we can't gather data.")
my_LSTM(ticker)
if (feature == 'Pattern Recognition'):
st.title('Pattern Recognition')
st.write('A pattern is identified by a line that connects common price points, such as closing prices or highs or lows, during a specific period of time.')
st.write('Technical analysts and chartists seek to identify patterns as a way to anticipate the future direction of a security’s price.')
st.write('We automated this thing, for a specific ticker/symbol we scan through all the candlestick patterns and generate signals.')
st.markdown('- Neutral - Not such activity or no trendline present at current moment')
st.markdown('- Bullish - The stock is in up trendline ')
st.markdown('- Bearish - The stock is in down trendline')
st.write('#### Select Stock ')
ticker_input = st.selectbox('Enter or Choose NSE listed stock', symbol,index=symbol.index('ITMG.JK'))
#plotiing prices
show = st.radio(
"Show/Hide Prices",
('Show', 'Hide'))
st.write('<style>div.row-widget.stRadio > div{flex-direction:row;}</style>', unsafe_allow_html=True)
# closing price chart
if (show == 'Show'):
st.write("Enter period to check price of ", ticker_input)
# getting date input
min_value = dt.datetime.today() - dt.timedelta(10 * 365)
max_value = dt.datetime.today()
start_inputt = st.date_input(
'Enter starting date',
value=dt.datetime.today() - dt.timedelta(90),
min_value=min_value, max_value=max_value, help='Enter the starting date from which you have to look the price'
)
end_inputt = st.date_input(
'Enter last date',
value=dt.datetime.today(),
min_value=min_value, max_value=max_value, help='Enter the last date till which you have to look the price'
)
# retrieving data from database
cursor.execute("SELECT * from stock_price where symbols= (?) and Date > (?) and Date < (?)",
(ticker_input, start_inputt, end_inputt))
hist_price = pd.DataFrame(cursor.fetchall(),
columns=['Date', 'symbols', 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume'])
hist_price['Date'] = pd.to_datetime(hist_price['Date'])
# radio button ro switch between style
chart = st.radio(
"Choose Style",
('Candlestick', 'Line Chart'))
st.write('<style>div.row-widget.stRadio > div{flex-direction:row;}</style>', unsafe_allow_html=True)
if (chart == 'Line Chart'):
# line chart plot
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=hist_price['Date'],
y=hist_price['Adj Close'],
name='Closing price'
)
)
fig.update_layout(
title={
'text': 'Stock Prices of ' + ticker_input,
'y': 0.9,
'x': 0.5,
'xanchor': 'center',
'yanchor': 'top'}, height=600, template='gridon')
fig.update_yaxes(tickprefix='₹')
st.plotly_chart(fig, use_container_width=True)
if (chart == 'Candlestick'):
fig = go.Figure()
fig.add_trace(
go.Candlestick(
x=hist_price['Date'],
open=hist_price['Open'],
high=hist_price['High'],
low=hist_price['Low'],
close=hist_price['Close'],
name='OHLC'
)
)
fig.update_layout(
title={