ModifyPositionAsync within OnPositionsModified loops 4 times

Created at 04 Dec 2018, 10:46
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!
sifneos4fx's avatar

sifneos4fx

Joined 15.11.2017

ModifyPositionAsync within OnPositionsModified loops 4 times
04 Dec 2018, 10:46


Hello Panagiotis

Actually, the code bellow should loop for over (which of course I don't want) but it loops exactly 4 times. Got several trades on the same asset, what I want to achieve is, when I drag one TP  to a new price, all remaiing order of the same asset should also change TP to that new price. It works, however, it loops 4 times, so it changes TP for times for every order. Any suggestions?

        void OnPositionsModified(PositionModifiedEventArgs obj)
        {
            foreach (Position pos in Positions.FindAll(obj.Position.SymbolCode))
            {

                ModifyPositionAsync(pos, obj.Position.StopLoss, obj.Position.TakeProfit);

                System.Threading.Thread.Sleep(500);
            }
        }

Thank you

Patrick


@sifneos4fx
Replies

PanagiotisCharalampous
04 Dec 2018, 10:57

Hi Patrick,

A suggestion is to check the position's take profit before modifying. If it is the same with the position you want to copy from, skip modification. This should break the loop.

Best Regards,

Panagiotis


@PanagiotisCharalampous

sifneos4fx
04 Dec 2018, 11:08

RE:

Yeah, great!!! sorry for that uncessary question!!!!

 

Panagiotis Charalampous said:

Hi Patrick,

A suggestion is to check the position's take profit before modifying. If it is the same with the position you want to copy from, skip modification. This should break the loop.

Best Regards,

Panagiotis

 


@sifneos4fx