Positions / PendingOrders .Count & .Find - Inconsistency
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:
- // ### Positions.Count --> OK but missing label
- // ### Positions.Find --> OK (incl. label)
- // ### PendingOrders.Count --> OK but missing label
- // ### PendingOrders.Find --> NOK (not available)
Thank you!
Replies
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
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:
Now regarding PendingOrders, you can use Linq here too, no need for any extra helper methods:
And don't forget to call "ToArray" at the end:
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