Overall Statistics
Total Trades
33
Average Win
1.21%
Average Loss
-3.93%
Compounding Annual Return
-85.239%
Drawdown
68.700%
Expectancy
-0.891
Net Profit
-43.511%
Sharpe Ratio
-0.322
Probabilistic Sharpe Ratio
20.349%
Loss Rate
92%
Win Rate
8%
Profit-Loss Ratio
0.31
Alpha
-0.552
Beta
0.246
Annual Standard Deviation
1.432
Annual Variance
2.05
Information Ratio
-0.579
Tracking Error
1.435
Treynor Ratio
-1.872
Total Fees
$33.00
Estimated Strategy Capacity
$290000000.00
from Alphas.MacdAlphaModel import MacdAlphaModel

class WellDressedYellowGreenFish(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2021, 1, 1)  # Set Start Date
        self.SetCash(10000)  # Set Strategy Cash
        #self.AddAlpha(MacdAlphaModel(12, 26, 9, MovingAverageType.Simple, Resolution.Daily))
        
        self.AddUniverse(self.CoarseSelectionFunction)
        self.UniverseSettings.Resolution = Resolution.Daily
        self.averages = { }
    
    def CoarseSelectionFunction(self, universe):  
        selected = []
        universe = sorted(universe, key=lambda c: c.DollarVolume, reverse=True)  
        universe = [c for c in universe if c.Price > 5][:100]

        for coarse in universe:  
            symbol = coarse.Symbol
            
            if symbol not in self.averages:
                # 1. Call history to get an array of 200 days of history data
                history = self.History(symbol, 200, Resolution.Daily)
                
                #2. Adjust SelectionData to pass in the history result
                self.averages[symbol] = SelectionData(history) 

            self.averages[symbol].update(self.Time, coarse.AdjustedPrice)
            
            if  self.averages[symbol].is_ready() and self.averages[symbol].fast > self.averages[symbol].slow:
                selected.append(symbol)
        
        return selected[:5]
        
    def OnData(self, data):
        for symbol, symbol_data in self.averages.items():
            tolerance = 0.0025
            holdings = self.Portfolio[symbol].Quantity if self.Portfolio.ContainsKey(symbol) else 0
            signalDeltaPercent = (symbol_data.macd.Current.Value - symbol_data.macd.Signal.Current.Value)/symbol_data.macd.Fast.Current.Value
            if holdings <= 0 and signalDeltaPercent > tolerance:  # 0.01%
                # longterm says buy as well
                self.SetHoldings(symbol, 0.2)
            elif holdings >= 0 and signalDeltaPercent < -tolerance:
                self.Liquidate(symbol)
                

class SelectionData():
    #3. Update the constructor to accept a history array
    def __init__(self, history):
        self.slow = ExponentialMovingAverage(100)
        self.fast = ExponentialMovingAverage(20)
        self.macd = MovingAverageConvergenceDivergence(12, 26, 9, MovingAverageType.Exponential)
        
        #4. Loop over the history data and update the indicators
        for bar in history.itertuples():
            self.fast.Update(bar.Index[1], bar.close)
            self.slow.Update(bar.Index[1], bar.close)
            self.macd.Update(bar.Index[1], bar.close)
            
    def is_ready(self):
        return self.slow.IsReady and self.fast.IsReady
        
    def update(self, time, price):
        self.fast.Update(time, price)
        self.slow.Update(time, price)
        self.macd.Update(time, price)