iHigh and ILow not working

 
extern int setdayofweek = 0;
extern int sethour = 12;
extern int lookback_hour = 1;
extern double buy_points_buffer = 7.0;
extern double sell_points_buffer = 10.0;
extern double lots = .3; 
extern double stop_loss_points = 10.0;
extern double take_profit_points = 10.0;
 
 


 
int ticket = 0;
int buy_enable_trade = 1;
int sell_enable_trade = 1;
int set_buy_modify = 0;
int set_sell_modify = 0;
int order_type = 3;
int timedayofweek = 0;
int timehour = 0;
int enable_stops = 1;
int activate_sound = 0;
int adjust_time = 1;
int adjusted_hour = 0;
 
 
double order_profit = 0.0;
double sell_stop_loss_points = 0.0;
double sell_stop_price = 0.0;
double buy_stop_price = 0.0;
double high = 0.0;
double low = 0.0;
double range = 0.0;

bool res_buy = 0;
bool res_sell = 0;

 

int init()
  {
 
   return(0);
  }
 
int deinit()
  {
 
   return(0);
  }
 
int start()
  {
 
  timedayofweek = DayOfWeek();
  timehour = Hour();
 
  
  if(adjust_time == 1)
   {
   
   
    adjusted_hour = sethour;
    adjust_time = 0;
   
   
   }
    
  
  
  
   
  if((timedayofweek == setdayofweek) && (timehour == adjusted_hour) && (enable_stops == 1))
  {
  
  PlaySound("eagle");  


  
  high = iHigh(Symbol(),PERIOD_H1,lookback_hour);
  low = iLow(Symbol(),PERIOD_H1,lookback_hour);  
  







  range = high - low;
  
    
   
 buy_stop_price = high + (buy_points_buffer * .0001); 
 sell_stop_price = low - (sell_points_buffer * .0001);
     
  if(buy_enable_trade == 1)
   {
   
    ticket = OrderSend(Symbol(),OP_BUYSTOP,lots,buy_stop_price,3,buy_stop_price - stop_loss_points * .0001,buy_stop_price + take_profit_points * .0001,0,0,0);
    if(ticket<0)
     {
      Print("OrderSend failed with error #",GetLastError());
     }
   else
      Print("OrderSend placed successfully"); 
      
      buy_enable_trade = 0;
      set_buy_modify = 1;
     
    }
   
  if(set_buy_modify == 1)
   {
    if(OrderSelect(ticket,SELECT_BY_TICKET))
     {
       res_buy = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice() - stop_loss_points * .0001,OrderOpenPrice() + (take_profit_points * .0001),0,0); 
       
            if(!res_buy)
               Print("Error in OrderModify. Error code=",GetLastError());
            else
               Print("Order modified successfully.");
        
      set_buy_modify = 0;
     
     } 
   }
 
 
 if(sell_enable_trade == 1)
   {
    
     ticket = OrderSend(Symbol(),OP_SELLSTOP,lots,sell_stop_price,3,sell_stop_price + stop_loss_points * .0001,sell_stop_price - take_profit_points * .0001,0,0,0);
     if(ticket<0)
     {
      Print("OrderSend failed with error #",GetLastError());
     }
   else
      Print("OrderSend placed successfully");
      sell_enable_trade = 0;
      set_sell_modify = 1;
      
    }
   
  if(set_sell_modify == 1)
   {
    if(OrderSelect(ticket,SELECT_BY_TICKET))
     {
      res_sell = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice() + stop_loss_points * .0001 ,OrderOpenPrice() - (take_profit_points * .0001),0,0);  
      
     if(!res_sell)
               Print("Error in OrderModify. Error code=",GetLastError());
            else
               Print("Order modified successfully."); 
       
      set_sell_modify = 0;
       enable_stops = 0;
       
     } 
   }

   
 }
 
  
  
 
 
 
 
   return(0);
  }
 


This ea works erratically.   It should go active at the 12th hour candle and using the high and low of the 11th candle, the default values.   Today, on the USD/CAD it went active at the 13th candle using the high and low of the 12th candle to set the buy and sell stops.  Could someone tell why this happened?  I have had another incident of this a few days ago without changing the default values.

 
  1. Use the debugger or print out your variables, including _LastError and find out why.

  2. sell_stop_price,3,sell_stop_price + stop_loss_points * .0001
    Don't hard code numbers. Code fails on JPY pairs, exotics (e.g. USDZAR where spread is over 500 points,) and metals. Compute what a PIP is and use it, not points.
              How to manage JPY pairs with parameters? - MQL4 and MetaTrader 4 - MQL4 programming forum

  3. You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3
    • The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)

  4. There are no such functions (iHigh/iLow) in MT5
              List of MQL5 Functions - Reference on algorithmic/automated trading language for MetaTrader 5

    Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum

Reason: