-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from bsdz/plotly_strat_runner
Plotly strat runner
- Loading branch information
Showing
15 changed files
with
22,693 additions
and
857 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "yabte" | ||
version = "0.3.5" | ||
version = "0.3.6" | ||
description = "Yet another backtesting engine" | ||
authors = ["Blair Azzopardi <[email protected]>"] | ||
license = "MIT" | ||
|
@@ -39,6 +39,7 @@ optional = true | |
|
||
[tool.poetry.group.notebooks.dependencies] | ||
matplotlib = "^3.6.2" | ||
plotly = "^5.10.0" | ||
ipykernel = "^6.20.2" | ||
pyfeng = "^0.2.5" | ||
nbconvert = "^7.2.9" | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
from typing import Any | ||
|
||
import numpy as np | ||
import pandas as pd | ||
import plotly.graph_objects as go | ||
from plotly.subplots import make_subplots | ||
|
||
from ....backtest import StrategyRunner | ||
|
||
|
||
def plot_strategy_runner(sr: StrategyRunner, settings: dict[str, Any] | None = None): | ||
"""Display the results of a strategy run using plotly. | ||
Plots a grid of charts with each column representing a book and rows representing | ||
each asset's price series along with long/short positioning and volumne series. A | ||
bottom row shows the value of each book as a price series. | ||
""" | ||
default_settings: dict[str, Any] = {} | ||
|
||
if isinstance(settings, dict): | ||
default_settings = default_settings | settings | ||
s = pd.Series(default_settings, dtype=object) | ||
|
||
traded_assets = [ | ||
a for a in sr.assets if a.name in sr.transaction_history.asset_name.unique() | ||
] | ||
|
||
dpi = 100 | ||
col_width = 8 * dpi | ||
row_unit_height = 3 * dpi | ||
ncols = len(sr.books) | ||
nrows = 1 + len(traded_assets) | ||
|
||
subplot_titles = [a.name for a in (traded_assets)] + ["Book Value"] | ||
row_heights = [10 for a in (traded_assets)] + [2] | ||
specs = [[{"secondary_y": True} for c in range(ncols)] for r in range(nrows)] | ||
|
||
fig = make_subplots( | ||
rows=nrows, | ||
cols=ncols, | ||
shared_xaxes=True, | ||
subplot_titles=subplot_titles, | ||
row_heights=row_heights, | ||
start_cell="top-left", | ||
specs=specs, | ||
vertical_spacing=0.05, | ||
) | ||
|
||
for col, book in enumerate(sr.books, start=1): | ||
for row, asset in enumerate(traded_assets, start=1): | ||
prices = sr.data[asset.data_label] | ||
|
||
fig.add_trace( | ||
go.Candlestick( | ||
x=prices.index, | ||
open=prices.Open, | ||
high=prices.High, | ||
low=prices.Low, | ||
close=prices.Close, | ||
), | ||
row=row, | ||
col=col, | ||
secondary_y=True, | ||
) | ||
|
||
fig.add_trace( | ||
go.Bar( | ||
x=prices.index, | ||
y=prices.Volume, | ||
marker_color="lightgrey", | ||
), | ||
row=row, | ||
col=col, | ||
secondary_y=False, | ||
) | ||
|
||
fig.update_xaxes(rangeslider_visible=False, row=row, col=col) | ||
fig.update_yaxes( | ||
title=asset.denom, secondary_y=True, showgrid=True, row=row, col=col | ||
) | ||
fig.update_yaxes( | ||
title="Volume", secondary_y=False, showgrid=False, row=row, col=col | ||
) | ||
|
||
# add some range selector buttons | ||
fig.update_xaxes( | ||
rangeselector=dict( | ||
buttons=[ | ||
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"), | ||
] | ||
), | ||
row=1, | ||
col=col, | ||
) | ||
|
||
trans_hist = sr.transaction_history.query( | ||
"[email protected] and [email protected]" | ||
) | ||
pos_hist = ( | ||
trans_hist.groupby("ts") | ||
.agg( | ||
quantity=("quantity", np.sum), | ||
labels=( | ||
"order_label", | ||
lambda L: " ".join(l for l in L if l is not None), | ||
), | ||
) | ||
.reindex(prices.index) | ||
) | ||
|
||
shorts = prices[pos_hist.eval("quantity < 0")][["Low"]].join( | ||
pos_hist.labels | ||
) | ||
fig.add_trace( | ||
go.Scatter( | ||
x=shorts.index, | ||
y=shorts.Low, | ||
customdata=shorts.labels, | ||
mode="markers", | ||
marker_symbol="arrow-down", | ||
marker_color="red", | ||
marker_size=10, | ||
hovertemplate="%{x}<br>%{y}<br>%{customdata}<extra></extra>", | ||
), | ||
row=row, | ||
col=col, | ||
secondary_y=True, | ||
) | ||
|
||
longs = prices[pos_hist.eval("quantity > 0")][["High"]].join( | ||
pos_hist.labels | ||
) | ||
fig.add_trace( | ||
go.Scatter( | ||
x=longs.index, | ||
y=longs.High, | ||
customdata=shorts.labels, | ||
mode="markers", | ||
marker_symbol="arrow-up", | ||
marker_color="green", | ||
marker_size=10, | ||
hovertemplate="%{x}<br>%{y}<br>%{customdata}<extra></extra>", | ||
), | ||
row=row, | ||
col=col, | ||
secondary_y=True, | ||
) | ||
|
||
row = nrows | ||
bh = sr.book_history.loc[:, book.name] | ||
fig.add_trace( | ||
go.Scatter( | ||
x=bh.index, | ||
y=bh.total, | ||
), | ||
row=row, | ||
col=col, | ||
) | ||
|
||
fig.update_xaxes(rangeslider_visible=True, row=row, col=col) | ||
fig.update_yaxes(title=book.denom, showgrid=True, row=row, col=col) | ||
fig.update_xaxes(title="Date", row=row, col=col) | ||
|
||
fig.update_layout( | ||
height=nrows * row_unit_height, | ||
width=ncols * col_width, | ||
showlegend=False, | ||
title_text="Strategy Runner Report", | ||
) | ||
|
||
return fig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters