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.


We start by creating a Scheduled Event, MonthEnd(), that will trigger the algorithm to buy SPY.

    self.TimeRules.AfterMarketOpen(self.spy, 1),

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.SetHoldings(self.spy, 1)
    self.days = 0

def OnData(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')


  1. Quantpedia - Turn of the Month in Equity Indexes