Overall Statistics
Total Trades
420
Average Win
1.60%
Average Loss
-1.72%
Compounding Annual Return
1.292%
Drawdown
24.800%
Expectancy
0.076
Net Profit
25.201%
Sharpe Ratio
0.197
Loss Rate
44%
Win Rate
56%
Profit-Loss Ratio
0.93
Alpha
0.045
Beta
-1.444
Annual Standard Deviation
0.082
Annual Variance
0.007
Information Ratio
-0.046
Tracking Error
0.082
Treynor Ratio
-0.011
Total Fees
$2216.16
class TurnOfMonthSPY(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2001,1, 11)  #Set Start Date
        self.SetEndDate(2018,7,11)    #Set End Date
        self.SetCash(100000)           #Set Strategy Cash
        self.AddEquity("SPY", Resolution.Daily)
        self.sell_flag = False
        self.days = 0
        #this event triggers the algorithm to sell during the first trading day of the month
        self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.AfterMarketOpen("SPY", 1), self.rebalance)
        #this event triggers the algorithm to purchase during the last trading day of the month
        self.Schedule.On(self.DateRules.MonthEnd("SPY"), self.TimeRules.AfterMarketOpen("SPY", 1), self.purchase)
    
    #immediately purchases the ETF at market opening    
    def purchase(self):
        self.SetHoldings("SPY", 1)
    
    #switches the sell_flag to True to wait 3 trading days before liquidating
    def rebalance(self):
        self.sell_flag = True
        
    def OnData(self, data):
        if self.sell_flag:
            self.days += 1
            #liquidates and resets self.days and the sell_flag
            if self.days == 3:
                self.Liquidate()
                self.sell_flag = False
                self.days = 0