Skip to content

Commit

Permalink
Merge pull request #22 from hummingbot/feat/improve_pnl_graph
Browse files Browse the repository at this point in the history
Feat/improve pnl graph
  • Loading branch information
fengtality authored May 19, 2023
2 parents 81ab46c + 6103b35 commit fc5d976
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 83 deletions.
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.ONESHELL:
.PHONY: run
.PHONY: conda_remove
.PHONY: conda_create

run:
streamlit run main.py

env_remove:
conda env remove -n dashboard

env_create:
conda env create -f environment.yml

docker_build:
docker build -t dashboard:latest .

docker_run:
docker run -p 8501:8501 dashboard:latest
6 changes: 2 additions & 4 deletions pages/2_🚀_Strategy_Performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,11 @@ def get_ohlc(trading_pair: str, exchange: str, interval: str, start_timestamp: i
st.metric(label='Average Buy Price', value=round(strategy_data_filtered.average_buy_price, 4))
st.metric(label='Average Sell Price', value=round(strategy_data_filtered.average_sell_price, 4))

cg = CandlesGraph(candles_df_filtered, show_volume=True, extra_rows=4)
cg = CandlesGraph(candles_df_filtered, show_volume=True, extra_rows=2)
cg.add_buy_trades(strategy_data_filtered.buys)
cg.add_sell_trades(strategy_data_filtered.sells)
cg.add_base_inventory_change(strategy_data_filtered)
cg.add_net_pnl(strategy_data_filtered)
cg.add_trade_pnl(strategy_data_filtered)
cg.add_trade_fee(strategy_data_filtered)
cg.add_pnl(strategy_data_filtered)
fig = cg.figure()
st.plotly_chart(fig, use_container_width=True)

Expand Down
103 changes: 24 additions & 79 deletions utils/graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pandas_ta as ta # noqa: F401
import streamlit as st

from utils.data_manipulation import StrategyData
from utils.data_manipulation import StrategyData, SingleMarketStrategyData
import plotly.graph_objs as go


Expand Down Expand Up @@ -171,60 +171,49 @@ def add_base_inventory_change(self, strategy_data: StrategyData, row=3):
)
self.base_figure.update_yaxes(title_text='Base Inventory Change', row=row, col=1)

def add_net_pnl(self, strategy_data: StrategyData, row=4):
def add_pnl(self, strategy_data: SingleMarketStrategyData, row=4):
merged_df = self.get_merged_df(strategy_data)

self.base_figure.add_trace(
go.Scatter(
x=merged_df.datetime,
y=merged_df["net_pnl_continuos"],
name="Cumulative Net PnL",
mode="lines+markers+text",
marker=dict(color="black", size=6),
line=dict(color="mediumpurple", width=2),
text=merged_df["net_pnl_continuos"],
textposition="top center",
texttemplate="%{text:.2f}"
x=merged_df["datetime"],
y=merged_df["cum_fees_in_quote"].apply(lambda x: round(-x, 2)),
name="Cum Fees",
mode='lines',
line_color='teal',
fill="tozeroy", # Fill to the line below (trade pnl)
stackgroup='one'
),
row=row, col=1
)
self.base_figure.update_yaxes(title_text='Cum Net PnL', row=row, col=1)

def add_trade_pnl(self, strategy_data: StrategyData, row=5):
merged_df = self.get_merged_df(strategy_data)
self.base_figure.add_trace(
go.Scatter(
x=merged_df.datetime,
y=merged_df["trade_pnl_continuos"],
name="Cumulative Trade PnL",
mode="lines+markers+text",
marker=dict(color="black", size=6),
line=dict(color="crimson", width=2),
text=merged_df["trade_pnl_continuos"],
textposition="top center",
texttemplate="%{text:.1f}"
x=merged_df["datetime"],
y=merged_df["trade_pnl_continuos"].apply(lambda x: round(x, 2)),
name="Cum Trade PnL",
mode='lines',
line_color='pink',
fill="tonexty", # Fill to the line below (net pnl)
stackgroup='one'
),
row=row, col=1
)
self.base_figure.update_yaxes(title_text='Cum Trade PnL', row=row, col=1)

def add_trade_fee(self, strategy_data: StrategyData, row=6):
merged_df = self.get_merged_df(strategy_data)

self.base_figure.add_trace(
go.Scatter(
x=merged_df.datetime,
y=merged_df["cum_fees_in_quote"],
name="Cumulative Fees",
x=merged_df["datetime"],
y=merged_df["net_pnl_continuos"].apply(lambda x: round(x, 2)),
name="Cum Net PnL",
mode="lines+markers+text",
marker=dict(color="black", size=6),
line=dict(color="seagreen", width=2),
text=merged_df["cum_fees_in_quote"],
line=dict(color="black", width=2),
textposition="top center",
texttemplate="%{text:.2f}"
text=merged_df["net_pnl_continuos"],
texttemplate="%{text:.1f}"
),
row=row, col=1
)
self.base_figure.update_yaxes(title_text='Cum Trade Fees', row=row, col=1)
self.base_figure.update_yaxes(title_text='PNL', row=row, col=1)

def update_layout(self):
self.base_figure.update_layout(
Expand Down Expand Up @@ -256,47 +245,3 @@ def get_merged_df(self, strategy_data: StrategyData):
merged_df["trade_pnl_continuos"] = merged_df["unrealized_trade_pnl"] + merged_df["cum_net_amount"] * merged_df["close"]
merged_df["net_pnl_continuos"] = merged_df["trade_pnl_continuos"] - merged_df["cum_fees_in_quote"]
return merged_df


def get_bar_plot_volume_of_trades(strategy_data: StrategyData):
grouped_df = strategy_data.trade_fill.groupby("trade_type").agg({"amount": "sum", "order_id": "count"})
# Create figure with secondary y-axis
fig = go.Figure()
fig.add_trace(go.Bar(
y=['Total Amount'],
x=grouped_df.loc["BUY", ["amount"]],
name='Buy Amount',
orientation='h',

))
fig.add_trace(go.Bar(
y=['Total Amount'],
x=grouped_df.loc["SELL", ["amount"]],
name='Sell Amount',
orientation='h',
))
fig.update_layout(template="plotly_white", title="Volume analysis", height=300,
xaxis_title="Amount in Base Asset")
return fig


def get_bar_plot_quantity_of_trades(strategy_data: StrategyData):
grouped_df = strategy_data.trade_fill.groupby("trade_type").agg({"amount": "sum", "order_id": "count"})

fig = go.Figure()
fig.add_trace(go.Bar(
y=['Quantity of Orders'],
x=grouped_df.loc["BUY", ["order_id"]],
name='Quantity of Buys',
orientation='h',

))
fig.add_trace(go.Bar(
y=['Quantity of Orders'],
x=grouped_df.loc["SELL", ["order_id"]],
name='Quantity of Sells',
orientation='h',
))
fig.update_layout(template="plotly_white", title="Excution Analysis", height=300,
xaxis_title="Quantity of orders")
return fig

0 comments on commit fc5d976

Please sign in to comment.