Im sorry, a newbie here learning how to code. I'm having trouble trying to write a line that checks the portfolio if the options underlying matches with the group of tickers that i'm following; to stop trading. However, it seems that my code execute the same option every minute.
Can someone help?
Mia Alissi
From the provided code, it seems like the algorithm is buying options every minute because there is no check to prevent it from buying the same option again if the conditions are met.
To prevent this, you can add a check in your
buyoptions
andbuyputs
methods to see if you already hold the option contract before buying it. Here is an example of how you can do this:This will prevent the algorithm from buying the same option again if it is already in the portfolio. You can add a similar check in the
buyputs
method.Remember, this is just a suggestion based on the provided code and the described problem. Depending on the specifics of your algorithm, you might need to adjust this solution.
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.
Muhammad Yusuf Bin Salleh
I tried adding that but it gives the following error:
Runtime Error: This asset symbol (AMD 2ZXJXM099P2W6|AMD R735QTJ8XC9X) was not found in your security list. Please add this security or check it exists before using it with 'Securities.ContainsKey("AMD 2ZXJXM099P2W6|AMD R735QTJ8XC9X")'
at buyputs
if self.Portfolio[self.put].Invested:
in main.py: line 140
at OnData
self.buyputs(security)
in main.py: line 45
Muhammad Yusuf Bin Salleh
Unfortunately, it is still buying every minute.
Ashutosh
Hi Muhammad Yusuf Bin Salleh
Liked your work and I was curious to run a backtest myself and debug.
To eye a view on your issue of buying the same contract every minute you can put a simple logic to store the options bought in a list and remove them as you liquidate positions to avoid buying them back every minute.
I also observed that there are not a lot of options being filtered using the current filtration logic so you might want to check this and make a few changes here:
I have attached the backtest for your reference. Hope this gives some direction.
Best,
Ashutosh
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.
Muhammad Yusuf Bin Salleh
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!