How to stop this bot after closing all prositions?

ahadfirdosi since: 01 Feb 2022;

  30 Aug 2022, 08:50
How to stop this bot after closing all prositions?

This bot works on Gold with very good results if you stop it on time. I made some modifications to it. So now, when equity reaches your specified profit or loss level then it closes all the positions. What I want is that when it closes all the positions it should also stop without opening any new positions. Can anyone please help?

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

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class NdnghiaMACD : Robot
    {
        private MacdCrossOver _MACD;

        [Parameter("Initial Volume Percent", DefaultValue = 0.01, MinValue = 0.01)]
        public double InitialVolumePercent { get; set; }

        [Parameter("Period", DefaultValue = 1100)]
        public int Period { get; set; }

        [Parameter("Long Cycle", DefaultValue = 3)]
        public int LongCycle { get; set; }

        [Parameter("Short Cycle", DefaultValue = 1)]
        public int ShortCycle { get; set; }

        [Parameter("Stop Loss", DefaultValue = 100)]
        public int StopLoss { get; set; }
        [Parameter("Take Profit", DefaultValue = 300)]
        public int TakeProfit { get; set; }
        
        [Parameter(DefaultValue = 0.0)]
        public double EquityTakeProfit { get; set; }

        [Parameter(DefaultValue = 0.0)]
        public double EquityStopLoss { get; set; }

        [Parameter("Signal-line crossover true:if Signal-line crossover false: Zero crossover", DefaultValue = true)]
        public bool IsSignalLineCrossover { get; set; }

        protected override void OnStart()
        {
            _MACD = Indicators.MacdCrossOver(LongCycle, ShortCycle, Period);
        }

        protected override void OnBar()
        {
            var volumne = Math.Floor(Account.Balance * 0 * InitialVolumePercent / 100) + 1;


            {


                if (_MACD.MACD.Last(2) < _MACD.Signal.Last(2) && _MACD.MACD.Last(1) > _MACD.Signal.Last(1))
                {
                    var position = Positions.Find("NdnghiaMACD");
                    if (position != null && position.TradeType == TradeType.Sell)
                    {

                    }
                    ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumne, "NdnghiaMACD", 0, 60);

                    // ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumne, "NdnghiaMACD", StopLoss, TakeProfit);
                }

                if (_MACD.MACD.Last(2) < _MACD.Signal.Last(2) && _MACD.MACD.Last(1) > _MACD.Signal.Last(1))
                {
                    var position = Positions.Find("NdnghiaMACD");
                    if (position != null && position.TradeType == TradeType.Buy)
                    {

                    }
                    ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volumne, "NdnghiaMACD", 0, 60);
                    // ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volumne, "NdnghiaMACD", StopLoss, TakeProfit);
                }


                {

                    {

                    }
                }
            }
        }
        
        protected override void OnTick()
        {
            //if account equity is more than equity take profit or less than equity stop loss, we close all positions
            if (Account.Equity > EquityTakeProfit || Account.Equity < EquityStopLoss)
                foreach (var position in Positions)
                    ClosePosition(position);
                    
        }
       
    }

}
PanagiotisCharalampous's avatar

PanagiotisCharalampous since: 13 Jan 2017;

  30 Aug 2022, 09:05

Hi there,

You can use the Stop() method.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

Head of Community Management at cTrader

ahadfirdosi since: 01 Feb 2022;

  30 Aug 2022, 17:55
RE:

PanagiotisCharalampous said:

Hi there,

You can use the Stop() method.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

 Hi, Thanks for your reply. It is not working. I am just a beginner in coding. Maybe I am doing something wrong. :(

protected override void OnTick()
        {
            //if account equity is more than equity take profit or less than equity stop loss, we close all positions
            if (Account.Equity > EquityTakeProfit || Account.Equity < EquityStopLoss)
                foreach (var position in Positions)
                    ClosePosition(position);
                    Stop();
        }

ahadfirdosi since: 01 Feb 2022;

  30 Aug 2022, 21:15
RE:

PanagiotisCharalampous said:

Hi there,

You can use the Stop() method.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

& also I don't know why it is working only for Gold and not on any other pair. Can you help me to enable other pairs as well?

PanagiotisCharalampous's avatar

PanagiotisCharalampous since: 13 Jan 2017;

  31 Aug 2022, 08:19

Hi there,

You need to provide more information regarding what is not working. But an obvious issue is that you do not use brackets for your if statement. So the Stop() method is always executed.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

Head of Community Management at cTrader

ahadfirdosi since: 01 Feb 2022;

  31 Aug 2022, 11:26
RE:

PanagiotisCharalampous said:

Hi there,

You need to provide more information regarding what is not working. But an obvious issue is that you do not use brackets for your if statement. So the Stop() method is always executed.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

I appreciate your help. It's working just fine now. I have just put brackets:

protected override void OnTick()
       {
            //if account equity is more than equity take profit or less than equity stop loss, we close all positions
            if (Account.Equity > Account.Balance + Positions.Count || Account.Equity < EquityStopLoss || Account.Equity > EquityTakeProfit)
              {
              foreach (var position in Positions)
                    ClosePosition(position);
                    Stop();
                    }
                
        }

ahadfirdosi since: 01 Feb 2022;

  31 Aug 2022, 11:47
Only Work With Gold

This bot is working just fine now on Gold as I want. But I am still not sure why it is working only with Gold and not other pairs. Can you please let me know why and how to make it work with other pairs as well?

PanagiotisCharalampous's avatar

PanagiotisCharalampous since: 13 Jan 2017;

  31 Aug 2022, 11:49

Hi there,

You need to explain to us in detail what is not working with examples. Please provide examples of what is happening and what did you expect to happen instead.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

Head of Community Management at cTrader