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')


Reference

  1. Quantpedia - Turn of the Month in Equity Indexes