reduce "consecutive number of losing trade" and restart

Created at 07 Jun 2023, 15:24
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!
GR

greggx2

Joined 30.05.2023

reduce "consecutive number of losing trade" and restart
07 Jun 2023, 15:24


Hi friends,

I am not familiar with coding, and i need help with this legacy Martingale cbot. I have set it to be one directional trade, I need help for the trade to restart the trade, after the "stop" for the maximum allowed consecutive loss. I want it to restart a new trade with the initial volume, one trade at a time.. and continue the cycle.

 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
 
namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleMartingaleRobot : Robot
  
  
  {
        [Parameter("Initial Volume", DefaultValue = 1000, MinValue = 0)]
        public int InitialVolume { get; set; }
 
        [Parameter("Stop Loss", DefaultValue = 25)]
        public int StopLoss { get; set; }
 
        [Parameter("Take Profit", DefaultValue = 50)]
        public int TakeProfit { get; set; }
         
        private Random random = new Random();
 
        protected override void OnStart()
        {
            Positions.Closed += OnPositionsClosed;
 
               
            ExecuteOrder(InitialVolume, GetRandomTradeType());
        }
 
        private void ExecuteOrder(long volume, TradeType tradeType)
        {
            var result = ExecuteMarketOrder(tradeType, SymbolName, volume, "Martingale", StopLoss, TakeProfit);
 
            if (result.Error == ErrorCode.NoMoney)
                Stop();
                
        }
 
        private void OnPositionsClosed(PositionClosedEventArgs args)
      
     
      {  
      
           { 
           
           
            Print("Closed");
            var position = args.Position;
 
            if (position.Label != "Martingale" || position.SymbolName != Symbol.Name)
                return;
 
            if (position.GrossProfit > 0)
            
                        
            {
            ExecuteOrder(InitialVolume, GetRandomTradeType());                                             
            }
         
            else
            {
               ExecuteOrder((int)position.VolumeInUnits * 2, position.TradeType);
            }        
            
           //after 5 consecutive loss stop opening new trade, cosult exeuction spreadsheet for calculations, this is for 1000, SL:25, TP:50
            
            if  ((int)position.GrossProfit < -36)
            Stop();
           
           // I want the trade to restart a new trade from here after the consecutive losses, then start all over again,(one trade per time) I just cant get it to work

           
          }
         
           
         }
            
         private TradeType GetRandomTradeType()
        {
            return random.Next(2) == 0 ? TradeType.Sell : TradeType.Sell;
        }  
         
    }
    

}


@greggx2
Replies

PanagiotisChar
08 Jun 2023, 08:19

Hi there,

If you need professional support, feel free to contact me at development@clickalgo.com

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


@PanagiotisChar