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
-2.378
Tracking Error
0.137
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
import pandas as pd

class CalculatingFluorescentYellowDogfish(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 10, 13)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash
        
        self.AddUniverse(self.Universe.DollarVolume.Top(3))
        self.AddAlpha(MyAlpha())


class MyAlpha(AlphaModel):
    symbol_data_by_symbol = {}
    
    def Update(self, algorithm, data):
        if data.Time.minute != 0:
            return []
        
        for symbol, symbol_data in self.symbol_data_by_symbol.items():
            if symbol_data.IsReady:
                algorithm.Plot("Indicator", str(symbol), symbol_data.indicator.Current.Value)
        return []
    
    def OnSecuritiesChanged(self, algorithm, changes):
        for security in changes.AddedSecurities:
            self.symbol_data_by_symbol[security.Symbol] = SymbolData(algorithm, security.Symbol)
        
        for security in changes.RemovedSecurities:
            symbol_data = self.symbol_data_by_symbol.pop(security.Symbol, None)
            if symbol_data:
                symbol_data.dispose()
                

class SymbolData:
    def __init__(self, algorithm, symbol):
        self.algorithm = algorithm
        self.symbol = symbol
        
        self.price = None
        self.indicator = SimpleMovingAverage(10)
        
        self.event = algorithm.Schedule.On(algorithm.DateRules.EveryDay(symbol), \
                                   algorithm.TimeRules.BeforeMarketClose(symbol, 30), 
                                   self.before_market_close)
        
        # Setup consolidator
        self.consolidator = TradeBarConsolidator(1)
        self.consolidator.DataConsolidated += self.consolidation_handler
        algorithm.SubscriptionManager.AddConsolidator(symbol, self.consolidator)

        # Warm up indicator
        history = algorithm.History(symbol, int(60*6.5*10), Resolution.Minute)
        if history.empty or 'close' not in history.columns:
            return
        for _, close_by_minute in history.loc[symbol].close.groupby(pd.Grouper(freq='D')):
            if len(close_by_minute) >= 30:
                self.indicator.Update(close_by_minute.index[-30], close_by_minute.iloc[-30])
    
    @property
    def IsReady(self):
        return self.indicator.IsReady
    
    def consolidation_handler(self, sender, bar):
        self.price = bar.Close
    
    def before_market_close(self):
        if self.price:
            self.indicator.Update(self.algorithm.Time, self.price)
    
    def dispose(self):
        self.algorithm.SubscriptionManager.RemoveConsolidator(self.symbol, self.consolidator)
        self.algorithm.Schedule.Remove(self.event)