Jump to next bar if SL is hit

 

Hello Community,


Sorry if this question is an idiotic one, but I am rather new to this whole MQL thing. I am coding a small expert advisor, and would like the following to be achieved: 

If any of my buy orders, hits its stop loss, I would like to stop all operations  and jump to next bar, ( I am using it on a daily chart). In other words, if SL is hit, skip the whole bar and wait for a new day.

Is there an easy way to do this?  Kindly find a piece of my code below.

if(prevClose > prevOpen && curAsk < condValue) { 
      if ( nbOrders == 0 ) {
         ticket1 = OrderSend(Symbol(),OP_BUY,0.1,curAsk,5,stop1BLoss,NormalizeDouble((curAsk + 0.07*curAsk),Digits));
            if( OrderSelect(ticket1,SELECT_BY_TICKET)){
               takeProfit = OrderTakeProfit();
               initAsk = OrderOpenPrice();
   }
   }
       
         if ( nbOrders == 1) {
           RefreshRates();
            if( curAsk >= initAsk + 300*Point){
               ticket2 = OrderSend(Symbol(),OP_BUY,0.1,curAsk,5,initAsk,takeProfit);
               OrderSelect(ticket2,SELECT_BY_TICKET);
               Entry2 = OrderOpenPrice();
               OrderModify(OrderTicket(),OrderOpenPrice(),initAsk,takeProfit,0);
     }   
     }

Best Regards,

Lord Odin

 

In other words, if SL is hit, skip the whole bar and wait for a new day.

I think you are asking how to detect when a new bar forms.

This article describes a time-based method to do so.

https://www.mql5.com/en/articles/159

The "New Bar" Event Handler
The "New Bar" Event Handler
  • www.mql5.com
Authors of indicators and experts have always been interested in writing the compact code in terms of execution time. You can approach to this problem from different angles. From this broad topic in this article we will cover the problem, that is seemingly already have been solved: check for a new bar. This is quite a popular way to limit the...
 
Anthony Garot:

I think you are asking how to detect when a new bar forms.

This article describes a time-based method to do so.

https://www.mql5.com/en/articles/159

Thank you for your fast reply mate.
The article is an interesting read, but it has a lot of explanation and MQL5 coding methods, which I find rather confusing.  ( I'm using MQL4). The time thing used to detect a new bar is great, but how will I know at the start of my condition that my orders' Stop Loss is hit? 

Best Regards,
Lord Odin
 

Lord Odin:

but how will I know at the start of my condition that my orders' Stop Loss is hit?

I don't use MQL4, but a quick search suggests this might help:

https://www.mql5.com/en/forum/113425

Determining when Stop Loss is hit
Determining when Stop Loss is hit
  • 2008.12.18
  • www.mql5.com
How can an EA know when a position was closed by the stop loss? I need some sample code or location where to find it...
 

Lord Odin: Sorry if this question is an idiotic one, but I am rather new to this whole MQL thing. I am coding a small expert advisor, and would like the following to be achieved:

If any of my buy orders, hits its stop loss, I would like to stop all operations  and jump to next bar, ( I am using it on a daily chart). In other words, if SL is hit, skip the whole bar and wait for a new day.

Is there an easy way to do this?  Kindly find a piece of my code below.

Best Regards,

Lord Odin

Detecting a new Bar and/or New Day if on a Daily Chart ...

// Attention: Untested and uncompiled code to serve only as an example
// Compatible with both MQL4 and MQL5

void OnTick()
{
   // Check for New Current Bar
   static datetime dtBarCurrent = WRONG_VALUE;
   datetime dtBarPrevious = dtBarCurrent;
   dtBarCurrent = (datetime) SeriesInfoInteger( _Symbol, _Period, SERIES_LASTBAR_DATE );
   boolNewBarFlag = ( dtBarCurrent != dtBarPrevious );

   if( boolNewBarFlag ) // Check for New Bar
   {
      // Do something ...
   }
   else
   {
      // Do something else ...
   }

   // Do things ...
}
 

Thank you both for your replies.

I understand now how it should be done. Cheers


Best Regards,

Lord Odin