Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vbt.Portfolio.from_signals calculation issue #780

Open
tiiiecherle opened this issue Mar 17, 2025 · 1 comment
Open

vbt.Portfolio.from_signals calculation issue #780

tiiiecherle opened this issue Mar 17, 2025 · 1 comment

Comments

@tiiiecherle
Copy link

tiiiecherle commented Mar 17, 2025

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.

@polakowo
Copy link
Owner

Do you use stop_exit_price="close" or the default (which is "price")? By default, VBT will execute a stop order using open price if the stop order has been hit at open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants