martingale cbot, stop doubling after 4 consecutive loses, please help me code this.

Created at 09 Jul 2017, 19:04
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!
AM

amantalpur007@gmail.com

Joined 06.06.2017

martingale cbot, stop doubling after 4 consecutive loses, please help me code this.
09 Jul 2017, 19:04


hi, i want a code that stops doubling the position after 4 loses in a row and start with initial amount which has been set. please help me by telling me the code for martingale cbot to stop doubling after 4 trades. please


@amantalpur007@gmail.com
Replies

tradermatrix
12 Jul 2017, 00:19

RE:

amantalpur007@gmail.com said:

hi, i want a code that stops doubling the position after 4 loses in a row and start with initial amount which has been set. please help me by telling me the code for martingale cbot to stop doubling after 4 trades. please

Hi

Calculate the max volume based on the number of losses you accept.
example:
  6 losses..vol 1000 ... Max volume ... return = 32000
If unfortunately you arrive at the end of the logic the vume returns to the initial volume ...
This is what I have most simple in my drawers ...
Good trades.



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

        [Parameter(" Max volume...return ", DefaultValue = 40000)]
        public int MaxRun { get; set; }

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

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

        private Random random = new Random();

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

            ExecuteOrder(InitialVolume, GetRandomTradeType());
        }

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

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

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

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

            if (position.GrossProfit > 0)
            {
                ExecuteOrder(InitialVolume, GetRandomTradeType());
            }
            else
            {
                ExecuteOrder(getNewVolume((int)position.Volume), position.TradeType);
            }
        }

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

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

        }
    }
}




 


@tradermatrix