Overall Statistics Total Trades0Average Win0%Average Loss0%Compounding Annual Return0%Drawdown0%Expectancy0Net Profit0%Sharpe Ratio0Probabilistic Sharpe Ratio0%Loss Rate0%Win Rate0%Profit-Loss Ratio0Alpha0Beta0Annual Standard Deviation0Annual Variance0Information Ratio-2.268Tracking Error0.113Treynor Ratio0Total Fees\$0.00
from collections import deque
from datetime import datetime, timedelta
from numpy import sum
import numpy as np
from scipy import stats

### Demonstrates how to create a custom indicator and register it for automatic updated
class CustomIndicatorAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2019,1,1)
self.SetEndDate(2019,12,31)

self.custom = My_Custom('My_Custom', 100)
self.RegisterIndicator("SPY", self.custom, Resolution.Daily)

history = self.History("SPY", 100, Resolution.Daily)
self.custom.Warmup(history)

def OnData(self, data):
self.Plot("My_Custom_Slope", "Slope", self.custom.Slope)
self.Plot("My_Custom_Intercept", "Intercept", self.custom.Intercept)
self.Plot("My_Custom_R_value", "R_value", self.custom.R_value)
#self.Plot("My_Custom_P_value", "P_value", self.custom.P_value)
#self.Plot("My_Custom_Std_err", "Std_err ", self.custom.Std_err )

# Python implementation of Custom Indicator
class My_Custom(PythonIndicator):
def __init__(self, name, period):
self.Name = name
self.Time = datetime.min
self.Value = 0
self.Slope = 0
self.Intercept = 0
self.R_value = 0
self.P_value = 0
self.Std_err = 0
self.queue = deque(maxlen=period)

# Update method is mandatory
def Update(self, input):
self.queue.appendleft(input.Close)
count = len(self.queue)
self.Time = input.EndTime
#### start here the indicator calulation
if count == self.queue.maxlen:
y = np.log(self.queue)
x = [range(len(y))]
slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
self.Slope = slope * 10000 # value is very small an will display 0 if not multiplyed
self.Intercept = intercept
self.R_value = r_value * 10
self.P_value = p_value
self.Std_err = std_err
#### finish the custom indicator
return count == self.queue.maxlen

def Warmup(self,history):
if self.Name not in history:
return
for index, row in history.loc[self.Name].iterrows():
self.Close=row["close"]
self.Time=index