Overall Statistics
Total Trades
1521
Average Win
1.51%
Average Loss
-0.57%
Compounding Annual Return
71.652%
Drawdown
15.200%
Expectancy
0.331
Net Profit
1396.082%
Sharpe Ratio
2.869
Probabilistic Sharpe Ratio
99.440%
Loss Rate
63%
Win Rate
37%
Profit-Loss Ratio
2.64
Alpha
0.778
Beta
0.03
Annual Standard Deviation
0.273
Annual Variance
0.075
Information Ratio
1.795
Tracking Error
0.328
Treynor Ratio
26.541
Total Fees
$3353.89
Estimated Strategy Capacity
$35000000.00
Lowest Capacity Asset
AMZN R735QTJ8XC9X
# 11613  trend0 custom python indicator by Derek Melchin
# inspired by Warren Harding https://www.quantconnect.com/forum/discussion/11613/algo-trend0/p1

import numpy as np

class UglyBrownDolphin(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2016, 6, 10)
        self.SetCash(100000) 
        self.symbol = self.AddEquity("AMZN", Resolution.Hour).Symbol
        self.indicator = Trend0('Trend0', period = 10, power = 1.5)
        self.RegisterIndicator(self.symbol, self.indicator, Resolution.Hour)


    def OnData(self, data):
        if not (data.ContainsKey(self.symbol) and data[self.symbol] is not None):
            return
        if not self.indicator.IsReady:
            return
                
        if self.indicator.Value > 0 and self.Portfolio[self.symbol].Quantity <= 0:
            self.SetHoldings(self.symbol, 0.99)
        elif self.indicator.Value < 0 and self.Portfolio[self.symbol].Quantity >= 0:
            self.SetHoldings(self.symbol, -0.99)
        
           
class Trend0(PythonIndicator):
    def __init__(self, name, period, power):
        self.Name = name
        self.period = period
        self.power = power
        self.Time = datetime.min
        self.Value = 0
        self.prices = np.array([])
        

    def Update(self, input):
        self.prices = np.append(self.prices, input.Close)[-self.period:]

        if len(self.prices) != self.period:
            self.Value = 0
            return False
        
        self.Value = self.calc_trend()
        return True
        
    
    def calc_trend(self):
        changes = np.array([])
        for i in range(len(self.prices) - 1):
            _return = (self.prices[i + 1] - self.prices[i]) / self.prices[i]
            changes = np.append(changes, _return)
        return self.power_weighted_moving_average(changes)
        
    
    def power_weighted_moving_average(self, changes):
        return self.weighted_average(changes, self.power_weights(len(changes)))
        
        
    def power_weights(self, length):
        weights = np.array([])
        for i in range(length):
            w = i + 1
            weights = np.append(weights, w**self.power)
        return weights
        
        
    def weighted_average(self, changes, weights):
        products = []
        for i in range(len(changes)):
            products.append(changes[i] * weights[i])
        return sum(products) / sum(weights)