-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstreamlit_app.py
204 lines (165 loc) · 5.36 KB
/
streamlit_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
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
import streamlit as st
import yfinance as yf
import datetime
import pandas as pd
import requests
import os
import sys
import subprocess
# check if the library folder already exists, to avoid building everytime you load the pahe
if not os.path.isdir("/tmp/ta-lib"):
# Download ta-lib to disk
with open("/tmp/ta-lib-0.4.0-src.tar.gz", "wb") as file:
response = requests.get(
"http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz"
)
file.write(response.content)
# get our current dir, to configure it back again. Just house keeping
default_cwd = os.getcwd()
os.chdir("/tmp")
# untar
os.system("tar -zxvf ta-lib-0.4.0-src.tar.gz")
os.chdir("/tmp/ta-lib")
os.system("ls -la /app/equity/")
# build
os.system("./configure --prefix=/home/appuser")
os.system("make")
# install
os.system("make install")
# back to the cwd
os.chdir(default_cwd)
sys.stdout.flush()
# add the library to our current environment
from ctypes import *
lib = CDLL("/home/appuser/lib/libta_lib.so.0.0.0")
# import library
try:
import talib
except ImportError:
subprocess.check_call([sys.executable, "-m", "pip", "install", "--global-option=build_ext", "--global-option=-L/home/appuser/lib/", "--global-option=-I/home/appuser/include/", "ta-lib"])
finally:
import talib
st.write("Hello")
yf.pdr_override()
# # def get_symbol(symbol):
# # url = "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query={}®ion=1&lang=en".format(
# # symbol
# # )
# # result = requests.get(url).json()
# # for x in result["ResultSet"]["Result"]:
# # if x["symbol"] == symbol:
# # return x["name"]
st.sidebar.header("User Input Parameters")
today = datetime.date.today()
date_from = date_from = (today - datetime.timedelta(days=366)).strftime("%Y-%m-%d")
symbol = st.sidebar.selectbox(
"Ticker", ["PETR4.SA", "VALE3.SA", "ABEV3.SA", "AZUL4.SA"]
)
# company_name = get_symbol(symbol.upper())
st.write(symbol.upper())
start = pd.to_datetime(date_from)
end = pd.to_datetime(today)
# Read data
data = yf.download(symbol, start, end)
st.write("Yahoo .... ok")
# # Adjusted Close Price
# st.header(f"Adjusted Close Price")
# st.line_chart(data["Adj Close"])
# df_table = data.copy()
# del df_table["Open"]
# del df_table["High"]
# del df_table["Low"]
# del df_table["Close"]
# del df_table["Volume"]
# # Candlesticks - Plotly
# data["Date"] = data.index
# fig = go.Figure(
# data=[
# go.Candlestick(
# x=data["Date"],
# open=data["Open"],
# high=data["High"],
# low=data["Low"],
# close=data["Close"],
# name=symbol,
# )
# ]
# )
# fig.update_layout(
# title=symbol + " Daily Chart",
# xaxis_title="Date",
# yaxis_title="Price ($)",
# # font=dict(family="Courier New, monospace", size=12, color="black"),
# )
# st.plotly_chart(fig, use_container_width=True)
# # Candlesticks - ALtair
# base = alt.Chart(data).encode(
# alt.X("Date:T", axis=alt.Axis(labelAngle=-45)),
# color=alt.condition(
# "datum.Open <= datum.Close", alt.value("#06982d"), alt.value("#ae1325")
# ),
# )
# chart = alt.layer(
# base.mark_rule().encode(
# alt.Y("Low:Q", title="Price", scale=alt.Scale(zero=False)), alt.Y2("High:Q")
# ),
# base.mark_bar().encode(alt.Y("Open:Q"), alt.Y2("Close:Q")),
# ).interactive()
# st.altair_chart(chart, use_container_width=True)
# # open_close_color = alt.condition(
# # "datum.Open <= datum.Close", alt.value("#06982d"), alt.value("#ae1325")
# # )
# # base = alt.Chart(data).encode(x="Date")
# # rule = base.mark_rule().encode(
# # y=alt.Y("Low", scale=alt.Scale(zero=False), axis=alt.Axis(title="Price")),
# # y2=alt.Y2("High"),
# # color=open_close_color,
# # )
# # bar = base.mark_bar().encode(y="Open", y2="Close", color=open_close_color)
# # st.altair_chart(rule + bar, use_container_width=True)
# # # Candlesticks Bokeh
# # from bokeh.sampledata.stocks import MSFT
# # from math import pi
# # df = pd.DataFrame(MSFT)[:50]
# # df["date"] = pd.to_datetime(df["date"])
# # inc = df.close > df.open
# # dec = df.open > df.close
# # w = 12 * 60 * 60 * 1000 # half day in ms
# # TOOLS = "pan,wheel_zoom,box_zoom,reset"
# # p = figure(
# # x_axis_type="datetime", tools=TOOLS, plot_width=1000, title="MSFT Candlestick"
# # )
# # p.xaxis.major_label_orientation = pi / 4
# # p.grid.grid_line_alpha = 0.3
# # p.segment(df.date, df.high, df.date, df.low, color="black")
# # p.vbar(
# # df.date[inc],
# # w,
# # df.open[inc],
# # df.close[inc],
# # fill_color="#06982d",
# # line_color="#06982d",
# # )
# # p.vbar(
# # df.date[dec],
# # w,
# # df.open[dec],
# # df.close[dec],
# # fill_color="#ae1325",
# # line_color="#ae1325",
# # )
# # st.bokeh_chart(p, use_container_width=True)
# ma_periods_int = 13
# data["SMA"] = talib.SMA(data["Adj Close"], timeperiod=ma_periods_int)
# df_table["SMA"] = data["SMA"]
# # Exponential Moving Average
# data["EMA"] = talib.EMA(data["Adj Close"], timeperiod=ma_periods_int)
# df_table["EMA"] = data["EMA"]
# # Plot
# st.header(f"SMA/EMA - Periods: {ma_periods_int}")
# st.line_chart(data[["Adj Close", "SMA", "EMA"]])
# if st.checkbox("View raw data"):
# if st.checkbox("Reverse", value=True):
# "Raw Data", data[::-1]
# else:
# "Raw Data", data