how to close all positions if GrossProfit reached an amount?

Created at 03 Jul 2017, 23:33
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!
HI

hiba7rain

Joined 20.07.2014

how to close all positions if GrossProfit reached an amount?
03 Jul 2017, 23:33


Hi need cbot or cbot code on how to close all positons of a same label once grossprofit reached xx amount 

i have used the below code but it still close each positon alone at the gross profit and not all positions 

what am looking for is to have the cbot close all positions of the same label once the colactive gross profit from all positions is equl to the gross profit set in my parameter   

 

[Parameter("Aprofit", DefaultValue = 17.0)]
        public double Aprofit { get; set; }

protected override void OnTick()
        {

            var cBotPositions = Positions.FindAll(cBotLabel);

            
            foreach (var position in cBotPositions)
            {
                if (position.GrossProfit >= Aprofit)
                    ClosePosition(position);
            }

 

 

 

 


@hiba7rain
Replies

Jiri
04 Jul 2017, 13:58

Using LINQ queries.

var positions = Positions.Where(position => position.Label == cBotLabel).ToList();

if (positions.Sum(position => position.GrossProfit) >= Aprofit)
{
    positions.ForEach(position => ClosePositionAsync(position));
}

 


@Jiri

hiba7rain
04 Jul 2017, 14:48

RE:

thanks G for your support 

I think I did it worng as below 

i have question what would be the difference  between label if set as

[Parameter(DefaultValue = "TcBot")]
public string cBotLabel { get; set; }

​Or to use this 

private const string label = "TcBot"

 

protected override void OnTick
        {
           var netProfit = 0.0;
            foreach (var openedPosition in Positions)
            {
                netProfit += openedPosition.NetProfit + openedPosition.Commissions;
            }        
            {
                if (Account.Equity - Account.Balance >= Aprofit)
                {

                    foreach (var openedPosition in Positions)
                    {
                      ClosePosition(openedPosition);
                        foreach (var pendingOrder in PendingOrders)
                        {
                            CancelPendingOrder(pendingOrder);
                        }
                    }
                }
            }

 

tmc. said:

Using LINQ queries.

var positions = Positions.Where(position => position.Label == cBotLabel).ToList();

if (positions.Sum(position => position.GrossProfit) >= Aprofit)
{
    positions.ForEach(position => ClosePositionAsync(position));
}

 

 


@hiba7rain

Jiri
04 Jul 2017, 15:15

Public: The type or member can be accessed by any other code in the same assembly or another assembly that references it.
Private: The type or member can only be accessed by code in the same class or struct.

Const is a keyword that indicates a constant. It describes an entity that cannot be changed at program runtime.


@Jiri