Overall Statistics
Total Trades
208
Average Win
0%
Average Loss
0.00%
Compounding Annual Return
-0.110%
Drawdown
0.200%
Expectancy
-1
Net Profit
-0.231%
Sharpe Ratio
-3.799
Probabilistic Sharpe Ratio
0%
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.001
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-0.467
Tracking Error
0.229
Treynor Ratio
-19.303
Total Fees
$208.00
class ResistanceVentralCircuit(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2018, 6, 17)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash
        
        self.symbol = self.AddEquity('SPY', Resolution.Minute).Symbol
        
        self.UniverseSettings.Resolution = Resolution.Daily
        self.AddUniverse(self.CoarseFilter)
        
        self.Schedule.On(self.DateRules.EveryDay(self.symbol), self.TimeRules.BeforeMarketClose(self.symbol, 10), self.BeforeClose)

        self.stop_trading = False
        
        self.made_trade = False
        
        self.num_securities = 50
        
        self.Gaps = {}
        
        self.curr_day = -1

    def CoarseFilter(self, coarse):
        sortedByDollarVolume = sorted(coarse, key=lambda c: c.DollarVolume, reverse=True)[:self.num_securities]
        return [x.Symbol for x in sortedByDollarVolume]
    
    def OnData(self, data):
        if not self.IsMarketOpen(self.symbol):
            return
        
        # get current day open
        if self.curr_day != self.Time.day:
            for symbol in self.Gaps:
                self.Gaps[symbol].StopTrading = False
                self.Gaps[symbol].MadeTrade = False
                if data.ContainsKey(symbol) and data[symbol] is not None:
                    self.Gaps[symbol].Open = data[symbol].Open
                else:
                    self.Gaps[symbol].Open = -1
    
        #to_buy = []
        self.curr_day = self.Time.day
        
        for symbol, gap in self.Gaps.items():
            if gap.StopTrading:
                continue
            #
            elif gap.Open < 0 or gap.Close < 0:
                gap.StopTrading = True
                continue
            # if gap down doesn't meet our 1% threshold, don't trade
            elif gap.Open / gap.Close > .99:
                gap.StopTrading = True
                continue
            elif not data.ContainsKey(symbol) or data[symbol] is None:
                continue
            
            curr_price = data[symbol].Close
            
            # breakout past yesterday's close
            if not gap.MadeTrade and curr_price > 1.005 * gap.Close and curr_price < 1.01 * gap.Close:
                self.SetHoldings(symbol, .02)
                #self.MarketOrder(symbol, 1)
                gap.MadeTrade = True
       
            elif gap.MadeTrade and (curr_price < gap.Close or curr_price > 1.02 * gap.Close):
                self.Liquidate(symbol)
                gap.StopTrading = True
        
    def BeforeClose(self):
        self.Liquidate()
        for symbol in self.Gaps:
            if self.CurrentSlice.ContainsKey(symbol) and self.CurrentSlice[symbol] is not None:
                self.Gaps[symbol].Close = self.Securities[symbol].Price
            else:
                self.Gaps[symbol].Close = -1
            self.Gaps[symbol].StopTrading = True
            
    def OnSecuritiesChanged(self, changed):
        for security in changed.RemovedSecurities:
            self.Gaps.pop(security.Symbol)
        
        for security in changed.AddedSecurities:
            self.Gaps[security.Symbol] = Gap()
        
class Gap:
    def __init__(self):
        self.Close = -1
        self.Open = -1
        self.MadeTrade = False
        self.StopTrading = False