I've written this class to parse UCITS ETF data from Yahoo Finance, but I get the following error trying to do a backtest, what's wrong?

  1. class UcitsEtf(PythonData):
  2. def __init__(self, start_date):
  3. self.start_date = start_date
  4. def GetSource(self, config, date, isLiveMode):
  5. ticker = config.Symbol.Value
  6. start = int(time.mktime(datetime.strptime(self.start_date, "%Y-%m-%d").timetuple()))
  7. end = int(time.time())
  8. url = f"https://query1.finance.yahoo.com/v7/finance/download/{ticker}?period1={start}&period2={end}&interval=1d&events=history&includeAdjustedClose=true"
  9. return SubscriptionDataSource(url, SubscriptionTransportMedium.RemoteFile)
  10. def handle_nulls(self, df):
  11. df.replace("null", np.nan, inplace=True)
  12. df.fillna(df.rolling(2, min_periods=1).mean(), inplace=True)
  13. return df
  14. def Reader(self, config, line, date, isLiveMode):
  15. index = UcitsEtf(self.start_date)
  16. index.Symbol = config.Symbol
  17. try:
  18. data = line.split(',')
  19. if data[0] == "Date":
  20. return None
  21. data = self.handle_nulls(pd.DataFrame(data[1:], columns=["open", "high", "low", "close", "adj_close", "volume"]))
  22. index.Time = datetime.strptime(data[0], "%Y-%m-%d")
  23. index.EndTime = index.Time + timedelta(days=1)
  24. index.Value = decimal.Decimal(data["close"])
  25. index["open"] = float(data["open"])
  26. index["high"] = float(data["high"])
  27. index["low"] = float(data["low"])
  28. index["close"] = float(data["close"])
  29. index["adj_close"] = float(data["adj_close"])
  30. index["volume"] = int(data["volume"])
  31. except Exception as e:
  32. print(f"Error: {line}")
  33. print(f"Exception: {e}")
  34. return None
  35. return index
+ Expand
  1. self.TEST = self.AddData(UcitsEtf("01-01-2022"), "XS2L.MI", Resolution.Daily).Symbol
  1. Error Message
  2. During the algorithm initialization, the following exception has occurred: Index was outside the bounds of the array.
  3. at Initialize
  4. self.TEST = self.AddData(UcitsEtf("01-01-2022") in main.py: line 84
  5. Index was outside the bounds of the array.

 

Author

AleNoc

December 2023