Modify Global Close All to Instrument active Close All once Profit Target has been achieved.

Created at 13 May 2020, 13:48
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!
Symposium's avatar

Symposium

Joined 16.07.2018

Modify Global Close All to Instrument active Close All once Profit Target has been achieved.
13 May 2020, 13:48


Hello, the code below is a fantastically simple profit close Utility by Mario Hennenberger from Swingfish.com out of Singapore and posted to this forum a few years ago..... 

As it is currently coded it closes all positions globally ie: if it is running on one pair it will close all open trades on the platform once the Profit Target has been meet.

 

Can someone please alter the code, removing the global operation of this Cbot so it will only close the positions on the instrument it is active in.

It is a very handy Cbot as it can be used instead of a TP, enabling stealth like functionality to traders positions.

Thanks in Advance......

 

 

 

 

 

 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

/*
GlobalTP+ 0.22.1
 
Mario Hennenberger http://www.swingfish.trade/tools
 
terminates ALL open Positions and delete ALL Pending Orders of the Net Profit is reached
 
ToDo:
    - use percentages or absolute targets
    - switch between ALL and the current Product/Pair
 
Lizense:
    Creative Common - you are REQUIRED to mention me or swingfish.trade if you re-publish this.
 
 
get Updates:
    - https://ctdn.com/algos/cbots/show/1664
    - http://swingfish.trade/tools
 
Changes:
    - remove cents display
    - better text and display equity
    - reset equity OnTick
    - bug in calculating the total equity
    - use net instead of Gross
    - convert api names to new version
*/

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class GlobalTPPlus : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double CashTarget { get; set; }

        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        protected override void OnTick()
        {
            closeWhenTarget(CashTarget);
            ChartObjects.DrawText("CashTarget0", "Target: $" + CashTarget + " " + Account.Currency + " | Dist: " + Math.Round((Account.Equity - Account.Balance - CashTarget), 0) + " | Equity: $" + Math.Round((Account.Equity), 0), StaticPosition.TopLeft);
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
            // will put a log here .. need to figure out how that works 
        }


        private void closeWhenTarget(double target)
        {
            double profit = 0;

            foreach (Position p in Positions)
                profit += p.NetProfit;

            // when profit is negative, we're done
            if (profit < target)
                return;

            // target is reached 
            foreach (Position p in Positions)
                ClosePosition(p);

            //  close all pending orders
            foreach (PendingOrder o in PendingOrders)
                CancelPendingOrder(o);


            // go sleep
            Stop();
        }
    }
}

 


@Symposium
Replies

PanagiotisCharalampous
13 May 2020, 14:13

Hi Symposium,

Here it is

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

/*
GlobalTP+ 0.22.1
 
Mario Hennenberger http://www.swingfish.trade/tools
 
terminates ALL open Positions and delete ALL Pending Orders of the Net Profit is reached
 
ToDo:
    - use percentages or absolute targets
    - switch between ALL and the current Product/Pair
 
Lizense:
    Creative Common - you are REQUIRED to mention me or swingfish.trade if you re-publish this.
 
 
get Updates:
    - https://ctdn.com/algos/cbots/show/1664
    - http://swingfish.trade/tools
 
Changes:
    - remove cents display
    - better text and display equity
    - reset equity OnTick
    - bug in calculating the total equity
    - use net instead of Gross
    - convert api names to new version
*/

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class GlobalTPPlus : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double CashTarget { get; set; }

        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        protected override void OnTick()
        {
            closeWhenTarget(CashTarget);
            ChartObjects.DrawText("CashTarget0", "Target: $" + CashTarget + " " + Account.Currency + " | Dist: " + Math.Round((Account.Equity - Account.Balance - CashTarget), 0) + " | Equity: $" + Math.Round((Account.Equity), 0), StaticPosition.TopLeft);
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
            // will put a log here .. need to figure out how that works 
        }


        private void closeWhenTarget(double target)
        {
            double profit = 0;

            foreach (Position p in Positions)
                profit += p.NetProfit;

            // when profit is negative, we're done
            if (profit < target)
                return;

            // target is reached 
            foreach (Position p in Positions.Where(x => x.SymbolName == Symbol.Name))
                ClosePosition(p);

            //  close all pending orders
            foreach (PendingOrder o in PendingOrders)
                CancelPendingOrder(o);


            // go sleep
            Stop();
        }
    }
}

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

Symposium
13 May 2020, 14:48

RE:

Wow.... Thank you Panagiotis, Hopefully It will be a useful piece of coding for many using the impressive Ctrader Platform.... 

PS... I'm a member.... We've chatted in the Telegram room a while back.... Haven't been logged in for a while... StaySafe and Many Thanks again.

 

 

 


@Symposium