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
-9.062
Tracking Error
0.055
Treynor Ratio
0
Total Fees
$0.00
class Sample(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 12, 1)
        self.SetCash(100000)
        self.strategies = {}
        self.AddUniverse(self.selection_coarse)

    def selection_coarse(self, coarse):
        selected = [ x for x in coarse if x.HasFundamentalData and (x.Price >= 20) ]
        dollar_volume = sorted(selected, key=lambda x: x.DollarVolume, reverse=True)
        top = dollar_volume[:5]
        symbols = [x.Symbol for x in top]
        return symbols

    def OnSecuritiesChanged(self, changes):
        for security in changes.AddedSecurities:
            symbol = security.Symbol
            if symbol not in self.strategies:
                self.strategies[symbol] = Strategy(self, symbol)
                
        for security in changes.RemovedSecurities:
            symbol = security.Symbol
            if symbol in self.strategies:
                strategy = self.strategies.pop(symbol, None)
                self.SubscriptionManager.RemoveConsolidator(symbol, strategy.consolidator_intraday)
    
    def OnData(self, data):
        pass

class Strategy:
    def __init__(self, qc, symbol):
        self.qc = qc
        self.symbol = symbol
        
        self.consolidator_intraday = TradeBarConsolidator(timedelta(5))
        self.consolidator_intraday.DataConsolidated += self.handler_intraday
        self.qc.SubscriptionManager.AddConsolidator(self.symbol, self.consolidator_intraday)
        
        self._ema_intraday = ExponentialMovingAverage(8)
        self.ema_intraday = RollingWindow[ExponentialMovingAverage](5)

        # HISTORY PULL TO UPDATE CONSOLIDATOR
        self.history = self.qc.History(self.symbol, 300).loc[self.symbol]
        for bar in self.history.itertuples():
            tradebar = TradeBar(bar.Index, self.symbol, bar.open, bar.high, bar.low, bar.close, bar.volume, timedelta(minutes=1))
            self.consolidator_intraday.Update(tradebar)
            
        # CHECK IF INDICATOR READY
        if self._ema_intraday.IsReady:
            self.qc.Debug(f'{symbol} - Indicator Ready')
        else:
            self.qc.Debug(f'{symbol} - Indicator Not Ready')

    def handler_intraday(self, sender, bar):
        self._ema_intraday.Update(bar.EndTime, bar.Close)
        self.ema_intraday.Add(self._ema_intraday)