Overall Statistics
Total Trades
8
Average Win
8.16%
Average Loss
-0.34%
Compounding Annual Return
38.964%
Drawdown
7.000%
Expectancy
17.494
Net Profit
25.727%
Sharpe Ratio
1.838
Probabilistic Sharpe Ratio
70.822%
Loss Rate
25%
Win Rate
75%
Profit-Loss Ratio
23.66
Alpha
0.336
Beta
-0.028
Annual Standard Deviation
0.181
Annual Variance
0.033
Information Ratio
0.499
Tracking Error
0.41
Treynor Ratio
-11.929
Total Fees
$14.66
class TradeBeforeMonthEnd(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2020, 1, 1)
        self.SetCash(100000)
        self.DaysBefore = 3
        
        self.symbol = "SPY"    
        self.AddEquity(self.symbol, Resolution.Daily)

        self.Schedule.On(self.DateRules.EveryDay(self.symbol),
                         self.TimeRules.BeforeMarketClose(self.symbol, 30),
                         self.rebalance)


    def daysBeforeMonthEnd(self):
        bds = self.TradingCalendar.GetDaysByType(TradingDayType.BusinessDay,
                                                 self.Time,
                                                 self.Time+timedelta(days=30))
        return len([1 for day in bds if day.Date.month == self.Time.month]) - 1

    def rebalance(self):
        if self.daysBeforeMonthEnd() == self.DaysBefore:
            if not self.Portfolio.Invested:
                self.SetHoldings(self.symbol, 1)
            else:
                self.Liquidate()