OnPositionsOpened() works strange

Created at 03 Jan 2022, 04:07
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!
YS

ys2310

Joined 03.12.2021

OnPositionsOpened() works strange
03 Jan 2022, 04:07


Hi,

I'm trying to count how many positions are open at each OnPositionsOpened() event inside of 

this OnPositionsOpened() function. the num_of_open_position variable increment like 1,1, 2,2, 

3, 3, 4, 4, 5, 5, 6, 6, instead of 1, 2, 3, 4, 5, 6. Anyone understands what's happening in this case? 

---

 private void OnPositionsOpened(PositionOpenedEventArgs args)
        {            
            int num_of_open_position = 0;
            foreach (var p in Positions)
                num_of_open_position++;

            var position = args.Position;
            Print("OnPositionOpened: {0} {1} {2} {3} {4}", position.Id, position.EntryTime, position.TradeType, position.Comment, num_of_open_position);
        }
        


@ys2310
Replies

amusleh
03 Jan 2022, 08:57

Hi,

If you want to get the count of open Positions then you can use Positions.Count:

        private void OnPositionsOpened(PositionOpenedEventArgs args)
        {
            Print("Positions Count: ", Positions.Count);
        }

And regarding your code, there is no issue, I tested it and it does work fine, but you don't have to count the Position by yourself, just use Positions.Count.


@amusleh

ys2310
03 Jan 2022, 09:13 ( Updated at: 21 Dec 2023, 09:22 )

RE:

ys2310 said:

In my first screen shot, why do I see 5, 5, 6, 6 instead of 4, 5, 6, 7?

The last number is the number of opened position. it 4, 4, 4, 4, 4, 6, 6, 4, 4

positions.Count increments by 2 and the same number appears multiple times instead of 

1, 2, 3, 4, 5... Am I misunderstanding anything here?

Hi,

I'm trying to count how many positions are open at each OnPositionsOpened() event inside of 

this OnPositionsOpened() function. the num_of_open_position variable increment like 1,1, 2,2, 

3, 3, 4, 4, 5, 5, 6, 6, instead of 1, 2, 3, 4, 5, 6. Anyone understands what's happening in this case? 

---

 private void OnPositionsOpened(PositionOpenedEventArgs args)
        {            
            int num_of_open_position = 0;
            foreach (var p in Positions)
                num_of_open_position++;

            var position = args.Position;
            Print("OnPositionOpened: {0} {1} {2} {3} {4}", position.Id, position.EntryTime, position.TradeType, position.Comment, num_of_open_position);
        }
        

 


@ys2310