Positions / PendingOrders .Count & .Find - Inconsistency

Created at 23 Jan 2022, 17:21
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

Positions / PendingOrders .Count & .Find - Inconsistency
23 Jan 2022, 17:21


Hello,

I've noticed the following, what I believe to be an inconsistency when using .Find / .Count on Positions / PendingOrders:

  • PendingOrders.Find: Not available, unlike Positions.Find?
  • Positions.Count & PendingOrders.Count: Unlike .Find, a label is not allowed here. How to count PendingOrders / Positions of a single cBot instance only? Should a cycle be created as an alternative to this?

In short:                

  1. // ### Positions.Count --> OK but missing label
  2. // ### Positions.Find --> OK (incl. label)
  3. // ### PendingOrders.Count --> OK but missing label
  4. // ### PendingOrders.Find --> NOK (not available)

 

Thank you!


@ncel01
Replies

amusleh
24 Jan 2022, 09:16 ( Updated at: 25 Jan 2022, 10:18 )

Hi,

The Find and FindAll methods are only available on Positions, it's basically a shorthand version of Linq where extension method:

var myPositions = Positions.Find("myLabel");
// Is equivalent to
var myPositions = Positions.Where(position => position.Label == "myLabel");
// Note: Don't use == for string equality as it will use the current thread culture,
// instead use Equal method of string and set the type of comparison you are looking
// to do, personally I prefer to use
  var myPositions = Positions.Where(position => position.Label.Equals("MyLabel", StringComparison.OrdinalIgnoreCase));
// which is both case insensitive and has much better performance

Now regarding PendingOrders, you can use Linq here too, no need for any extra helper methods:

var myOrders = PendingOrders.Where(order => order.Label.Equals("MyLabel", StringComparison.OrdinalIgnoreCase));

And don't forget to call "ToArray" at the end:

var myPositions = Positions.Where(position => position.Label.Equals("MyLabel", StringComparison.OrdinalIgnoreCase)).ToArray();
var myOrders = PendingOrders.Where(order => order.Label.Equals("MyLabel", StringComparison.OrdinalIgnoreCase)).ToArray();

Otherwise anytime you iterate over it a new allocation will be made and the Linq query will be executed several times.

The Find/FindAll methods are added only to make things little bit easier for those who have no knowledge of .NET or Linq, but I strongly recommend you to use Linq.


@amusleh

ncel01
24 Jan 2022, 21:38

RE:

amusleh said:

Hi,

The Find and FindAll methods are only available on Positions, it's basically a shorthand version of Linq where extension method:

var myPositions = Positions.Find("myLabel");
// Is equivalent to
var myPositions = Positions.Where(position => position.Label == "myLabel");
// Note: Don't use == for string equality as it will use the current thread culture,
// instead use Equal method of string and set the type of comparison you are looking
// to do, personally I prefer to use
  var myPositions = Positions.Where(position => position.Label.Equals("MyLabel", StringComparison.InvariantCultureIgnoreCase));
// which is both case insensitive and has much better performance

Now regarding PendingOrders, you can use Linq here too, no need for any extra helper methods:

var myOrders = PendingOrders.Where(order => order.Label.Equals("MyLabel", StringComparison.OrdinalIgnoreCase));

And don't forget to call "ToArray" at the end:

var myPositions = Positions.Where(position => position.Label.Equals("MyLabel", StringComparison.OrdinalIgnoreCase)).ToArray();
var myOrders = PendingOrders.Where(order => order.Label.Equals("MyLabel", StringComparison.OrdinalIgnoreCase)).ToArray();

Otherwise anytime you iterate over it a new allocation will be made and the Linq query will be executed several times.

The Find/FindAll methods are added only to make things little bit easier for those who have no knowledge of .NET or Linq, but I strongly recommend you to use Linq.

Hi amusleh,

That's great.

Thanks for clarifying!


@ncel01