Hi,
I have created an indicator for the ratio between PEPSI (PEP) and Coca Cola (COKE) and am trying to create a 5 days simple moving average (sma) from this indicator using IndicatorExtension and then plot the result. Please see the attached project for the code.
However when I run the code in QuantConnect I get the error:
Runtime Error: Trying to dynamically access a method that does not exist throws a TypeError exception. To prevent the exception, ensure each parameter type matches those required by the 'int'>) method. Please checkout the API documentation.
at OnData
ratio_spread = IndicatorExtensions.SMA(self.log_ratio_spread in main.py: line 53
(Open Stacktrace)
which is associated with the following line:
self.log_ratio_spread_sma = IndicatorExtensions.SMA(self.log_ratio_spread,5,Resolution.Daily)
What am I doing wrong?
Fred Painchaud
Hi Simon,
The attached project is not attached or else, I don't see it.
It looks like the output of the error is somewhat truncated but from what there is, it might be an error inside SMA with respect to comparison with >. But “the ‘int’>) method” does not look complete to me…
It will be easier with the code.
Fred
Simon Coleman
Hi Fred,
Sorry, having a problem attaching the backtest for the project…. Please find the code below
Thanks
Simon
import numpy as np
from collections import deque #double sideded que that allows you to append and remove elemets from both sides of the collection. You can append or remove from the left or right - so useful when adding new data to the right and removing old data from the left
class SquareLightBrownPanda(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 1, 1) # Set Start Date
self.SetEndDate(2021,1,1) # Set End Date
self.SetCash(100000) # Set Strategy Cash
self.pep = self.AddEquity("PEP", Resolution.Daily).Symbol # Add data for PEP
self.coke = self.AddEquity("COKE", Resolution.Daily).Symbol # Add data for COKE
self.log_ratio_spread=0
#the commented out section below shows how the standardised way to work out an sma - just commented out to show how the customer indictor works
self.sma_pep = self.SMA(self.pep,5,Resolution.Daily) #create 30 simple moving average indicator
self.sma_coke = self.SMA(self.coke,5,Resolution.Daily) #create 30 simple moving average indicator
closing_price_pep = self.History(self.pep,5,Resolution.Daily)["close"] #historic informaton for the past 30 bars. Indexed by "close" so we can save it to the sma close price column
closing_price_coke = self.History(self.coke,5,Resolution.Daily)["close"] #historic informaton for the past 30 bars. Indexed by "close" so we can save it to the sma close price column
#self.sma_log_ratio_spread = self.SMA(self.log_ratio_spread,5,Resolution.Daily) #create 30 simple moving average indicator
for time, price in closing_price_pep.loc[self.pep].items(): #iterate over time and price infoamtion in the closing price dataframe
self.sma_pep.Update(time, price) #update sma indicator is ready to use from the beginning
for time, price in closing_price_coke.loc[self.coke].items(): #iterate over time and price infoamtion in the closing price dataframe
self.sma_coke.Update(time, price) #update sma indicator is ready to use from the beginning
#self.sma_ratio = CustomSimpleMovingAverage("CustomSMA",5,self.log_ratio_spread) #create instance of custom sma class
#self.RegisterIndicator(self.log_ratio_spread,self.sma_ratio, Resolution.Daily)#register the instance to be an indicator for the algorithm
def OnData(self, data):
if not self.sma_pep.IsReady and not self.sma_coke.IsReady:
return
#save the high and low price of the last year - not efficient use rolling window
hist_pep = self.History(self.pep, timedelta(365),Resolution.Daily)
hist_coke = self.History(self.coke, timedelta(365),Resolution.Daily)
price_pep = self.Securities[self.pep].Price ##save PEP's most recent price to the price variable
price_coke = self.Securities[self.coke].Price ##save PEP's most recent price to the price variable
log_ratio_pep = np.log(price_pep/self.sma_pep.Current.Value)
log_ratio_coke = np.log(price_coke/self.sma_coke.Current.Value)
self.log_ratio_spread = log_ratio_pep - log_ratio_coke
ratio_spread = IndicatorExtensions.SMA(self.log_ratio_spread,5,Resolution.Daily)
self.Plot("Spread","log_ratio_spread",self.log_ratio_spread)
Fred Painchaud
Hi Simon,
In
the first param of SMA() must be another indicator (inheriting from IndicatorBase). In your code, self.log_ratio_spread is a float.
I believe you simply want to use self.SMA() over the spread to calculate the SMA of the spread.
Fred
Simon Coleman
Hi Fred/All,
I swapped out :
for
However I received the following error:
Runtime Error: Trying to dynamically access a method that does not exist throws a TypeError exception. To prevent the exception, ensure each parameter type matches those required by the 'int'>) method. Please checkout the API documentation.
at OnData
self.ratio_spread = self.SMA(self.log_ratio_spread in main.py: line 54 (Open Stacktrace)
Any thoughts please?
Fred Painchaud
Hi Simon,
You need to create an SMA indie with a symbol, a period and a res. log_ratio_spread is not a symbol.
The best I believe, or the easiest, would be to store your spread ratios in a Rolling Window and calculate the average from it. SMAs and the likes are there to work on symbol data - not values that you compute yourself.
Fred
Vladimir
Simon Coleman,
Try this
Simon Coleman
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!