cBot Martingale with interval timer

Created at 01 Jun 2016, 19:27
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!
PT

PTrader

Joined 25.03.2016

cBot Martingale with interval timer
01 Jun 2016, 19:27


Hi all,

I would add an interval timer on cBot Martingale. For example: the bot go at 11 am to 18 pm and restart at 11 am the day later and stop at 18 pm etc.

It's possible? Can you help me?

Sorry for my bad bad bad... English!

Thank you!


@PTrader
Replies

... Deleted by UFO ...

PTrader
02 Jun 2016, 13:03

RE:

Thank you Lucian for your reply, but the cbot don't start.

lucian said:

Try something like this:

 



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 PTrader : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

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

        [Parameter("Start hour", DefaultValue = 11)]
        public int _start_hour { get; set; }
        [Parameter("End Hour", DefaultValue = 18)]
        public int _end_hour { get; set; }


        private Random random = new Random();

        protected override void OnStart()
        {
            if ((Server.Time.Hour > _start_hour) && (Server.Time.Hour < _end_hour))
            {
                Positions.Closed += OnPositionsClosed;

                ExecuteOrder(InitialVolume, GetRandomTradeType());
            }
        }

        private void ExecuteOrder(long volume, TradeType tradeType)
        {
            var result = ExecuteMarketOrder(tradeType, Symbol, volume, "Martingale", StopLoss, TakeProfit);

            if (result.Error == ErrorCode.NoMoney)
                Stop();
        }

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            Print("Closed");
            var position = args.Position;

            if (position.Label != "Martingale" || position.SymbolCode != Symbol.Code)
                return;

            if ((Server.Time.Hour > _start_hour) && (Server.Time.Hour < _end_hour))
            {
                if (position.GrossProfit > 0)
                {
                    ExecuteOrder(InitialVolume, GetRandomTradeType());
                }
                else
                {
                    ExecuteOrder((int)position.Volume * 2, position.TradeType);
                }
            }
            else
            {
                OnStart();
            }
        }

        private TradeType GetRandomTradeType()
        {
            return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
        }
    }
}

 

 


@PTrader

... Deleted by UFO ...

PTrader
02 Jun 2016, 14:14

RE:

Thank you. I tested cbot with changes, but don't start.

lucian said:

Sorry.

 

Change ( both OnStart and  OnPositionsClosed)

 if ((Server.Time.Hour > _start_hour) && (Server.Time.Hour < _end_hour))

 

with:

 

 if ((Server.Time.Hour >= _start_hour) && (Server.Time.Hour < _end_hour))

 


@PTrader

... Deleted by UFO ...

PTrader
02 Jun 2016, 20:12 ( Updated at: 21 Dec 2023, 09:20 )

RE:

Thank you very much, it's ok in real time, but not in backtesting. But it's fine!

Have a great week end, Lucian!

lucian said:

The _end_hour must be higher then _start_hour. 

and

   must be between _start_hour and _end_hour.

 


@PTrader

PTrader
03 Jun 2016, 12:11 ( Updated at: 21 Dec 2023, 09:20 )

RE:

Sorry, the bot end but not auto restart. I stop and restart manual. :-(

lucian said:

The _end_hour must be higher then _start_hour. 

and

   must be between _start_hour and _end_hour.

 


@PTrader

... Deleted by UFO ...

PTrader
06 Jun 2016, 12:09

RE:

Thank you Lucian, but not work.

When I click play the bot start, but at the stop loss or take profit the first position, it don't restart.

lucian said:

Try backtest with:

 



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 PTrader : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

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

        [Parameter("Start hour", DefaultValue = 11)]
        public int _start_hour { get; set; }
        [Parameter("End Hour", DefaultValue = 18)]
        public int _end_hour { get; set; }

        public bool trigger;
        private Random random = new Random();

        protected override void OnStart()
        {
            trigger = false;
            Positions.Closed += OnPositionsClosed;

        }

        protected override void OnTick()
        {


            if ((Server.Time.Hour == _start_hour) && (Positions.Count == 0) && (!trigger))
            {
                ExecuteOrder(InitialVolume, GetRandomTradeType());
                trigger = true;
            }
        }

        private void ExecuteOrder(long volume, TradeType tradeType)
        {

            var result = ExecuteMarketOrder(tradeType, Symbol, volume, "Martingale", StopLoss, TakeProfit);

            if (result.Error == ErrorCode.NoMoney)
                Stop();
        }

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            if (Server.Time.Hour > _start_hour)
            {
                trigger = false;
            }
            var position = args.Position;

            if (position.Label != "Martingale" || position.SymbolCode != Symbol.Code)
                return;


            if ((Server.Time.Hour >= _start_hour) && (Server.Time.Hour < _end_hour))
            {
                if (position.GrossProfit > 0)
                {
                    ExecuteOrder(InitialVolume, GetRandomTradeType());
                }
                else
                {
                    ExecuteOrder((int)position.Volume * 2, position.TradeType);
                }
            }

        }


        private TradeType GetRandomTradeType()
        {
            return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
        }
    }
}

 

 


@PTrader

amantalpur007@gmail.com
09 Jul 2017, 19:09

RE:

HI, i want sample martingale cbot to stop doubling my position after 5 consecutive loses. please tell me the coding to do so. i shall be really really grateful to you lucian.

lucian said:

Try backtest with:

 



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 PTrader : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

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

        [Parameter("Start hour", DefaultValue = 11)]
        public int _start_hour { get; set; }
        [Parameter("End Hour", DefaultValue = 18)]
        public int _end_hour { get; set; }

        public bool trigger;
        private Random random = new Random();

        protected override void OnStart()
        {
            trigger = false;
            Positions.Closed += OnPositionsClosed;

        }

        protected override void OnTick()
        {


            if ((Server.Time.Hour == _start_hour) && (Positions.Count == 0) && (!trigger))
            {
                ExecuteOrder(InitialVolume, GetRandomTradeType());
                trigger = true;
            }
        }

        private void ExecuteOrder(long volume, TradeType tradeType)
        {

            var result = ExecuteMarketOrder(tradeType, Symbol, volume, "Martingale", StopLoss, TakeProfit);

            if (result.Error == ErrorCode.NoMoney)
                Stop();
        }

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            if (Server.Time.Hour > _start_hour)
            {
                trigger = false;
            }
            var position = args.Position;

            if (position.Label != "Martingale" || position.SymbolCode != Symbol.Code)
                return;


            if ((Server.Time.Hour >= _start_hour) && (Server.Time.Hour < _end_hour))
            {
                if (position.GrossProfit > 0)
                {
                    ExecuteOrder(InitialVolume, GetRandomTradeType());
                }
                else
                {
                    ExecuteOrder((int)position.Volume * 2, position.TradeType);
                }
            }

        }


        private TradeType GetRandomTradeType()
        {
            return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
        }
    }
}

 

 


@amantalpur007@gmail.com