Stop Cbot at Target Equity

Created at 27 Jan 2022, 20:00
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!
UM

umadrat2012

Joined 18.09.2021

Stop Cbot at Target Equity
27 Jan 2022, 20:00


Hello , can anybody give me a piece of code I can put into my cbot to stop it completely when a certain equity target is reached, let's say when equity has increased by $100. Thank you


@umadrat2012
Replies

amusleh
28 Jan 2022, 08:31

Hi,

Here is an example:

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class DrawIcon : Robot
    {
        private double _initialEquity;

        [Parameter("Equity Increase Amount", DefaultValue = 100)]
        public double EquityIncreaseAmount { get; set; }

        protected override void OnStart()
        {
            _initialEquity = Account.Equity;
        }

        protected override void OnTick()
        {
            if (Account.Equity - _initialEquity >= EquityIncreaseAmount)
            {
                Stop();
            }
        }
    }
}

It works properly only if your cBot trades the current chart symbol, if it trades multiple symbols then you have to use all symbols Tick events and check equity every time a symbol tick is received.


@amusleh

umadrat2012
28 Jan 2022, 11:05

RE: thank you, how would the code look like if my cbot trades multiple currencies at the same time?

amusleh said:

Hi,

Here is an example:

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class DrawIcon : Robot
    {
        private double _initialEquity;

        [Parameter("Equity Increase Amount", DefaultValue = 100)]
        public double EquityIncreaseAmount { get; set; }

        protected override void OnStart()
        {
            _initialEquity = Account.Equity;
        }

        protected override void OnTick()
        {
            if (Account.Equity - _initialEquity >= EquityIncreaseAmount)
            {
                Stop();
            }
        }
    }
}

It works properly only if your cBot trades the current chart symbol, if it trades multiple symbols then you have to use all symbols Tick events and check equity every time a symbol tick is received.

 


@umadrat2012

amusleh
28 Jan 2022, 12:02

Hi,

Here is a multi symbol example:

using System;
using System.Linq;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PartialCloseSample : Robot
    {
        private double _initialEquity;

        [Parameter("Equity Increase Amount", DefaultValue = 100)]
        public double EquityIncreaseAmount { get; set; }

        protected override void OnStart()
        {
            _initialEquity = Account.Equity;

            foreach (var group in Positions.GroupBy(position => position.SymbolName))
            {
                var positionSymbol = Symbols.GetSymbol(group.Key);

                positionSymbol.Tick -= PositionSymbol_Tick;
            }

            Positions.Opened += Positions_Opened;
            Positions.Closed += Positions_Closed;
        }

        private void Positions_Closed(PositionClosedEventArgs obj)
        {
            // If there are other positions from same symbol then don't remove the symbol Tick event handler
            if (Positions.Any(position => position.SymbolName.Equals(obj.Position.SymbolName, StringComparison.Ordinal)))
            {
                return;
            }

            // If there is no other position from the closed position symbol then remove the Tick event handler
            var positionSymbol = Symbols.GetSymbol(obj.Position.SymbolName);

            positionSymbol.Tick -= PositionSymbol_Tick;
        }

        private void Positions_Opened(PositionOpenedEventArgs obj)
        {
            // If there are other positions from same symbol then don't add the symbol Tick event handler
            // Because we already have one
            if (Positions.Count(position => position.SymbolName.Equals(obj.Position.SymbolName, StringComparison.Ordinal)) > 1)
            {
                return;
            }

            // Add position symbol tick event handler
            var positionSymbol = Symbols.GetSymbol(obj.Position.SymbolName);

            positionSymbol.Tick += PositionSymbol_Tick;
        }

        private void PositionSymbol_Tick(SymbolTickEventArgs obj)
        {
            if (Account.Equity - _initialEquity >= EquityIncreaseAmount)
            {
                Stop();
            }
        }
    }
}

@amusleh

iandelmar305
26 Jan 2023, 22:42

Hello, could you add a "close all cbots" and all open orders? that would be nice" thanks

amusleh said:

Hi,

Here is an example:

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class DrawIcon : Robot
    {
        private double _initialEquity;

        [Parameter("Equity Increase Amount", DefaultValue = 100)]
        public double EquityIncreaseAmount { get; set; }

        protected override void OnStart()
        {
            _initialEquity = Account.Equity;
        }

        protected override void OnTick()
        {
            if (Account.Equity - _initialEquity >= EquityIncreaseAmount)
            {
                Stop();
            }
        }
    }
}

It works properly only if your cBot trades the current chart symbol, if it trades multiple symbols then you have to use all symbols Tick events and check equity every time a symbol tick is received.

 


@iandelmar305