Hey, I want to create a new class called Pairs().
In need this class to keep track of the historical data for my specific pair. That's why I tried to create a rolling window inside this new class which I wanted to update. Unfortunately I don't have access to indicators or any other functionality like self.History in this new class. I tried to inherit QCAlgorithm but it gives me back an error. How can I use these provided indicators in my new class?
Error Message:
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 'float'>) method. Please checkout the API documentation.
at __init__
self.s1_closeWindow.Add(close_s1)
File "main.py" in main.py: line 233
I only could find one thread releated to this the topic, but I still couldn't figure it out.
from sklearn.linear_model import LinearRegression
from statsmodels.tsa.stattools import coint, adfuller
import statsmodels.api as sm
import pandas as pd
import numpy as np
from AlgorithmImports import *
above: all my libraries I've imported
below: the class I tried to create, but I don't have access to the Window.Add() method
class Pair():
#holds the symbols of the 2 two chosen stocks
#calculates the spread (w/o given alpha/beta) for a time period
#calculates the spread (w/ given alpha/beta) for a time period
#holds a rolling window of the price history to update in the OnData method
def __init__(self, s1, s2, lookback, history_s1, history_s2):
self.pair_symbols = (s1,s2)
self.s1_closeWindow = RollingWindow[TradeBar](lookback)
self.s2_closeWindow = RollingWindow[TradeBar](lookback)
df_s1 = np.log(history_s1.loc[s1]['close'])
df_s2 = np.log(history_s2.loc[s2]['close'])
for close_s1 in df_s1[:]:
self.s1_closeWindow.Add(close_s1)
for close_s2 in df_s2[:]:
self.s2_closeWindow.Add(close_s2)
self.lenght_s1 = len(self.self.s1_closeWindow)
self.lenght_s2 = len(self.self.s2_closeWindow)
Fred Painchaud
Hi Freidavid,
Your main algorithm must inherit from QCAlgorithm. Like this:
And the error with your Pair class is that you create a RollingWindow[TradeBar] but then you Add float in it (close values), not TradeBar.
Fred
Freidavid
Hey Fred
Thank you so much for your answer. Unfortunately I always get an error when inheriting the QCAlgorithm in the pairs class. I've fixed the datatype of the Window to “float” thank you
The problem was that I didn't have access to the Add() method of the window. I could solve this problem just by renaming the window. It was a problem that I used a number in the name for a self.attribute.
Thank yu so much!
Fred Painchaud
You are welcome Freidavid.
One day, we will have a book on LEAN. In the meantime, I'll only say that I don't think you want your class Pair to inherit from QCAlgorithm. QCAlgorithm is the class used by the LEAN engine to basically structure EVERYTHING for you, all the logic behind the strategy/all the data you need to act upon.
Pair is just a data structure for you that you are going to use in your future algo. At least, this is my understanding.
From the package you import, you are not going to do a simple strategy 😊 so I certainly don't have the complete picture here. But try the following code for Pair. My “instinct” is telling me that is what you intend to have:
From what I recall, you had access to the Add method of the rolling windows BUT your parameterized types did not match so you were getting a TypeError between TradeBar and decimal (we were in C# if I recall right - so TradeBar and float in Python). RollingWindow is a generic class. When you create RollingWindow[TradeBar], you cannot then do Add(decimal). It won't exist, especially in C# (strongly-typed language).
Fred
Freidavid
Okey, I'll fix that with the Tradebar and Float, thank you very much:)
Freidavid
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!