Introduction
The turn of the month is an effect on stock Indices which states that stocks will rise during the last day before the end of the month and the first three days of each month. Researchers believe this significance comes as a result of pension funds receiving cash flows and reinvesting in the market, along with this period being a natural point for portfolio rebalancing between retail and professional investors. This algorithm is an approximation of the following strategy.
Method
We start by creating a Scheduled Event, MonthEnd()
, that will trigger the algorithm to buy SPY.
self.schedule.on(
self.date_rules.month_end(self.spy),
self.time_rules.after_market_open(self.spy, 1),
self.purchase)
We will purchase the SPY immediately, and we will wait 3 trading days, as suggested, before liquidating our portfolio. The boolean self.portfolio.invested
will help us wait 3 days before executing the liquidate order in OnData(). The Equity Index is bought and sold every month.
def purchase(self):
''' Immediately purchases the ETF at market opening '''
self.set_holdings(self.spy, 1)
self.days = 0
def on_data(self, data):
if self.portfolio.invested:
self.days += 1
# Liquidates after 3 days
if self.days > 3:
self.liquidate(self.spy, 'Liquidate after 3 days')
Derek Melchin
See the attached backtest for an updated version of the algorithm in PEP8 style.
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.
Jing Wu
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!