Easier Way To Get Open Position Volume

09 Sep 2019, 17:32Easier Way To Get Open Position Volume#1
jumpycalmposts: 14since: 28 Mar 2019

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);

 

09 Sep 2019, 17:37#2
Panagiotis Charalampousposts: 3381since: 13 Jan 2017

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


Head of Community Management at cTrader
09 Sep 2019, 17:39RE:#3
jumpycalmposts: 14since: 28 Mar 2019

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!