Replies

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

RE: RE:

HI, i use Sample MArtingale Cbot. in that cbot. i want this cbot to stop doubling my postion after 5 consecutive loses. please tell me the code to do so. i saw your comment and in that comment you've added MAs. i just want to add a setting so that cbot stops doubling after 5 consecutive loses. my email is amantalpur007@gmail.com. i've inserted code of that cbot. please write the whole coding including my need. i shall be really grateful to you.

// -------------------------------------------------------------------------------------------------
//
//    This code is a cAlgo API sample.
//
//    This cBot is intended to be used as a sample and does not guarantee any particular outcome or
//    profit of any kind. Use it at your own risk
//
//    The "Sample Martingale cBot" creates a random Sell or Buy order. If the Stop loss is hit, a new 
//    order of the same type (Buy / Sell) is created with double the Initial Volume amount. The cBot will 
//    continue to double the volume amount for  all orders created until one of them hits the take Profit. 
//    After a Take Profit is hit, a new random Buy or Sell order is created with the Initial Volume amount.
//
// -------------------------------------------------------------------------------------------------

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 SampleMartingalecBot : Robot
    {
        [Parameter("Initial Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double InitialQuantity { get; set; }

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

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

        private Random random = new Random();

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

            ExecuteOrder(InitialQuantity, GetRandomTradeType());
        }

        private void ExecuteOrder(double quantity, TradeType tradeType)
        {
            var volumeInUnits = Symbol.QuantityToVolume(quantity);
            var result = ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "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 (position.GrossProfit > 0)
            {
                ExecuteOrder(InitialQuantity, GetRandomTradeType());
            }
            else
            {
                ExecuteOrder(position.Quantity * 2, position.TradeType);
            }
        }

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

 

tradermatrix said:

nirin said:

Hi,

Please help,

With the code below, how can i insert my own signals from indicators without using a random Sell or Buy order,

and stop to double the volume amount after 3 consecutive loss trades (every position from my signals).

 

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 SampleMartingalecBot : Robot
    {
        [Parameter("Initial Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double InitialQuantity { get; set; }

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

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

        private Random random = new Random();

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

            ExecuteOrder(InitialQuantity, GetRandomTradeType());
        }

        private void ExecuteOrder(double quantity, TradeType tradeType)
        {
            var volumeInUnits = Symbol.QuantityToVolume(quantity);
            var result = ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "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 (position.GrossProfit > 0)
            {
                ExecuteOrder(InitialQuantity, GetRandomTradeType());
            }
            else
            {
                ExecuteOrder(position.Quantity * 2, position.TradeType);
            }
        }

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

 

Hello
according to this model,
you can add a martingale your robots.
"Max Run" allows you to adjust the number of rounds you want and control your losses.
example
if volume 10000
and loss maxi 40000
set "max run" 41000 .... so stop loss has touched 40000 ....
 martingale stop... complete series ....
back to other trades
there is no doubt of other solutions,
I am using this method.
Best regards.

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 TrenDMartingale : Robot
    {


        [Parameter(DefaultValue = " TrenD ")]
        public string cBotLabel { get; set; }

        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

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

        [Parameter("Slow Periods", DefaultValue = 10)]
        public int SlowPeriods { get; set; }

        [Parameter("Fast Periods", DefaultValue = 5)]
        public int FastPeriods { get; set; }

        [Parameter(DefaultValue = 3)]
        public int MaxPositions { get; set; }

        [Parameter(DefaultValue = 5000)]
        public int Volume { get; set; }

        [Parameter("Stop Loss 1    (pips)", DefaultValue = 120)]
        public int sl1 { get; set; }

        [Parameter("Take Profit 1  (pips)", DefaultValue = 30)]
        public int tp1 { get; set; }

        ////////////////////////////////martingale///////////////////////////////////////

        [Parameter("Start martingale ", DefaultValue = true)]
        public bool Startmartingale { get; set; }

        [Parameter("Stop Loss martingale ", DefaultValue = 120)]
        public double sl2 { get; set; }

        [Parameter("Take Profit martingale ", DefaultValue = 120)]
        public double tp2 { get; set; }

        [Parameter("Multiplier Ordre ", DefaultValue = 2.1)]
        public double Multiplier { get; set; }

        [Parameter(" Max Run ", DefaultValue = 41000)]
        public int MaxRun { get; set; }

        private MovingAverage slowMa;
        private MovingAverage fastMa;


        private const string label2 = " TrenD.X ";



        protected override void OnStart()
        {
            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
            Positions.Closed += OnPositionsClosed;
        }

        protected override void OnBar()
        {

            var cBotPositions = Positions.FindAll(cBotLabel);

            if (cBotPositions.Length > MaxPositions)
                return;



            var currentSlowMa = slowMa.Result.Last(0);
            var currentFastMa = fastMa.Result.Last(0);
            var previousSlowMa = slowMa.Result.Last(1);
            var previousFastMa = fastMa.Result.Last(1);


            if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa)

                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, cBotLabel, sl1, tp1);


            else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa)


                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, cBotLabel, sl1, tp1);



        }


        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            if (Startmartingale == true)
            {

                Print("Closed");
                var position = args.Position;
                if (position.Label != cBotLabel || position.SymbolCode != Symbol.Code)
                    if (position.Label != label2 || position.SymbolCode != Symbol.Code)
                        return;
                if (position.GrossProfit < 0)
                {

                    {
                        ExecuteOrder(getNewVolume((int)position.Volume), position.TradeType);
                    }
                }
            }
        }
        private void ExecuteOrder(long volume, TradeType tradeType)
        {
            var result = ExecuteMarketOrder(tradeType, Symbol, volume, label2, sl2, tp2);
            if (result.Error == ErrorCode.NoMoney)
                Stop();
        }

        private int getNewVolume(int posVol)
        {
            var newVol = posVol * 2;
            newVol = Math.Min(newVol, MaxRun);
            if (newVol == MaxRun)
                newVol = Volume;
            Print("newVol = {0}", newVol);
            return newVol;
        }
    }
}

 

 


@amantalpur007@gmail.com

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