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.613
Tracking Error
0.132
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
from datetime import datetime

### Demonstrates how to create a custom indicator and register it for automatic updates
class CustomIndicatorAlgorithm(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2021, 1, 1)
        self.SetEndDate(2021, 5, 1)
        symbol = self.AddEquity("TSLA", Resolution.Daily).Symbol

        # Create custom indicator
        self.indicator = CustomIndicator('custom', 8, 10, 4)
        
        # Register inidicator for automatic updates
        self.RegisterIndicator("TSLA", self.indicator, Resolution.Daily)
        
        # Warm up indicator
        history = self.History(symbol, self.indicator.Period, Resolution.Daily)
        if history.empty or 'close' not in history.columns:
            return
        for time, row in history.loc[symbol].iterrows():
            tradebar = TradeBar(time, symbol, row.open, row.high, row.low, row.close, row.volume)
            self.indicator.Update(tradebar)

    def OnData(self, data):
        if self.indicator.IsReady:
            self.Plot("Custom", "Value", self.indicator.Value)


class CustomIndicator(PythonIndicator):
    def __init__(self, name, n1, n2, n3):
        self.Name = name
        self.Time = datetime.min
        self.Value = 0
        
        # Internal indicators
        self.esa = ExponentialMovingAverage(n1) 
        self.d = ExponentialMovingAverage(n1)
        self.tci = ExponentialMovingAverage(n2)
        self.wt2 = SimpleMovingAverage(n3)
        
        self.Period = n1 + n1 + n2 + n3


    # Update method is mandatory
    def Update(self, input):
        self.Time = input.EndTime
        self.Value = 0
        
        # get the hlc3 price for average
        ap = (input.High + input.Low + input.Close) / 3
        
        # get the ema based on the hlc3
        if not self.esa.Update(input.Time, ap):
            return False
        
        # generate ema based on the absolute value of the average price - the ema on period n1
        if not self.d.Update(input.Time, abs(ap - self.esa.Current.Value)) or self.d.Current.Value == 0:
            return False
            
        # return some calculated value with some tolerance
        ci = (ap - self.esa.Current.Value) / (0.015 * self.d.Current.Value)
        
        # generate ema based on above
        if not self.tci.Update(input.Time, ci):
            return False
        
        # generate sma from tci
        if not self.wt2.Update(input.Time, self.tci.Current.Value):
            return False
            
        # Set indicator value
        self.Value = self.wt2.Current.Value
        
        # Return IsReady
        return True