ClosePosition SymbolName

Created at 18 Nov 2020, 00:19
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!
MG

mgpublic

Joined 06.11.2020

ClosePosition SymbolName
18 Nov 2020, 00:19


Hi,

I need help with the code below so that it only closes the position with the indicated SymbolName for example "USDJPY"

I'm using the code below and it closes all positions.

if ()                   

{
foreach (var position in Positions)
ClosePositionAsync(position);
}

Thx for help!

//mgpublic


@mgpublic
Replies

mparama
18 Nov 2020, 00:40

RE:

mgpublic said:

Hi,

I need help with the code below so that it only closes the position with the indicated SymbolName for example "USDJPY"

I'm using the code below and it closes all positions.

if ()                   

{
foreach (var position in Positions)
ClosePositionAsync(position);
}

Thx for help!

//mgpublic

You should use:

            var posBuy = Positions.FindAll(Label, SymbolName, TradeType.Buy);
            var posSell = Positions.FindAll(Label, SymbolName, TradeType.Sell);

           

if ()                   

{
foreach (var position in posBuy)
ClosePositionAsync(position);
}


@mparama

mgpublic
18 Nov 2020, 01:08

RE: RE:

mparama said:

mgpublic said:

Hi,

I need help with the code below so that it only closes the position with the indicated SymbolName for example "USDJPY"

I'm using the code below and it closes all positions.

if ()                   

{
foreach (var position in Positions)
ClosePositionAsync(position);
}

Thx for help!

//mgpublic

You should use:

            var posBuy = Positions.FindAll(Label, SymbolName, TradeType.Buy);
            var posSell = Positions.FindAll(Label, SymbolName, TradeType.Sell);

           

if ()                   

{
foreach (var position in posBuy)
ClosePositionAsync(position);
}

Thx for reply and help.

How do I code so that it finds and closes the position with SymbolName "USDJPY" either if its posBuy or posSell. In the example above its only posBuy.


@mgpublic

nguyendan81985
21 Jan 2021, 11:08

RE: RE: RE:

mgpublic said:

 

Thx for reply and help.

How do I code so that it finds and closes the position with SymbolName "USDJPY" either if its posBuy or posSell. In the example above its only posBuy.

hi

 

you can try as below:

 

foreach (var position in Positions)
                {
                    if (position.SymbolName == Symbol.Name)
                    {

                        ClosePosition(position);
                    }

                }


@nguyendan81985