Help me what is wrong here: 

 

  1. 20250331 14:03:03.150 ERROR:: Extensions.SetRuntimeError(): Extensions.SetRuntimeError(): RuntimeError at 03/28/2025 04:00:00 UTC.
  2. Context: Consolidators update System.InvalidCastException: Specified cast is not valid.
  3. at Python.Runtime.PyObject.ToDecimal(IFormatProvider provider)
  4. at QuantConnect.StringExtensions.ConvertInvariant(Object value, Type conversionType) in Common/StringExtensions.cs:line 108
  5. at QuantConnect.StringExtensions.ConvertInvariant[T](Object value) in Common/StringExtensions.cs:line 38
  6. at QuantConnect.Data.Consolidators.DynamicDataConsolidator.GetNamedPropertyOrValueProperty(DynamicData data, String propertyName) in
  7. Common/Data/Consolidators/DynamicDataConsolidator.cs:line 112
  8. at QuantConnect.Data.Consolidators.DynamicDataConsolidator.AggregateBar(TradeBar& workingBar, DynamicData data) in
  9. Common/Data/Consolidators/DynamicDataConsolidator.cs:line 73
  10. at QuantConnect.Data.Consolidators.PeriodCountConsolidatorBase`2.Update(T data) in Common/Data/Consolidators/PeriodCountConsolidatorBase.cs:line
  11. 208
  12. at QuantConnect.Data.Consolidators.DataConsolidator`1.Update(IBaseData data) in Common/Data/Consolidators/DataConsolidator.cs:line 41
  13. at QuantConnect.Lean.Engine.AlgorithmManager.Run(AlgorithmNodePacket job, IAlgorithm algorithm, ISynchronizer synchronizer, ITransactionHandler
  14. transactions, IResultHandler results, IRealTimeHandler realtime, ILeanManager leanManager, CancellationTokenSource cancellationTokenSource) in
  15. Engine/AlgorithmManager.cs:line 420

 

  1. # region imports
  2. from AlgorithmImports import *
  3. # endregion
  4. from dydx_perpetual_data import DydxPerpetualData
  5. import os
  6. class DydxTestAlgorithm(QCAlgorithm):
  7. def initialize(self):
  8. self.set_start_date(2025, 3, 28)
  9. self.set_end_date(2025, 3, 31)
  10. self.set_cash(100000)
  11. symbolBTC = "BTC-USD"
  12. symbolETH = "ETH-USD"
  13. self._btc_symbol = self.add_data(DydxPerpetualData, symbolBTC, Resolution.MINUTE).symbol
  14. self._eth_symbol = self.add_data(DydxPerpetualData, symbolETH, Resolution.MINUTE).symbol
  15. self.set_benchmark(symbolBTC)
  16. self.set_benchmark(symbolETH)
  17. self._btc_short_ma = self.sma(self._btc_symbol, 60)
  18. self._btc_long_ma = self.sma(self._btc_symbol, 240)
  19. self._eth_short_ma = self.sma(self._eth_symbol, 60)
  20. self._eth_long_ma = self.sma(self._eth_symbol, 240)
  21. def on_data(self, data: Slice):
  22. if not (self._btc_symbol in data and self._eth_symbol in data):
  23. return
  24. btc_bullish_trend = self._btc_short_ma.current.value > self._btc_long_ma.current.value
  25. btc_current_price = data[self._btc_symbol].close
  26. if btc_bullish_trend and not self.portfolio[self._btc_symbol].invested:
  27. self.set_holdings(self._btc_symbol, 0.5)
  28. elif not btc_bullish_trend and self.portfolio[self._btc_symbol].invested:
  29. self.liquidate(self._btc_symbol)
  30. eth_bullish_trend = self._eth_short_ma.current.value > self._eth_long_ma.current.value
  31. eth_current_price = data[self._eth_symbol].close
  32. if eth_bullish_trend and not self.portfolio[self._eth_symbol].invested:
  33. self.set_holdings(self._eth_symbol, 0.5)
  34. elif not eth_bullish_trend and self.portfolio[self._eth_symbol].invested:
  35. self.liquidate(self._eth_symbol)
+ Expand

 

  1. import os
  2. from datetime import datetime
  3. from QuantConnect import *
  4. from QuantConnect.Data import *
  5. from QuantConnect.Algorithm import *
  6. from QuantConnect.Indicators import *
  7. from QuantConnect.Python import PythonData
  8. from decimal import Decimal, InvalidOperation
  9. class DydxPerpetualData(PythonData):
  10. def __init__(self):
  11. self.open = 0.0
  12. self.high = 0.0
  13. self.low = 0.0
  14. self.close = 0.0
  15. self.volume = 0.0
  16. self.openInterest = 0.0
  17. def get_source(self, config: SubscriptionDataConfig, date: datetime, isLiveMode: bool) -> SubscriptionDataSource:
  18. resolution = "1MIN" if config.Resolution == Resolution.MINUTE else "1DAY"
  19. symbol = config.Symbol.Value
  20. file_path = os.path.join(Globals.data_folder, "dydx", "perpetual", resolution, f"{symbol}.csv")
  21. # file_path = os.path.join("D:\\MyProjects\\doxia-investing\\DoxiaInvesting-QuantConnect-dYdx\\DydxBacktestingQuantConnect\\data\\dydx\\perpetual", resolution, f"{symbol}.csv")
  22. return SubscriptionDataSource(file_path, SubscriptionTransportMedium.LOCAL_FILE)
  23. def safe_decimal(self, value: str) -> Decimal:
  24. """Safely convert a string to Decimal, returning 0 on failure."""
  25. try:
  26. return Decimal(value.strip()) if value.strip() else Decimal(0)
  27. except (InvalidOperation, ValueError, TypeError):
  28. return Decimal(0)
  29. def reader(self, config: SubscriptionDataConfig, line: str, date: datetime, isLiveMode: bool) -> BaseData:
  30. if not line or line.startswith("Date"):
  31. return None
  32. data = DydxPerpetualData()
  33. parts = line.split(',')
  34. try:
  35. data.time = datetime.strptime(parts[0], "%Y%m%d %H:%M")
  36. data.open = self.safe_decimal(parts[1])
  37. data.high = self.safe_decimal(parts[2])
  38. data.low = self.safe_decimal(parts[3])
  39. data.close = self.safe_decimal(parts[4])
  40. data.volume = self.safe_decimal(parts[5])
  41. data.openInterest = self.safe_decimal(parts[6])
  42. data.symbol = config.Symbol
  43. data.value = data.close
  44. return data
  45. except Exception as e:
  46. # self.Log(f"Error parsing line: {line}. Exception: {str(e)}")
  47. return None
+ Expand

Author

Oilan Online

a month ago