backtesting.py
backtesting.py copied to clipboard
Plot P/L triangles in absolute currency units
I have set the reference price for the cash PnL at the spread between two securities (I am backtesting a mean reversion strategy). However, in the html output of the plot I am getting the PnL in terms of the differnce in the spread as a function of the entry price of the spread when the trade was initiated. Instead, I want to get the PnL based off of the prices of the sum of the two securities (VIX futures) which make up the spread. This would look like cash difference in spread / sum(security_1, security_2). I have tried to add an additional class which could be inherited from Order but it's not working out.
Any ideas on how to provide custom data points (column names of our data) to compute the PnL during run()?
html output of the plot
Basically, you'd like plotted Profit/Loss triangles positioned on y axis in absolute "dollar" units instead of percents? :raised_eyebrow:
I'm willing to tie that on Backtest.plot(relative_equity=False) argument, which already works for equity plot. PR welcome. :+1:
Detailed trade data, including P/L in absolute units, is available in stats._trades results data frame. You can use it like:
stats._trades.PnL / pd.Series(security_1 + security_2).iloc[stats._trades.EntryBar]