Martingale not reseting after profit

Created at 04 Jul 2024, 23: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!
ES

esarku

Joined 12.04.2015

Martingale not reseting after profit
04 Jul 2024, 23:48


Can anyone please check if my martingale setting is wrong. I tried to create a cbot with martingale feature. I expect the martingale to reset when trade close in profit but it still opens new with a higher volume instead of the initial volume.  code is as below. Thank you

 

using System;
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 CandlePatternRobot : Robot
    {
        [Parameter("Initial Volume (units)", DefaultValue = 10000)]
        public double InitialVolumeInUnits { get; set; }

        [Parameter("Martingale Factor", DefaultValue = 2)]
        public double MartingaleFactor { get; set; }

        private Position buyPosition;
        private Position sellPosition;
        private double currentVolume;

        protected override void OnStart()
        {
            Positions.Opened += OnPositionOpened;
            Positions.Closed += OnPositionClosed;
            currentVolume = InitialVolumeInUnits;
        }

        protected override void OnBar()
        {
            // Get the close and open prices of the previous candle
            double previousClose = Bars.ClosePrices.Last(1);
            double previousOpen = Bars.OpenPrices.Last(1);
            
            double volume = currentVolume;

            // Check if previous close is greater than previous open
            if (previousClose  > previousOpen)
            {
                // Close sell position if there is a sell position
                if (sellPosition != null)
                {
                    ClosePosition(sellPosition);
                }
                // Place a buy trade if there's no buy position already
                if (buyPosition == null)
                {
                    TradeResult buyResult = ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, "Buy Order", null, null, "yes");
                    if (!buyResult.IsSuccessful)
                        currentVolume *= MartingaleFactor; // Increase volume in case of loss
                }
            }
            // Check if previous close is less than previous open
            else if (previousClose < previousOpen)
            {
                // Close buy position if there is a buy position
                if (buyPosition != null)
                {
                    ClosePosition(buyPosition);
                }
                // Place a sell trade if there's no sell position already
                if (sellPosition == null)
                {
                    TradeResult sellResult = ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, "Sell Order", null, null, "yes");
                    if (!sellResult.IsSuccessful)
                        currentVolume *= MartingaleFactor; // Increase volume in case of loss
                }
            }
            else
            {
                // No action needed if previous close is equal to previous open
            }
        }

        private void OnPositionOpened(PositionOpenedEventArgs args)
        {
            if (args.Position.TradeType == TradeType.Buy)
                buyPosition = args.Position;
            else if (args.Position.TradeType == TradeType.Sell)
                sellPosition = args.Position;
        }

        private void OnPositionClosed(PositionClosedEventArgs args)
        {
            if (args.Position.NetProfit >= 0)
            {
                // Reset volume if position closed in profit
                currentVolume = InitialVolumeInUnits;
            }
            else
            {
                // Increase volume using Martingale factor
                currentVolume *= MartingaleFactor;
            }

            if (args.Position == buyPosition)
                buyPosition = null;
            else if (args.Position == sellPosition)
                sellPosition = null;
        }
    }
}


@esarku
Replies

PanagiotisCharalampous
05 Jul 2024, 05:15

Hi there,

You are increasing the position size when a trade is successful and when the position closes. In position closing event, you should only reset the volume size and not increase it.

Best regards,

Panagiotis


@PanagiotisCharalampous

esarku
05 Jul 2024, 19:49 ( Updated at: 06 Jul 2024, 05:42 )

RE: Martingale not reseting after profit

PanagiotisCharalampous said: 

Hi there,

You are increasing the position size when a trade is successful and when the position closes. In position closing event, you should only reset the volume size and not increase it.

Best regards,

Panagiotis

Hello PanagiotisCharalampous,

 

thank you for the reply but when I reset the volume size, the martingale no longer works. can you please test code and see.

Thanks


@esarku

PanagiotisCharalampous
06 Jul 2024, 05:46

Martingale not reseting after profit

esarku said: 

PanagiotisCharalampous said: 

Hi there,

You are increasing the position size when a trade is successful and when the position closes. In position closing event, you should only reset the volume size and not increase it.

Best regards,

Panagiotis

Hello PanagiotisCharalampous,

 

thank you for the reply but when I reset the volume size, the martingale no longer works. can you please test code and see.

Thanks

Can you share the fixed code?


@PanagiotisCharalampous

esarku
06 Jul 2024, 12:42

RE: not reseting after profit

PanagiotisCharalampous said: 

esarku said: 

PanagiotisCharalampous said: 

Hi there,

You are increasing the position size when a trade is successful and when the position closes. In position closing event, you should only reset the volume size and not increase it.

Best regards,

Panagiotis

Hello PanagiotisCharalampous,

 

thank you for the reply but when I reset the volume size, the martingale no longer works. can you please test code and see.

Thanks

Can you share the fixed code?

 

 

