Martingale function does not check the script

Created at 08 Dec 2022, 17:48
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!
AL

Alwin123

Joined 28.10.2022

Martingale function does not check the script
08 Dec 2022, 17:48


Is there anyone who can help improve the script this script checks when it starts the martingale doesn't check the strategy without checking if the strategy is true

 

 

I flipped the martingale to Paroli (reverse martingale)

 

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


        [Parameter(DefaultValue = " Normal ")]
        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)]
        public double Multiplier { get; set; }

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

        //[Parameter("Max Consecutive Winn (zero = infinite)", Group = "Deviation Paroli", DefaultValue = 6, MinValue = 0, Step = 1)]
        // public int DMMaxWinn { get; set; }


        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private Position position;



        private const string label2 = " Paroli ";



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

        }

        protected override void OnBar()
        {
            if (position != null)
                return;

            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, SymbolName, Volume, cBotLabel, sl1, tp1);


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


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

        }


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

                Print("Closed");
                var position = args.Position;
                if (position.Label != cBotLabel || position.SymbolName != SymbolName)
                    if (position.Label != label2 || position.SymbolName != SymbolName)
                        return;
                if (position.GrossProfit > 0)
                {

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

        private int getNewVolume(int posVol)
        {
            var newVol = posVol * Multiplier;
            newVol = Math.Min(newVol, MaxRun);
            if (newVol == MaxRun)
                newVol = Volume;
            Print("newVol = {0}", newVol);
            return (int)newVol;
        }
        /* 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;
         }*/
    }
}


@Alwin123