Spaces:
Runtime error
Runtime error
Jumper-Clown
commited on
Commit
•
eda3576
1
Parent(s):
46ebf5d
experiment with spectrograms and mouse click tracking between plots
Browse files- __pycache__/calculator.cpython-312.pyc +0 -0
- __pycache__/page_symbol_details.cpython-312.pyc +0 -0
- __pycache__/trends.cpython-312.pyc +0 -0
- __pycache__/ui.cpython-312.pyc +0 -0
- app.py +16 -2
- calculator.py +13 -1
- page_symbol_details.py +53 -35
- requirements.txt +2 -0
- trends.py +2 -7
- ui.py +45 -1
__pycache__/calculator.cpython-312.pyc
CHANGED
Binary files a/__pycache__/calculator.cpython-312.pyc and b/__pycache__/calculator.cpython-312.pyc differ
|
|
__pycache__/page_symbol_details.cpython-312.pyc
CHANGED
Binary files a/__pycache__/page_symbol_details.cpython-312.pyc and b/__pycache__/page_symbol_details.cpython-312.pyc differ
|
|
__pycache__/trends.cpython-312.pyc
CHANGED
Binary files a/__pycache__/trends.cpython-312.pyc and b/__pycache__/trends.cpython-312.pyc differ
|
|
__pycache__/ui.cpython-312.pyc
CHANGED
Binary files a/__pycache__/ui.cpython-312.pyc and b/__pycache__/ui.cpython-312.pyc differ
|
|
app.py
CHANGED
@@ -1,8 +1,22 @@
|
|
1 |
-
import streamlit
|
2 |
|
3 |
import page_symbol_details
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
if __name__ == '__main__':
|
6 |
st.set_page_config(layout="wide")
|
7 |
-
|
8 |
page_symbol_details.run()
|
|
|
1 |
+
import streamlit
|
2 |
|
3 |
import page_symbol_details
|
4 |
|
5 |
+
# test
|
6 |
+
import calculator
|
7 |
+
from scipy.io import wavfile
|
8 |
+
import ui
|
9 |
+
from plotly.subplots import make_subplots
|
10 |
+
import plotly.graph_objects as go
|
11 |
+
import numpy as np
|
12 |
+
from streamlit_plotly_events import plotly_events
|
13 |
+
import plotly.express as px
|
14 |
+
import streamlit as st
|
15 |
+
|
16 |
+
# test end
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
if __name__ == '__main__':
|
21 |
st.set_page_config(layout="wide")
|
|
|
22 |
page_symbol_details.run()
|
calculator.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import numpy as np
|
|
|
2 |
from datetime import datetime
|
3 |
|
4 |
|
@@ -52,4 +53,15 @@ def linear_regression(dates, y_list):
|
|
52 |
y_low = m * dates[0] + c
|
53 |
y_high = m * dates[-1] + c
|
54 |
|
55 |
-
return y_low, y_high
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import numpy as np
|
2 |
+
import scipy as sp
|
3 |
from datetime import datetime
|
4 |
|
5 |
|
|
|
53 |
y_low = m * dates[0] + c
|
54 |
y_high = m * dates[-1] + c
|
55 |
|
56 |
+
return y_low, y_high
|
57 |
+
|
58 |
+
|
59 |
+
def normalize(data_list, high=1.0, low=-1.0):
|
60 |
+
data = np.array(data_list)
|
61 |
+
min_val = np.min(data)
|
62 |
+
max_val = np.max(data)
|
63 |
+
delta = max_val - min_val
|
64 |
+
new_delta = high - low
|
65 |
+
|
66 |
+
data = ((data - min_val) * new_delta / delta) + low
|
67 |
+
return data
|
page_symbol_details.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
import numpy as np
|
2 |
import streamlit as st
|
3 |
from plotly.subplots import make_subplots
|
|
|
|
|
4 |
|
5 |
import data_retriever
|
6 |
import trends
|
@@ -8,40 +10,43 @@ import ui
|
|
8 |
|
9 |
|
10 |
def run():
|
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 |
# symbol candlestick graph
|
47 |
candleFigure = make_subplots(rows=1, cols=1)
|
@@ -67,7 +72,20 @@ def run():
|
|
67 |
xaxis_title='Date',
|
68 |
yaxis_title="Price per Share",
|
69 |
template='plotly_dark')
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
if st.checkbox('Sector Trends'):
|
73 |
# plot the trend of the market as a candlestick graph.
|
|
|
1 |
import numpy as np
|
2 |
import streamlit as st
|
3 |
from plotly.subplots import make_subplots
|
4 |
+
from streamlit_plotly_events import plotly_events
|
5 |
+
import plotly.graph_objects as go
|
6 |
|
7 |
import data_retriever
|
8 |
import trends
|
|
|
10 |
|
11 |
|
12 |
def run():
|
13 |
+
col1, col2 = st.columns(2)
|
14 |
+
with col1:
|
15 |
+
exchange_names = data_retriever.get_exchange_code_names()
|
16 |
+
exchanges_selectbox = st.selectbox(
|
17 |
+
'Exchange:',
|
18 |
+
exchange_names,
|
19 |
+
index=exchange_names.index('US exchanges (NYSE, Nasdaq)')
|
20 |
+
)
|
21 |
+
exchange_name = exchanges_selectbox
|
22 |
+
exchange_index = exchange_names.index(exchange_name)
|
23 |
+
exchange = data_retriever.get_exchange_codes()[exchange_index]
|
24 |
+
|
25 |
+
symbols = data_retriever.get_symbols(exchange)
|
26 |
+
symbols_selectbox = st.selectbox(
|
27 |
+
'Stock:',
|
28 |
+
symbols,
|
29 |
+
index=symbols.index('AAPL')
|
30 |
+
)
|
31 |
+
symbol = symbols_selectbox
|
32 |
+
|
33 |
+
with col2:
|
34 |
+
# max time period
|
35 |
+
st.text_input('No. of years look-back:', value=1, key="years_back")
|
36 |
+
years_back = int(st.session_state.years_back)
|
37 |
+
weeks_back = years_back*12*4
|
38 |
+
|
39 |
+
symbol_prices = data_retriever.get_current_stock_data(symbol, weeks_back)
|
40 |
+
if not any(symbol_prices):
|
41 |
+
return
|
42 |
+
dates = symbol_prices.index.format()
|
43 |
+
|
44 |
+
# back test
|
45 |
+
st.text_input('No. of days back-test:', value=0, key="backtest_period")
|
46 |
+
n_days_back = int(st.session_state.backtest_period)
|
47 |
+
end_date = data_retriever.n_days_before(data_retriever.today(), n_days_back)
|
48 |
+
symbol_prices_backtest = symbol_prices[symbol_prices.index <= end_date]
|
49 |
+
backtest_dates = symbol_prices_backtest.index.format()
|
50 |
|
51 |
# symbol candlestick graph
|
52 |
candleFigure = make_subplots(rows=1, cols=1)
|
|
|
72 |
xaxis_title='Date',
|
73 |
yaxis_title="Price per Share",
|
74 |
template='plotly_dark')
|
75 |
+
selected_points = plotly_events(candleFigure, click_event=True, hover_event=False, select_event=False, key='3')
|
76 |
+
|
77 |
+
if st.checkbox('Tests'):
|
78 |
+
testFigure = make_subplots(rows=1, cols=1)
|
79 |
+
ui.create_line(testFigure, dates, symbol_prices['Volume'], "Volume")
|
80 |
+
ui.add_mouse_indicator(testFigure, selected_points, min=np.min(symbol_prices['Volume']),
|
81 |
+
max=np.max(symbol_prices['Volume']))
|
82 |
+
test_id = st.plotly_chart(testFigure, use_container_width=True, key='test')
|
83 |
+
|
84 |
+
fft_steps = st.slider(label='FFT Steps', min_value=8, max_value=128, value=16, step=8)
|
85 |
+
specFig1 = ui.create_spectrogram(dates, symbol_prices['Close'], sampling_frequency=1, num_points_fft=fft_steps,
|
86 |
+
overlap_percent=0.0, title="Close Price Spectrogram")
|
87 |
+
specFig2 = ui.create_spectrogram(dates, symbol_prices['Volume'], sampling_frequency=1, num_points_fft=fft_steps,
|
88 |
+
overlap_percent=0.0, title="Volume Spectrogram")
|
89 |
|
90 |
if st.checkbox('Sector Trends'):
|
91 |
# plot the trend of the market as a candlestick graph.
|
requirements.txt
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
streamlit
|
|
|
2 |
|
3 |
yfinance
|
4 |
finnhub-python
|
@@ -9,3 +10,4 @@ datetime
|
|
9 |
plotly
|
10 |
|
11 |
numpy
|
|
|
|
1 |
streamlit
|
2 |
+
streamlit_plotly_events
|
3 |
|
4 |
yfinance
|
5 |
finnhub-python
|
|
|
10 |
plotly
|
11 |
|
12 |
numpy
|
13 |
+
scipy
|
trends.py
CHANGED
@@ -33,13 +33,8 @@ def sector_trends(symbol, weeks_back):
|
|
33 |
indicator_stock_data_normalized_sum = indicator_stock_data_normalized_peer_sum[indicator]
|
34 |
|
35 |
indicator_values = peer_stock_data[indicator]
|
36 |
-
min_val = indicator_values
|
37 |
-
max_val = indicator_values
|
38 |
-
for value in indicator_values:
|
39 |
-
if value < min_val:
|
40 |
-
min_val = value
|
41 |
-
if value > max_val:
|
42 |
-
max_val = value
|
43 |
delta = max_val - min_val
|
44 |
|
45 |
value_idx = 0
|
|
|
33 |
indicator_stock_data_normalized_sum = indicator_stock_data_normalized_peer_sum[indicator]
|
34 |
|
35 |
indicator_values = peer_stock_data[indicator]
|
36 |
+
min_val = np.min(indicator_values)
|
37 |
+
max_val = np.max(indicator_values)
|
|
|
|
|
|
|
|
|
|
|
38 |
delta = max_val - min_val
|
39 |
|
40 |
value_idx = 0
|
ui.py
CHANGED
@@ -4,6 +4,11 @@ import plotly.graph_objects as go
|
|
4 |
from plotly.validators.scatter.marker import SymbolValidator
|
5 |
from plotly.subplots import make_subplots
|
6 |
|
|
|
|
|
|
|
|
|
|
|
7 |
# chart datapoint icons
|
8 |
raw_symbols = SymbolValidator().values
|
9 |
up_arrow = raw_symbols[5]
|
@@ -76,7 +81,7 @@ def create_markers(fig, dates, dataset, title, y_label, marker_symbol=3, marker_
|
|
76 |
fig.add_trace(line)
|
77 |
|
78 |
|
79 |
-
def create_line(fig, dates, dataset, title, y_label, marker_symbol=4, marker_size=15):
|
80 |
line = go.Scatter(name=title, x=dates, y=dataset, marker_line_color="yellow")
|
81 |
fig.add_trace(line)
|
82 |
|
@@ -95,3 +100,42 @@ def create_fill_area(fig, dates, y_low, y_high, title, color='rgba(0,100,80,0.2)
|
|
95 |
line=dict(color=color)
|
96 |
)
|
97 |
fig.add_trace(fill_area)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from plotly.validators.scatter.marker import SymbolValidator
|
5 |
from plotly.subplots import make_subplots
|
6 |
|
7 |
+
import numpy as np
|
8 |
+
from scipy import signal
|
9 |
+
|
10 |
+
import calculator
|
11 |
+
|
12 |
# chart datapoint icons
|
13 |
raw_symbols = SymbolValidator().values
|
14 |
up_arrow = raw_symbols[5]
|
|
|
81 |
fig.add_trace(line)
|
82 |
|
83 |
|
84 |
+
def create_line(fig, dates, dataset, title="title", y_label="values", marker_symbol=4, marker_size=15):
|
85 |
line = go.Scatter(name=title, x=dates, y=dataset, marker_line_color="yellow")
|
86 |
fig.add_trace(line)
|
87 |
|
|
|
100 |
line=dict(color=color)
|
101 |
)
|
102 |
fig.add_trace(fill_area)
|
103 |
+
|
104 |
+
|
105 |
+
def create_spectrogram(dates, data_list, sampling_frequency=1, num_points_fft=128, overlap_percent=50.0, title="title"):
|
106 |
+
data_list = calculator.normalize(data_list, 1, -1)
|
107 |
+
|
108 |
+
# Spectrogram
|
109 |
+
w = signal.blackman(num_points_fft)
|
110 |
+
freqs, bins, pxx = signal.spectrogram(data_list, sampling_frequency, window=w, nfft=num_points_fft, noverlap=int(num_points_fft*overlap_percent/100.0))
|
111 |
+
|
112 |
+
dates_subset = [dates[int(bin)] for bin in bins]
|
113 |
+
|
114 |
+
trace = [go.Heatmap(
|
115 |
+
x=dates_subset,
|
116 |
+
y=freqs,
|
117 |
+
z=10 * np.log10(pxx),
|
118 |
+
colorscale='Jet',
|
119 |
+
)]
|
120 |
+
layout = go.Layout(
|
121 |
+
title=title,
|
122 |
+
yaxis=dict(title='Frequency'), # x-axis label
|
123 |
+
xaxis=dict(title='Time'), # y-axis label
|
124 |
+
)
|
125 |
+
fig = go.Figure(data=trace, layout=layout)
|
126 |
+
st.plotly_chart(fig, use_container_width=True)
|
127 |
+
|
128 |
+
return fig
|
129 |
+
|
130 |
+
def add_mouse_indicator(fig, selected_points, min, max):
|
131 |
+
if any(selected_points):
|
132 |
+
fig.add_shape(
|
133 |
+
go.layout.Shape(
|
134 |
+
type='line',
|
135 |
+
x0=selected_points[0]['x'],
|
136 |
+
x1=selected_points[0]['x'],
|
137 |
+
y0=min,
|
138 |
+
y1=max,
|
139 |
+
line=dict(color='red', width=2),
|
140 |
+
)
|
141 |
+
)
|