using System;
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 CandlePatternRobot : Robot
    {
        [Parameter("Initial Volume (units)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double InitialVolumeInUnits { get; set; }

        [Parameter("Martingale Factor", DefaultValue = 2)]
        public double MartingaleFactor { get; set; }

        private Position buyPosition;
        private Position sellPosition;
        private double currentVolume;

        protected override void OnStart()
        {
            Positions.Opened += OnPositionOpened;
            Positions.Closed += OnPositionClosed;
            currentVolume = InitialVolumeInUnits;
        }

        protected override void OnBar()
        {
            // Get the close and open prices of the previous candle
            double previousClose = Bars.ClosePrices.Last(1);
            double previousOpen = Bars.OpenPrices.Last(1);
            
            double volume = currentVolume;

            // Check if previous close is greater than previous open
            if (previousClose > previousOpen)
            {
                // Close sell position if there is a sell position
                if (sellPosition != null)
                {
                    ClosePosition(sellPosition);
                }
                // Place a buy trade if there's no buy position already
                if (buyPosition == null)
                {
                    TradeResult buyResult = ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, "Buy Order", null, null, "yes");
                    if (!buyResult.IsSuccessful)
                    {
                        currentVolume *= MartingaleFactor; // Increase volume in case of loss
                        Print("Martingale applied: New Volume = " + currentVolume);
                    }
                }
            }
            // Check if previous close is less than previous open
            else if (previousClose < previousOpen)
            {
                // Close buy position if there is a buy position
                if (buyPosition != null)
                {
                    ClosePosition(buyPosition);
                }
                // Place a sell trade if there's no sell position already
                if (sellPosition == null)
                {
                    TradeResult sellResult = ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, "Sell Order", null, null, "yes");
                    if (!sellResult.IsSuccessful)
                    {
                        currentVolume *= MartingaleFactor; // Increase volume in case of loss
                        Print("Martingale applied: New Volume = " + currentVolume);
                    }
                }
            }
            else
            {
                // No action needed if previous close is equal to previous open
            }
        }

        private void OnPositionOpened(PositionOpenedEventArgs args)
        {
            if (args.Position.TradeType == TradeType.Buy)
                buyPosition = args.Position;
            else if (args.Position.TradeType == TradeType.Sell)
                sellPosition = args.Position;
        }

        private void OnPositionClosed(PositionClosedEventArgs args)
        {
            if (args.Position.TradeType == TradeType.Buy)
                buyPosition = null;
            else if (args.Position.TradeType == TradeType.Sell)
                sellPosition = null;

            // Reset the volume size
            currentVolume = InitialVolumeInUnits;
        }
    }
}

@esarku

PanagiotisCharalampous
07 Jul 2024, 06:07

Why did you remove the condition to reset the volume only when the position is in profit? Now you are resetting it every time

        private void OnPositionClosed(PositionClosedEventArgs args)
        {
            if (args.Position.TradeType == TradeType.Buy)
                buyPosition = null;
            else if (args.Position.TradeType == TradeType.Sell)
                sellPosition = null;

            // Reset the volume size
            currentVolume = InitialVolumeInUnits;
        }

Best regards,

Panagiotis


@PanagiotisCharalampous

esarku
08 Jul 2024, 21:13

RE: not reseting after profit

PanagiotisCharalampous said: 

Why did you remove the condition to reset the volume only when the position is in profit? Now you are resetting it every time

        private void OnPositionClosed(PositionClosedEventArgs args)        {            if (args.Position.TradeType == TradeType.Buy)                buyPosition = null;            else if (args.Position.TradeType == TradeType.Sell)                sellPosition = null;            // Reset the volume size            currentVolume = InitialVolumeInUnits;        }

Best regards,

Panagiotis

I have tried to modify it but getiing the same result. any chance you could please add the correct line as a guide. 

Thanks

if (position.GrossProfit >= 0)
            {
                currentVolume = InitialVolume;
                Print("Trade successful. Volume reset to initial: ", InitialVolume);
            }
            else
            {
                currentVolume = currentVolume * MartingaleFactor;
                Print("Trade unsuccessful. Volume increased to: ", currentVolume);
            }

            if (position == buyPosition)
            {
                buyPosition = null;
            }
            else if (position == sellPosition)
            {
                sellPosition = null;
            }

@esarku

PanagiotisCharalampous
09 Jul 2024, 06:01

RE: RE: not reseting after profit

esarku said: 

PanagiotisCharalampous said: 

Why did you remove the condition to reset the volume only when the position is in profit? Now you are resetting it every time

        private void OnPositionClosed(PositionClosedEventArgs args)        {            if (args.Position.TradeType == TradeType.Buy)                buyPosition = null;            else if (args.Position.TradeType == TradeType.Sell)                sellPosition = null;            // Reset the volume size            currentVolume = InitialVolumeInUnits;        }

Best regards,

Panagiotis

I have tried to modify it but getiing the same result. any chance you could please add the correct line as a guide. 

Thanks

if (position.GrossProfit >= 0)            {                currentVolume = InitialVolume;                Print("Trade successful. Volume reset to initial: ", InitialVolume);            }            else            {                currentVolume = currentVolume * MartingaleFactor;                Print("Trade unsuccessful. Volume increased to: ", currentVolume);            }            if (position == buyPosition)            {                buyPosition = null;            }            else if (position == sellPosition)            {                sellPosition = null;            }

Try removing this part only

            else
            {
                currentVolume = currentVolume * MartingaleFactor;
                Print("Trade unsuccessful. Volume increased to: ", currentVolume);
            }

@PanagiotisCharalampous