blankly-finance / blankly

🚀 💸 Easily build, backtest and deploy your algo in just a few lines of code. Trade stocks, cryptos, and forex across exchanges w/ one package.
https://package.blankly.finance
GNU Lesser General Public License v3.0
2.03k stars 261 forks source link

Could be cool to do in-house plots. #207

Open SamsTheGreatest opened 1 year ago

SamsTheGreatest commented 1 year ago

Hej!

I think it is very useful to debug algos using something more than equity curves and metrics. Other frameworks like Vectorbt use Plotly for this, you guys chose tradingview (questionably quality right now). As I usually use Jupyter notebooks for everything, here is my suggestion right now before you can make a smooth integration with Tradingview:

from plotly.subplots import make_subplots
import plotly.graph_objects as go

def plot_results(result,symbol=None):
    if symbol is None:
        symbol = list(result.history.keys())[0]

    history = result.history[symbol]
    returns = result.get_returns()
    trades = result.trades['created'].copy()
    trades = {k:np.array([_[k] for _ in trades]) for k in ['time','side','price']}

    fig = make_subplots(rows=1, cols=1,shared_xaxes=True,vertical_spacing=None)

    fig.add_trace(
        go.Candlestick(
            x=pd.to_datetime(history.time,unit='s'),
            open=history.open,
            high=history.high,
            low=history.low,
            close=history.close),
         col=1,row=1
    )
    fig.update_layout(xaxis_rangeslider_visible=False)

    fig.add_trace(
        go.Scatter(
            x=pd.to_datetime(trades['time'],unit='s'),
            y=trades['price'],
            mode='markers',
            marker=dict(size=10,color=np.where(trades['side']=='buy','green','red'),symbol=np.where(trades['side']=='buy','triangle-up','triangle-down') ,line=dict(color='black',width=1)),
            ),
        col=1,row=1,
    )
    return fig
EmersonDove commented 1 year ago

This is neat, thanks for the implementation. We were thinking about ways to improve visual debugging for the models with different charting libraries so this might come in handy.

bfan1256 commented 1 year ago

Hey @SamsTheGreatest , thanks for the help here. Yes, we've been actively looking for a better solution compared to the Blankly Platform as we currently are working with an external data provider for the TradingView charts and they are just not producing the right data. As a result, I think the best approach that the team is taking is considering a desktop rendition of the platform that enables you to read directly from the underlying CSV and backtest results themselves while still keeping and maintaining the features of the platform, with additional syncing capabilities if you do work in a team.

That way you get the best of the TradingView experience on the right data :D. We're also considering adding a blankly.viz API soon that enables you to easily and quickly add lines at various price targets, plot additional data and markers as needed, hopefully a future where you can adjust hyperparameters purely based on a dashboard.

SamsTheGreatest commented 1 year ago

Sounds like a solid plan, really enjoying the development you guys made, you must keep on it! On a side note, if for now I want to plot custom variables, is it possible to access state.variables after the backtest to plot them? It seems like the backtest result does not include the final state?

bfan1256 commented 1 year ago

The best way is to use global variables that are defined outside of the strategy (so anything that is outside of your strategy will still be stored and you can interact with them inside your strategy as well.