Back

AddForex() method is giving me the error "NullReferenceException : Object reference not set to an instance of an object"

I am trying to get the Basic Template Forex Algorithm code to run:

from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Data import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from datetime import timedelta
import numpy as np

### <summary>
### Algorithm demonstrating FOREX asset types and requesting history on them in bulk. As FOREX uses
### QuoteBars you should request slices
### </summary>
### <meta name="tag" content="using data" />
### <meta name="tag" content="history and warm up" />
### <meta name="tag" content="history" />
### <meta name="tag" content="forex" />
class BasicTemplateForexAlgorithm(QCAlgorithm):

def Initialize(self):
# Set the cash we'd like to use for our backtest
self.SetCash(100000)

# Start and end dates for the backtest.
self.SetStartDate(2013, 10, 7)
self.SetEndDate(2013, 10, 11)

# Add FOREX contract you want to trade
# find available contracts here https://www.quantconnect.com/data#forex/oanda/cfd
self.AddForex("EURUSD", Resolution.Minute)
self.AddForex("GBPUSD", Resolution.Minute)
self.AddForex("EURGBP", Resolution.Minute)

self.History(5, Resolution.Daily)
self.History(5, Resolution.Hour)
self.History(5, Resolution.Minute)

history = self.History(TimeSpan.FromSeconds(5), Resolution.Second)

for data in sorted(history, key=lambda x: x.Time):
for key in data.Keys:
self.Log(str(key.Value) + ": " + str(data.Time) + " > " + str(data[key].Value))

def OnData(self, data):
# Print to console to verify that data is coming in
for key in data.Keys:
self.Log(str(key.Value) + ": " + str(data.Time) + " > " + str(data[key].Value))


algo = BasicTemplateForexAlgorithm()
algo.Initialize()

However I keep running into this error:

Algorithm.Initialize() Error: During the algorithm initialization, the following exception has occurred: Loader.TryCreatePythonAlgorithm(): Unable to import python module ./cache/algorithm/main.py. AlgorithmPythonWrapper(): NullReferenceException : Object reference not set to an instance of an object
at QuantConnect.Algorithm.QCAlgorithm.AddSecurity[T] (QuantConnect.SecurityType securityType, System.String ticker, QuantConnect.Resolution resolution, System.String market, System.Boolean fillDataForward, System.Decimal leverage, System.Boolean extendedMarketHours) [0x0008e] in <e5aa4033954c479fab6d1b73330b4508>:0
at QuantConnect.Algorithm.QCAlgorithm.AddForex (System.String ticker, QuantConnect.Resolution resolution, System.String market, System.Boolean fillDataForward, System.Decimal leverage) [0x00001] in <e5aa4033954c479fab6d1b73330b4508>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <b0e1ad7573a24fd5a9f2af9595e677e7>:0
at <module> in main.py:line 56
:: algo.Initialize()
at Initialize in main.py:line 35
NullReferenceException : Object reference not set to an instance of an object
at QuantConnect.Algorithm.QCAlgorithm.AddSecurity[T] (QuantConnect.SecurityType securityType, System.String ticker, QuantConnect.Resolution resolution, System.String market, System.Boolean fillDataForward, System.Decimal leverage, System.Boolean extendedMarketHours) [0x0008e] in <e5aa4033954c479fab6d1b73330b4508>:0
at QuantConnect.Algorithm.QCAlgorithm.AddForex (System.String ticker, QuantConnect.Resolution resolution, System.String market, System.Boolean fillDataForward, System.Decimal leverage) [0x00001] in <e5aa4033954c479fab6d1b73330b4508>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <b0e1ad7573a24fd5a9f2af9595e677e7>:0 Stack Trace: QuantConnect.Lean.Engine.Setup.AlgorithmSetupException: During the algorithm initialization, the following exception has occurred: Loader.TryCreatePythonAlgorithm(): Unable to import python module ./cache/algorithm/main.py. AlgorithmPythonWrapper(): NullReferenceException : Object reference not set to an instance of an object
at QuantConnect.Algorithm.QCAlgorithm.AddSecurity[T] (QuantConnect.SecurityType securityType, System.String ticker, QuantConnect.Resolution resolution, System.String market, System.Boolean fillDataForward, System.Decimal leverage, System.Boolean extendedMarketHours) [0x0008e] in <e5aa4033954c479fab6d1b73330b4508>:0
at QuantConnect.Algorithm.QCAlgorithm.AddForex (System.String ticker, QuantConnect.Resolution resolution, System.String market, System.Boolean fillDataForward, System.Decimal leverage) [0x00001] in <e5aa4033954c479fab6d1b73330b4508>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <b0e1ad7573a24fd5a9f2af9595e677e7>:0
at <module> in main.py:line 56
:: algo.Initialize()
at Initialize in main.py:line 35
NullReferenceException : Object reference not set to an instance of an object
at QuantConnect.Algorithm.QCAlgorithm.AddSecurity[T] (QuantConnect.SecurityType securityType, System.String ticker, QuantConnect.Resolution resolution, System.String market, System.Boolean fillDataForward, System.Decimal leverage, System.Boolean extendedMarketHours) [0x0008e] in <e5aa4033954c479fab6d1b73330b4508>:0
at QuantConnect.Algorithm.QCAlgorithm.AddForex (System.String ticker, QuantConnect.Resolution resolution, System.String market, System.Boolean fillDataForward, System.Decimal leverage) [0x00001] in <e5aa4033954c479fab6d1b73330b4508>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <b0e1ad7573a24fd5a9f2af9595e677e7>:0
at QuantConnect.Lean.Engine.Setup.BacktestingSetupHandler.CreateAlgorithmInstance (QuantConnect.Packets.AlgorithmNodePacket algorithmNodePacket, System.String assemblyPath) [0x0009c] in <4aaa8484de144245b7fdcf42493c0ea9>:0
at QuantConnect.Lean.Engine.Engine.Run (QuantConnect.Packets.AlgorithmNodePacket job, QuantConnect.Lean.Engine.AlgorithmManager manager, System.String assemblyPath) [0x00140] in <4aaa8484de144245b7fdcf42493c0ea9>:0 (Open Stacktrace)

 

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 Veralitix ,

Python script algorithms in Lean are embedded in a C# engine and cannot contain the following instance initialization: 

algo = BasicTemplateForexAlgorithm()
algo.Initialize()

Please follow the instructions on how to run python algorithms here.

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.


Thanks.  I'm confused though because I'm running the algo on your platform, not on my local machine, and it seems like the instructions in your link are for py installed on local machines.

0

Hi,

Alex's note about Python algorithms not containing 

algo = BasicTemplateForexAlgorithm()
algo.Initialize()

still applies for developing in the IDE online. All that is necessary is the code you have but without those two lines, as the main.py file will be called by the Lean engine and executed based on the QCAlgorithm class instance in that file.

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.


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