Spaces:
Runtime error
Runtime error
Commit
·
ee56cf8
1
Parent(s):
f3ec673
Visualize candlestick and volume charts
Browse files
app.py
CHANGED
@@ -1,21 +1,85 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
|
|
|
|
3 |
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# The UI of the demo defines here.
|
8 |
with gr.Blocks() as demo:
|
9 |
gr.Markdown("Auto trade bot.")
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
gr.Markdown("Candle polts may goes here.")
|
15 |
-
gr.components.Image()
|
16 |
# for plotly it should follow this: https://gradio.app/plot-component-for-maps/
|
17 |
-
|
18 |
-
|
19 |
|
20 |
with gr.Row():
|
21 |
with gr.Column():
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
+
import plotly.graph_objects as go
|
4 |
+
from plotly.subplots import make_subplots
|
5 |
|
6 |
|
7 |
+
import datetime
|
8 |
+
def get_time():
|
9 |
+
return datetime.datetime.now().time()
|
10 |
+
|
11 |
+
|
12 |
+
counter = 0
|
13 |
+
start_year, test_year = 2018, 2023
|
14 |
+
datetime_column = "Date"
|
15 |
+
df_data = pd.read_csv(f"./data/EURUSD_Candlestick_1_M_BID_01.01.{start_year}-04.02.2023_processed.csv")
|
16 |
+
df_data[datetime_column] = pd.to_datetime(df_data[datetime_column], format="%Y-%m-%d") # %d.%m.%Y %H:%M:%S.000 GMT%z
|
17 |
+
|
18 |
+
# Removing all empty dates
|
19 |
+
# Build complete timeline from start date to end date
|
20 |
+
dt_all = pd.date_range(start=df_data[datetime_column].tolist()[0], end=df_data[datetime_column].tolist()[-1])
|
21 |
+
# Retrieve the dates that ARE in the original dataset
|
22 |
+
dt_obs = set([d.strftime("%Y-%m-%d") for d in pd.to_datetime(df_data[datetime_column])])
|
23 |
+
# Define dates with missing values
|
24 |
+
dt_breaks = [d for d in dt_all.strftime("%Y-%m-%d").tolist() if not d in list(dt_obs)]
|
25 |
+
|
26 |
+
|
27 |
+
df_data_test = df_data[df_data['Date'].dt.year == test_year]
|
28 |
+
df_data_train = df_data[df_data['Date'].dt.year != test_year]
|
29 |
+
|
30 |
+
|
31 |
+
def trading_plot():
|
32 |
+
global counter
|
33 |
+
global df_data_train
|
34 |
+
|
35 |
+
if counter < len(df_data_test):
|
36 |
+
df_data_train = df_data_train.append(df_data_test.iloc[counter])
|
37 |
+
counter += 1
|
38 |
+
else:
|
39 |
+
df_data_train = df_data
|
40 |
+
|
41 |
+
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, vertical_spacing=0.02, row_heights=[0.7, 0.3],
|
42 |
+
subplot_titles=['OHLC chart', ''])
|
43 |
+
|
44 |
+
# Plot OHLC on 1st subplot
|
45 |
+
fig.add_trace(go.Candlestick(x=df_data_train[datetime_column].tolist(),
|
46 |
+
open=df_data_train["Open"].tolist(), close=df_data_train["Close"].tolist(),
|
47 |
+
high=df_data_train["High"].tolist(), low=df_data_train["Low"].tolist(),
|
48 |
+
name=""), row=1, col=1)
|
49 |
+
|
50 |
+
# Plot volume trace on 2nd row
|
51 |
+
colors = ['red' if row['Open'] - row['Close'] >= 0 else 'green' for index, row in df_data_train.iterrows()]
|
52 |
+
fig.add_trace(go.Bar(x=df_data_train[datetime_column], y=df_data_train['Volume'], name="", marker_color=colors,
|
53 |
+
hovertemplate="%{x}<br>Volume: %{y}"), row=2, col=1)
|
54 |
+
|
55 |
+
# Add chart title and Hide dates with no values and remove rangeslider
|
56 |
+
fig.update_layout(title="", height=600, showlegend=False,
|
57 |
+
xaxis_rangeslider_visible=False,
|
58 |
+
xaxis_rangebreaks=[dict(values=dt_breaks)])
|
59 |
+
|
60 |
+
# Update y-axis label
|
61 |
+
fig.update_yaxes(title_text="Price", row=1, col=1)
|
62 |
+
fig.update_yaxes(title_text="Volume", row=2, col=1)
|
63 |
+
|
64 |
+
fig.update_xaxes(showspikes=True, spikecolor="green", spikesnap="cursor", spikemode="across")
|
65 |
+
fig.update_yaxes(showspikes=True, spikecolor="orange", spikethickness=2)
|
66 |
+
fig.update_layout(spikedistance=1000, hoverdistance=100)
|
67 |
+
|
68 |
+
fig.layout.xaxis.range = ("2022-12-01", "2023-03-01")
|
69 |
+
|
70 |
+
return fig
|
71 |
+
|
72 |
|
73 |
# The UI of the demo defines here.
|
74 |
with gr.Blocks() as demo:
|
75 |
gr.Markdown("Auto trade bot.")
|
76 |
+
|
77 |
+
# dt = gr.Textbox(label="Current time")
|
78 |
+
# demo.queue().load(get_time, inputs=None, outputs=dt, every=1)
|
79 |
+
|
|
|
|
|
80 |
# for plotly it should follow this: https://gradio.app/plot-component-for-maps/
|
81 |
+
candlestick_plot = gr.Plot().style()
|
82 |
+
demo.queue().load(trading_plot, [], candlestick_plot, every=1)
|
83 |
|
84 |
with gr.Row():
|
85 |
with gr.Column():
|