Overall Statistics
Total Trades
5
Average Win
0%
Average Loss
0%
Compounding Annual Return
989.539%
Drawdown
10.700%
Expectancy
0
Net Profit
8.169%
Sharpe Ratio
13.799
Probabilistic Sharpe Ratio
72.736%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
6.635
Beta
-3.015
Annual Standard Deviation
0.517
Annual Variance
0.268
Information Ratio
12.661
Tracking Error
0.577
Treynor Ratio
-2.368
Total Fees
$42.02
Estimated Strategy Capacity
$0
Lowest Capacity Asset
NMRD WRILNY45M0DH
class SymbolData:
    
    def __init__(self, algorithm, symbol):
        
        self.algorithm = algorithm
        self.symbol = symbol
        
        # Daily consolidator definition and subscription to data
        self.daily_consolidator = TradeBarConsolidator(timedelta(days=1))
        self.algorithm.SubscriptionManager.AddConsolidator(self.symbol, self.daily_consolidator)
        self.daily_consolidator.DataConsolidated += self.OnDailyBar
        
        # signal resolution consolidator definition and subscription to data
        self.consolidator = TradeBarConsolidator(timedelta(minutes=30))
        self.algorithm.SubscriptionManager.AddConsolidator(self.symbol, self.consolidator)
        self.consolidator.DataConsolidated += self.OnBar
        
        # self.algorithm.ResolveConsolidator
        # minute consolidator definition and subscription to data
        self.minute_consolidator = TradeBarConsolidator(timedelta(minutes=1))
        self.algorithm.SubscriptionManager.AddConsolidator(self.symbol, self.minute_consolidator)
        self.minute_consolidator.DataConsolidated += self.OnMinuteBar
        
        # Volume daily SMA
        self.vol_sma = SimpleMovingAverage(20)
        
        # 30 min resolution - 10 period SMA for price
        self.sma = SimpleMovingAverage(10)
        self.algorithm.RegisterIndicator(self.symbol, self.sma, self.consolidator)
        
        self.minute_bars = RollingWindow[TradeBar](30)
        self.bars = RollingWindow[TradeBar](3)
        
        self.WarmUpIndicators()
        
    def WarmUpIndicators(self):
        
        # returns a dataframe - to warmup daily volume sma
        history = self.algorithm.History(self.symbol, 20, Resolution.Daily)
        
        # to warm up 30 minute price sma and minute bar window
        # minute_history = self.algorithm.History(self.symbol, 330, Resolution.Minute)
        
        # # gets rid of symbol/ticker index in df
        # minute_history = minute_history.droplevel(0, 0)
        
        # # creates 30 min bars from minute bars
        # _30_minute_history = minute_history.resample('30T').ohlc()
        
        # # picks out required columns
        # _30_minute_history = _30_minute_history.drop([c for c in _30_minute_history.columns if c not in ['close', 'volume']], 1)
        
        
        # for bar in minute_history.itertuples():
            
        #     time = bar.Index 
        #     close = bar.close
        #     open = bar.open
        #     low = bar.low
        #     high = bar.high
        #     volume = bar.volume
            
        #     min_bar = TradeBar(time, self.symbol, open, high, low, close, volume)
                
        #     self.minute_bars.Add(min_bar)
        
        # m
        # for row in _30_minute_history.itertuples():
    
        #     time = row[0]
        #     open = row[1]
        #     high = row[2]
        #     low = row[3]
        #     close = row[4]
        #     volume = row[8]
            
        #     _30_min_bar = TradeBar(time, self.symbol, open, high, low, close, volume)
        
        #     self.bars.Add(_30_min_bar)
        #     self.sma.Update(time, close)

        
        
        for bar in history.itertuples():
            
            time = bar.Index[1]
            
            open = bar.open
            high = bar.high
            low = bar.low
            close = bar.close
            volume = bar.volume
            
            daily_bar = TradeBar(time, self.symbol, open, high, low, close, volume)
            
            self.vol_sma.Update(time, volume)
           
        
        
    def OnDailyBar(self, sender, bar):
        # Updates volume sma with latest daily bar data
        self.vol_sma.Update(bar.EndTime, bar.Volume)
       
       
    def OnBar(self, sender, bar):
       
        # Saves signal resolution bars
        self.bars.Add(bar)
    
    def OnMinuteBar(self, sender, bar):
        
        # Saves minute bars
        self.minute_bars.Add(bar)
        
    @property
    def IsReady(self):
        
        return self.vol_sma.IsReady and self.sma.IsReady and self.minute_bars.IsReady and \
            self.bars.IsReady
        
        
    def KillConsolidators(self):
        
        self.algorithm.SubscriptionManager.RemoveConsolidator(self.symbol, self.consolidator)
        
        self.algorithm.SubscriptionManager.RemoveConsolidator(self.symbol, self.minute_consolidator)
        
        self.algorithm.SubscriptionManager.RemoveConsolidator(self.symbol, self.daily_consolidator)
        
    @property
    def RecentDollarVolume(self):
        
        dollar_volume = 0
        
        for bar in list(self.minute_bars):
            
            dollar_volume += bar.Volume * bar.Close    
        
        return dollar_volume
        
    @property
    def CandlePatternSignal(self):
        
        # Close > Open
        # Wick < .5*Body
        # Body > 1.02*Open
        
        most_recent_bar = self.bars[0]
        
        close = most_recent_bar.Close
        open = most_recent_bar.Open
        high = most_recent_bar.High
        wick = high - close
        body = close - open
            
        return close > open and wick < 0.5 * body and close > 1.02 * open
    
    @property
    def UnusualVolume(self):
        
        vol_sma = self.vol_sma.Current.Value
        recent_volume = self.bars[0].Volume
        
        return recent_volume > 3 * vol_sma
    
    @property
    def UnusualVolumeSignal(self):
        volLim = bool(self.RecentDollarVolume > 500000)
        if volLim and self.CandlePatternSignal and self.UnusualVolume:
            return True
        
        return False
        #return self.RecentDollarVolume > 500000 and self.CandlePatternSignal and self.UnusualVolume
        
        
        
        
    
