Trouble error when get average Pips of Positions Label where starts with some Prefix

Created at 31 May 2024, 03:54
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!
MongolTrader's avatar

MongolTrader

Joined 12.02.2015

Trouble error when get average Pips of Positions Label where starts with some Prefix
31 May 2024, 03:54


I want to get average pips where positions label starts with prefix and below is code. 

///////////////////////////////////////////////////////////////////////////

private double AveragePips(string Name)
{
   var CheckPosition = Positions.Where(x => x.Label.StartsWith(Name)); 
   double AveragePips = 0;
   if (CheckPosition != null)
   {
       AveragePips = Positions.Where(x => x.Label.StartsWith(Name)).Average(y => y.Pips);
   }
   return AveragePips;
}

/////////////////////////////////////////////////////////////////////////
But it gives error below

--Crashed in OnBarClosed with NullReferenceException: Object reference not set to an instance of an object.

Please help fix this error.

 

 

 


@MongolTrader
Replies

PanagiotisCharalampous
31 May 2024, 05:52

Hi there,

This is probably thrown because your Label is null. Always check if Label is not null before you use it.

Best regards,

Panagiotis


@PanagiotisCharalampous

MongolTrader
31 May 2024, 05:59

RE: Trouble error when get average Pips of Positions Label where starts with some Prefix

PanagiotisCharalampous said: 

Hi there,

This is probably thrown because your Label is null. Always check if Label is not null before you use it.

Best regards,

Panagiotis

My bot always open position with label and also i see there positions with label. Also you can check above code section this code is check whether this position not null.


@MongolTrader

PanagiotisCharalampous
31 May 2024, 08:08

RE: RE: Trouble error when get average Pips of Positions Label where starts with some Prefix

MongolTrader said: 

PanagiotisCharalampous said: 

Hi there,

This is probably thrown because your Label is null. Always check if Label is not null before you use it.

Best regards,

Panagiotis

My bot always open position with label and also i see there positions with label. Also you can check above code section this code is check whether this position not null.

Then please share the complete cBot code and provide instructions how to reproduce the problem


@PanagiotisCharalampous