self.Portfolio.Invested seems to miss that I have options in my holdings. Is there another way to check that I have invested options?
QUANTCONNECT COMMUNITY
self.Portfolio.Invested seems to miss that I have options in my holdings. Is there another way to check that I have invested options?
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.
Eugene Ng
See this backtest, where I get the error Insufficient Buying Power - makes no sense to me as I am net positive when this happens. I threw in a Liquidate to see what would happen but nothing works. Can someone help?
Jack Simonson
Hi Eugene,
The reason that the algorithm is saying there is Insufficient Buying Power is because the account doesn't have enough capital to exercise all the options contracts when they expire. Given the filter in the algorithm, the options contracts that the algorithm decides to trade will expire Jan 12. Each contract gives the purchaser the right to buy or sell 100 shares of the underlying, and so given the price of SPY around Jan 12 (~$270), the total cost of exercising one contract would be about $27,000 and so the cost of exercising 5 calls and 5 puts would go well beyond the amount of margin available.
Try setting the initial cash to a larger number or reducing the number of contracts that the algorithm purchases. Alternatively, you can add trade logic to check for when the contract tickers are about to delist (expire) and ensure that your portfolio doesn't hold any contracts at their expiration date if that's the objective of the algorithm.
Eugene Ng
Thanks Jack for reviewing my code, it's much appreciated - I see your point. But shouldn't the liquidate function sell those options at 3:45 every day - therefore I should never be holding them when they expire? And I am always purchasing options a week out from expiry as defined by the filter. I must be missing something here; does SetFilter only check for those options at the StartDate? In that case, I would have to reset the filter every day, wouldn't I? I suppose it would go in my OnData function?
Jack Simonson
Right now the Schedule On method is telling the algorithm to liquidate every Monday. To make sure it liquidates every day at 3:45, you would want to change it toÂ
self.Schedule.On(self.DateRules.EveryDay(), \
self.TimeRules.At(3, 45), \
self.SellTime)
Set Filter gets updated every minute, so you the Options in the algorithm's Universe will be updated accordingly. I would recommend altering the Scheduled Event and then seeing if the algorithm runs as you had hoped. Otherwise, you can set a filter with a wider expiration date filter and then filter for those options you want to trade inside the OnData() method, similar to the method demonstrated in this algorithm or in the Documentation for Options Data here.
Eugene Ng
Oh, I see, it looks like I'm just stupid! Should've caught that myself. Thanks Jack for your help - think I've got it now.Â
Jack Simonson
Happy to help! One last thing to note is that datetime uses a 24-hour format, so 3:45 pm is rendered as 15:45 and then your Scheduled Event should be codedÂ
self.Schedule.On(self.DateRules.EveryDay(), \
self.TimeRules.At(15, 45), \
self.SellTime)
My apologies for not catching that earlier!
Eugene Ng
Hi Jack, perhaps you could take a look at this backtest as well. Referring back to your comments about altering the filter and then altering the scheduled event. So I changed the filter to look for expiration dates 15 - 20 days out and set the scheduled event to occur every Friday; yet there are still options that I end up holding until expiry. How can this be? Another thing I'm missing?
Jack Simonson
Eugene,
Your Scheduled Event liquidate code only executes every Friday and sometimes the Portfolio is holding Put Options that expire before a Friday, and so these will automatically exercise at expiration before the algorithm has a chance to liquidate the position. One option expired on Feb 14, which was a Wednesday, and another option expired on March 5, which was a Monday. I've added a few Log statements to the backtest you attached so you can see what the expiration date of the contract being purchased is. It looks like the ATM Put Option that is being traded is often the same as the previous one as well. You can check to see if an option is about to expire by comparing Expiry and current dates to ensure the position is Liquidated before expiration if you still only want to execute the Scheduled Event on specific days of the week. I would recommend adding this in the OnData method to ensure you have a chance to liquidate just before expiration.
Eugene Ng
Hm, I think the confusing thing about that one is why I bought those in the first place, given my filter. I had set the filter to search for expiration to 15-20 days out. In the first case I bought the 2/14 expirations on 2/9, and on the second it looks like I bought the 3/5 options on 3/2. Shouldn't those have been filtered out by my universe? (I really have no desire to purchase the closest expiry options!)
Eugene Ng
Ah, Jack I think I figured it out. I was using timedelta in the wrong manner. It looks like TimeSpan.FromDays() works much better! Thanks again for looking at my code, and for the sample code.Â
Â
EDIT: I seem to have spoken too soon. TimeSpan.FromDays() does the same thing.
Eugene Ng
Been doing some tests.
Best I can tell, it seems as if the options universe does not get completely reset - it just adds new options to the universe every time I set the filter. My fix has been to pick the options with the furthest expiry (ie the options I've added to the universe upon every slice) and go ahead with those. In my previous tests I believe I just sort them and pick the ones that are closest ATM.
Jack Simonson
Hi Eugene,
So I've debugged some more and the options universe is updating every minute, so no options outside of the filters are sticking around, except when the algorithm holds a position in such an option. The way the algorithm is written right now, the Liquidate statement fires at 15:45 on Fridays but this still leaves the option that you no longer want to purchase in the universe and so the algorithm goes back and buys it again before the universe has a chance to re-set. There are a few ways to handle this, but the easiest would be to add a switcher that prevents the algorithm from trading after the Liquidate call through to Market Close. Have a look at the backtest I've attached to see the modification to the original algorithm.
Eugene Ng
Jack, that's perfect. Makes a ton of sense, too. Wow, thank you for taking the time to go through all that!!
Eugene Ng
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!