Topics
25 Mar 2017, 13:16
 1720
 1
25 Dec 2016, 18:10
 2917
 2
20 Dec 2016, 12:57
 0
 2352
 2
17 Dec 2016, 22:03
 1861
 4
29 Oct 2016, 23:04
 1938
 1
Replies

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

collinganesh
05 Jan 2017, 08:51

Renko bars Pleaseeeee


@collinganesh

collinganesh
25 Dec 2016, 18:15

Hello,

 

Can anyone help me to change my cBot EMA indicator from the normal candle stick EMA Close to a EMA that is used on a Renko Chart which uses a EMA RenkoChart close value to plot the EMA line. Sorry for the duplicate post. 

 



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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class martingale : Robot
    {
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

        [Parameter("Source")]
        public DataSeries SourceSeries { get; set; }

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

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

        [Parameter("MA_Slow_Period", DefaultValue = 200)]
        public int _MA_Slow_Period { get; set; }

        [Parameter("MA_Fast_Period", DefaultValue = 50)]
        public int _MA_Fast_Period { get; set; }

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



        private ExponentialMovingAverage i_Moving_Average;
        private ExponentialMovingAverage i_Moving_Average_1;
        private ExponentialMovingAverage i_Moving_Average_2;
        private ExponentialMovingAverage i_Moving_Average_3;
        private const string label = "Martingale";
        bool _Compare;
        bool _Compare_1;
        bool _MA_cross_down;
        bool _MA_cross_up;


        DateTime LastTradeExecution = new DateTime(0);

        protected override void OnStart()
        {
            Positions.Closed += OnPositionsClosed;
            i_Moving_Average = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Slow_Period);
            i_Moving_Average_1 = Indicators.ExponentialMovingAverage(MarketSeries.Close, (int)_MA_Fast_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(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);

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


            _MA_cross_down = (!_Compare && _Compare_1);
            _MA_cross_up = (_Compare && !_Compare_1);




            if (_MA_cross_up && longPosition == null)
            {
                if (shortPosition != null)
                    ClosePosition(shortPosition);
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, TakeProfit);
            }
            else if (_MA_cross_down && shortPosition == null)
            {
                if (longPosition != null)
                    ClosePosition(longPosition);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label, StopLoss, TakeProfit);

            }
        }

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;
            if (position.GrossProfit < 0)
            {
                TradeType tt = TradeType.Sell;
                if (position.TradeType == TradeType.Sell)
                    tt = TradeType.Buy;
                ExecuteMarketOrder(tt, Symbol, Symbol.NormalizeVolume(position.Volume * 2), "Martingale", StopLoss, TakeProfit);
            }
        }
    }
}

 


@collinganesh

collinganesh
17 Dec 2016, 23:40

Thanks Lucian. The code works perfectly.

Merry Christmas to you.


@collinganesh

collinganesh
17 Dec 2016, 22:44

Thank you


@collinganesh

collinganesh
23 Nov 2016, 20:05

Will try. Thank you for your help.

Kind regards


@collinganesh

collinganesh
23 Nov 2016, 18:30

Thanks David. I will try the code.


@collinganesh

collinganesh
07 Nov 2016, 11:55

RE:

lucian said:

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 SlootWegte : Robot
    {
        [Parameter("Profit $", DefaultValue = 5.0)]
        public double _profit { get; set; }
        [Parameter("Loss $", DefaultValue = -3.0)]
        public double _loss { get; set; }
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int _volume { get; set; }
        private Random random = new Random();

        protected override void OnStart()
        {
            ExecuteOrder(_volume, GetRandomTradeType());
            Positions.Closed += Positions_Closed;
        }

        protected override void OnTick()
        {
            foreach (var trade in Positions)
            {

                if ((trade.Label.StartsWith("SlootWege")) && ((trade.GrossProfit >= _profit) || (trade.GrossProfit <= _loss)))
                {
                    ClosePosition(trade);
                }
            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
        private void Positions_Closed(PositionClosedEventArgs args)
        {
            if (args.Position.GrossProfit <= _loss)
            {
                if (args.Position.TradeType == TradeType.Buy)
                {
                    ExecuteOrder(_volume, TradeType.Sell);
                }
                else
                {
                    ExecuteOrder(_volume, TradeType.Buy);
                }
            }
            if (args.Position.GrossProfit >= _profit)
            {
                if (args.Position.TradeType == TradeType.Buy)
                {
                    ExecuteOrder(_volume, TradeType.Buy);
                }
                else
                {
                    ExecuteOrder(_volume, TradeType.Sell);
                }
            }
        }
        private void ExecuteOrder(long volume, TradeType tradeType)
        {
            var result = ExecuteMarketOrder(tradeType, Symbol, volume, "SlootWege", null, null);

            if (result.Error == ErrorCode.NoMoney)
                Stop();
        }
        private TradeType GetRandomTradeType()
        {
            return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
        }
    }
}

Hi  Lucian,

                Is it posible for you to code the bot to open buystop orders when buy order stop loss is triggered, instead of reversing the order and same

                for the opposite. 

 

              Thanks

 


@collinganesh