Topics
Replies

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

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

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