1
+ # importing required libraries
2
+ import pandas as pd
3
+ import numpy as np
4
+ import dash
5
+ import dash_core_components as dcc
6
+ import dash_html_components as html
7
+ from dash .dependencies import Input , Output , State
8
+ from datetime import datetime
9
+ import plotly .express as px
10
+ import pandas_datareader as pdr
11
+ from prophet import Prophet
12
+
13
+ # initiate the app
14
+ app = dash .Dash ()
15
+ server = app .server
16
+
17
+ crypcode = pd .read_csv ("crypcode.csv" )
18
+ cryp_value = crypcode ["currency code" ].tolist ()
19
+ cryp_label = crypcode ["currency name" ].tolist ()
20
+ # crypcode.head()
21
+
22
+ todate = datetime .today ().strftime ("%Y-%m-%d" )
23
+ firstdate = pd .to_datetime (todate , format = "%Y-%m-%d" ) - pd .to_timedelta (
24
+ 365 / 4 , unit = "d"
25
+ )
26
+
27
+ # read more about inline-block & flex
28
+ # https://www.geeksforgeeks.org/what-is-the-difference-between-inline-flex-and-inline-block-in-css/
29
+ # read more about padding and margin
30
+ # https://stackoverflow.com/questions/35620419/what-is-difference-between-css-margin-and-padding
31
+
32
+ # format the app
33
+ colors = {"background" : "#222A2A" , "text" : "#479B55" , "box" : "#7F7F7F" }
34
+
35
+ app .layout = html .Div (
36
+ children = [
37
+ html .H1 (
38
+ "Crypto Forecast" ,
39
+ style = {
40
+ "textAlign" : "center" ,
41
+ "paddingTop" : "30px" ,
42
+ "verticalAlign" : "bottom" ,
43
+ },
44
+ ),
45
+ html .H2 (
46
+ "Forecast Commonly Traded Cryptocurrencies with Facebook's Prophet Procedure" ,
47
+ style = {
48
+ "textAlign" : "center" ,
49
+ "paddingTop" : "5px" ,
50
+ "verticalAlign" : "middle" ,
51
+ },
52
+ ),
53
+ # the from crypto dropdown
54
+ html .Div (
55
+ [
56
+ html .H3 ("Select Crypto" , style = {"paddingRight" : "30px" }),
57
+ dcc .Dropdown (
58
+ id = "SelectCrypto" ,
59
+ options = [{"label" : i , "value" : i } for i in cryp_value ],
60
+ value = "BTC" ,
61
+ clearable = False ,
62
+ style = {
63
+ "fontsize" : 24 ,
64
+ "width" : 90 ,
65
+ "color" : colors ["text" ],
66
+ }, # this style controls the text inside the dropdown
67
+ ),
68
+ ],
69
+ style = { # this style controls the layout of the dropdown box
70
+ "display" : "inline-block" ,
71
+ "verticalAlign" : "middle" ,
72
+ "paddingLeft" : "20px" ,
73
+ "paddingBottom" : "20px" ,
74
+ },
75
+ ),
76
+ # the submit button
77
+ html .Div (
78
+ [
79
+ html .Button (
80
+ id = "submit-button" ,
81
+ n_clicks = 0 ,
82
+ children = "Forecast" ,
83
+ style = {
84
+ "fontSize" : 20 ,
85
+ "verticalAlign" : "middle" ,
86
+ "color" : colors [
87
+ "text"
88
+ ], # this style controls the text inside the submit button
89
+ },
90
+ )
91
+ ],
92
+ style = {
93
+ "display" : "inline-block" ,
94
+ "verticalAlign" : "bottom" ,
95
+ "width" : 90 ,
96
+ # "height": 20,
97
+ "paddingLeft" : "7px" ,
98
+ "paddingBottom" : "20px" , # this style controls the entire submit button position etc.
99
+ },
100
+ ),
101
+ # the graphs
102
+ dcc .Graph (id = "forecast" , style = {"color" : colors ["text" ]}),
103
+ ],
104
+ style = {
105
+ # "display": "inline-block",
106
+ "backgroundColor" : colors ["background" ],
107
+ "color" : colors ["text" ],
108
+ # "horizontalAlign": "middle",
109
+ "width" : "100%" ,
110
+ "height" : "100%" ,
111
+ "align-items" : "center" ,
112
+ # "justify-content": "center", # this style controls the entire app
113
+ },
114
+ )
115
+
116
+ # app functions
117
+ @app .callback (
118
+ Output (component_id = "forecast" , component_property = "figure" ),
119
+ # Output(component_id="Coef_Value", component_property="figure"),
120
+ # Output(component_id="Model_Evaluation", component_property="figure"),
121
+ [Input ("submit-button" , "n_clicks" )],
122
+ [
123
+ State ("SelectCrypto" , "value" ),
124
+ # State("amount", "value"),
125
+ # State("isFlaggedSite", "value"),
126
+ # State("isUnrecognizedDevice", "value"),
127
+ # State("isOutsideLocation", "value"),
128
+ ],
129
+ )
130
+
131
+ # start the function
132
+ def CryptoForecast (n_clicks , SelectCrypto ):
133
+ cryptodata = pdr .get_data_yahoo (
134
+ [SelectCrypto + "-" + "USD" ], start = firstdate , end = todate
135
+ ).reset_index ()
136
+ cryptodata .columns = cryptodata .columns .get_level_values (0 )
137
+
138
+ cryp_pro = cryptodata [["Date" , "Close" ]]
139
+ cry_pro = cryp_pro .rename (columns = {"Date" :"ds" , "Close" :"y" })
140
+
141
+ model = Prophet ()
142
+ model .fit (cryp_pro )
143
+ future = model .make_future_dataframe (periods = 60 )
144
+ forecast = m .predict (future )
145
+ forecast = forecast [['ds' , 'yhat' , 'yhat_lower' , 'yhat_upper' ]]
146
+ forecast = forecast .remane (columns = {"ds" :"Date" })
147
+
148
+ plot_data = forecast .merge (crypto , on = "Date" , how = "outer" )
149
+
150
+ fig_forecast = px .scatter (
151
+ plot_data ,
152
+ x = "Date" ,
153
+ y = "Close" ,
154
+ size = "Volume" ,
155
+ title = SelectCrypto ,
156
+ labels = {"Date" : "Date" , "Close" : "Closing Price" },
157
+ template = "plotly_dark" ,
158
+ ).update_traces (mode = "lines+markers" , marker = dict (color = "green" , opacity = 0.4 ))
159
+
160
+ fig_forecast .add_trace (go .Scatter (x = plot_data ["Date" ], y = plot_data ["yhat" ], mode = 'lines' , name = "Predicted Price" ))
161
+ fig_forecast .add_trace (go .Scatter (x = plot_data ["Date" ], y = plot_data ["yhat_lower" ], mode = 'markers' , name = "Lower Band" ))
162
+ fig_forecast .add_trace (go .Scatter (x = plot_data ["Date" ], y = plot_data ["yhat_upper" ], mode = 'markers' , name = "Upper Band" ))
163
+
164
+
165
+ return fig
166
+
167
+
168
+ # launch the app
169
+ if __name__ == "__main__" :
170
+ app .run_server (debug = False , threaded = False )
0 commit comments