Hi,
I'm trying to run very simple code with filtering SPXW weekly options. It just loops over option chains between 2012 and 2024 years. At some point (around 35% progress) I get the System.outOfMemoryException or more strange error “Error invoking ?SPXW data reader. Line: StreamReader Error: string must be splittable on space into two parts”. I have a backtesting node with 8 Gb RAM. I do not understand why memory consumption increases while executing the code. Could somebody help to resolve the issue?
from AlgorithmImports import *
class WeeklyPuts(QCAlgorithm):
def initialize(self):
self.set_start_date(2012, 1, 2)
self.set_end_date(2024, 12, 12)
self.set_cash(1_000_000)
self.resolution = Resolution.HOUR
self.index = self.add_index('SPX', resolution=self.resolution)
self.index_symbol = self.index.symbol
self.spxw_option = self.add_index_option(self.index_symbol, "SPXW", resolution=self.resolution)
self.spxw_option.set_filter(self.spxw_filter)
self.spxw_symbol = self.spxw_option.symbol
def spxw_filter(self, universe: OptionFilterUniverse) -> OptionFilterUniverse:
# return universe.include_weeklys().strikes(-1, 1).puts_only().expiration(0, 8)
return universe.weeklys_only().strikes(-1, 1).puts_only().expiration(0, 8)
def on_data(self, data: Slice):
spxw_chain = data.option_chains.get(self.spxw_symbol)
if spxw_chain is None or len(spxw_chain.contracts) == 0:
return
Mia Alissi
To address the memory issue Alexander is facing with the SPXW weekly options backtesting, here are some suggestions based on best practices and potential optimizations:
Optimize Data Resolution: Consider reducing the data resolution from hourly to daily. This can significantly decrease the amount of data processed and reduce memory usage.
Limit Option Chains: Implement a more restrictive filter on the option chains to limit the number of contracts loaded into memory. You can narrow the strike range or expiration window further. For example, you might consider filtering for options that are closer to the money or have shorter expiration periods.
Use Lean Data: Utilize Lean data normalization and compression techniques to minimize memory consumption during backtesting. Ensure that you are only requesting the data you need.
Monitor Memory Usage: Add logging to monitor memory usage throughout the backtest to identify specific points where memory spikes occur. This can help pinpoint the operations that are consuming the most memory.
Incremental Backtesting: Break down the backtesting period into smaller segments and run them incrementally. This approach can help manage memory usage more effectively by processing smaller chunks of data at a time.
Upgrade Resources: If possible, consider upgrading the backtesting node to a higher memory capacity to accommodate the large dataset being processed.
Here's an updated version of the code with some of these optimizations:
By implementing these changes, you should see an improvement in memory usage and potentially resolve the errors you are encountering.
Sources: Option Expiration Week Effect, Basic Template S P X Weekly Index Options Algorithm, Asset Growth Effect, Upcoming Holiday Momentum for Amazon, Basic Template Index Options Algorithm
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.
Alexander Suvorov
The memory consumption is pretty much the same even if I use daily data
Alexander Suvorov
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!