Hi All,
I have noticed in the backtesting of my algorithm that the code is not purchasing calls when it should be. Sometimes it will buy them, but other times it will go 15-20 days without making one purchase even though there should be at least 3 buys per day based off of the indicator that I am giving it. I took my buy conditions out for security purposes, but put in arbitrary buy signals in and you can see that it is not purchasing the contracts when it should be. I placed some Debug strings in to see where it is failing and it seems that whenever I reach for j in data.OptionChains: chains = j.Value, self.BuyCall(chains), (line ~72) it only iterates through every once in a while. Could someone explain what is going on here and how to get around it? I also know that this code is still very ugly; I still have a bit of work to do but didnt want to work on anything else until I can get these buys down properly. Another side question Ill ask while I'm here: can you call your BuyCall() and pass an option chain without being in the OnData method? I didn't think you could, but my algo figures out the buy conditions in a consolidator's event handler, so at the moment, I just have it setting a flag and then the OnData will look at that flag each time and make decisions based off of the flag. Is that the right way of going about that? I know that means that buy and sells will be one minute later than the point in which the flag is set, so I'm sure there is a better way of doing it. Thank you for the help in advance!! Oh, and I am obviously not worried about the result/profits of the backtest as I don't have my strategies implemented into this code.
Nico Xenox
Hey jclute17,
the problem here is that you have an option filter that is extremely tight. This will result in less tradable option contracts that fall under the category of the specific filter.
You will have to change some values and see how it reacts.
To learn more about options please take a look at this and this.
Best,
Nico
Jclute17
Hi Nico,
Thank you for your response. I have thought about that and played with the values, and I am also very familiar with both of the pages you linked. The filter set is to look for 1 bar itm and 0 dte. I know that there are many options every single day that have those values, yet my algo will go many days in a row without purchasing a call option even when the buy signal tells it to. I have a hard time believing that there are just no contracts that are close to the strike price and zero day expiration on all of those days. I do understand that data.OptionChains is empty a lot of the time and that can be traced back to the filter, but even if I increase the range for expiration, it will still go up to a month without taking a trade even if the buy conditions are correct. Also, while on the subject of the the filter, I have found that if I set the expiry range to 0, 1, then have reverse=True, it will pull contracts that still have 20 days til expiration. I don't understand how this is happening if the filter should only be pulling contracts with 0 or 1dte. I would really appreciate some explanation of what I am doing wrong here with SetFilter(). Thanks again
Nico Xenox
Hey jclute17,
I played around with your code and this is what I found out:
CHAIN_NONE(pink): prints when there are no values in the option chain
LIQ(red): liquidation process
BUYFLAG(green): every time buyflag gets triggered to TRUE
BUYCALL(yellow): process when it enters buy function
QUANT(bars, blue): len(chains)
As you can see it does not have option chains data all the time. I will add my code so you can play around with it.
So I spent even more time trying to show that there isn't data trying to request options in the notebook. This is the result:
As you can see these dates are exactly the ones where the algo buys options. These were the only expiry dates in that timeframe.
The notebook code and algo code should be attached so that you can test them out.
Please accept my answer if you're satisfied with my answer.
Best,
Nico
Jclute17
Hi Nico, thank you for your response and I apologize for not being able to get back/accept your answer quicker. I like this backtesting tool, it will definitely help in backtesting this code as well as future projects as well. I understand that the options chain is empty due to the small filter, but I still don't understand why there are no available contracts when (in my opinion) there should be.
Nico Xenox
Hey jclute17,
sorry for making you wait. I looked a bit over the code and tried out something new. Normally weeklys are not automatically added. So I added a universe filter for options:
Now I think that I archived what you are looking for.
Hope it helps ;)
Please accept this if you're satisfied with my answer 😊
Best,
Nico
Jclute17
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!