I need add funtion when order hit maxlotsize robot will return restart to find new order.

Created at 07 Nov 2023, 04:22
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!
MA

maxxx2532

Joined 11.10.2023

I need add funtion when order hit maxlotsize robot will return restart to find new order.
07 Nov 2023, 04:22


 using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class CombinedBot : Robot
    
    {
        private MacdHistogram _macd;
        private Position _position;
        private bool tradeOpened = false; // Flag to track if a trade has been opened

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

        [Parameter("Period", DefaultValue = 3)]
        public int Period { get; set; }

        [Parameter("Long Cycle", DefaultValue = 6)]
        public int LongCycle { get; set; }

        [Parameter("Short Cycle", DefaultValue = 12)]
        public int ShortCycle { get; set; }

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

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

        [Parameter("Max Lotsize", Group = "Protection", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double MaxLotsize { get; set; }

        [Parameter("Max Orders", DefaultValue = 5, MinValue = 1)]
        public int MaxOrders { get; set; }
        
    
        protected override void OnStart()
        {
            _macd = Indicators.MacdHistogram(LongCycle, ShortCycle, Period);
            Positions.Closed += OnPositionsClosed;
        }

        protected override void OnBar()
        {
            if (Trade.IsExecuting || tradeOpened || _position != null || Positions.Count >= MaxOrders)
                return;

            bool isLongPositionOpen = _position != null && _position.TradeType == TradeType.Buy;
            bool isShortPositionOpen = _position != null && _position.TradeType == TradeType.Sell;

            if (_macd.Histogram.LastValue > 0.0 && _macd.Signal.IsRising() && !isLongPositionOpen)
            {
                ClosePosition();
                Buy();
            }

            if (_macd.Histogram.LastValue < 0.0 && _macd.Signal.IsFalling() && !isShortPositionOpen)
            {
                ClosePosition();
                Sell();
            }
        }

        private void ClosePosition()
        {
            if (_position != null)
            {
                Trade.Close(_position);
                _position = null;
            }
        }

        private void Buy()
        {
            ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "combinedBot", TakeProfit, StopLoss);
            tradeOpened = true; // Set the flag to true when a trade is opened
        }

        private void Sell()
        {
            ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, "combinedBot", TakeProfit, StopLoss);
            tradeOpened = true; // Set the flag to true when a trade is opened
        }

        private void ExecuteOrder(double quantity, TradeType tradeType)
        {
            var volumeInUnits = Symbol.QuantityToVolumeInUnits(quantity);
            var result = ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "combinedBot", StopLoss, TakeProfit);

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

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

    if (position.Label != "combinedBot" || position.SymbolName != SymbolName)
        return;

    if (position.GrossProfit < 0)
    {
        ExecuteOrder(position.Quantity * 2, position.TradeType);
    }

    // Set the flag to false when a trade is opened
    tradeOpened = false;

    // If the position quantity reaches MaxLotsize, open a new position
    if (Renew)
    {
        ClosePosition();
        RefreshData();
    }
}
}
}
 


@maxxx2532
Replies

PanagiotisCharalampous
07 Nov 2023, 06:48

Hi there,

You need to describe better what you are asking for. Personally I do not understand what you need.

Best regards,

Panagiotis


@PanagiotisCharalampous