Back

PLEASE I need your help with this C# part of code

I have the following code which returns me an error:

Failed to initialize algorithm: Initialize(): System.NullReferenceException: Object reference not set to an instance of an object
at QuantConnect.Algorithm.CSharp.MyFirstAlgorithm.Initialize () [0x00113] in <c3378781b1e44bb3a34c27f412a5505b>:0
at QuantConnect.Lean.Engine.Setup.BacktestingSetupHandler+<>c__DisplayClass19_0.<Setup>b__0 () [0x00066] in <1f6d6705efa94d3eb00c00b8efeb3d8b>:0 (Open Stacktrace)

 

What am I doing wrong? I try to add an exponential moving average as value in a dictionary with key the specific symbol.

What is the object it talks about whose reference is not set?

Maybe is a basic question but I am beginner with CSharp. Thanks for your time in advance!

 

        string _clSymbol;

  Dictionary<string,ExponentialMovingAverage> _sma,_smaLong = new Dictionary<string, ExponentialMovingAverage>();

        public override void Initialize()
        {
            _ImportedSecurities.Add("BCOUSD",new KeyValuePair<double, int>(0.1, 10));           

            foreach (string security in _ImportedSecurities.Keys)

            {
            _clSymbol = security;                
            AddCfd(_clSymbol, Resolution.Minute, Market.Oanda);
           _sma.Add(_clSymbol,EMA(_clSymbol, 7, Resolution.Daily));

         

Update Backtest








Spyridon please attach the full algorithm for assistance.

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.


:( allow me not to publish the whole algorithm as there is a lot i wouldn't like to show... I was doing OK with signle product but now what I am trying to do is to create a dictionary so that i get the indicators separate for each symbol that i want to use in the same algorithm.

I have checked it is just there... that line which produces the error:

->->-> _sma.Add(_clSymbol,EMA(_clSymbol, 7, Resolution.Daily));

Thinking of it, probably it is because the EMA(_clSymbol, 7, Resolution.Daily) doesn't have any value yet at Initialize(). could it be?

If yes, how could I make it to update the _sma[mySymbolVariable] with the new EMA every day? this is all i actually need to do with the above code which gives the error.

0

Null Reference Exception means that you're trying to execute method or get property of an object that doesn't have value, it is Null.

Specifically, in the code that you posted you have 2 class variables, sma and smaLong, the last one is initialized but you forgot to assign any value to the first one, so probably it's giving you an error.

Before using this variable you should initialize the variable itself.

_sma = new Dictionary<string, ExponentialMovingAverage>();
_sma.Add(_clSymbol,EMA(_clSymbol, 7, Resolution.Daily));
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