Error code 1 while modifying the order open price using OrderModify() function in MQL4

 

Hi


I am a newbie to the forex coding...

 

I have been trying to modify the open price of an order when the pre defined conditions are met. But while executing the order modify function, it is returning false and the error code is showing as 1.

 

I have observed that order open price and modified open price are different. But still the error code 1 is displaying.

 

I have also normalized the prices for comparison.


The code is as follows....      Please



///+------------------------------------------------------------------+
//|                                                      FirstEa.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//#property strict

//#include <stdlib.mqh>


extern bool   UseTrailingStop=True;
extern double    TslGoodPips=2;
extern double   TslMove=5;
extern bool   UseMoveToBreakeven=True;
extern int    BeGoodPips=1;
extern int    BeStopLossMove=1;
extern double LotSize = 0.01;
extern double StopLoss = 60.0;
extern double TakeProfit = 60.0;
double        pips;
int           MagicNumber=6666;
double BModPrice;
double SModPrice;
double BMStopLoss;
double BMTakeProfit;
double SMStopLoss;
double SMTakeProfit;
double BOpenPrice;
double SOpenPrice;
double BidNom;
double BCondCalc;
double BEqualCalc;
double SCondCalc;
double SEqualCalc;
double CondPips = 2.0;
double MovePips = 10.0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
double ticksize=MarketInfo(Symbol(),MODE_TICKSIZE);
   if(ticksize==0.00001 || ticksize==0.001)
   pips=ticksize*10;
   else pips=ticksize;
   
   return(INIT_SUCCEEDED);

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
       
       //OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      //if(UseMoveToBreakeven)MoveTobreakeven();
      // if(UseTrailingStop)AdjustTrail();
       
       if(IsNewCandle())CheckForMaTrade();
       
    
       
      
      
      }
       
   
   
   
//+------------------------------------------------------------------+
//int OpenOrdersThisPair(string pair)
//{
  // int total = 0;
   //MessageBox(OrdersTotal());
   //for(int i=OrdersTotal()-1; i >= 0; i--)
     // {
        // OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      // if(OrderSymbol()==pair)total++;
      //}
      //return(total);
