vectorbt
vectorbt copied to clipboard
vbt.Portfolio.from_signals calculation issue
Hey,
As mentioned here I am working on backtesting that is in sync with my TradingView strategy. I am making progress and vectorBT is really doing great.
When I started implementing take profit and stop loss I added open, high and low to the portfolio in addition to close for the calculation. When doing that I think I found an issue in the vectorBT calculation.
When trying to get things in sync with TV I encountered differences and tracked them down to the following issue. I tested with multiple indicators and take profit values. In some rare cases vectorBT uses the open price of the next timeframe instead of the close price of the current timeframe for the calculation. I could not find the reason or a pattern in which cases this happens. Not all trades for the same calculation are affected. All other trades in the same portfolio for the same strategy in the same portfolio record are handled correctly with close. I'm sorry I could't find the reason. The ohlc that is used is complete and in correct format.
As open seems not necessary for the tp and sl calculation I tried
open_for_portfiolio = close.shift(1)
and it really fixes the issue and the calculation is in perfect sync with TV.
Setting price=close in the portfolio did not help.
I hope I was able to describe the issue enough. Thanks for looking into it and fixing it.