Spaces:
Runtime error
Runtime error
Renjith Karimattathil SASIDHARAN
commited on
Commit
·
7689762
1
Parent(s):
7e6817d
Add trades tables
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import gradio as gr
|
|
4 |
import pandas as pd
|
5 |
from pymongo import MongoClient
|
6 |
import plotly.express as px
|
|
|
7 |
|
8 |
DB_USER = os.getenv("DB_USER")
|
9 |
DB_PASSWORD = os.getenv("DB_PASSWORD")
|
@@ -29,15 +30,19 @@ def fetch_orders_for_strategy(strategy):
|
|
29 |
orders = orders.dropna(axis=0, subset=['open_price', 'close_price'])
|
30 |
orders['open_price'] = orders['open_price'].astype(str).astype(float)
|
31 |
orders['close_price'] = orders['close_price'].astype(str).astype(float)
|
|
|
32 |
orders['created_ts'] = pd.to_datetime(orders['created_ts'])
|
33 |
orders['closed_ts'] = pd.to_datetime(orders['closed_ts'])
|
34 |
orders['profit'] = orders['close_price'] - orders['open_price']
|
|
|
35 |
orders['created_date'] = orders['created_ts'].dt.date
|
|
|
|
|
36 |
orders = orders.sort_values(by='created_ts', ascending=False)
|
37 |
return orders
|
38 |
|
39 |
def create_latest_profit_plot(strategy, profits):
|
40 |
-
if "
|
41 |
return gr.update(visible=False)
|
42 |
orders = fetch_orders_for_strategy(strategy)
|
43 |
latest_day = orders[orders['strategy_id'] == strategy].sort_values(by='created_ts', ascending=False)['created_date'].unique()[0]
|
@@ -48,7 +53,28 @@ def create_latest_profit_plot(strategy, profits):
|
|
48 |
plot = px.line(cumsum, x="created_ts", y="profit",
|
49 |
title=f"Profits for day for {strategy}")
|
50 |
plot.update_layout(legend=dict(x=0.5, y=0.99), title_x=0.5, legend_title_text="")
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
|
54 |
with gr.Blocks() as demo:
|
@@ -60,15 +86,19 @@ with gr.Blocks() as demo:
|
|
60 |
with gr.Column():
|
61 |
with gr.Box():
|
62 |
gr.Markdown("## Select graphs to display")
|
63 |
-
|
64 |
with gr.Row():
|
65 |
fetch = gr.Button(value="Fetch")
|
66 |
with gr.Row():
|
67 |
with gr.Column():
|
68 |
profits_plot = gr.Plot(visible=False)
|
|
|
|
|
|
|
|
|
69 |
|
70 |
-
fetch.click(create_latest_profit_plot, inputs=[strategy,
|
71 |
-
# fetch.click(
|
72 |
# fetch.click(create_issue_plot, inputs=[libraries, issues], outputs=issue_plot)
|
73 |
|
74 |
|
|
|
4 |
import pandas as pd
|
5 |
from pymongo import MongoClient
|
6 |
import plotly.express as px
|
7 |
+
import plotly.graph_objects as go
|
8 |
|
9 |
DB_USER = os.getenv("DB_USER")
|
10 |
DB_PASSWORD = os.getenv("DB_PASSWORD")
|
|
|
30 |
orders = orders.dropna(axis=0, subset=['open_price', 'close_price'])
|
31 |
orders['open_price'] = orders['open_price'].astype(str).astype(float)
|
32 |
orders['close_price'] = orders['close_price'].astype(str).astype(float)
|
33 |
+
# orders['created_ts'] = pd.to_datetime(orders['created_ts'])
|
34 |
orders['created_ts'] = pd.to_datetime(orders['created_ts'])
|
35 |
orders['closed_ts'] = pd.to_datetime(orders['closed_ts'])
|
36 |
orders['profit'] = orders['close_price'] - orders['open_price']
|
37 |
+
orders['profit'] = orders['profit'].round(2)
|
38 |
orders['created_date'] = orders['created_ts'].dt.date
|
39 |
+
orders['ticker_open_price'] = orders['ticker_open_price'].astype(int)
|
40 |
+
orders['ticker_close_price'] = orders['ticker_close_price'].astype(int)
|
41 |
orders = orders.sort_values(by='created_ts', ascending=False)
|
42 |
return orders
|
43 |
|
44 |
def create_latest_profit_plot(strategy, profits):
|
45 |
+
if profits not in ["Profits", "Trades"]:
|
46 |
return gr.update(visible=False)
|
47 |
orders = fetch_orders_for_strategy(strategy)
|
48 |
latest_day = orders[orders['strategy_id'] == strategy].sort_values(by='created_ts', ascending=False)['created_date'].unique()[0]
|
|
|
53 |
plot = px.line(cumsum, x="created_ts", y="profit",
|
54 |
title=f"Profits for day for {strategy}")
|
55 |
plot.update_layout(legend=dict(x=0.5, y=0.99), title_x=0.5, legend_title_text="")
|
56 |
+
|
57 |
+
table = go.Figure(data=[go.Table(
|
58 |
+
header=dict(values=list(['Strategy', 'Created', 'Closed', 'Ticker Open', 'Ticker Close', 'Market Type', 'Symbol', 'Open', 'Close', 'Qty', 'Profit']),
|
59 |
+
align='left'),
|
60 |
+
cells=dict(values=[orders.strategy_id,
|
61 |
+
orders.created_ts,
|
62 |
+
orders.closed_ts,
|
63 |
+
orders.ticker_open_price,
|
64 |
+
orders.ticker_close_price,
|
65 |
+
orders.market_type,
|
66 |
+
orders.tradingsymbol,
|
67 |
+
orders.open_price,
|
68 |
+
orders.close_price,
|
69 |
+
orders.quantity,
|
70 |
+
orders.profit])
|
71 |
+
)
|
72 |
+
])
|
73 |
+
|
74 |
+
if profits == 'Profits':
|
75 |
+
return gr.update(value=plot, visible=True)
|
76 |
+
if profits == 'Trades':
|
77 |
+
return gr.update(value=table, visible=True)
|
78 |
|
79 |
|
80 |
with gr.Blocks() as demo:
|
|
|
86 |
with gr.Column():
|
87 |
with gr.Box():
|
88 |
gr.Markdown("## Select graphs to display")
|
89 |
+
type = gr.Radio(choices=["Profits", "Trades"], label="", value="Profits")
|
90 |
with gr.Row():
|
91 |
fetch = gr.Button(value="Fetch")
|
92 |
with gr.Row():
|
93 |
with gr.Column():
|
94 |
profits_plot = gr.Plot(visible=False)
|
95 |
+
with gr.Row():
|
96 |
+
with gr.Column():
|
97 |
+
trades_plot = gr.Plot(visible=False)
|
98 |
+
|
99 |
|
100 |
+
fetch.click(create_latest_profit_plot, inputs=[strategy, type], outputs=profits_plot)
|
101 |
+
# fetch.click(create_latest_profit_plot, inputs=[strategy, type], outputs=trades_plot)
|
102 |
# fetch.click(create_issue_plot, inputs=[libraries, issues], outputs=issue_plot)
|
103 |
|
104 |
|