How can I pick the contract that is closest to expire (apparently this usually has the most liquidity).
So do I just set this in initialize? What if rollover happens? I will need a way to update it periodically incase it changed? Or maybe always check right before making an order?
Anyways, I'm not sure the best way to do any of this.
Thanks!
Mia Alissi
When selecting the contract closest to expiration and handling rollovers using the QuantConnect platform, here is a refined Python script to guide you through setting it up effectively:
Key Points
set_filter
method is for defining the range of expirations you are interested in. Here, we filter for contracts expiring within the next 90 days.date_rules.every_day()
checks daily, whilerollover_contract
manages the rollover logic.timedelta
for setting up correct time intervals.This setup ensures your algorithm trades on the most current and liquid futures contracts efficiently.
Sources: Basic Template Future Rollover Algorithm, Handling Data - QuantConnect.com, Handling Data - QuantConnect.com, Security Identifiers - QuantConnect.com, Universes - QuantConnect.com
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.
QuantHeart
Thanks! Also, should the timedelta on line 10 be updated?
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.
QuantHeart
Mia Alissi should I also call self.rollover_contract in initialize?
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
While it's not directly necessary to call
self.rollover_contract
ininitialize
, calling it at the beginning can be beneficial to ensure that your algorithm starts with the correct future contract immediately. This way, you avoid having to wait for the scheduled event when your algorithm starts.Here's an example of how you can incorporate this in your
initialize
method:By calling
self.rollover_contract()
ininitialize
, you start with the most appropriate future contract for trading immediately. Additionally, the scheduled rollover event ensures that you continue trading the correct contract as time progresses.Sources: Basic Template Future Rollover Algorithm, Handling Data - QuantConnect.com, Rolling Window Algorithm, Initialization - QuantConnect.com, Getting Started - QuantConnect.com
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!