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
-1.409
Tracking Error
0.093
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
# Custom Average Dollar Volume Indicator

import numpy as np

# -------------------------------------------------------------------
SMA = 63;
# -------------------------------------------------------------------

class CustomIndicatorAlgorithm(QCAlgorithm):
    
    def Initialize(self):
        self.SetStartDate(2021, 7, 1)  
        self.SetCash(1000000)
        
        self.AddUniverse(self.CoarseUniverseSelection)
        self.adv = {}
        
    def CoarseUniverseSelection(self, coarse):
        sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)[:100]
        
        for sec in sortedByDollarVolume:
            if sec not in self.adv:
                # initiate a new instance of the class
                self.adv[sec.Symbol] = AverageDollarVolume(SMA)
            
                # warm up
                history = self.History(sec.Symbol, SMA, Resolution.Daily)
                for bar in history.iloc[:-1].itertuples():     # leave the last row
                    self.adv[sec.Symbol].Update(bar.close, bar.volume)
            
            # Update with newest dollar volume
            self.adv[sec.Symbol].Update(sec.AdjustedPrice, sec.Volume)
        
        # Sort by SMA of dollar volume and take top 10
        sortedBySMADV = sorted(self.adv.items(), key=lambda x: x[1].Value, reverse=True)[:10]
        # Get Symbol object (the key of dict)
        self.filtered = [x[0] for x in sortedBySMADV]
        
        return self.filtered

    def OnEndOfDay(self, symbol):
        if self.IsWarmingUp or not any(self.adv): return
        
        self.Plot("Indicator", symbol, float(self.adv[symbol].Value))
        
            
class AverageDollarVolume(PythonIndicator):  # Average Dollar Volume
    def __init__(self, SMA):
        self.dv = RollingWindow[float](SMA)
        self.Value = 0
    
    def Update(self, price, volume):
        self.dv.Add(price * volume)

        if self.dv.IsReady:
            self.Value = pd.Series(self.dv).mean()
            return True   
            
        return False