Goal: Create a scheduled event to liquidate all positions for a set of symbols 30 minutes before market close. (This set of symbols is found using a dynamic universe from coarse and fine filters.)
Intuitively, I know the event scheduler should be called once which has a method loop through each symbol to liquate the position. I'm struggling with this because of the single symbol callout in the BeforeMarketClose example which always use SPY and not a ‘symbol’ variable.
Below is an example I found on the discussion forum for a single stock, so I figure it'll take a similar form for an algorithm with a set of symbols.
Lastly, does the scheduled event belong in Initialize or somewhere else?
# In Initialize
# This defines a scheduled event which fires self.ClosePositions everyday SPY is trading 1 minute before SPY stops trading
self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.BeforeMarketClose("SPY", 1), self.ClosePositions)
def ClosePositions(self):
if self.Portfolio.Invested:
self.Liquidate()
Fred Painchaud
Hi Justin,
“for a set of symbols” and “found using universe selection”.
If you mean you want to liquidate ALL your portfolio (i.e. the current set of symbols you have a position in):
If you mean to liquidate a set of symbols that you have defined yourself in your code:
The above is simply ONE way to define the set of symbols you want to liquidate each day. It might not fit what you want to do.
Fred
Justin E
Thanks Fred, both of these work because I want them all liquidated so as not to carry overnight risk.
Do you mind explaining the function of “SPY” in the event schedulers DateRule and TimeRule? It seems irrelevant and I couldn't find an explanation in the documentation. Why not just remove it?
Justin
Fred Painchaud
Hi Justin,
Sure. In fact it is really “important”.
self.DateRules.EveryDay("SPY"): it means “for every day that SPY trades”. So the event will not fire on Saturdays and Sundays, for instance. It will also not fire for holidays for the market which trades SPY… But markets for other assets sometimes have other holidays…
self.TimeRules.BeforeMarketClose("SPY", 30): you can then infer here. Same deal, but for market close, so 16h00 here.
So those rules are “smart” wrt to trading days. You want your events NOT to fire when they should not…
Fred
Justin E
Got it, this makes much more sense when used this way. Perhaps the documentation could be more clear that the symbol is the reference for the market time, and not that the event scheduler is only initialized for that specific symbol. Thanks again Fred!
Justin E
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!