History.Where --> NullReferenceException: Object reference not set to an instance of an object

Created at 25 Sep 2022, 15:30
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
NC

ncel01

Joined 19.03.2020

History.Where --> NullReferenceException: Object reference not set to an instance of an object
25 Sep 2022, 15:30


Hi,

I've noticed that the following code is okay when backtesting but not when running in real-time:

  • TEST 1
  • Crashed in OnStart with NullReferenceException: Object reference not set to an instance of an object.
  • CBot instance [New cBot, US500, h1] crashed with error "Crashed in OnStart with NullReferenceException: Object reference not set to an instance of an object."

What can be a reason for this?

Thank you.

using System;
using System.Linq;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = "Hello world!")]
        public string Message { get; set; }

        protected override void OnStart()
        {
            Print("TEST 1");

            var targetTrades = History.Where(o => o.Label.Contains("#", StringComparison.OrdinalIgnoreCase)).ToArray();

            Print("TEST 2");
        }

        protected override void OnTick()
        {
            // Handle price updates here
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

 


@ncel01
Replies

PanagiotisCharalampous
26 Sep 2022, 11:42

Hi ncel01,

I tried this on real time and I get no exceptions. You need to debug this in VS and check what is actually a null reference.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

ncel01
27 Sep 2022, 22:46 ( Updated at: 21 Dec 2023, 09:22 )

RE:

Hi Panagiotis,

Thanks for your reply.

Below is a screenshot taken from VS when debugging. Unfortunately, I don't think this helped a lot.

Given this, could you please provide more details on what can be a reson for this issue?

This happens when runnning the cBot in real-time but not when backtesting. Wasn't the outcome suppose to be the same for both the cases?
As far as I know, History is a property for the Account and I am using the same account when running both tests. Moreover, there is no variable other than targetTrades here defined.

It is also not clear to me why you get no exceptions, considering the exact same code, while I get the following:

Thank you once again!


@ncel01

PanagiotisCharalampous
28 Sep 2022, 08:35

Hi ncel01,

I think the exception is very clear. You are trying to call a method on Label which sometimes is null. You need to check if Label is null first, before calling Equals. Try the below 

var targetTrades = History.Where(o => o.Label != null && o.Label.Contains("#", StringComparison.OrdinalIgnoreCase)).ToArray();

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

ncel01
29 Sep 2022, 22:46

RE:

Hi Panagiotis,

Yes, it solved the problem! In fact, I was expecting that only the closed trades with a label defined would be part of targetTrades.

I've also noticed that no exception was thrown when backtesting since History collection will only contain the backtest trades.

Thanks for helping with this!

 


@ncel01

PanagiotisChar
01 Oct 2022, 10:14

Hi ncel01,

You are welcome!

Aieden Technologies

Need Help? Join us on Telegram

 


@PanagiotisChar