I'd like to backtest an option strategy on multiple underlying assets (either indices, futures or stocks). How can I see the returns from each underlier separately, without running a separate backtest for each underlier?
Don't have an account? Join QuantConnect Today
QuantConnect Community Discussions
QUANTCONNECT COMMUNITY
LEAN is the open-source algorithmic trading engine powering QuantConnect. Founded in 2012 LEAN has been built by a global community of 180+ engineers and powers more than 300+ hedge funds today.
Join QuantConnect's Discord server for real-time support, where a vibrant community of traders and developers awaits to help you with any of your QuantConnect needs.
The Open-Quant League is a quarterly competition between universities and investment clubs for the best-performing strategy. The previous quarter's code is open-sourced, and competitors must adapt to survive.
Refer to our Research Guidelines for high quality research posts.
Create an account on QuantConnect for the latest community delivered to your inbox.
Sign Up Today
|
|
|||||||
|
|
||||||||
|
How can I split backtest performance by underlyer?
Richard Bounds | April 2024
I'd like to backtest an option strategy on multiple underlying assets (either indices, futures or stocks). How can I see the returns from each underlier separately, without running a separate backtest for each underlier?
QuantConnectâ„¢ 2025. All Rights Reserved
Mia Alissi
To backtest an option strategy on multiple underlying assets and see the returns from each underlier separately without running separate backtests for each, you can utilize the concept of Universes in QuantConnect to dynamically select the underlying assets you're interested in. Then, within your algorithm, you can track the performance of options strategies for each underlying asset separately. Here's a simplified approach to achieve this:
Define Universes: Use the Universe selection feature to dynamically select the underlying assets for your options strategies. This could be based on criteria like market cap, volume, or membership in an index.
Subscribe to Option Chains: For each underlying asset selected by your Universe, subscribe to its option chain.
Implement the Strategy: Implement your options trading strategy within the
OnData
method. This is where you'll decide when to enter or exit positions based on your criteria.Track Performance Separately: Create a dictionary or a custom class to track the performance of the options strategy for each underlying asset separately. You can update this tracking mechanism whenever you make trades.
Logging and Analysis: Use logging to record the performance of each underlying asset. Alternatively, you can use custom charts or insights to analyze the performance during or after the backtest.
Here's a very basic example to illustrate the concept:
This example is quite basic and needs to be adapted to your specific strategy, including how you select universes, filter options, and define your entry/exit criteria. The key takeaway is to use the universe selection to manage multiple underliers and track their performance individually within the same backtest.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Richard Bounds
Thank you. The most complex part of your suggestion is left for the user, where you comment:
# Update self.performanceTracker[underlyingSymbol] based on the strategy's performance
Do I need to do all of the accounting to calculate strategy performance myself, or are there already functions that I can use to do that for each symbol?
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Mia Alissi
In QuantConnect, while there are built-in functions to manage portfolio and security transactions, tracking the performance of individual strategies or symbols, especially in a complex scenario like options trading across multiple underliers, often requires custom implementation. However, you can leverage some of the platform's features and data structures to simplify this process. Here's how you can approach it:
Profit and Loss Tracking: Use the
SecurityHolding.Profit
andSecurityHolding.TotalFees
properties to track the profit and loss (P&L) and fees for individual positions. This can help you calculate net performance for trades on each symbol.Portfolio Contribution: To understand the contribution of each symbol to the overall portfolio, you can use the
Portfolio
object, which contains information about all holdings. By comparing the value of individual holdings (SecurityHolding
) against the total portfolio value, you can gauge the relative performance.Custom Data Structure for Performance Tracking: While you need to manually update your performance tracking, you can structure it to minimize effort. For example, you could track realized and unrealized P&L, fees, and entry/exit points for each trade.
Transactions and Orders: Leverage the
Transactions
object to track when orders are filled. This allows you to capture the exact points of entry and exit for trades, which is crucial for performance analysis.Here's an example of how you might extend the performance tracking for each symbol:
This example is simplified and assumes each exit corresponds to the last entry (FIFO). Depending on your strategy, you might need a more sophisticated method to match entries and exits, especially for strategies that open multiple positions in the same symbol over time.
Remember, this custom tracking is necessary because QuantConnect's built-in performance metrics are generally portfolio-wide and don't automatically break down performance by individual symbols or strategies. By implementing a custom solution, you gain flexibility in how you analyze and interpret the performance data.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!