Easier Way To Get Open Position Volume

Created at 09 Sep 2019, 17:32
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!
jumpycalm's avatar

jumpycalm

Joined 28.03.2019

Easier Way To Get Open Position Volume
09 Sep 2019, 17:32


Hi Panagiotis, quick question. Suppose I have multiple open position, for example 1000 unit buy, 3000 unit buy, and 5000 unit sell position open, I want to cBot find the total buy direction volume (in this case 4000 unit) and total sell direction volume (in this case 5000) unit. One way to do is to use foreach to loop through all the buy and sell positions, see code below. My question for you is is there an easier way to do? Such as something like this "buyTotalVolume = Positions.TotalVolumeInUnits.TradeType.Buy"?

int buyTotalVolume = 0;
int sellTotalVolume = 0;
foreach (var position in Positions)
{
  if (position.SymbolName == SymbolName && position.TradeType == TradeType.Buy)
      buyTotalVolume += (int) position.VolumeInUnits;
}
foreach (var position in Positions)
{
   if (position.SymbolName == SymbolName && position.TradeType == TradeType.Sell)
       sellTotalVolume += (int)position.VolumeInUnits;
}
Print("buyTotalVolume = {0}, sellTotalVolume = {1}", buyTotalVolume, sellTotalVolume);

 


@jumpycalm
Replies

PanagiotisCharalampous
09 Sep 2019, 17:37

Hi jumpycalm,

See below

            var buyTotalVolume = Positions.Where(x => x.TradeType == TradeType.Buy).Sum(x => x.VolumeInUnits);
            var sellTotalVolume = Positions.Where(x => x.TradeType == TradeType.Sell).Sum(x => x.VolumeInUnits);

Best Regards,

Panagiotis


@PanagiotisCharalampous

jumpycalm
09 Sep 2019, 17:39

RE:

Panagiotis Charalampous said:

Hi jumpycalm,

See below

            var buyTotalVolume = Positions.Where(x => x.TradeType == TradeType.Buy).Sum(x => x.VolumeInUnits);
            var sellTotalVolume = Positions.Where(x => x.TradeType == TradeType.Sell).Sum(x => x.VolumeInUnits);

Best Regards,

Panagiotis

Very helpful, thank you for the quick response!


@jumpycalm