-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
174 lines (144 loc) · 5.72 KB
/
app.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
import pandas as pd
import yfinance as yf
import ta
from ta import add_all_ta_features
from ta.utils import dropna
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import streamlit as st
from datetime import date
import datetime
import io
yf.pdr_override()
st.set_option('deprecation.showfileUploaderEncoding', False)
st.write("""
# Technical Analysis Dashboard for any stock using **yfinance** and **ta**
""")
st.sidebar.header('User Input Parameters')
time = pd.to_datetime('now')
today = datetime.date.today()
def user_input_features():
ticker = st.sidebar.text_input("Ticker", 'XRP-EUR')
start_date = st.sidebar.text_input("Start Date", '2019-01-01')
end_date = st.sidebar.text_input("End Date", f'{today}')
buying_price = st.sidebar.number_input("Buying Price", value=0.2000, step=0.0001)
balance = st.sidebar.number_input("Quantity", value=0.0, step=0.0001)
file_buffer = st.sidebar.file_uploader("Choose a .csv or .xlxs file\n 2 columns are expected 'rate' and 'price'", type=['xlsx','csv'])
return ticker, start_date, end_date, buying_price, balance, file_buffer
symbol, start, end, buying_price, balance, file_buffer = user_input_features()
start = pd.to_datetime(start)
end = pd.to_datetime(end)
# Read data
data = yf.download(symbol,start,end)
data.columns = map(str.lower, data.columns)
df = data.copy()
df = ta.add_all_ta_features(df, "open", "high", "low", "close", "volume", fillna=True)
df_trends = df[['close','trend_sma_fast','trend_sma_slow','trend_ema_fast','trend_ema_slow',]]
df_momentum = df[['momentum_rsi', 'momentum_roc', 'momentum_tsi', 'momentum_uo', 'momentum_stoch', 'momentum_stoch_signal', 'momentum_wr', 'momentum_ao', 'momentum_kama']]
# Price
daily_price = data.close.iloc[-1]
portfolio = daily_price * balance
st.title(f"Streamlit and {symbol} :euro:")
st.header("DF last rows")
st.dataframe(data.tail())
st.header("DF today's value")
st.markdown(f'Daily {symbol} price: {daily_price}')
st.markdown(f'{symbol} price per quantity: {portfolio}')
if file_buffer is not None:
file = pd.read_excel(file_buffer)
file = pd.DataFrame(file)
st.dataframe(file)
weighted_rate = (file['price']*file['rate']).sum() / file['price'].sum()
st.markdown(f'{symbol} portfolio price: {weighted_rate}')
buying_price = weighted_rate
st.dataframe(data.tail(1))
st.code("""
time = pd.to_datetime('now')
today = datetime.date.today()
def user_input_features():
ticker = st.sidebar.text_input("Ticker", 'XRP-EUR')
start_date = st.sidebar.text_input("Start Date", '2019-01-01')
end_date = st.sidebar.text_input("End Date", f'{today}')
return ticker, start_date, end_date
symbol, start, end = user_input_features()
# Read data
data = yf.download(symbol,start,end)
""", language="python")
st.header(f"Candlestick for {symbol}")
# Initialize figure
fig = go.Figure()
# Candlestick
fig.add_trace(go.Candlestick(x=df.index,
open=df.open,
high=df.high,
low=df.low,
close=df.close,
visible=True,
name='Candlestick',))
if file_buffer is not None:
fig.add_trace(
go.Indicator(
mode = "number+delta",
value = daily_price,
delta = {"reference": weighted_rate, 'relative':True},
#title = {"text": "<span style='font-size:0.9em'>Daily Portfolio Performance</span>"},
domain = {'y': [0.8, 1], 'x': [0.25, 0.75]},
visible = True))
fig.add_shape(
# Line Horizontal
type="line",
x0=start,
y0=buying_price,
x1=end,
y1=buying_price,
line=dict(
color="black",
width=1.5,
dash="dash",
),
visible = True,
)
for column in df_trends.columns.to_list():
fig.add_trace(
go.Scatter(x = df_trends.index,y = df_trends[column],name = column,))
fig.update_layout(height=800,width=1000, xaxis_rangeslider_visible=False)
st.plotly_chart(fig)
st.header(f"Trends for {symbol}")
fig = go.Figure()
for column in df_trends.columns.to_list():
fig.add_trace(
go.Scatter(x = df_trends.index,y = df_trends[column],name = column,))
# Adapt buttons start
button_all = dict(label = 'All',method = 'update',args = [{'visible': df_trends.columns.isin(df_trends.columns),'title': 'All','showlegend':True,}])
def create_layout_button(column):
return dict(label = column,
method = 'update',
args = [{'visible': df_trends.columns.isin([column]),
'title': column,
'showlegend': True,
}])
fig.update_layout(updatemenus=[go.layout.Updatemenu(active = 0, buttons = ([button_all]) + list(df_trends.columns.map(lambda column: create_layout_button(column))))],)
# Adapt buttons end
# add slider
fig.update_layout(
xaxis=dict(
rangeslider=dict(
visible=True
),
type="date"
))
fig.update_layout(height=800,width=1000,updatemenus=[dict(direction="down",pad={"r": 10, "t": 10},showactive=True,x=0,xanchor="left",y=1.15,yanchor="top",)],)
# Candlestick
st.plotly_chart(fig)
# momentum indicators
st.header(f"Momentum Indicators for {symbol}")
trace=[]
Headers = df_momentum.columns.values.tolist()
for i in range(9):
trace.append(go.Scatter(x=df_momentum.index, name=Headers[i], y=df_momentum[Headers[i]]))
fig = make_subplots(rows=9, cols=1)
for i in range(9):
fig.append_trace(trace[i],i+1,1)
fig.update_layout(height=2200, width=1000)
st.plotly_chart(fig)