Positions.Count is returning 0 inside Positions_Opened()

Created at 24 Aug 2024, 17:38
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.Count is returning 0 inside Positions_Opened()
24 Aug 2024, 17:38


Dear cTrader Team,

I've noticed that Positions.Count is returning 0 inside Positions_Opened().
I've added some screenshots for your reference, as well as the cBot code, hoping you're able to reproduce this issue.

Should you require any additional information please let me know.


using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class MarginTest : Robot
    {
        protected override void OnStart()
        {
            Positions.Opened += Positions_Opened;
            Positions.Closed += Positions_Closed;

            Print("Symbol leverage : ", Symbol.DynamicLeverage[0].Leverage);
            Print("Account leverage : ", Account.PreciseLeverage);
            Print("Symbol currency : ", Symbol.QuoteAsset);
            Print("Account currency : ", Account.Asset);
            Print("Symbol pip size : ", Symbol.PipSize);

            PlaceLimitOrder(TradeType.Buy, SymbolName, Symbol.QuantityToVolumeInUnits(1), Symbol.Ask - 300, null, 1500 * Symbol.PipSize, 1500 * Symbol.PipSize);
            PlaceLimitOrder(TradeType.Buy, SymbolName, Symbol.QuantityToVolumeInUnits(1), Symbol.Ask - 600, null, 1500 * Symbol.PipSize, 1500 * Symbol.PipSize);
        }

        private void Positions_Opened(PositionOpenedEventArgs obj)
        {
            Print("**************** Positions_Opened ****************");
            Print("Amount of open positions : ", Positions.Count);

            double tradesTotalSize = 0;
            double accountMargin;

            foreach (var position in Positions)
                tradesTotalSize += position.VolumeInUnits * position.EntryPrice;

            accountMargin = tradesTotalSize / Symbol.DynamicLeverage[0].Leverage;

            Print("Calculated account margin : {0:0.00}", accountMargin);
            Print("Account.Margin : ", Account.Margin);

            Print("Error (%) : {0:0.0}", 100 * (1 - Account.Margin / accountMargin));
        }

        private void Positions_Closed(PositionClosedEventArgs obj)
        {
            if (PendingOrders.Count == 0 && Positions.Count == 0)
                Stop();
        }
    }
}

@ncel01
Replies

PanagiotisCharalampous
26 Sep 2024, 05:15

Hi there,

Unfortunately we were not able to reproduce the problem. Positions amount shows correctly for us

Best regards,

Panagiotis


@PanagiotisCharalampous

ncel01
26 Sep 2024, 05:31

RE: Positions.Count is returning 0 inside Positions_Opened()

PanagiotisCharalampous said: 

Hi there,

Unfortunately we were not able to reproduce the problem. Positions amount shows correctly for us

Best regards,

Panagiotis

Panagiotis,

Can you please check this under the same conditions (broker, symbol, etc) ?

This way you'll be (obviously) more likely to be able to reproduce the issue.

Remark: It has been already noticed in the past that many issues that applied to indices were not applicable to forex.

Thanks.


@ncel01

PanagiotisCharalampous
26 Sep 2024, 09:16

RE: RE: Positions.Count is returning 0 inside Positions_Opened()

ncel01 said: 

PanagiotisCharalampous said: 

Hi there,

Unfortunately we were not able to reproduce the problem. Positions amount shows correctly for us

Best regards,

Panagiotis

Panagiotis,

Can you please check this under the same conditions (broker, symbol, etc) ?

This way you'll be (obviously) more likely to be able to reproduce the issue.

Remark: It has been already noticed in the past that many issues that applied to indices were not applicable to forex.

Thanks.

We did but with no luck unfortunately. We will keep it in mind though in case we stumble upon it in any other occasion


@PanagiotisCharalampous