Overall Statistics
Total Trades
5
Average Win
2.96%
Average Loss
0%
Compounding Annual Return
24.893%
Drawdown
14.000%
Expectancy
0
Net Profit
12.956%
Sharpe Ratio
1.219
Probabilistic Sharpe Ratio
53.217%
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
0.267
Beta
-0.132
Annual Standard Deviation
0.178
Annual Variance
0.032
Information Ratio
-0.604
Tracking Error
0.265
Treynor Ratio
-1.642
Total Fees
$7.45
from datetime import timedelta
import numpy as np
from scipy import stats
from collections import deque

class ModulatedMultidimensionalReplicator(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 6, 1)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash
        self.AddAlpha(MOMAlphaModel())
        self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())

        
        self.AddEquity('GLD', Resolution.Daily)
        self.AddEquity('QQQ', Resolution.Daily)
        
    def OnData(self, data):
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
            Arguments:
                data: Slice object keyed by symbol containing the stock data
        '''

        # if not self.Portfolio.Invested:
        #    self.SetHoldings("SPY", 1)
        


class MOMAlphaModel(AlphaModel): 
    def __init__(self):
        self.mom = {} 
        self.indicator = True
        
    def OnSecuritiesChanged(self, algorithm, changes):
    
        
        for security in changes.AddedSecurities:
            symbol = security.Symbol
            
            self.mom[symbol] = My_Custom('My_Custom', symbol, 100)
            algorithm.RegisterIndicator(symbol, self.mom[symbol], Resolution.Daily)
        
            history = algorithm.History(symbol, 100, Resolution.Daily)
            self.mom[symbol].Warmup(history)
            
            if not self.mom[symbol]:
                self.indicator = False
               
          
    def Update(self, algorithm, data):
        insights = []
        
        # plot the indicator
        algorithm.Plot("Custom_Slope", "Value", list(self.mom.values())[0].Value )
        
        if self.indicator == True: 
            ordered = sorted(self.mom.items(), key=lambda x: x[1].Value, reverse=True)[:1]        
            for x in ordered:
                symbol = x[0]
                insights.append( Insight.Price(symbol, timedelta(1), InsightDirection.Up) )    
        
        return insights
        
        
# Python implementation of Custom Indicator
class My_Custom:
    def __init__(self, name, symbol, period):
        self.symbol = symbol
        self.Name = name
        self.Time = datetime.min
        self.Value = 0
        self.queue = deque(maxlen=period)
        self.IsReady = False

    # Update method is mandatory
    def Update(self, input):
        return self.Update2(input.Time, input.Close)
    
    def Update2(self, time, value):
        self.queue.appendleft(value)
        count = len(self.queue)
        self.Time = time
        
        self.IsReady = count == self.queue.maxlen
        
        #### start here the indicator calulation
        if self.IsReady:    
            y = np.log(self.queue)
            x = [range(len(y))]
            slope = stats.linregress(x, y)[0]
            self.Value = slope * 10000 # value is very small an will display 0 if not multiplyed
        #### finish the custom indicator
        
        # for testing self.IsReady = False
        self.IsReady = False
        
        return self.IsReady 
        
        
    def Warmup(self,history):
        for index, row in history.loc[self.symbol].iterrows():
            self.Update2(index, row['close'])