How do i close all positions super fast from cbot

Created at 18 Jun 2022, 04:12
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!
MongolTrader's avatar

MongolTrader

Joined 12.02.2015

How do i close all positions super fast from cbot
18 Jun 2022, 04:12


When i use foreach loop close all positions  ontick its so slow close all positions. Is there any other way close fast method act like click "close all positions" button.


@MongolTrader
Replies

firemyst
18 Jun 2022, 15:39

If you close each position synchronously, it waits until one position is closed before it closes the next... so on and so forth.

So try closing all positions asynchronously. This way, you send all the close commands, and it's just up to the server to process them as fast as it can.

Example:

foreach (Position p in Positions)
                            {
                                ClosePositionAsync(p, (TradeResult r) =>
                                {
                                    if (r.IsSuccessful)
                                    {
                                        //... do what you have to
                                    }
                                    else if (!r.IsSuccessful)
                                    {
					//... do what you have to
                                    }
                                });
                            }

 


@firemyst

MongolTrader
22 Jun 2022, 12:21

RE:

firemyst said:

If you close each position synchronously, it waits until one position is closed before it closes the next... so on and so forth.

So try closing all positions asynchronously. This way, you send all the close commands, and it's just up to the server to process them as fast as it can.

Example:

foreach (Position p in Positions)
                            {
                                ClosePositionAsync(p, (TradeResult r) =>
                                {
                                    if (r.IsSuccessful)
                                    {
                                        //... do what you have to
                                    }
                                    else if (!r.IsSuccessful)
                                    {
					//... do what you have to
                                    }
                                });
                            }

 

Its almost same as regular please any other way to close much fast all position by one command like perform as close all button from windows


@MongolTrader

m4trader4
22 Jun 2022, 12:48

RE: RE:

 Dear Mongol Trader,

The code will create a button and OnClick will close all the positions. If you are looking something to close from command line check this thread

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class Button : Robot
    {
       cAlgo.API.Button BCLP;

        void buttonCreate(string BName, string BText, string X, String Y, string clr, cAlgo.API.Button TButton)
        {


            TButton.Height = 15;
            TButton.Width = 24;
            TButton.FontSize = 9;
            TButton.HorizontalContentAlignment = 0;
            TButton.Padding = "0 0 0 0";
            //CornerRadius = 2,
            TButton.Text = BText;
            TButton.Margin = X + " " + Y;
            TButton.BackgroundColor = Color.FromHex(clr);
            TButton.HorizontalAlignment = cAlgo.API.HorizontalAlignment.Left;
            TButton.VerticalAlignment = VerticalAlignment.Bottom;
            //Chart.AddControl(TButton);

        }

        void CloseOrder()
        {

            BeginInvokeOnMainThread(() =>
            {
               
                    var positionsCBS = Positions.ToArray();
                    
                    foreach (var psnCBS in positionsCBS)
                    {
                       ClosePositionAsync(psnCBS);         
                    }

            });
        }


        protected override void OnStart()
        {

            BCLP = new cAlgo.API.Button();
            buttonCreate("CLP", "CLP", "1", "85", "#009345", BCLP);
            Chart.AddControl(BCLP);
            BCLP.Click += args => CloseOrder();
         
        }

        protected override void OnTick()
        {
            // Handle price updates here
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

 


@m4trader4

MongolTrader
22 Jun 2022, 13:02

RE: RE: RE:

m4trader4 said:

 Dear Mongol Trader,

The code will create a button and OnClick will close all the positions. If you are looking something to close from command line check this thread

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class Button : Robot
    {
       cAlgo.API.Button BCLP;

        void buttonCreate(string BName, string BText, string X, String Y, string clr, cAlgo.API.Button TButton)
        {


            TButton.Height = 15;
            TButton.Width = 24;
            TButton.FontSize = 9;
            TButton.HorizontalContentAlignment = 0;
            TButton.Padding = "0 0 0 0";
            //CornerRadius = 2,
            TButton.Text = BText;
            TButton.Margin = X + " " + Y;
            TButton.BackgroundColor = Color.FromHex(clr);
            TButton.HorizontalAlignment = cAlgo.API.HorizontalAlignment.Left;
            TButton.VerticalAlignment = VerticalAlignment.Bottom;
            //Chart.AddControl(TButton);

        }

        void CloseOrder()
        {

            BeginInvokeOnMainThread(() =>
            {
               
                    var positionsCBS = Positions.ToArray();
                    
                    foreach (var psnCBS in positionsCBS)
                    {
                       ClosePositionAsync(psnCBS);         
                    }

            });
        }


        protected override void OnStart()
        {

            BCLP = new cAlgo.API.Button();
            buttonCreate("CLP", "CLP", "1", "85", "#009345", BCLP);
            Chart.AddControl(BCLP);
            BCLP.Click += args => CloseOrder();
         
        }

        protected override void OnTick()
        {
            // Handle price updates here
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

 

Thank you very much


@MongolTrader