1

I'm just getting started with QuantConnect, but I understand Python fairly well, or so I thought. This is the important part of my code:

def Initialize(self):
    # Set the cash we'd like to use for our backtest
    # This is ignored in live trading 
    self.SetCash(5000)

    # Start and end dates for the backtest.
    # These are ignored in live trading.
    self.SetStartDate(2015,1,1)
    self.SetEndDate(2018,1,1)

    # Set Brokerage model to load OANDA fee structure.
    self.SetBrokerageModel(BrokerageName.OandaBrokerage)

    # Add assets you'd like to see
    # self.eurusd = self.AddForex("EURUSD", Resolution.Minute).Symbol
    self.usdjpy = self.AddForex("USDJPY", Resolution.Minute).Symbol
    # self.eurjpy = self.AddForex("EURJPY", Resolution.Minute).Symbol



def OnData(self, slice):

    rsi = self.RSI("USDJPY", 14, MovingAverageType.Simple)

    if rsi > 72:
        self.SetHoldings("USDJPY", 1)

    if rsi < 28:
        self.SetHoldings("USDJPY", 1)

This is the error I'm getting:

Runtime Error: TypeError : Cannot get managed object
  at OnData in main.py:line 36
 TypeError : Cannot get managed object

Stacktrace:

    System.Exception: TypeError : Cannot get managed object
     at OnData in main.py:line 73
     ---> Python.Runtime.PythonException: TypeError : Cannot get managed object
     at Python.Runtime.PyObject.Invoke (Python.Runtime.PyTuple args, 
     Python.Runtime.PyDict kw) [0x00033] in <0f995c28c5b446ad8835419f76b319a3>:0 
     at Python.Runtime.PyObject.InvokeMethod (System.String name, 
     Python.Runtime.PyTuple args, Python.Runtime.PyDict kw) [0x00007] in 
     <0f995c28c5b446ad8835419f76b319a3>:0 
      at Python.Runtime.PyObject.TryInvokeMember 
       (System.Dynamic.InvokeMemberBinder binder, System.Object[] args, 
      System.Object& result) [0x0003e] in <0f995c28c5b446ad8835419f76b319a3>:0 
      at (wrapper dynamic-method) 
 System.Object.CallSite.Target(System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object,QuantConnect.Data.Slice)

I have tried to edit the way I create the variable 'rsi' but nothing seems to work. Can someone tell me what I'm doing wrong?

orad
  • 15,272
  • 23
  • 77
  • 113
Hunter S
  • 25
  • 1
  • 4
  • Another comment: Line 35 refers to the first reference of 'self.SetHoldings'. – Hunter S Jul 16 '18 at 17:21
  • 3
    You should be initializing in the main function of your program. onData doesn't know rsi since it's been created for the first time in another method. – SamAtWork Jul 16 '18 at 17:23
  • 1
    Use `rsi = self.RSI("USDJPY", 14, MovingAverageType.Simple)` in your `OnData` function before conditioning on it. – Mankind_008 Jul 16 '18 at 17:28

2 Answers2

2

As you do with other attributes, you need to make this an instance variable by using self.

self.rsi = self.RSI(...)

...

if self.rsi > 72:

Alternatively, just move the definition into the OnData method.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
2

In QuantConnect/Lean, we have shortcut methods for indicators, they belong to the QCAlgorithm class (use self) and name are upper-cased. These helper methods create a new instance of a indicator object and hook it up to a data consolidator so that the indicator is automatically updated by the engine.

Since these methods create a new instance, we just should only to call it once (normally in Initialize) and assign it to a class variable to be accessed throughout the algorithm.

Please also note that the indicators are not numerical values, so we need to get its value in the Current.Value property:

def Initialize(self):
    self.SetCash(5000)
    self.SetStartDate(2015,1,1)
    self.SetEndDate(2018,1,1)
    self.SetBrokerageModel(BrokerageName.OandaBrokerage)

    self.usdjpy = self.AddForex("USDJPY", Resolution.Minute).Symbol
    self.rsi = self.RSI("USDJPY", 14, MovingAverageType.Simple)


def OnData(self, slice):
    if self.rsi.Current.Value > 72:
        self.SetHoldings("USDJPY", 1)

    if self.rsi.Current.Value < 28:
        self.SetHoldings("USDJPY", 1)