Do not trade current candle again after reaching stoploss level

 

Hello guys,


Iam pretty new to this MQL5 coding, but I manage to make my own EA in last 3 days and it seems to be working very well. Iam now fixing some issues to improve the EA.

My problem is that when is one trade closed by stoploss then alogirthm open new trade in current cadle (if coditions allow).

This is the way i can loose multiple trades in one candle. On the other hand it works with takeprofit the same (but I would like to keep this takeprofit issue - its the way to make more profit in one candle).


I have searched some articles and forum topics about this issue, I found only the way how to stop multiple trades in one candle, but not how to stop them if first trade hit stoploss only.  I have read about HistoryDealGetInteger() function, about DEAL_REASOn and about DEAL_REASON_SL, BUT it is too hard for me to understand the syntax and how to manage with this code. Or there is another way ?


Do you have any idea? Please help, Iam despered right now :/


Thank you very much.

 
ramp:

Hello guys,


Iam pretty new to this MQL5 coding, but I manage to make my own EA in last 3 days and it seems to be working very well. Iam now fixing some issues to improve the EA.

My problem is that when is one trade closed by stoploss then alogirthm open new trade in current cadle (if coditions allow).

This is the way i can loose multiple trades in one candle. On the other hand it works with takeprofit the same (but I would like to keep this takeprofit issue - its the way to make more profit in one candle).


I have searched some articles and forum topics about this issue, I found only the way how to stop multiple trades in one candle, but not how to stop them if first trade hit stoploss only.  I have read about HistoryDealGetInteger() function, about DEAL_REASOn and about DEAL_REASON_SL, BUT it is too hard for me to understand the syntax and how to manage with this code. Or there is another way ?


Do you have any idea? Please help, Iam despered right now :/


Thank you very much.

It sounds to me as if you are looking for a conditional time delay? If your position got closed by a profit taker you want to enable a next trade immediately. But if your position got closed by a stoploss you want to wait until the start of a next candle?

If this is indeed what you want then you need to calculate the time remaining on the current candle and use that as waiting time for your next trade, if the position got closed by stoploss.

 
WindmillMQL:

It sounds to me as if you are looking for a conditional time delay? If your position got closed by a profit taker you want to enable a next trade immediately. But if your position got closed by a stoploss you want to wait until the start of a next candle?

If this is indeed what you want then you need to calculate the time remaining on the current candle and use that as waiting time for your next trade, if the position got closed by stoploss.

Yes, it is what I need to do, exactly. But how to set up condtion with stop loss or take profit taken? 

It is not clear to me...
 
if(UpTrend)
   {
    if(lastbartime!=bartime) 
     if(CanOrder && UpSignal) OpenNew(OP_BUY);
    lastbartime=bartime;
}

This is I got so far. But it prevent multiple trades in single bar after stop loss or take profit is taken. How to solve the same only for stop loss ?

Thank you

 
ramp:

This is I got so far. But it prevent multiple trades in single bar after stop loss or take profit is taken. How to solve the same only for stop loss ?

Thank you

I would approach this in a slightly different way. I would first monitor the open order and see which one gets filled: stoploss or profit taker. Based on that would I decide on how long to wait until the next new order. In pseudo-code:

if(OrderExecuted)
{
   if(ProfittakerExecuted)
   {
      Delaytime = 0;
      //allow opening a new order immediately
   }
   if(StoplossExecuted)
   {
      Delaytime = calculateTimeDifference(Currenttime, BarSize);//calculate the time until the current bar ends
      //allow opening a new position after this delay time has passed
   }
}

But there are of course multiple ways to solve a challenge.

 
WindmillMQL:

I would approach this in a slightly different way. I would first monitor the open order and see which one gets filled: stoploss or profit taker. Based on that would I decide on how long to wait until the next new order. In pseudo-code:

But there are of course multiple ways to solve a challenge.

Thank you for your reply and code.

Please, can you give me exaple of code for bool "ProfittakerExecuted" or "StoplossExecuted" ? This is the part of the task I cannot handle myself...

 
ramp: My problem is that when is one trade closed by stoploss then alogirthm open new trade in current cadle (if coditions allow).
  1. Don't open if you already have a trade open.
  2. You are looking at a signal. Act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - Strategy Tester - Expert Advisors and Automated Trading - MQL5 programming forum #1
 
William Roeder:
  1. Don't open if you already have a trade open.
  2. You are looking at a signal. Act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - Strategy Tester - Expert Advisors and Automated Trading - MQL5 programming forum #1

I think you didnt understand me. I do not have problem with opening two trades in same tame. There is only one open trade, no more.

I dont want to go that way because it would also affect take profit trades in single candle.

 
hmmm, I think I found soloution, but my EA is written for MT4, and this is MQL5 code .... grrrr... what can I do ?
Stop Loss Take Profit
Stop Loss Take Profit
  • www.mql5.com
If a trade is closed by Stop loss, the volume is doubled; if it's closed by Take profit the minimum volume is used. OnTradeTransaction is used to determine whether a trade was performed after the activation of Stop loss or Take profit. The excellent enumeration ENUM_DEAL_REASON was added in build 1625: ENUM_DEAL_REASON Reason description It...
 
ramp:

Iam pretty new to this MQL5 coding, but....

ramp:
hmmm, I think I found soloution, but my EA is written for MT4, 

Make your mind up. Are you writing for MT4 or MT5?

 
ramp:

Thank you for your reply and code.

Please, can you give me exaple of code for bool "ProfittakerExecuted" or "StoplossExecuted" ? This is the part of the task I cannot handle myself...

What I wrote was not real code but pseudo-code, to describe the steps that should be taken. I don't have real code as I'm not trying to build what you are doing.
Reason: