Filter the net profit / loss of some positions

Created at 29 Sep 2014, 14:03
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!
Forex19's avatar

Forex19

Joined 13.06.2013

Filter the net profit / loss of some positions
29 Sep 2014, 14:03


Hi, 
if I'm running more cBot, Is there a way to get a variable that calculates the net profit / loss of the open positions by only one of the cBot? 

This variable, then I would like to use as a parameter to close all positions managed by the cBot.

Thanks.


@Forex19
Replies

Spotware
29 Sep 2014, 14:46

You can use Label for such purpose.

        [Parameter(DefaultValue = "cBot unique identifier")]
        public string Label { get; set; }

When you open position, you need to use Label:

ExecuteMarketOrder(TradeType.Buy, Symbol, someVolume, Label);

Then you will be able to calculate profit/loss for positions with that specific label:

var pnl = Positions.FindAll(Label).Sum(position => position.NetProfit);

You can also close all positions with specific label:

            foreach (var position in Positions.FindAll(Label))
            {
                ClosePosition(position);
            }

 


@Spotware

Forex19
29 Sep 2014, 16:01

RE:

Since I have already adapted the label to other needs of the code. 
is possible to obtain the same result in another way? I think for example to use a list where converge all positions of a cBot.

Spotware said:

You can use Label for such purpose.

        [Parameter(DefaultValue = "cBot unique identifier")]
        public string Label { get; set; }

When you open position, you need to use Label:

ExecuteMarketOrder(TradeType.Buy, Symbol, someVolume, Label);

Then you will be able to calculate profit/loss for positions with that specific label:

var pnl = Positions.FindAll(Label).Sum(position => position.NetProfit);

You can also close all positions with specific label:

            foreach (var position in Positions.FindAll(Label))
            {
                ClosePosition(position);
            }

 

 


@Forex19

Spotware
29 Sep 2014, 16:09

is possible to obtain the same result in another way?

Don't think so. If you find a solution, please post it here.


@Spotware

Forex19
29 Sep 2014, 18:31

RE:

I would like to close all position with a specific label, which generate a P&L less than -10, for example.

In OnTick, is correct put:

            var pnl = Positions.FindAll(Label).Sum(position => position.NetProfit);
            foreach (var position in Positions.FindAll(Label))
            {
                if (pnl <= -10)
                    ClosePosition(position);
            }

???

Spotware said:

Then you will be able to calculate profit/loss for positions with that specific label:

var pnl = Positions.FindAll(Label).Sum(position => position.NetProfit);

You can also close all positions with specific label:

            foreach (var position in Positions.FindAll(Label))
            {
                ClosePosition(position);
            }

 

 


@Forex19