Stop cBot after trade executed

Created at 31 Aug 2018, 18:09
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!
CO

collinganesh

Joined 14.10.2016

Stop cBot after trade executed
31 Aug 2018, 18:09


Hi,

How do I stop or switch off a cBot immediately after a trade is executed? The cBot attached stops the bot only after Take Profit or Stop Loss is triggered. I want the bot to stop or switch off immediately after a trade ib executed.

King regards 



using System;
using System.Threading;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.API.Requests;
using cAlgo.Indicators;


namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class MAcBotStop : Robot
    {

        [Parameter("Stop Loss", DefaultValue = 20)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", DefaultValue = 40)]
        public int TakeProfit { get; set; }

        [Parameter("MA_Fast_Period", DefaultValue = 15)]
        public int _MA_Fast_Period { get; set; }
        [Parameter("MA_Slow_Period", DefaultValue = 100)]
        public int _MA_Slow_Period { get; set; }





        [Parameter(DefaultValue = 1000, MinValue = 0)]
        public int Vol_1 { get; set; }
        [Parameter(DefaultValue = 2000, MinValue = 0)]
        public int Vol_2 { get; set; }



        //Global declaration

        private ExponentialMovingAverage i_Moving_Average;
        private ExponentialMovingAverage i_Moving_Average_1;
        private ExponentialMovingAverage i_Moving_Average_2;
        private ExponentialMovingAverage i_Moving_Average_3;
        bool _Compare;
        bool _Compare_1;
        bool _MA_cross_down;
        bool _MA_cross_up;

        DateTime LastTradeExecution = new DateTime(0);


        protected override void OnStart()
        {
            Positions.Closed += PositionClosed;

            i_Moving_Average = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Fast_Period);
            i_Moving_Average_1 = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Slow_Period);
            i_Moving_Average_2 = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Slow_Period);
            i_Moving_Average_3 = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Fast_Period);

        }

        protected override void OnTick()
        {
            if (Trade.IsExecuting)
                return;

            var longPosition = Positions.Find("Order1", Symbol, TradeType.Buy);
            var shortPosition = Positions.Find("Order#1", Symbol, TradeType.Sell);

            //Step 1

            //Step 2
            _Compare = (i_Moving_Average_3.Result.Last(1) >= i_Moving_Average_1.Result.Last(1));
            _Compare_1 = (i_Moving_Average.Result.Last(0) >= i_Moving_Average_2.Result.Last(0));

            //Step 3
            _MA_cross_up = (!_Compare && _Compare_1);
            _MA_cross_down = (_Compare && !_Compare_1);


            //Step 4

            if (_MA_cross_down && shortPosition == null && Positions.Count == 0)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, Vol_1, "Order#1", StopLoss, TakeProfit);
            }



            if (_MA_cross_up && longPosition == null && Positions.Count == 0)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Vol_1, "Order1", StopLoss, TakeProfit);
            }


        }




        private void PositionClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;


            if (position.SymbolCode == Symbol.Code && position.GrossProfit > 0)
            {
                foreach (var order in PendingOrders)
                {
                    if (order.SymbolCode == Symbol.Code)
                    {
                        CancelPendingOrder(order);
                    }
                }
            }
            Stop();
        }
    }
}



















 


@collinganesh
Replies

sifneos4fx
01 Sep 2018, 11:15

RE:

            //Step 4
 
            if (_MA_cross_down && shortPosition == null && Positions.Count == 0)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, Vol_1, "Order#1", StopLoss, TakeProfit);
                stop();
            }
 
 
 
            if (_MA_cross_up && longPosition == null && Positions.Count == 0)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Vol_1, "Order1", StopLoss, TakeProfit);
                stop();
            }

 

collinganesh said:

Hi,

How do I stop or switch off a cBot immediately after a trade is executed? The cBot attached stops the bot only after Take Profit or Stop Loss is triggered. I want the bot to stop or switch off immediately after a trade ib executed.

King regards 



using System;
using System.Threading;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.API.Requests;
using cAlgo.Indicators;


namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class MAcBotStop : Robot
    {

        [Parameter("Stop Loss", DefaultValue = 20)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", DefaultValue = 40)]
        public int TakeProfit { get; set; }

        [Parameter("MA_Fast_Period", DefaultValue = 15)]
        public int _MA_Fast_Period { get; set; }
        [Parameter("MA_Slow_Period", DefaultValue = 100)]
        public int _MA_Slow_Period { get; set; }





        [Parameter(DefaultValue = 1000, MinValue = 0)]
        public int Vol_1 { get; set; }
        [Parameter(DefaultValue = 2000, MinValue = 0)]
        public int Vol_2 { get; set; }



        //Global declaration

        private ExponentialMovingAverage i_Moving_Average;
        private ExponentialMovingAverage i_Moving_Average_1;
        private ExponentialMovingAverage i_Moving_Average_2;
        private ExponentialMovingAverage i_Moving_Average_3;
        bool _Compare;
        bool _Compare_1;
        bool _MA_cross_down;
        bool _MA_cross_up;

        DateTime LastTradeExecution = new DateTime(0);


        protected override void OnStart()
        {
            Positions.Closed += PositionClosed;

            i_Moving_Average = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Fast_Period);
            i_Moving_Average_1 = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Slow_Period);
            i_Moving_Average_2 = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Slow_Period);
            i_Moving_Average_3 = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Fast_Period);

        }

        protected override void OnTick()
        {
            if (Trade.IsExecuting)
                return;

            var longPosition = Positions.Find("Order1", Symbol, TradeType.Buy);
            var shortPosition = Positions.Find("Order#1", Symbol, TradeType.Sell);

            //Step 1

            //Step 2
            _Compare = (i_Moving_Average_3.Result.Last(1) >= i_Moving_Average_1.Result.Last(1));
            _Compare_1 = (i_Moving_Average.Result.Last(0) >= i_Moving_Average_2.Result.Last(0));

            //Step 3
            _MA_cross_up = (!_Compare && _Compare_1);
            _MA_cross_down = (_Compare && !_Compare_1);


            //Step 4

            if (_MA_cross_down && shortPosition == null && Positions.Count == 0)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, Vol_1, "Order#1", StopLoss, TakeProfit);
            }



            if (_MA_cross_up && longPosition == null && Positions.Count == 0)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Vol_1, "Order1", StopLoss, TakeProfit);
            }


        }




        private void PositionClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;


            if (position.SymbolCode == Symbol.Code && position.GrossProfit > 0)
            {
                foreach (var order in PendingOrders)
                {
                    if (order.SymbolCode == Symbol.Code)
                    {
                        CancelPendingOrder(order);
                    }
                }
            }
            Stop();
        }
    }
}



















 

 


@sifneos4fx

collinganesh
02 Sep 2018, 20:31

RE: RE:

patrick.sifneos@gmail.com said:


            //Step 4
 
            if (_MA_cross_down && shortPosition == null && Positions.Count == 0)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, Vol_1, "Order#1", StopLoss, TakeProfit);
                stop();
            }
 
 
 
            if (_MA_cross_up && longPosition == null && Positions.Count == 0)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Vol_1, "Order1", StopLoss, TakeProfit);
                stop();
            }

 

collinganesh said: Thank you for your help. Wil try it.

Hi,

How do I stop or switch off a cBot immediately after a trade is executed? The cBot attached stops the bot only after Take Profit or Stop Loss is triggered. I want the bot to stop or switch off immediately after a trade ib executed.

King regards 



using System;
using System.Threading;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.API.Requests;
using cAlgo.Indicators;


namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class MAcBotStop : Robot
    {

        [Parameter("Stop Loss", DefaultValue = 20)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", DefaultValue = 40)]
        public int TakeProfit { get; set; }

        [Parameter("MA_Fast_Period", DefaultValue = 15)]
        public int _MA_Fast_Period { get; set; }
        [Parameter("MA_Slow_Period", DefaultValue = 100)]
        public int _MA_Slow_Period { get; set; }





        [Parameter(DefaultValue = 1000, MinValue = 0)]
        public int Vol_1 { get; set; }
        [Parameter(DefaultValue = 2000, MinValue = 0)]
        public int Vol_2 { get; set; }



        //Global declaration

        private ExponentialMovingAverage i_Moving_Average;
        private ExponentialMovingAverage i_Moving_Average_1;
        private ExponentialMovingAverage i_Moving_Average_2;
        private ExponentialMovingAverage i_Moving_Average_3;
        bool _Compare;
        bool _Compare_1;
        bool _MA_cross_down;
        bool _MA_cross_up;

        DateTime LastTradeExecution = new DateTime(0);


        protected override void OnStart()
        {
            Positions.Closed += PositionClosed;

            i_Moving_Average = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Fast_Period);
            i_Moving_Average_1 = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Slow_Period);
            i_Moving_Average_2 = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Slow_Period);
            i_Moving_Average_3 = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Fast_Period);

        }

        protected override void OnTick()
        {
            if (Trade.IsExecuting)
                return;

            var longPosition = Positions.Find("Order1", Symbol, TradeType.Buy);
            var shortPosition = Positions.Find("Order#1", Symbol, TradeType.Sell);

            //Step 1

            //Step 2
            _Compare = (i_Moving_Average_3.Result.Last(1) >= i_Moving_Average_1.Result.Last(1));
            _Compare_1 = (i_Moving_Average.Result.Last(0) >= i_Moving_Average_2.Result.Last(0));

            //Step 3
            _MA_cross_up = (!_Compare && _Compare_1);
            _MA_cross_down = (_Compare && !_Compare_1);


            //Step 4

            if (_MA_cross_down && shortPosition == null && Positions.Count == 0)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, Vol_1, "Order#1", StopLoss, TakeProfit);
            }



            if (_MA_cross_up && longPosition == null && Positions.Count == 0)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Vol_1, "Order1", StopLoss, TakeProfit);
            }


        }




        private void PositionClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;


            if (position.SymbolCode == Symbol.Code && position.GrossProfit > 0)
            {
                foreach (var order in PendingOrders)
                {
                    if (order.SymbolCode == Symbol.Code)
                    {
                        CancelPendingOrder(order);
                    }
                }
            }
            Stop();
        }
    }
}



















 

 

 


@collinganesh