Close all positions in 1 click on the screen

Created at 30 May 2017, 19:53
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!
TR

tradermatrix

Joined 24.07.2012

Close all positions in 1 click on the screen
30 May 2017, 19:53


Hello
I close my positions with total net winnings.
Each cbot or each indicator or instance is connected to a label..and total earnings individually and automatically closes each of them.
As if below, I visualize on the screen my operations .....
But if for one reason or another I want to take my profits or losses on a single instance (which can be composed of several open trades) I am obliged to cancel them one after the other on the screen or on the board .
Is it possible to add a code that allows to manually close all positions (by label) in one click on the screen ..? a logo or a cross....etc
Here is a sample code.
thank you very much
 

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter("Use EUR/USD", DefaultValue = true)]
        public bool useEURUSD { get; set; }

        [Parameter("Label EUR/USD", DefaultValue = "eurusd")]
        public string RobotId1 { get; set; }

        [Parameter("Gains Total ", DefaultValue = 3)]
        public double totalgains1 { get; set; }

        /////////////////////////////////////////////////////////////////////////////////

        [Parameter("Use GBP/USD", DefaultValue = true)]
        public bool useGBPUSD { get; set; }

        [Parameter("Label GBP/USD", DefaultValue = "gbpusd")]
        public string RobotId2 { get; set; }

        [Parameter("Gains Total ", DefaultValue = 5)]
        public double totalgains2 { get; set; }

        /////////////////////////////////////////////////////////////////////////////////

        [Parameter("Use USD/JPY", DefaultValue = true)]
        public bool useUSDJPY { get; set; }

        [Parameter("Label USD/JPY", DefaultValue = "usdjpy")]
        public string RobotId3 { get; set; }

        [Parameter("Gains Total ", DefaultValue = 2)]
        public double totalgains3 { get; set; }



        public double earn1;
        public double earn2;
        public double earn3;



        protected override void OnStart()
        {
            DisplayEarn();
        }

        protected override void OnTick()
        {

            DisplayEarn();

            gains1();
            gains2();
            gains3();
        }
        private void DisplayEarn()
        {

            ChartObjects.DrawText("text", GenerateText(), StaticPosition.TopRight, Colors.Aqua);
        }
        private string GenerateText()
        {

            var e1 = "";
            var e111 = "";
            var e2 = "";
            var e222 = "";
            var e3 = "";


            var earnText = "";




            e1 = "\n.Net.EUR/USD =   " + earn1;

            e111 = "\n-------------------------------";

            e2 = "\n.Net.GBP/USD =   " + earn2;

            e222 = "\n-------------------------------";

            e3 = "\n.Net.USD/JPY =   " + earn3;




            earnText = e1 + e111 + e2 + e222 + e3;
            return (earnText);
        }


        private void gains1()
        {


            earn1 = 0;

            foreach (var pos in Positions)
            {

                if (pos.Label.StartsWith(RobotId1))
                {
                    earn1 += pos.NetProfit + pos.Commissions;
                }

            }


            if (earn1 > totalgains1)
            {
                foreach (var pos in Positions)



                    if (pos.Label.StartsWith(RobotId1))
                    {
                        ClosePosition(pos);

                    }
            }
        }



        private void gains2()
        {


            earn2 = 0;

            foreach (var pos in Positions)
            {

                if (pos.Label.StartsWith(RobotId2))
                {
                    earn2 += pos.NetProfit + pos.Commissions;
                }

            }


            if (earn2 > totalgains2)
            {
                foreach (var pos in Positions)



                    if (pos.Label.StartsWith(RobotId2))
                    {
                        ClosePosition(pos);
                    }
            }
        }



        private void gains3()
        {


            earn3 = 0;

            foreach (var pos in Positions)
            {

                if (pos.Label.StartsWith(RobotId3))
                {
                    earn3 += pos.NetProfit + pos.Commissions;
                }

            }


            if (earn3 > totalgains3)
            {
                foreach (var pos in Positions)



                    if (pos.Label.StartsWith(RobotId3))
                    {
                        ClosePosition(pos);


                    }
            }
        }
    }
}


 


@tradermatrix
Replies

croucrou
30 May 2017, 21:06

I don't know if this helps, but after giving aan adequate label for each instance, you can though find position by label and symbol with:

var pos = Positions.Find("Label", Symbol); //Symbol optionally

check gains with:

pos.NetProfit

and close it:

ClosePosition(pos);

@croucrou

tradermatrix
31 May 2017, 12:03

RE:

Thank you croucrou
But my system works well.
I just need a code if it is possible to close all my positions (independently for each label) in a single mouse click on the screen.
But I do not know if that's possible ...


@tradermatrix

croucrou
02 Jun 2017, 01:42

To my knowledge, charts are not interactive on cTrader?

You can create a closing robot with a single string parameter, to input an id of an instance. By starting the robot, it closes the positions and stops.

If you wanted the robot not to stop, you can make another parameter and close positions by bool. If false, than close.


@croucrou

kricka
09 Jun 2017, 23:01

Close Positions in one click!

Try out Close Positions cBot. Free to download and use. One click and you are done!

Information and download link:  Close Positions cBot


@kricka