Back

TypeError : No method matches given arguments for Add

Hello everyone,

I am currently setting up my first algorithm on this platform and tried to backtest it. It looks good in the beginning. I can see the values changing, but then after a while 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 Add method. Please checkout the API documentation.
at OnData in main.py:line 50
TypeError : No method matches given arguments for Add (Open Stacktrace)
"

The mentioned line 50, which is actually line 45 in my algo, looks like this:

self.tradeBarWindow.Add(data["BTCUSD"])

I honestly, do not see my mistake, could someone help me out?

Error Log: https://www.quantconnect.com/backtest/60871/2815074/4ead5896542dd044dd5fb4f783eda1bf-log.txt

Update Backtest







0

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.


Hi, please could you attach the backtest or otherwise paste the code (using the Insert Code Snippet button on the same row as the bold and italics buttons)?

Your error could be caused by any number of things and it's impossible to tell without seeing the full code.

0

I deleted a few lines and added some new, but basically, the mistake is the same

class Trendchange(QCAlgorithm):

def Initialize(self):
self.length = 50
self.SetStartDate(2013, 12, 26) # Set Start Date
self.SetEndDate(2020, 12, 26) # Set Start Date
self.SetCash(10000) # Set Strategy Cash
self.SetWarmUp(self.length)
self.symbol = "EURUSD"
self.AddForex(self.symbol, Resolution.Hour)
#self.AddCrypto("BTCUSD", Resolution.Hour)
self.SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Cash)
self.SetBenchmark(self.symbol)
self.longOnly = True
self.tradeBarWindow = RollingWindow[TradeBar](self.length) # Store the last 100 values
self.support = 0.0
self.resistance = 0.0
self.high = 0.0
self.low = 9999999999.9
self.newLL = False
self.newHH = False

def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Arguments:
data: Slice object keyed by symbol containing the stock data
'''
def findMax(self):
high = 0.0
i = self.length-1
while i > 0:
if self.tradeBarWindow[i].High > high:
high = self.tradeBarWindow[i].High
i -= 1
return high

def findMin(self):
low = 999999999.9
i = self.length-1
while i > 0:
if self.tradeBarWindow[i].Low < low:
low = self.tradeBarWindow[i].Low
i -= 1
return low

# Update our rolling windows
self.tradeBarWindow.Add(data[self.symbol])

if data[self.symbol].Close > data[self.symbol].Open:
self.high = data[self.symbol].High
elif data[self.symbol].Close < data[self.symbol].Open:
self.low = data[self.symbol].Low

# Wait for windows to be ready.
if not self.tradeBarWindow.IsReady: return
if self.IsWarmingUp: return
holdings = self.Portfolio[self.symbol].Quantity
currOpen = self.tradeBarWindow[0].Open
currHigh = self.tradeBarWindow[0].High
currLow = self.tradeBarWindow[0].Low
currClose = self.tradeBarWindow[0].Close
tp = 6
sl = 3
##### RUN SOME LOGIC HERE...
longCondition = False
shortCondition = False

if currLow < findMin(self):
self.resistance = self.high
self.newLL = True

if self.newLL:
if currClose > self.resistance:
longCondition = True
self.newLL = False

if currHigh > findMax(self):
self.support = self.low
self.newHH = True

if self.newHH:
if currClose < self.support:
shortCondition = True
self.newHH = False
##### END OF LOGIC


#### TRADE EXECUTION
if (holdings <= 0.0) and longCondition:
self.LimitOrder(self.symbol, 1.0, currClose)
self.LimitOrder(self.symbol, -1.0, currClose*(1+(tp/100)))

elif (holdings >= 0.0) and shortCondition:
self.LimitOrder(self.symbol, -1.0, currClose)
self.LimitOrder(self.symbol, 1.0, currClose*(1-(tp/100)))

 

 

0

Thank you for attaching that. I don't immediately see what's wrong but I have two thoughts:

  1. Forex hourly data comes in as QuoteBars rather than TradeBars. You describe that things seem to be going fine until they don't - I'm not sure why this error doesn't come up earlier, so I don't know if this is the root cause. It could be because Add is implicitly casting from QuoteBar to TradeBar and then suddenly there comes along a bar it can't cast. Regardless, you should adjust your rolling window to accommodate the correct type of data.
  2. You should never assume the data object coming into OnData() contains a particular entry for a particular symbol. You should always check if the slice actually contains a key for your symbol. It might be that there's an empty slice being passed in for some reason and it's crashing your algo. If you run the C# version and break at the OnData point, you'll see data["EURUSD"] is actually a dynamic data type, precisely to handle situations where the data is incomplete or missing.
1

Thank you Douglas, I will do what you suggest.

0

Update Backtest





0

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.


Loading...

This discussion is closed