Easier Way To Get Open Position Volume

jumpycalm's avatar
jumpycalm since: 28 Mar 2019; 
 09 Sep 2019, 17:32
Easier Way To Get Open Position Volume

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);
Panagiotis Charalampous's avatar
Panagiotis Charalampous since: 13 Jan 2017; 
 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

Head of Community Management at cTrader
jumpycalm's avatar
jumpycalm since: 28 Mar 2019; 
 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!