Consecutive losses

 

Hello Guyz.

I need help. For the last 10 months I've been working on creating my own EA. Happy to say I'm pleased with what I've done.

I noticed however through back testing  that sometimes for days I will will hit an almost endless stream of one loss after another.

What I would like is to include a code that will record consecutive losses and perform an operation based on X number of losses

ie. Stop Trading or Change some variable.

It would be nice if I can also include a code to halt trading after closing a trade for X amount of bars.

Any advice, clues on what to do will be appreciated. I ask with this case because I've no idea where to start.

Will I need to include account info mqh or position info mqh. Not sure. Thanks.

 
royzen:

Hello Guyz.

I need help. For the last 10 months I've been working on creating my own EA. Happy to say I'm pleased with what I've done.

I noticed however through back testing  that sometimes for days I will will hit an almost endless stream of one loss after another.

What I would like is to include a code that will record consecutive losses and perform an operation based on X number of losses

ie. Stop Trading or Change some variable.

It would be nice if I can also include a code to halt trading after closing a trade for X amount of bars.

Any advice, clues on what to do will be appreciated. I ask with this case because I've no idea where to start.

Will I need to include account info mqh or position info mqh. Not sure. Thanks.

You do not need to record consecutive losses since it is already recorded https://www.mql5.com/en/docs/trading/historyselect

For the second issue, keep a counter of the bars and simply not open new trades when bar counter is below a certain threshold.

 
royzen :

Hello Guyz.

I need help. For the last 10 months I've been working on creating my own EA. Happy to say I'm pleased with what I've done.

I noticed however through back testing  that sometimes for days I will will hit an almost endless stream of one loss after another.

What I would like is to include a code that will record consecutive losses and perform an operation based on X number of losses

ie. Stop Trading or Change some variable.

It would be nice if I can also include a code to halt trading after closing a trade for X amount of bars.

Any advice, clues on what to do will be appreciated. I ask with this case because I've no idea where to start.

Will I need to include account info mqh or position info mqh. Not sure. Thanks.

I suggest making an MQL5 adviser, but first you need to divide your task into several small ones:

  • unprofitable transactions (the number or amount of losses or the amount of losses for N-days or ...)
  • halt trading after closing a trade for X amount of bars (honestly, I don’t understand what it is - your explanation is needed)
 
royzen:

Hello Guyz.

I need help. For the last 10 months I've been working on creating my own EA. Happy to say I'm pleased with what I've done.

I noticed however through back testing  that sometimes for days I will will hit an almost endless stream of one loss after another.

What I would like is to include a code that will record consecutive losses and perform an operation based on X number of losses

ie. Stop Trading or Change some variable.

It would be nice if I can also include a code to halt trading after closing a trade for X amount of bars.

Any advice, clues on what to do will be appreciated. I ask with this case because I've no idea where to start.

Will I need to include account info mqh or position info mqh. Not sure. Thanks.

Hi, retest this EA on history from start trading and compare with statement if you have big difference, your EA stoped work. Regards Greg 

 
Enrique Dangeroux:

You do not need to record consecutive losses since it is already recorded https://www.mql5.com/en/docs/trading/historyselect

For the second issue, keep a counter of the bars and simply not open new trades when bar counter is below a certain threshold.

Hello Enrique

Thanks for your quick response.

Took a look at that Historyselect() and could not get code to focus on only consecutive losing trades. Here's what I've done.  In order to

try and get EA to stop trading after 3 consecutive losses. What the EA does is count 3 tickets. So long as it is winning it won't stop and that's fine.

When a loss occurs however, the conditions for stopping more trades is met. Trading is stopped immediately by depressing the Auto Trade button.

The losses have to be one after another else it does not help. I've been at this for hours now and I'm stuck. Please help.

Please look and tell me what I need to fix.

void History()
  {

//--- create objects
   uint     total=HistoryDealsTotal();
   ulong    ticket;
   double   profit;
   
//---get history
     HistorySelect(0,TimeCurrent());  

//--- for deal
   for(uint i=0;i<total;i++)
     {
      //--- deal ticket
      if((ticket=HistoryDealGetTicket(i))>0)
        {
         //--- deals properties
         profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
           {
            if(profit<0&&ticket>=3)
            {
             ObjectSetInteger(0,"cm Auto",OBJPROP_STATE,false);
             }
           }
        }
     }
  } 
 
Vladimir Karputov:

I suggest making an MQL5 adviser, but first you need to divide your task into several small ones:

  • unprofitable transactions (the number or amount of losses or the amount of losses for N-days or ...)
  • halt trading after closing a trade for X amount of bars (honestly, I don’t understand what it is - your explanation is needed)

Hello Vlad.

My EA is fully autonomous and it uses a ''cm Auto'' trading button. All I want is for it to stop trading after 3 losses. With this button off

no trading can happen. Have a look at the code above and please advise. Thank you so much for responding. I'm new to programming.

What I mean by halt trading is to pause trading for at least 1 more full candle after having closed a previous deal. Only familiar with

programming based on past candles.

 
Greg Pawlak:

Hi, retest this EA on history from start trading and compare with statement if you have big difference, your EA stoped work. Regards Greg 

Hey Greg.

Greg. What statement are you referring to? 🤷‍♀️

 
royzen :

Hello Vlad.

My EA is fully autonomous and it uses a ''cm Auto'' trading button. All I want is for it to stop trading after 3 losses. With this button off

no trading can happen. Have a look at the code above and please advise. Thank you so much for responding. I'm new to programming.

What I mean by halt trading is to pause trading for at least 1 more full candle after having closed a previous deal. Only familiar with

programming based on past candles.

I would add a filter: by symbol and by magic.

 
royzen:

Hey Greg.

Greg. What statement are you referring to? 🤷‍♀️

Hi, compare your account statement to back test EA. Regards Greg

 
I found a nice solution. Thanks people.
 
What was your solution?