File size: 6,370 Bytes
46ebf5d
 
 
 
 
 
eda3576
 
 
 
 
46ebf5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0789d79
 
46ebf5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eda3576
 
9f633b0
eda3576
 
 
 
 
 
 
 
9f633b0
 
 
 
 
eda3576
 
 
9f633b0
eda3576
 
 
 
 
 
 
 
9f633b0
eda3576
 
 
 
9f633b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eda3576
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st

import plotly.graph_objects as go
from plotly.validators.scatter.marker import SymbolValidator
from plotly.subplots import make_subplots

import numpy as np
from scipy import signal

import calculator

# chart datapoint icons
raw_symbols = SymbolValidator().values
up_arrow = raw_symbols[5]
down_arrow = raw_symbols[6]


def create_candlestick(fig, dates, dataset, title, y_label):
    candlestick = go.Candlestick(name=y_label, 
                                 x=dates,
                                 open=dataset['Open'],
                                 high=dataset['High'],
                                 low=dataset['Low'],
                                 close=dataset['Close'])
    fig.add_trace(candlestick)
    fig.update_xaxes(
        rangeslider_visible=True,
        rangeselector=dict(
            buttons=list([
                dict(count=1, label="1m", step="month", stepmode="backward"),
                dict(count=6, label="6m", step="month", stepmode="backward"),
                dict(count=1, label="YTD", step="year", stepmode="todate"),
                dict(count=1, label="1y", step="year", stepmode="backward"),
                dict(step="all")
            ])
        )
    )


def create_indicators(fig, datasets):
    for indicator in datasets:
        indicator_data = datasets[indicator]

        marker_color="lightskyblue"
        marker_symbol = 0
        if 'IsBullish' in indicator_data:
            if indicator_data['IsBullish']:
                marker_color = 'green'
                marker_symbol = 5
            else:
                marker_color = 'red'
                marker_symbol = 6

        indicator_plot = go.Scatter(name=indicator,
                                    mode="markers",
                                    x=indicator_data['Date'],
                                    y=indicator_data['Values'],
                                    marker_symbol=marker_symbol,
                                    marker_line_color="midnightblue",
                                    marker_color=marker_color,
                                    marker_line_width=2,
                                    marker_size=15,
                                    hovertemplate="%{indicator}: %{y}%{x}<br>number: %{marker.symbol}<extra></extra>")
        fig.add_trace(indicator_plot)


def create_lines(fig, dates, datasets, title, y_label):
    for key in datasets:
        line = go.Scatter(name=key, x=dates, y=datasets[key])
        fig.add_trace(line)


def create_markers(fig, dates, dataset, title, y_label, marker_symbol=3, marker_color="blue", marker_size=15):
    line = go.Scatter(name=title, x=dates, y=dataset,
                      mode="markers",
                      marker_symbol=marker_symbol,
                      marker_line_color="midnightblue",
                      marker_color=marker_color,
                      marker_line_width=2,
                      marker_size=marker_size)
    fig.add_trace(line)


def create_line(fig, dates, dataset, title="title", y_label="values", marker_symbol=4, marker_size=15, color='rgba(0,100,80,0.2)'):
    line = go.Scatter(name=title, x=dates, y=dataset, marker_line_color="yellow", fillcolor=color)
    fig.add_trace(line)


def create_fill_area(fig, dates, y_low, y_high, title, color='rgba(0,100,80,0.2)'):
    # line_low = go.Scatter(name=title, x=dates, y=y_low, fillcolor=color, showlegend=False)
    # fig.add_trace(line_low)
    # line_high = go.Scatter(name=title, x=dates, y=y_low, fillcolor=color, showlegend=False)
    # fig.add_trace(line_high)
    fill_area = go.Scatter(
        name=title,
        x=dates + dates[::-1],
        y=y_high + y_low[::-1],
        fill='toself',
        fillcolor=color,
        line=dict(color=color)
    )
    fig.add_trace(fill_area)


def create_spectrogram(dates, data_list, sampling_frequency=1, num_points_fft=128, overlap_percent=50.0, title="title", log_scale=True):
    data_list = calculator.normalize(data_list, 1, -1)

    # Spectrogram
    w = signal.blackman(num_points_fft)
    freqs, bins, pxx = signal.spectrogram(data_list, sampling_frequency, window=w, nfft=num_points_fft, noverlap=int(num_points_fft*overlap_percent/100.0))

    dates_subset = [dates[int(bin)] for bin in bins]

    if log_scale:
        z = 10 * np.log10(pxx)
    else:
        z = pxx

    trace = [go.Heatmap(
        x=dates_subset,
        y=freqs,
        z=z,
        colorscale='Jet',
    )]
    layout = go.Layout(
        title=title,
        yaxis=dict(title='Frequency'),  # x-axis label
        xaxis=dict(title='Time'),  # y-axis label
    )
    fig = go.Figure(data=trace, layout=layout)
    fig.update_layout(title=title)
    st.plotly_chart(fig, use_container_width=True)

    return fig


def create_heatmap(dates, data_list, bin_count=10, time_steps=20, title='title'):
    time_width = int(len(dates)/time_steps)
    dates_subset = [dates[index*time_width] for index in range(time_steps)]
    min_val = np.min(data_list)
    max_val = np.max(data_list)
    delta = (max_val-min_val)
    min_val -= 0.2*delta
    max_val += 0.2*delta
    delta = (max_val-min_val)

    bin_width = delta/(bin_count + 1)
    bins = np.arange(min_val, max_val, bin_width)

    values = np.empty(shape=(time_steps, bin_count))
    for time_index in range(time_steps):
        data_subset = data_list[time_index*time_width:time_index*time_width+time_width]
        counts, res_bins = np.histogram(data_subset, bins=bins)
        values[time_index:] = counts

    trace = [go.Heatmap(
        x=dates_subset,
        y=bins,
        z=values.transpose(),
        colorscale='Jet',
    )]
    layout = go.Layout(
        title=title,
        yaxis=dict(title='Values'),  # x-axis label
        xaxis=dict(title='Time'),  # y-axis label
    )
    fig = go.Figure(data=trace, layout=layout)
    fig.update_layout(title=title)
    st.plotly_chart(fig, use_container_width=True)

    return fig


def add_mouse_indicator(fig, selected_points, min, max):
    if any(selected_points):
        fig.add_shape(
                go.layout.Shape(
                    type='line',
                    x0=selected_points[0]['x'],
                    x1=selected_points[0]['x'],
                    y0=min,
                    y1=max,
                    line=dict(color='red', width=2),
                )
            )