为什么会出现此错误? (QuantConnect 算法)
Why am I getting this error? (QuantConnect Algorithm)
我刚刚开始使用 QuantConnect,但我理解 Python 相当好,或者我是这么认为的。这是我的代码的重要部分:
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)
这是我遇到的错误:
Runtime Error: TypeError : Cannot get managed object
at OnData in main.py:line 36
TypeError : Cannot get managed object
堆栈跟踪:
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)
我已经尝试编辑我创建变量的方式 'rsi' 但似乎没有任何效果。有人可以告诉我我做错了什么吗?
与其他属性一样,您需要使用 self
.
将其设为实例变量
self.rsi = self.RSI(...)
...
if self.rsi > 72:
或者,只需将定义移动到 OnData 方法中。
在QuantConnect/Lean中,我们有指标的快捷方法,它们属于QCAlgorithm class(使用self)并且名称是大写的。这些辅助方法创建一个指标对象的新实例并将其连接到数据整合器,以便指标由引擎自动更新。
由于这些方法创建了一个新实例,我们只需要调用它一次(通常在 Initialize 中)并将其分配给要访问的 class 变量整个算法。
另外请注意指标不是数值,所以我们需要在Current.Value属性:[=11=中得到它的值]
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)
我刚刚开始使用 QuantConnect,但我理解 Python 相当好,或者我是这么认为的。这是我的代码的重要部分:
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)
这是我遇到的错误:
Runtime Error: TypeError : Cannot get managed object
at OnData in main.py:line 36
TypeError : Cannot get managed object
堆栈跟踪:
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)
我已经尝试编辑我创建变量的方式 'rsi' 但似乎没有任何效果。有人可以告诉我我做错了什么吗?
与其他属性一样,您需要使用 self
.
self.rsi = self.RSI(...)
...
if self.rsi > 72:
或者,只需将定义移动到 OnData 方法中。
在QuantConnect/Lean中,我们有指标的快捷方法,它们属于QCAlgorithm class(使用self)并且名称是大写的。这些辅助方法创建一个指标对象的新实例并将其连接到数据整合器,以便指标由引擎自动更新。
由于这些方法创建了一个新实例,我们只需要调用它一次(通常在 Initialize 中)并将其分配给要访问的 class 变量整个算法。
另外请注意指标不是数值,所以我们需要在Current.Value属性:[=11=中得到它的值]
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)