I am testing code from the documentation.
Individual Contracts : Example 5: Scan and Update Option Chain Every 5 Minutes
I am trying to understand why Filter method is called so many times?
- Should it be called so many times,
- if yes then why.
- If no, then any suggestions on fixing it.
Sample output: (happens at 6:30 AM)
There are more, I just showed a few
2023-01-02 00:00:00 :Populating option chain...2023-01-02 00:00:00 :1 Populating option chain for SPY 2U|SPY R735QTJ8XC9X...2023-01-02 00:00:00 :Filtering option chain...2023-01-02 00:00:00 :1 Filtering option chain for SPY 2U|SPY R735QTJ8XC9X...2023-01-03 00:00:00 :Filtering option chain...2023-01-03 00:00:00 :1 Filtering option chain for SPY 2U|SPY R735QTJ8XC9X...2023-01-03 06:30:00 :Filtering option chain...2023-01-03 06:30:00 :1 Filtering option chain for SPY 2U|SPY R735QTJ8XC9X...2023-01-03 06:30:00 :Filtering option chain...2023-01-03 06:30:00 :1 Filtering option chain for SPY 2U|SPY R735QTJ8XC9X...2023-01-03 06:30:00 :Filtering option chain...2023-01-03 06:30:00 :1 Filtering option chain for SPY 2U|SPY R735QTJ8XC9X...2023-01-03 06:30:00 :Filtering option chain...2023-01-03 06:30:00 :1 Filtering option chain for SPY 2U|SPY R735QTJ8XC9X...2023-01-03 06:30:00 :Filtering option chain...2023-01-03 06:30:00 :1 Filtering option chain for SPY 2U|SPY R735QTJ8XC9X...2023-01-03 06:30:00 :Filtering option chain...2023-01-03 06:30:00 :1 Filtering option chain for SPY 2U|SPY R735QTJ8XC9X...
Mia Alissi
Here are the findings and recommendations for Pranav's code regarding why Filter() is called so many times and how to improve this:
Issue Identified:
Recommendation:
Corrected code for the scheduling/filter section:
Summary:
Filter() is called so often because it is (1) directly scheduled and (2) also called at the end of PopulateOptionChain, which itself is scheduled after market open and at algorithm start. By removing the call to Filter from within PopulateOptionChain and letting the schedule handle it, you avoid double and clustered Filter calls. This keeps your filtering logic efficient and clear.
Sources: US Index Option Universe - QuantConnect.com, Opening Range Breakout for Stocks in Play
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.
Pranav Shah
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!