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
-0.921
Tracking Error
0.17
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
# FIR_Filter custom indicator for prices SMA
# Inspired by Warren Harding and Derek Melchin for their Trend0 algorithms
# https://www.quantconnect.com/forum/discussion/11613/algo-FIR_Filter/p1
# ----------------------------------------
STOCK = 'AMZN'; PERIOD = 63; POWER = 0; 
# ----------------------------------------
import numpy as np

class UglyBrownDolphin(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2016, 6, 10)
        self.SetCash(100000) 
        self.stock = self.AddEquity(STOCK, Resolution.Daily).Symbol
        self.indicator = FIR_Filter('FIR_Filter', period = PERIOD, power = POWER)
        self.RegisterIndicator(self.stock, self.indicator, Resolution.Daily)
        self.sma = self.SMA(self.stock, PERIOD, Resolution.Daily)
        self.SetWarmUp(PERIOD)


    def OnData(self, data):
        if self.IsWarmingUp or not self.indicator.IsReady: return

        price = self.Securities[self.stock].Price
        
        mean = self.History([self.stock], PERIOD, Resolution.Daily)['close'].unstack(level=0).mean()

        self.Plot("Indicator", "mean", mean)
        self.Plot("Indicator", "ma", self.indicator.Value)
        self.Plot("Indicator", "sma", self.sma.Current.Value)
        
        
           
class FIR_Filter(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_filter()
        return True
        
    
    def calc_filter(self):
        prices = np.array([])
        for i in range(len(self.prices) - 1):
            price = self.prices[i + 1] 
            prices = np.append(prices, price)
        return self.power_weighted_moving_average(prices)
        
    
    def power_weighted_moving_average(self, prices):
        return self.weighted_average(prices, self.power_weights(len(prices)))
        
        
    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, prices, weights):
        products = []
        for i in range(len(prices)):
            products.append(prices[i] * weights[i])
        return sum(products) / sum(weights)