Code fix for Highest and lowest price pending order

 

Hi Fellow Coders,

Could you please help me fix this basic code. Trying to set pending orders based on some simple indicators but I want the price to be placed as a BUYSTOP 3 pips higher than the highest of 5 candles back. and same for the lowest but I keep getting the error "invalid stops" Can't see the problem please help.

//+------------------------------------------------------------------+
//|     TRADE PLACING FUNCTION                                                             |
//+------------------------------------------------------------------+
void EnterTrade(int type){

   int err=0;
   double price=0,
          sl=0, 
          tp=0,
          lotsize = Lotsize;
          

 
              
   if(type==OP_BUYSTOP) price=HighPrice() + 3*pips;
      if(type==OP_SELLSTOP) price=LowPrice() -3*pips;
              
              
              
int ticket =  OrderSend(Symbol(),type,lotsize,price,30,0,0,"normal trade",magic,0,Magenta); 
   if(ticket>0)
   {
      if(OrderSelect(ticket,SELECT_BY_TICKET))
      {
         if(OrderType() == OP_SELLSTOP)
         {
         
           sl = OrderOpenPrice()+(StopLoss*pips);
            if(StopLoss==0)sl=0;
            tp=OrderOpenPrice()-(TakeProfit*pips);
            
             
         }
         else if(OrderType()==OP_BUYSTOP)
         {
         
          sl = OrderOpenPrice()-(StopLoss*pips);
            if(StopLoss==0)sl=0;
            tp=OrderOpenPrice()+(TakeProfit*pips);
           
            
         }
         //---Modify This Order
         if(!OrderModify(ticket,price,sl,tp,0,Magenta)) 
         {
            err = GetLastError();
            Print("Encountered an error during modification!"+(string)err+" "+ErrorDescription(err)  );
         }
          //---Modify The Other sell Orders that are Open.
         if(TotalOpenSellOrders() > 1)         
         {
            for(int i=0;i<OrdersTotal()-1;i++)
            {
               if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
                  if(OrderMagicNumber()==magic)
                     if(OrderType()==OP_SELL)
                        if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),tp,0,clrMagenta))                              
                        {
                           err = GetLastError();
                           Print("Encountered an error during modification!"+(string)err+" "+ErrorDescription(err)  );
                        }
                        
            }
         }
          //---Modify The Other buy Orders that are Open.
         if(TotalOpenBuyOrders() > 1)         
         {
            for(int i=0;i<OrdersTotal()-1;i++)
            {
               if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
                 if(OrderMagicNumber()==magic)
                     if(OrderType()==OP_BUY)
                        if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),tp,0,clrMagenta))                              
                        {
                           err = GetLastError();
                           Print("Encountered an error during modification!"+(string)err+" "+ErrorDescription(err)  );
                        }
                        
            }
         }
         
      }
      else
      {//in case it fails to select the order for some reason 
         Print(__FUNCTION__,"Failed to Select Order ",ticket);
         err = GetLastError();
         Print("Encountered an error while seleting order "+(string)ticket+" error number "+(string)err+" "+ErrorDescription(err)  );
      }
   }
   else
   {//in case it fails to place the order and send us back a ticket number.
      err = GetLastError();
      Print("Encountered an error during order placementb!"+(string)err+" "+ErrorDescription(err)  );
      if(err==ERR_TRADE_NOT_ALLOWED)MessageBox("You can not place a trade because \"Allow Live Trading\" is not checked in your options. Please check the \"Allow Live Trading\" Box!","Check Your Settings!");
   }
}
  

Here are the Highprice and Lowprice functions

//+------------------------------------------------------------------+
//|  Highest candle price                                                                |
//+------------------------------------------------------------------+
double HighPrice()
{
  
         double highestprice=iHighest(NULL,0,MODE_HIGH,5,0);
   
   return(highestprice);
   }
//+------------------------------------------------------------------+
//|  Lowest candle price                                                                |
//+------------------------------------------------------------------+
double LowPrice()
{
  
         double Lowestprice=iLowest(NULL,0,MODE_LOW,5,0);
   
   return(Lowestprice);
   }

Thanks in advance

 
In future please post in the correct section
I will move your topic to the MQL4 and Metatrader 4 section.
 
          sl = OrderOpenPrice()-(StopLoss*pips);
  1. We have no idea what StopLoss, TakeProfit, and pips are (value and type.)

  2. You buy at the Ask and sell at the Bid.
    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using the Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    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.)
      Most brokers with variable spread widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134.
 
Thanks but I found the problem it was the functions Lowprice() Highprice() the code only gives me the location of the highest or lowest candle hence. incorrect parameters to use as an entry price and creating an invalid stoploss message. The correct code is to locate that candle from the iHigh or iLow then return the indexed value, hence the entry price to place the pending order. please see below for anyone that needs it.
//+------------------------------------------------------------------+
//|  Highest candle price                                                                |
//+------------------------------------------------------------------+
double HighPrice()
{
  double Highprice =0;
//--- calculating the highest value on the 10 consequtive bars in the range 
//--- from the 1st to the 19th index inclusive on the current chart 
   int val_index=iHighest(NULL,0,MODE_HIGH,10,0); 
   if(val_index!=-1) Highprice=High[val_index]; 
   else PrintFormat("Error in iLowest. Error code=%d",GetLastError());
  
   
   return(Highprice);
   }
//+------------------------------------------------------------------+
//|  Lowest candle price                                                                |
//+------------------------------------------------------------------+
double LowPrice()
{
  double Lowprice =0;
//--- calculating the lowest value on the 10 consequtive bars in the range 
//--- from the 1st to the 10th index inclusive on the current chart 
   int val_index=iLowest(NULL,0,MODE_LOW,10,0); 
   if(val_index!=-1) Lowprice=Low[val_index]; 
   else PrintFormat("Error in iLowest. Error code=%d",GetLastError());
  
   
   return(Lowprice);