Overall Statistics
Total Trades
24
Average Win
0.90%
Average Loss
-0.45%
Compounding Annual Return
94.138%
Drawdown
1.500%
Expectancy
0.503
Net Profit
2.702%
Sharpe Ratio
6.296
Probabilistic Sharpe Ratio
84.774%
Loss Rate
50%
Win Rate
50%
Profit-Loss Ratio
2.01
Alpha
0.564
Beta
-0.3
Annual Standard Deviation
0.127
Annual Variance
0.016
Information Ratio
4.013
Tracking Error
0.392
Treynor Ratio
-2.655
Total Fees
$29.34
Estimated Strategy Capacity
$45000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
# Opening Range Breakout  

# --------------------------------------------------------------
STOCK = "SPY"; PERIOD = 30; SL = -0.005; TP = 0.01;
# --------------------------------------------------------------

class OpeningRangeBreakout(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2022, 4, 28)  
        self.SetEndDate(2022, 5, 12)  
        self.SetCash(100000) 
        self.stock = self.AddEquity(STOCK, Resolution.Minute).Symbol
        self.hh = self.MAX(self.stock, PERIOD, Resolution.Minute, Field.High)
        self.ll = self.MIN(self.stock, PERIOD, Resolution.Minute, Field.Low)
        self.SetWarmUp(PERIOD, Resolution.Minute)
        self.first_30_min_HH = 0
        self.first_30_min_LL = 0
        self.Schedule.On(self.DateRules.EveryDay(self.stock), self.TimeRules.AfterMarketOpen(self.stock, 31), 
            self.MorningCheck)
        self.Schedule.On(self.DateRules.EveryDay(self.stock), self.TimeRules.BeforeMarketClose(self.stock, 1), 
            self.EOD)
            

    def MorningCheck(self):
        if self.IsWarmingUp: return
        if not (self.hh.IsReady or self.ll.IsReady): return
    
        self.first_30_min_HH = self.hh.Current.Value
        self.first_30_min_LL = self.ll.Current.Value
        
    
    def OnData(self, data):
        if self.IsWarmingUp: return
        if not (self.hh.IsReady or self.ll.IsReady): return
    
        price = self.Securities[self.stock].Price
        pnl = self.Securities[self.stock].Holdings.UnrealizedProfitPercent
    
        if self.Portfolio[self.stock].Invested:
            if pnl >= TP: 
                self.Liquidate(self.stock, "Take Profit") 

            elif pnl < SL: 
                self.Liquidate(self.stock, "Stop Loss") 

        if (self.Time.time() < time(10, 2) or self.Time.time() >= time(11, 2)): return []
        if (self.first_30_min_HH == 0 or self.first_30_min_LL == 0): return
        

        if not self.Portfolio[self.stock].Invested:
            if price > self.first_30_min_HH:
                self.SetHoldings(self.stock, 1)
                
            elif price < self.first_30_min_LL:
                self.SetHoldings(self.stock, -1)
                

                

    def EOD(self): 
        self.first_30_min_HH = 0
        self.first_30_min_LL = 0
        if self.Portfolio[self.stock].Invested:
            self.Liquidate(self.stock, "EOD")