-
Notifications
You must be signed in to change notification settings - Fork 238
Fix position tracking for buy_to_close orders in backtesting #856
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
base: dev
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review by Korbit AI
Korbit automatically attempts to detect when you fix issues in new commits.
| Category | Issue | Status |
|---|---|---|
| Missing business context in side conversion comments ▹ view | 🧠 Not in standard |
Files scanned
| File Path | Reviewed |
|---|---|
| lumibot/backtesting/backtesting_broker.py | ✅ |
Explore our documentation to understand the languages and file types we support and the files we ignore.
Check out our docs on how you can make Korbit work best for you and your team.
| # If the order has a "buy_to_open" or "buy_to_close" side, then we should change it to "buy" | ||
| if order.is_buy_order(): | ||
| order.side = Order.OrderSide.BUY | ||
| # If the order has a "sell_to_open" or "sell_to_close" side, then we should change it to "sell" | ||
| if order.is_sell_order(): | ||
| order.side = Order.OrderSide.SELL |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
im not sure this is a good idea, this will return less data back to the user, no? it sounds like we will be pretending that buy_to_open order and buy_to_close would be the same thing. maybe im wrong, but we should be preserving those values. perhaps we can do this transformation somewhere else where do not have to modify the data
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You raise a good point about preserving order type information. Let's explore alternatives that maintain order specificity while addressing the position tracking issue. Perhaps we could introduce a separate field for normalized sides used in position calculations, keeping the original order type intact. This would preserve data fidelity while ensuring consistent position tracking. What are your thoughts on this approach?
| # If the order has a "buy_to_open" or "buy_to_close" side, then we should change it to "buy" | ||
| if order.is_buy_order(): | ||
| order.side = Order.OrderSide.BUY | ||
| # If the order has a "sell_to_open" or "sell_to_close" side, then we should change it to "sell" | ||
| if order.is_sell_order(): | ||
| order.side = Order.OrderSide.SELL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
im not sure this is a good idea, this will return less data back to the user, no? it sounds like we will be pretending that buy_to_open order and buy_to_close would be the same thing. maybe im wrong, but we should be preserving those values. perhaps we can do this transformation somewhere else where do not have to modify the data
Bug fix: Properly handle buy_to_close and sell_to_close orders in backtesting position tracking
Issue
When using buy_to_close orders to close short positions in backtesting, the positions were not being properly tracked due to a mismatch in order side handling. The issue occurred because
_process_crypto_quote()was checking for"buy"string directly, but our order sides could be"buy_to_close"or"buy_to_open".Changes
_process_filled_order()to normalize order sides before processing:buy_to_closeandbuy_to_openare normalized toBUYsell_to_closeandsell_to_openare normalized toSELLImpact
This fixes position tracking in backtesting, particularly for :
Example
Before the fix, a short position closed with buy_to_close would show :
Testing
take_profit_priceandstop_loss_priceDescription by Korbit AI
What change is being made?
Convert buy_to_open/buy_to_close orders to BUY and sell_to_open/sell_to_close orders to SELL when processing filled orders in the backtester, to align filled orders with correct position updates.
Why are these changes being made?
To fix incorrect backtest position tracking caused by misinterpreting buy_to_* and sell_to_* fills when no broker updates positions.