Hi!
I've managed to attach custom data to my code using csv from google drive. It works correctly in a simple Buy and Hold strategy. Unfortunately, when I want to add rolling window to my data, I get this 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 'converter.Data'>) method. Please checkout the API documentation.
at OnData
self.prevPrices[symbol].Add( data[symbol] )
at Python.Runtime.PythonException.ThrowLastAsClrException()
at Python.Runtime.PyObject.Invoke(PyTuple args in main.py: line 56
Do you have any idea what could be causing this and how to solve this issue? I found out that this type of error has been mentioned before: https://www.quantconnect.com/forum/discussion/11553/rolling-window-not-working-lean-locally/p1 but without any final solution.
# region imports
from AlgorithmImports import *
class MyCustomDataType(PythonData):
def GetSource(self,
config: SubscriptionDataConfig,
date: datetime,
isLive: bool) -> SubscriptionDataSource:
return SubscriptionDataSource("http//link=csv", SubscriptionTransportMedium.RemoteFile)
def Reader(self,
config: SubscriptionDataConfig,
line: str,
date: datetime,
isLive: bool) -> BaseData:
if not (line.strip()):
return None
index = MyCustomDataType()
index.Symbol = config.Symbol
try:
data = line.split(',')
index.Time = datetime.strptime(data[0], "%Y%m%d %H:%M")
print (index.Time)
index.EndTime = index.Time + timedelta(days=1)
index.Value = float (data[4])
index["open"] = float(data[1])
index["close"] = float(data[6])
except ValueError as a:
print (a)
# Do nothing
return None
return index
class CustomIndexStrategy(QCAlgorithm):
def Initialize(self):
self.Pair_1 = self.AddData(MyCustomDataType, "EURSEK_DAILY", Resolution.Daily).Symbol
self.holdingDays = 1
self.SetStartDate (2020, 1, 1)
self.SetEndDate(2022,7,1)
self.SetCash(10000)
self.symbols = [self.Pair_1]
self.prevPrices = { symbol : RollingWindow[QuoteBar](7) for symbol in self.symbols }
self.ticketPair1 = None
self.ticketPair2 = None
def OnData(self,data):
for symbol in self.symbols:
if data.ContainsKey(symbol):
self.prevPrices[symbol].Add( data[symbol] )
if not all([ window.IsReady for window in self.prevPrices.values() ]):
return
Pair1_window = self.prevPrices[self.Pair_1]
Pair1_1D = Pair1_window[1].Close
Pair1_0D = Pair1_window[0].Close
#if self.ticketPair1 is None and self.Securities[self.Pair_1].Exchange.ExchangeOpen is True and Pair1_0D < Pair1_1D < Pair1_2D < Pair1_3D < Pair1_4D < Pair1_5D < Pair1_6D :
if not self.Portfolio.Invested and Pair1_0D < Pair1_1D < Pair1_2D < Pair1_3D:
self.MarketOrder(self.Pair_1, 1)
Sebastian Wozniczka
Just to add, I've also changed the 49th code line from
to:
as was suggested at the end of the topic which I had mentioned previously, but the same issue still occurs.
Derek Melchin
Hi Sebastian,
RollingWindow objects can't currently store PythonData objects. For reference, see Supported Types.
Best,
Derek Melchin
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
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.
Sebastian Wozniczka
@derek-melchin
Thanks for pointing out!
Solved with deque and numpy :)
Sebastian Wozniczka
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!