Hey, i have been attempting to create  a rolling window for EMA values, that i can then use to reference back to it as a condition to enter into a trade but i have been unsuccessful. i keep getting this error:

Runtime Error: TypeError : Cannot get managed object
at OnData
PEMABelowPreviousP = previousEMA < previous_price

Can someone help?

Here is the code i'm working with.

  1. from datetime import datetime,timedelta
  2. PriceEqual2Slow = None
  3. SlowEqual2Price = None
  4. PEMABelowPreviousP = None
  5. PEMA2BelowPreviousP2 = None
  6. PEMA3BelowPreviousP3 = None
  7. PEMA4BelowPreviousP4 = None
  8. PEMAOverPreviousP = None
  9. PEMA2OverPreviousP2 = None
  10. PEMA3OverPreviousP3 = None
  11. PEMA4OverPreviousP4 = None
  12. class HipsterFluorescentPinkFrog(QCAlgorithm):
  13. def Initialize(self):
  14. self.SetStartDate(2019, 1, 30) # Set Start Date
  15. self.SetEndDate(2020, 1, 30)
  16. self.SetCash(100000) # Set Strategy Cash
  17. self.ticker = "EURUSD" #"USDJPY","GBPUSD", "USDCAD","EURUSD".
  18. # Rolling Windows to hold bar close data keyed by symbol
  19. self.Data = {}
  20. #for self.ticker in self.tickers:
  21. symbol = self.AddForex(self.ticker , Resolution.Hour, Market.Oanda).Symbol
  22. self.Data[symbol] = SymbolData(self, symbol)
  23. #self.tolerance = 0.0000
  24. self.stopLossLevel = -0.05 # stop loss percentage
  25. self.stopProfitLevel = 0.01# stop profit percentage
  26. self.SetWarmUp(200, Resolution.Hour)
  27. def OnData(self, data):
  28. if self.IsWarmingUp: #Data to warm up the algo is being collected.
  29. return
  30. for symbol, symbolData in self.Data.items(): #Return the dictionary's key-value pairs:
  31. if not (data.ContainsKey(symbol) and data[symbol] is not None and symbolData.IsReady):
  32. continue
  33. slowEMA = symbolData.slowema.Current.Value
  34. #slowEMA = float(symbolData.slowWindow[0])
  35. self.Log(f"{symbolData.slowWindow[1]} ema values ")
  36. #sWindow = symbolData.slowWindow
  37. previousEMA = symbolData.slowWindow[1]
  38. previous2EMA = symbolData.slowWindow[2]
  39. previous3EMA = symbolData.slowWindow[3]
  40. previous4EMA = symbolData.slowWindow[4]
  41. current_price = symbolData.closeWindow[0]
  42. previous_price = symbolData.closeWindow[1]
  43. previous2_price = symbolData.closeWindow[2]
  44. previous3_price = symbolData.closeWindow[3]
  45. previous4_price = symbolData.closeWindow[4]
  46. if self.Portfolio[symbol].Invested:
  47. if self.isLong:
  48. condStopProfit = (current_price - self.buyInPrice)/self.buyInPrice > self.stopProfitLevel
  49. condStopLoss = (current_price - self.buyInPrice)/self.buyInPrice < self.stopLossLevel
  50. if condStopProfit:
  51. self.Liquidate(symbol)
  52. self.Log(f"{self.Time} Long Position Stop Profit at {current_price}")
  53. if condStopLoss:
  54. self.Liquidate(symbol)
  55. self.Log(f"{self.Time} Long Position Stop Loss at {current_price}")
  56. else:
  57. condStopProfit = (self.sellInPrice - current_price)/self.sellInPrice > self.stopProfitLevel
  58. condStopLoss = (self.sellInPrice - current_price)/self.sellInPrice < self.stopLossLevel
  59. if condStopProfit:
  60. self.Liquidate(symbol)
  61. self.Log(f"{self.Time} Short Position Stop Profit at {current_price}")
  62. if condStopLoss:
  63. self.Liquidate(symbol)
  64. self.Log(f"{self.Time} Short Position Stop Loss at {current_price}")
  65. if not self.Portfolio[symbol].Invested:
  66. PriceEqual2Slow = (current_price - slowEMA) == 0.0000
  67. SlowEqual2Price = (current_price - slowEMA) == 100.0000
  68. PEMABelowPreviousP = previousEMA < previous_price
  69. PEMA2BelowPreviousP2 = previous2EMA < previous2_price
  70. PEMA3BelowPreviousP3 = previous3EMA < previous3_price
  71. PEMA4BelowPreviousP4 = previous4EMA < previous4_price
  72. PEMAOverPreviousP = previousEMA > previous_price
  73. PEMA2OverPreviousP2 = previous2EMA > previous2_price
  74. PEMA3OverPreviousP3 = previous3EMA > previous3_price
  75. PEMA4OverPreviousP4 = previous4EMA > previous4_price
  76. if PriceEqual2Slow and PEMAOverPreviousP and PEMA2OverPreviousP2 and PEMA3OverPreviousP3 and PEMA4OverPreviousP4:
  77. self.SetHoldings(symbol, 1)
  78. # get buy-in price for trailing stop loss/profit
  79. self.buyInPrice = current_price
  80. # entered long position
  81. self.isLong = True
  82. self.Log(f"{self.Time} Entered Long Position at {current_price}")
  83. if SlowEqual2Price and PEMABelowPreviousP and PEMA2BelowPreviousP2 and PEMA3BelowPreviousP3 and PEMA4BelowPreviousP4:
  84. self.SetHoldings(symbol, -1)
  85. # get sell-in price for trailing stop loss/profit
  86. self.sellInPrice = current_price
  87. # entered short position
  88. self.isLong = False
  89. self.Log(f"{self.Time} Entered Short Position at {current_price}")
  90. class SymbolData:
  91. def __init__(self, algorithm, symbol):
  92. self.slowema= ExponentialMovingAverage(200)
  93. self.slowWindow = RollingWindow[IndicatorDataPoint](10) #setting the Rolling Window for the fast MACD indicator, takes two values
  94. algorithm.RegisterIndicator(symbol, self.slowema, timedelta(hours=1))
  95. self.slowema.Updated += self.SlowEMAUpdated #Updating those two values
  96. self.closeWindow = RollingWindow[float](10)
  97. # Add consolidator to track rolling close prices
  98. self.consolidator = QuoteBarConsolidator(1)
  99. self.consolidator.DataConsolidated += self.CloseUpdated
  100. algorithm.SubscriptionManager.AddConsolidator(symbol, self.consolidator)
  101. def SlowEMAUpdated (self, sender, updated):
  102. '''Event holder to update the MACD Rolling Window values.'''
  103. if self.slowema.IsReady:
  104. self.slowWindow.Add(updated)
  105. def CloseUpdated(self, sender, bar):
  106. '''Event holder to update the close Rolling Window values'''
  107. self.closeWindow.Add(bar.Close)
  108. @property
  109. def IsReady(self):
  110. return self.slowWindow.IsReady and self.closeWindow.IsReady
+ Expand

Author

Samwel Kibet

February 2022