-
Notifications
You must be signed in to change notification settings - Fork 641
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
Portfolio support for Bar data #2236
Comments
Why not just use the close of a bar ? The order execution is already responsible for distributing volume across a bar. I suppose subscribing to external bars should be enough to have the most granular bars. |
Yeah, I am fully open 👍 |
I'll have a look at some point, curious about it |
I've solved both issues, using bars, and realized_pnl = 0 after position closed (it's not 0 anymore). I really like your examples, testing often takes me as much or more time than writing code. |
Unbelievable how quickly 🙏 |
I've worked on some complicated features, this one was easy in comparison, on the other hand experience is starting to accumulate as well, I'm starting to know more and more components of nautilus. Also the bugs you find and I fix sometimes, or features, are things that will be useful to me as well. |
I think the hardest feature I worked on was the mixed data request client + catalog, this was hard, required to touch a lot of things int he system. Also aggregating bars before the start of a backtest was hard, especially for time bars, there are a lot of particular cases. Maybe you could test that as well more, I think it works, but the more people use it the better. |
thank you faysou, I really need it❤️ |
Current Behavior
The Portfolio component doesn't fully support bar-based backtesting, leading to incorrect PnL calculations when using OHLCV bar data. Specifically:
Portfolio.unrealized_pnl()
returnsNone
while positions are openPortfolio.realized_pnl()
returns incorrect values (zero profit) after positions are closedProposed Solution
Implement bar support based on Bar-to-Tick Conversion:
Reproduction
A minimal example demonstrating the current limitation is attached.
run_backtest.py.zip
It is fully reproducible example in one file (including artificial bars data).
Just unzip and run.
Strategy setup is extremely simple:
I've added two debug points in the code to make it easy to see empty values being returned from
Portfolio
object:unrealized_pnl
returnsNone
instead of profitrealized_pnl
returns0
when there should be profitWhy this is important
This is my main rationale for why bar data should be treated as a first-class citizen in Nautilus in the future.
Three key factors support this perspective:
9.03 USD
92.59 USD
(10x more expensive)These factors make bar data support essential for practical usability in these basic, meaningful and widely needed scenarios.
2 possible implementation approaches
There are two viable approaches to handle bar data in the Portfolio component:
(as mentioned in this Discord message
1. Pre-processing Bars to Ticks (Optimal Performance)
This approach involves converting bars to synthetic ticks before adding them to the engine:
Advantages:
Implementation:
2. On-the-fly Bar Conversion (Maximum Convenience)
This approach lets Nautilus automatically convert bars to ticks during runtime:
Advantages:
Implementation:
The text was updated successfully, but these errors were encountered: