Overall Statistics
Total Orders
162
Average Win
0.98%
Average Loss
-0.94%
Compounding Annual Return
-1.647%
Drawdown
14.900%
Expectancy
-0.064
Start Equity
100000
End Equity
94671.21
Net Profit
-5.329%
Sharpe Ratio
-0.72
Sortino Ratio
-0.339
Probabilistic Sharpe Ratio
0.504%
Loss Rate
54%
Win Rate
46%
Profit-Loss Ratio
1.05
Alpha
-0.04
Beta
0.044
Annual Standard Deviation
0.052
Annual Variance
0.003
Information Ratio
-0.648
Tracking Error
0.145
Treynor Ratio
-0.847
Total Fees
$195.13
Estimated Strategy Capacity
$64000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
Portfolio Turnover
13.41%
# region imports
from AlgorithmImports import *
from QuantConnect.Indicators import RollingWindow
# endregion
class QCConsolidationRollingWindows(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2018, 1, 1)
        self.SetStartDate(2021, 1, 1)
        self.SetCash(100000)
        self.spy = self.AddEquity("SPY", Resolution.MINUTE).Symbol
        
        self.rw = RollingWindow[TradeBar](2)
        
        self.consolidate(self.spy, Resolution.DAILY, self.CustomBarHandler)
        self.schedule.on(self.DateRules.EveryDay(self.spy), 
                        self.time_rules.before_market_close(self.spy, 15),
                        self.ExitPositions)
    
    def OnData(self, data: Slice):
        if not self.rw.IsReady:
            return
            
        if not (self.time.hour == 9 and self.time.minute == 31):
            return
        if data[self.spy].Open >= 1.01 * self.rw[0].Close:
            self.set_holdings(self.spy, -1)
        elif data[self.spy].Open <= 0.99 * self.rw[0].Close:
            self.set_holdings(self.spy, 1)
    
    def CustomBarHandler(self, bar):
        self.rw.Add(bar)
    
    def ExitPositions(self):
        self.liquidate(self.spy)