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
-1.947
Tracking Error
0.151
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
# FIR_Filter for prices with the same period but different power
# 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 = 0; POWER_05 = 0.5; POWER_10 = 1.0; POWER_15 = 1.5; 
# ---------------------------------------------------------------------------------------
import numpy as np

class DigitalFilters(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 6, 10)
        self.SetCash(100000) 
        self.stock = self.AddEquity(STOCK, Resolution.Daily).Symbol
        self.indicator_0 = FIR_Filter('FIR_Filter_0', period = PERIOD, power = POWER_0)
        self.RegisterIndicator(self.stock, self.indicator_0, Resolution.Daily)
        self.indicator_05 = FIR_Filter('FIR_Filter_05', period = PERIOD, power = POWER_05)
        self.RegisterIndicator(self.stock, self.indicator_05, Resolution.Daily)
        self.indicator_10 = FIR_Filter('FIR_Filter_10', period = PERIOD, power = POWER_10)
        self.RegisterIndicator(self.stock, self.indicator_10, Resolution.Daily)
        self.indicator_15 = FIR_Filter('FIR_Filter_15', period = PERIOD, power = POWER_15)
        self.RegisterIndicator(self.stock, self.indicator_15, Resolution.Daily)

        self.SetWarmUp(PERIOD)


    def OnData(self, data):
        if self.IsWarmingUp or not self.indicator_0.IsReady: return
        if not self.indicator_05.IsReady or not self.indicator_10.IsReady: return
        if not self.indicator_15.IsReady: return
        
        price = self.Securities[self.stock].Price

        self.Plot("Indicator", "FIR_0", self.indicator_0.Value)
        self.Plot("Indicator", "FIR_05", self.indicator_05.Value)
        self.Plot("Indicator", "FIR_10", self.indicator_10.Value)
        self.Plot("Indicator", "FIR_15", self.indicator_15.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)