Overall Statistics
Total Trades
112
Average Win
0.78%
Average Loss
-0.82%
Compounding Annual Return
-3.127%
Drawdown
6.500%
Expectancy
-0.028
Net Profit
-1.568%
Sharpe Ratio
-0.192
Probabilistic Sharpe Ratio
15.674%
Loss Rate
50%
Win Rate
50%
Profit-Loss Ratio
0.94
Alpha
-0.012
Beta
0.024
Annual Standard Deviation
0.092
Annual Variance
0.008
Information Ratio
0.991
Tracking Error
0.224
Treynor Ratio
-0.73
Total Fees
$362.55
Estimated Strategy Capacity
$30000000.00
Lowest Capacity Asset
AAPL R735QTJ8XC9X
# Fading The Gap

from AlgorithmImports import *

# ---------------------------------------------------
STOCK = "AAPL"; PERIOD = 100; NUM_DEVIATIONS = 3.000; 
# ---------------------------------------------------

class FadingTheGap(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2022, 1, 1)
        self.SetEndDate(2022, 7, 1)
        self.SetCash(100000) 
        res = Resolution.Minute
        self.stock = self.AddEquity(STOCK, res).Symbol 
        self.SetWarmUp(PERIOD, res)
        self.Schedule.On(self.DateRules.EveryDay(self.stock), self.TimeRules.BeforeMarketClose(self.stock, 0), self.ClosingBar) 
        self.Schedule.On(self.DateRules.EveryDay(self.stock), self.TimeRules.AfterMarketOpen(self.stock, 1), self.OpeningBar)
        self.Schedule.On(self.DateRules.EveryDay(self.stock), self.TimeRules.AfterMarketOpen(self.stock, 45), self.ClosePositions)         
        self.window = RollingWindow[TradeBar](2)
        self.volatility = self.STD(self.stock, PERIOD, res)

    
    def OpeningBar(self):
        if self.IsWarmingUp: return
        if self.stock in self.CurrentSlice.Bars:
            self.window.Add(self.CurrentSlice[self.stock])         
        if not self.window.IsReady or not self.volatility.IsReady: return       
        delta = self.window[0].Open - self.window[1].Close         
        deviations = delta / self.volatility.Current.Value           
        if deviations < -NUM_DEVIATIONS:
            self.SetHoldings(self.stock, 1)
        elif deviations > NUM_DEVIATIONS:
            self.SetHoldings(self.stock, -1)
            
        
    def ClosePositions(self):
        self.Liquidate(self.stock)
        
        
    def ClosingBar(self):
        self.window.Add(self.CurrentSlice[self.stock])