Hi.
is there a way to get the number of business days till options expiration?
need to take into consideration holidays and weekends
thanks
QUANTCONNECT COMMUNITY
Hi.
is there a way to get the number of business days till options expiration?
need to take into consideration holidays and weekends
thanks
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.
Jared Broad
Yes, you can use `Time.TradableDates` function which takes a security object and the start and end DateTime objects.
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.
Gil Sapir
I'm not sure this method is enabled in python, I'm trying:
sec = self.Securities['SPY']
t = Time.TradeableDates( sec ,self.Time, self.Time+timedelta(6) )
and getting this error :
Runtime Error: Trying to dynamically access a method that does not exist throws a TypeError exception. To prevent the exception, ensure each parameter type matches those required by the TradeableDates method. Please checkout the API documentation.
at OnData in main.py:line 75
TypeError : No method matches given arguments for TradeableDates
Alethea Lin
Hi Gil,
You are right that this method is not supported in Python. Please use the following code:
# Method 1 days = list(Time.EachTradeableDay(self.Securities["SPY"], self.Time, self.Time + timedelta(6))) self.Log(f'Tradeable Dates 1: {len(days)}') # Method 2 days = list(self.TradingCalendar.GetDaysByType(TradingDayType.BusinessDay, self.Time, self.Time + timedelta(6))) self.Log(f'Tradeable Dates 2: {len(days)}')
You can also read more about TradingCalendar here. Hope this helps!
AM H
Ah, just what I was looking for. How would I add this to a close/liquidate options function X days beforfe the contract expiry, checkin geach day on the next contract etc.Â
Example:
BUY SPY 14 expiry contract every day, and each day SELL the one that is 7 days from expiration.Â
Would need to wait for the first cycle of 7 days to pass and then every day a contract is being bought and another sold.
AM H
Here is my liquidate code:
def close_options(self): self.Log("Close options every day") # liquidate options for x in self.Portfolio: if x.Value.Invested: self.Liquidate(x.Key)
And building with your code:
def close_options(self): self.Log("Close options every day") # liquidate options for x in self.Portfolio: x = list(Time.EachTradeableDay(self.Securities["SPY"], self.Time, self.Time + timedelta(6))) self.Log(f'Tradeable Dates 1: {len(x)}'): if x.Value.Invested: self.Liquidate(x.Key)
Â
Shile Wen
Hi AM H,
One way to get the days until expiration would be to access an option’s expiry date, and compute the timedelta between the expiry date and self.Time. Alternatively, if we wanted to exclude non trading days, we can use the method mentioned by Athelea. I’ve shown how to do both in the attached backtest.
Best,
Shile Wen
Gil Sapir
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!