Close ALL trades on profit

Created at 28 Dec 2017, 12:59
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!
AR

armstr.tradie

Joined 30.03.2016

Close ALL trades on profit
28 Dec 2017, 12:59


Hi there,

I am working on a bot that should close ALL positions when the NetProfit reaches a certain amount.

Unfortunately regardless of whether they are BUY or SELL positions, it will often leave one or more trades unclosed that should have been closed. Often it is a trade that is going against the general direction of the trend. For example, it may close four BUY positions but leave the one SELL position present still running.

Ideally it would close all open positions regardless of whether they are buy or sell.

Below are two different attempts I have at this logic. If anyone can help me with this it would be greatly appreciated.

        protected void NetProfit()
        {

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

            if (positions.Sum(position => position.NetProfit) >= ProfitLevel)
            {
                positions.ForEach(position => ClosePositionAsync(position));
                {
                    foreach (var order in PendingOrders)
                        CancelPendingOrderAsync(order);
                }
            }
        }

---------OR-------------

        protected void NetProfit()
        {
            double netProfit = 0;
            var positions = Positions.FindAll(Label);

            foreach (var position in Positions)
            {
                netProfit += Symbol.UnrealizedNetProfit;
            }
            if (netProfit >= ProfitLevel)
            {
                foreach (var position in positions)
                {
                    ClosePositionAsync(Positions.Find(Label));
                }
                foreach (var order in PendingOrders)
                {
                    CancelPendingOrderAsync(order);
                }
            }
        }

 

 


@armstr.tradie
Replies

ClickAlgo
28 Dec 2017, 13:33

You may be over complicating it a bit, try to keep your code simple and not overuse LINQ, you just need:

var positions = this.Positions.FindAll(Label);
double netProfit = positions.Sum(x => x.NetProfit);

if(netProfit >= ProfitTarget)
{
   foreach (var position in positions)
   {
      ClosePosition(position);
   }
}

I have not tested this, but it should work.

Paul Hayes
Sales & Marketing
Emailcontact@clickalgo.com
Phone: (44) 203 289 6573
Websitehttps://clickalgo.com


@ClickAlgo

armstr.tradie
07 Jan 2018, 07:57

Thank you Paul, it worked perfectly. Keep up the good work. I appreciate the help you provide on this forum. 


@armstr.tradie

Obiriec
12 Feb 2020, 20:12

RE: Break even with trailing

ClickAlgo said:

You may be over complicating it a bit, try to keep your code simple and not overuse LINQ, you just need:

var positions = this.Positions.FindAll(Label);
double netProfit = positions.Sum(x => x.NetProfit);

if(netProfit >= ProfitTarget)
{
   foreach (var position in positions)
   {
      ClosePosition(position);
   }
}

I have not tested this, but it should work.

Paul Hayes
Sales & Marketing
Emailcontact@clickalgo.com
Phone: (44) 203 289 6573
Websitehttps://clickalgo.com

Good morning, congratulations on your work here on this forum.
I apologize for my poor English, I am using google translator !!

I would like to know if it is possible to modify an existing cBot, which is used to close all positions of a specific currency pair (eg EURUSD) when these have reached a certain "break even" value and make sure that - - if the profit of all the open positions reaches the trigger and goes back, of course they close to reach the break even value --- but --- it would be very convenient if you could make sure that --- if once the trigger is reached, the profit continues to increase, you could move the trigger itself with a trail value, so everything follows the profit and when it comes back, the trail remains still.
A sort of Trailing Stop Loss for Break Even !!

The parameters that must be managed by the user, in addition to those used for managing the values, it should also be possible to enter the "label" (magic number) and the name of the currency pair in question.

I don't know if I'm asking for an impossible thing but I think I asked the right person the question.

Looking forward to your reply,
Yours sincerely.


@Obiriec

ClickAlgo
13 Feb 2020, 08:43

This should not be a problem.

If you would like to use our development service to do this for you just send an email to development@clickalgo.com together with your existing projects source code and a full description of what help you need.

Paul Hayes
Sales & Marketing
Emailcontact@clickalgo.com
Phone: (44) 203 289 6573
Websitehttps://clickalgo.com

Twitter | Facebook | YouTube | Pinterest | LinkedIn

PS: Why not join our instant chat group on Telegram.


@ClickAlgo

Obiriec
13 Feb 2020, 16:26

RE:

ClickAlgo said:

This should not be a problem.

If you would like to use our development service to do this for you just send an email to development@clickalgo.com together with your existing projects source code and a full description of what help you need.

Paul Hayes
Sales & Marketing
Emailcontact@clickalgo.com
Phone: (44) 203 289 6573
Websitehttps://clickalgo.com

Twitter | Facebook | YouTube | Pinterest | LinkedIn

PS: Why not join our instant chat group on Telegram.

Ok, thanks!


@Obiriec