Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
class FadingTheGap(QCAlgorithm):

    def Initialize(self):
        #Backtesting parameters
        self.SetStartDate(2017, 11, 1)
        self.SetEndDate(2018, 11, 1)
        self.SetCash(100000)
        self.minimumDollar = 100
        self.maximumDollar = 5000
        self.topVolume     = 100
        
        # Universe Settings
        self.UniverseSettings.Resolution = Resolution.Minute
        self.AddUniverse(self.CoarseSelectionFunction)
        
        # Securities list that is dynamically changes by the universe coarse filter
        self.SecuritiesList = []
        
        # Schedules
        self.AddEquity("SPY", Resolution.Minute)
        self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("SPY", 1), self.OpeningBar)
        self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("SPY", 45), self.ClosePositions) 
        
    def CoarseSelectionFunction(self, coarse):
        # Sort equities by volume - highest volume first
        sortedByDollarVolume = sorted(coarse, key=lambda c: c.DollarVolume, reverse=True)
        
        # Filter equities by price
        filteredByPrice = [c.Symbol for c in sortedByDollarVolume if c.Price > self.minimumDollar and c.Price < self.maximumDollar]
        
        # Retrieve top equities
        self.SecuritiesList = filteredByPrice[:self.topVolume] 
        return self.SecuritiesList 
        
    def OpeningBar(self):
        
        securityDelta      = {}
        securitySTD        = {}
        securityDeviations = {}
        
        # Go through all the top traded securities of the day
        for security in self.SecuritiesList:
            
            # Decode symbol
            symbol = self.Symbol(security).Value
            
            # Retrieve the previous closing price of those securities
            history = self.History(security, 2, Resolution.Daily)
            if history.empty: continue
            previousClose   = history.iloc[len(history.index) - 2]['close']
            if previousClose <= 0: continue
        
            # Retrieve the current day open of the same securities
            if not self.Securities.ContainsKey(security): continue
            if self.Securities[security] is None: continue 
            if self.Securities[security].Open <= 0: continue
            currentOpen  = self.Securities[security].Open
            
            delta = 0
            if currentOpen < previousClose:
                # Record the delta if the open is less than the previous close in securityDelta dictionary
                delta = currentOpen - previousClose
            else: continue
            
            # Retrieve the standard deviation of the security for the past 60 minutes
            velocityHistory = self.History(security, 60, Resolution.Minute)
            std = velocityHistory.loc[:, 'close'].std()
            
            # Calculate and record the deviation if it is 3 deviations away
            # 68% (1 sigma deviation)
            # 95% (2 sigma deviation)
            # 99.7% (3 sigma deviation)
            # < -3 says that the drop is a .3% deviation 
            deviation = delta / std
            if deviation < -3:
                securityDeviations[symbol] = deviation
                self.Log(deviation)
          
        if len(securityDeviations) > 0:     
            # Sort any notable deviations by ascending order - strongest deviations first
            sortedDeviations = sorted(securityDeviations.items(), key=lambda x: x[1])
            
            # Retrieve the top two winners
            topDeviations = sortedDeviations[:2]
            
            # Invest 100% in the security with the biggest deviation
            for security in topDeviations:
                self.SetHoldings(security[0], 1)

    def ClosePositions(self):
        self.Liquidate()