class TradeManagement:
    
    def __init__(self, algorithm, symbol):
        
        self.algorithm = algorithm
        self.symbol = symbol
        
        self.entry_limit_ticket = None
        self.stop_loss = None
        self.take_profit = None
        
        
    def LimitOrder(self, quantity, limit_price):
        
        # self.entry_limit_ticket = self.algorithm.LimitOrder(...)
        # self.stop_loss = ..
        # ....
        
        pass
    
    
    def CheckAndUpdate(self):
        
        current_market_price = self.algorithm.Securities[self.symbol].Price
        
        if not self.ActivePosition:
            return 
        
        # stop loss
        # take profit
        
        # self.algorithm.Liquidate
        
    
    def CancelEntryOrder(self):
        
        if self.entry_limit_ticket is not None:
            self.entry_limit_ticket.Cancel()
        
            self.stop_loss = None
            self.take_profit = None
        
        
    @property
    def ActivePosition(self):
        
        return self.algorithm.Portfolio[self.symbol].Invested
    
    
    def GetPositionSize(self):
        
        portfolio_value = self.algorithm.Portfolio.TotalPortfolioValue
        
        #...
from SymbolData import *

class LogicalRedOrangeGoshawk(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2021, 4, 30)
        self.SetEndDate(2021, 5, 11)
        self.SetCash(100000) 
        
        self.benchmark = "SPY"
        self.AddEquity("SPY", Resolution.Minute)
        self.AddEquity("IHT", Resolution.Minute)
        
        self.AddUniverse(self.SelectCoarse)
        self.UniverseSettings.Resolution = Resolution.Minute
        
        self.symbols = {}
        
        self.Schedule.On(self.DateRules.EveryDay(self.benchmark), self.TimeRules.AfterMarketOpen(self.benchmark, 31), self.Rebalance)
    
    def Rebalance(self):
        
        self.Debug(f"Universe Size... {len(self.symbols)}")
        
        for symbol, symbol_data in self.symbols.items():
            if not symbol_data.IsReady:
                self.Debug("we aint ready")
                continue
            # self.Debug(f"{symbol} - $V {symbol_data.RecentDollarVolume}, bar signal {symbol}")
            if symbol_data.UnusualVolumeSignal:
                self.SetHoldings(symbol, 0.10)
                
            # self.Debug(f"~~~~{symbol} - {self.Time}~~~~~")
            
            # self.Debug(f"Unusual Volume Signal: {symbol_data.UnusualVolumeSignal}")
            
            # self.Debug(f"30 Minute $ Volume: {symbol_data.RecentDollarVolume}")
            
            # self.Debug(f"Candle Pattern: {symbol_data.CandlePatternSignal}")
            
            # self.Debug(f"20 Day VOL SMA: {symbol_data.vol_sma.Current.Value}")
            # latest_bar = symbol_data.bars[0]
            # self.Debug(f"latest 30 min bar: {latest_bar.EndTime} --- {latest_bar}")
            
            # self.Debug(f"Unusual Volume: {symbol_data.UnusualVolume}")
            
            # self.Debug(f"{self.Time} -- {symbol} has a signal!")
        
    def SelectCoarse(self, coarse):
        
        filtered_by_price = [c for c in coarse if c.AdjustedPrice >= 4 and c.AdjustedPrice <= 8]
        return [c.Symbol for c in filtered_by_price]
        
    def OnSecuritiesChanged(self, changes):
        
        for security in changes.AddedSecurities:
            
            symbol = security.Symbol
            
            if symbol not in self.symbols and symbol.Value != self.benchmark:
                self.symbols[symbol] = SymbolData(self, symbol)
                self.Debug(symbol)
                
        for security in changes.RemovedSecurities:
            
            symbol = security.Symbol
            
            if symbol in self.symbols:
                
                symbol_data = self.symbols.pop(symbol, None)
                
                symbol_data.KillConsolidators()