Spaces:
Runtime error
Runtime error
Renjith Karimattathil SASIDHARAN
commited on
Commit
·
b1f9cea
1
Parent(s):
d2b14fd
Add application file
Browse files- app.py +76 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import os
|
3 |
+
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")
|
10 |
+
DB_HOST = os.getenv("DB_HOST")
|
11 |
+
client = MongoClient(host=f"mongodb+srv://{DB_USER}:{DB_PASSWORD}@{DB_HOST}/?retryWrites=true&w=majority")
|
12 |
+
|
13 |
+
#select database
|
14 |
+
db = client['algotrading']
|
15 |
+
#select the collection within the database
|
16 |
+
|
17 |
+
def fetch_all_strategy():
|
18 |
+
global db
|
19 |
+
orders_t = db.orders
|
20 |
+
strategies = pd.DataFrame(list(orders_t.find({}, {"strategy_id":1})))
|
21 |
+
return list(strategies['strategy_id'].unique())
|
22 |
+
|
23 |
+
|
24 |
+
def fetch_orders_for_strategy(strategy):
|
25 |
+
global db
|
26 |
+
orders_t = db.orders
|
27 |
+
#convert entire collection to Pandas dataframe
|
28 |
+
orders = pd.DataFrame(list(orders_t.find({"strategy_id": 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 "Latest" != profits:
|
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]
|
44 |
+
cumsum = orders[(orders['created_date'] == latest_day) & (orders['strategy_id'] == strategy)] \
|
45 |
+
.sort_values(by='created_ts', ascending=True) \
|
46 |
+
.groupby(['strategy_id', 'created_ts'])['profit'].sum() \
|
47 |
+
.groupby(level=0).cumsum().reset_index()
|
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 |
+
return gr.update(value=plot, visible=True)
|
52 |
+
|
53 |
+
|
54 |
+
with gr.Blocks() as demo:
|
55 |
+
with gr.Row():
|
56 |
+
with gr.Column():
|
57 |
+
with gr.Box():
|
58 |
+
gr.Markdown("## Select strategies to display")
|
59 |
+
strategy = gr.Radio(choices=fetch_all_strategy(), label="")
|
60 |
+
with gr.Column():
|
61 |
+
with gr.Box():
|
62 |
+
gr.Markdown("## Select graphs to display")
|
63 |
+
profits = gr.Radio(choices=["Latest", "All"], label="", value="Latest")
|
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, profits], outputs=profits_plot)
|
71 |
+
# fetch.click(create_star_plot, inputs=[libraries, stars], outputs=star_plot)
|
72 |
+
# fetch.click(create_issue_plot, inputs=[libraries, issues], outputs=issue_plot)
|
73 |
+
|
74 |
+
|
75 |
+
if __name__ == "__main__":
|
76 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
pymongo
|
2 |
+
pandas
|
3 |
+
plotly
|