Double lotsize Cbot

Created at 11 Aug 2024, 02:42
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!
ForexViet's avatar

ForexViet

Joined 05.05.2023

Double lotsize Cbot
11 Aug 2024, 02:42


Please me with this code. I want to creat a cBot to double lotsize folow this request:

Opens a trade with a specified label. 

Sets target profit. 

Includes a Double lotsize yes/no option. 

Sets the pip value for Double lotsize. 

Sets a maximum lotsize. 

// Double lotsize:

if Double lotsize is set to Yes: 

           then (check the trade is in losing with label) and Double lotsize of the trade if the conditions are met (i.e., the trade is in loss >= the Double lotsize Pips, and the lotsize of the losing trade is less than (2 * Max Lotsize) , 

           After double lotsizeand, set the target profit again

//End

 Doing Double lotsize again until (the trade is in loss < the Double lotsize Pips) or (the lotsize of the losing trade > (2 * Max Lotsize)

I asked for help from GPT chat in programming and it helped me with this code, however my requirement for doubling the volume is not to close a losing position and open a position of 2 times the size. My requirement is to keep the losing position the same, double the volume as in the Double Modify function below

 

this is the code by chat GPT

using cAlgo.API;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class DoubleSizeBot : Robot
    {
        [Parameter("Label", DefaultValue = "DoubleSizeBot")]
        public string Label { get; set; }

        [Parameter("Volume (Lots)", DefaultValue = 1000)]
        public int Volume { get; set; }

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

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

        [Parameter("Double Lotsize", DefaultValue = false)]
        public bool DoubleLotsize { get; set; }

        [Parameter("Double Lotsize Pips", DefaultValue = 10)]
        public int DoubleLotsizePips { get; set; }

        [Parameter("Max Lot Size", DefaultValue = 2000)]
        public int MaxLotSize { get; set; }

        protected override void OnStart()
        {
            // Open the initial trade
            ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, Label, StopLoss, TakeProfit);
            ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, Label, StopLoss, TakeProfit);
        }

        protected override void OnTick()
        {
            // Find the open position with the specified label
            var position = Positions.Find(Label);

            if (position != null)
            {
                try
                {
                    // Check if the trade is in a loss
                    double currentLossPips = position.Pips;

                    // If Double Lotsize is enabled and the trade is in significant loss
                    if (DoubleLotsize && currentLossPips <= -DoubleLotsizePips && position.Volume < 2 * MaxLotSize)
                    {
                        // Calculate the new volume
                        double newVolume = position.Volume * 2;

                        // Ensure the new volume does not exceed the max lot size
                        if (newVolume > MaxLotSize)
                        {
                            newVolume = MaxLotSize;
                        }

                        // Calculate the additional volume needed
                        double additionalVolume = newVolume - position.Volume;

                        if (additionalVolume > 0)
                        {
                            // Close the old position
                            position.Close();

                            // Open a new position with the updated volume
                            ExecuteMarketOrder(position.TradeType, SymbolName, (int)newVolume, Label, StopLoss, TakeProfit);
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    // Log the exception message
                    Print("Error: ", ex.Message);
                }
            }
        }
    }
}

 


@ForexViet