//}
//+------------------------------------------------------------------+
/*void MoveTobreakeven()
{
   for(int b=OrdersTotal()-1; b>=0; b--)
   {
   if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
      //if(OrderMagicNumber()==MagicNumber)
         if(OrderSymbol()==Symbol())
            if(OrderType()==OP_BUY)
               if(Bid-OrderOpenPrice()>BeGoodPips*pips)
                  if(OrderOpenPrice()>OrderStopLoss())
                   OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(BeStopLossMove*pips),OrderTakeProfit(),0,clrBlue);
  }
  for(int s=OrdersTotal()-1; s>=0; s--)
  {
  if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
    // if(OrderMagicNumber()==MagicNumber)
        if(OrderSymbol()==Symbol())
           if(OrderType()==OP_SELL)
              if(OrderOpenPrice()-Ask>BeGoodPips*pips)
                 if(OrderOpenPrice()<OrderStopLoss())
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(BeStopLossMove*pips),OrderTakeProfit(),0,clrOrange);
  }     
}   */ 
//+------------------------------------------------------------------+                     
/*void AdjustTrail()
{  //buy order section
   for(int b=OrdersTotal()-1;b>=0;b--)
   {
   if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
      //if(OrderMagicNumber()==MagicNumber)
         if(OrderSymbol()==Symbol())
            if(OrderType()==OP_BUY)
               if(Bid-OrderOpenPrice()>TslGoodPips*pips)
                  if(OrderStopLoss()<Bid-pips*TslMove)
                    OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(pips*TslMove),OrderTakeProfit(),0,clrYellow);
   }
   //sell order section
   for(int s=OrdersTotal()-1;s>=0;s--)
   {
   if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
      //if(OrderMagicNumber()==MagicNumber)
         if(OrderSymbol()==Symbol())
            if(OrderType()==OP_SELL)
               if(OrderOpenPrice()-Ask>TslGoodPips*pips)
                  if(OrderStopLoss()>Ask+TslMove*pips||OrderStopLoss()==0)
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(TslMove*pips),OrderTakeProfit(),0,clrPink);
   } 
}
//+------------------------------------------------------------------+*/
bool IsNewCandle()
{
   static int BarsOnChart=0;
   if (Bars == BarsOnChart)
   return(false);
   BarsOnChart=Bars;
   return(true);
}
//+------------------------------------------------------------------+
void CheckForMaTrade()
{

   double HMA0 = iCustom(NULL,0,"HMA",21,2,0,2,0);
   double HMA1 = iCustom(NULL,0,"HMA",21,2,0,2,1);
   double HMA2 = iCustom(NULL,0,"HMA",21,2,0,2,2);

   if (OrdersTotal()== 0){
   if (HMA0 >  HMA1){
   OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,(Ask - (StopLoss * pips)),(Ask +(TakeProfit * pips)),NULL,MagicNumber,0,clrGreen);
    
   }
   if (HMA1 > HMA0){
   OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,(Bid + (StopLoss * pips)),(Bid -(TakeProfit * pips)),NULL,MagicNumber,0,clrBlue);
   
   }
   
   }
   
    for(int b=OrdersTotal()-1; b>=0; b--)
   {
   if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
 
      if(OrderMagicNumber()==MagicNumber)
         if(OrderSymbol()==Symbol())
            if(OrderType()==OP_BUY)        
            {
             BOpenPrice = NormalizeDouble(OrderOpenPrice(),Digits); 
             MessageBox("BopenPrice is "+BOpenPrice);
             BidNom = NormalizeDouble(Bid,Digits);
            MessageBox("BidNom is "+BidNom);
            BCondCalc = NormalizeDouble((CondPips * pips),Digits);
            MessageBox("BCond calc is " +BCondCalc);
             if((BOpenPrice - BidNom)> BCondCalc)
             {
               BEqualCalc =  NormalizeDouble((MovePips * pips),Digits);
               MessageBox("BEqual Calc is " +BEqualCalc);
                BModPrice = (BOpenPrice - BEqualCalc); 
                MessageBox("BModPrice calc is " +BModPrice);
               // BMStopLoss = OrderStopLoss() - 1 * pips;
               // BMTakeProfit = OrderTakeProfit() - 1 * pips;
               if (BOpenPrice > BModPrice || BOpenPrice < BModPrice)
                 {           
             bool statt = OrderModify(OrderTicket(),BModPrice,OrderStopLoss(),OrderTakeProfit(),0,clrGreen);
              
              
           if(!statt) 
               MessageBox("Error in OrderModify. Error code="+GetLastError()); 
              }
              }
              
        } 
        
        }
        
    for(int s=OrdersTotal()-1; s>=0; s--)
  {
  if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))

     if(OrderMagicNumber()==MagicNumber)
        if(OrderSymbol()==Symbol())
           if(OrderType()==OP_SELL)
           
           {
           
           SOpenPrice = NormalizeDouble(OrderOpenPrice(),Digits);
           MessageBox("SopenPrice is "+SOpenPrice);
           BidNom = NormalizeDouble(Bid,Digits);
           MessageBox("BidNom is "+BidNom);
           SCondCalc = NormalizeDouble((CondPips * pips),Digits);
            MessageBox("SCond calc is " +SCondCalc);
             if((BidNom - SOpenPrice)> SCondCalc)
             {
             
               SEqualCalc =  NormalizeDouble((MovePips * pips),Digits);
               MessageBox("SEqual Calc is " +SEqualCalc);
                         
                SModPrice =(SOpenPrice + SEqualCalc); 
                 MessageBox("SModPrice calc is " +SModPrice);
                //SMStopLoss = OrderStopLoss() + 1 * pips;
                //SMTakeProfit = OrderTakeProfit() + 1 * pips;
                
                if (SOpenPrice > SModPrice || SOpenPrice < SModPrice)
                 { 
                
           bool stat =  OrderModify(OrderTicket(),SModPrice,OrderStopLoss(),OrderTakeProfit(),0,clrBlue);
           
           if(!stat) 
               MessageBox("Error in OrderModify. Error code="+GetLastError()); 
        }
        }
        }
       
             
      } 
  
           
  }
  
  
   
   
   
   
   
   
   

 

Moved to right section.

 
  1. Why did you post your MT4 question in the Root / N/A section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. BCondCalc = NormalizeDouble((CondPips * pips),Digits);
    Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong

  3. if (SOpenPrice > SModPrice || SOpenPrice < SModPrice)
    Doubles are rarely equal. So when will that if statement ever be false?
              The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum

  4. if(OrderType()==OP_SELL)
      :
       bool stat =  OrderModify(OrderTicket(),SModPrice,OrderStopLoss(),OrderTakeProfit(),0,clrBlue);
    You can't modify an open order's price and since the stop aren't being moved ERR_NO_RESULT
    You Server
    Change the SL to XIt is at X!
    Change the SL to X It is at X!
    Change the SL to XYou are insane
 

Hi whroeder1


Thank you for your inputs.


I have read the forum rules. Next time i will post in the correct place...

Reason: