Need help to round order last digit to 0 or 5

 

I have a problem with my EA because the spread of the currency that im trading is 5 , so, when the indicator give something ending with other number than 5 or 0, the error ocurrs

 

2016.10.22 13:29:38.787 2016.10.03 11:55:00   failed exchange sell 1.00 WINZ16 at 60739 sl: 60834 tp: 60584 [Invalid price]

 

I tried to use StringToDouble or DoubleToString but thats also give me an conversion error.

 

How can i round the numbers of the buy and sell order to end with 5 or 0 when they end with 1 2 3 4 6 7 8 or 9?

 

Here is the code to send the orders

 

   MqlTick latest_price;      // To be used for getting recent/latest price quotes
   MqlTradeRequest mrequest;  // To be used for sending our trade requests
   MqlTradeResult mresult;    // To be used to get our trade results
   MqlRates mrate[];          // To be used to store the prices, volumes and spread of each bar
   ZeroMemory(mrequest);      // Initialization of mrequest structure


    ZeroMemory(mrequest);
         mrequest.action = TRADE_ACTION_DEAL;                                  // immediate order execution
         mrequest.price = NormalizeDouble(latest_price.ask,_Digits);           // latest ask price
         mrequest.sl = NormalizeDouble(latest_price.ask - STP*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits); // Take Profit
         mrequest.symbol = _Symbol;                                            // currency pair
         mrequest.volume = Lot;                                                 // number of lots to trade
         mrequest.magic = EA_Magic;                                             // Order Magic Number
         mrequest.type = ORDER_TYPE_BUY;                                        // Buy Order
         mrequest.type_filling = ORDER_FILLING_IOC;                             // Order execution type
         mrequest.deviation=100;                                                // Deviation from current price
         //--- send order
         OrderSend(mrequest,mresult);
 

Regardless to your code, the following simple idea should work ...

NewPrice = NormalizeDouble(NormalizeDouble(OldPrice,Digits-1),Digits);

 Please let me know what happened ...

 

Dear Osama, i dont know if i its right but i got this errors while trying to implement your code

 

'Digits' - undeclared identifier

'Digits' - some operator expected PinbarTS.mq5

'NormalizeDouble' - wrong parameters count

 

 Here is the whole code 

 

//+------------------------------------------------------------------+
//|                                                  My_First_EA.mq5 |
//|                        Copyright 2010, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
//--- input parameters
input int      StopLoss=100;      // Stop Loss
input int      TakeProfit=200;   // Take Profit
input int      EA_Magic=12345;   // EA Magic Number
input double   Lot=1;          // Lots to Trade

//--- Other parameters
int pinbar_handle; // handle for our ADX indicator

double sinal[]; // Dynamic arrays to hold the values of +DI, -DI and ADX values for each bars
double p_close; // Variable to store the close value of a bar
int STP, TKP;   // To be used for Stop Loss & Take Profit values
  double MathRound(double latest_price);

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Get handle for ADX indicator
    pinbar_handle= iCustom(_Symbol,0,"pinbar",0,true,false,false,0.33,0.4,true,false,false,0.1,0.5,1,0,0.1);
    
    if(pinbar_handle==INVALID_HANDLE)
      {
         Alert(" Erro em pinbar_handle "+GetLastError());
        
              
      }
//--- Get the handle for Moving Average indicator

//--- What if handle returns Invalid Handle
   if(pinbar_handle<0)
     {
      Alert("Error Creating Handles for indicators - error: ",GetLastError(),"!!");
      return(-1);
     }

//--- Let us handle currency pairs with 5 or 3 digit prices instead of 4
   STP = StopLoss;
   TKP = TakeProfit;
  // if(_Digits==5 || _Digits==3)
    // {
      //STP = STP*10;
     // TKP = TKP*10;
    // }
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
//void OnDeinit(const int reason)
// {
//--- Release our indicator handles
//  IndicatorRelease(pinbar_handle);

// }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {


// We will use the static Old_Time variable to serve the bar time.
// At each OnTick execution we will check the current bar time with the saved one.
// If the bar time isn't equal to the saved time, it indicates that we have a new tick.

   static datetime Old_Time;
   datetime New_Time[1];
   bool IsNewBar=false;

// copying the last bar time to the element New_Time[0]
   int copied=CopyTime(_Symbol,_Period,0,1,New_Time);
   if(copied>0) // ok, the data has been copied successfully
     {
      if(Old_Time!=New_Time[0]) // if old time isn't equal to new bar time
        {
         IsNewBar=true;   // if it isn't a first call, the new bar has appeared
         if(MQL5InfoInteger(MQL5_DEBUGGING)) Print("We have new bar here ",New_Time[0]," old time was ",Old_Time);
         Old_Time=New_Time[0];            // saving bar time
        
    
        
        

        }
     }
   else
     {
      Alert("Error in copying historical times data, error =",GetLastError());
      ResetLastError();
      return;
     }

//--- EA should only check for new trade if we have a new bar
   if(IsNewBar==false)
     {
      return;
     }

//--- Do we have enough bars to work with
   int Mybars=Bars(_Symbol,_Period);
   if(Mybars<5) // if total bars is less than 60 bars
     {
      Alert("We have less than 5 bars, EA will now exit!!");
      return;
     }

//--- Define some MQL5 Structures we will use for our trade

   MqlTick latest_price;      // To be used for getting recent/latest price quotes
   MqlTradeRequest mrequest;  // To be used for sending our trade requests
   MqlTradeResult mresult;    // To be used to get our trade results
   MqlRates mrate[];          // To be used to store the prices, volumes and spread of each bar
   ZeroMemory(mrequest);      // Initialization of mrequest structure
/*
     Let's make sure our arrays values for the Rates, ADX Values and MA values
     is store serially similar to the timeseries array
*/

// the rates arrays

   ArraySetAsSeries(mrate,true);
   ArraySetAsSeries(sinal,true);
// the ADX DI+values array
   //ArraySetAsSeries(pincor,true);



//--- Get the last price quote using the MQL5 MqlTick Structure
   if(!SymbolInfoTick(_Symbol,latest_price))
     {
      Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
      return;
     }

//--- Get the details of the latest 3 bars
   if(CopyRates(_Symbol,_Period,0,3,mrate)<0)
     {
      Alert("Error copying rates/history data - error:",GetLastError(),"!!");
      ResetLastError();
      return;
     }

//--- Copy the new values of our indicators to buffers (arrays) using the handle
   if( CopyBuffer(pinbar_handle,1,1,2,sinal)<0)
      
     {
      Alert("Error copying pinbar indicator Buffers - error:",GetLastError(),"!!");
      ResetLastError();
      return;
     }
  
//--- we have no errors, so continue
//--- Do we have positions opened already?
   bool Buy_opened=false;  // variable to hold the result of Buy opened position
   bool Sell_opened=false; // variables to hold the result of Sell opened position

   if(PositionSelect(_Symbol)==true) // we have an opened position
     {
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
        {
         Buy_opened=true;  //It is a Buy
        }
      else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
        {
         Sell_opened=true; // It is a Sell
        }
     }

// Copy the bar close price for the previous bar prior to the current bar, that is Bar 1
   p_close=mrate[1].close;  // bar 1 close price

/*
    1. Check for a long/Buy Setup : MA-8 increasing upwards,
    previous price close above it, ADX > 22, +DI > -DI
*/

//--- Declare bool type variables to hold our Buy Conditions
   bool Buy_Condition_1 =   sinal[0] == 2.0; // MA-8 Increasing upwards


//--- Putting all together  
   if(Buy_Condition_1){
  
         // any opened Buy position?
         if(Buy_opened)
           {
            Alert("We already have a Buy Position!!!");
            return;    // Don't open a new Buy Position
           }
         ZeroMemory(mrequest);
         mrequest.action = TRADE_ACTION_DEAL;                                  // immediate order execution
         mrequest.price = NormalizeDouble(NormalizeDouble(latest_price.ask,Digits-1)Digits);           // latest ask price
         mrequest.sl = NormalizeDouble(latest_price.ask - STP*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits); // Take Profit
         mrequest.symbol = _Symbol;                                            // currency pair
         mrequest.volume = Lot;                                                 // number of lots to trade
         mrequest.magic = EA_Magic;                                             // Order Magic Number
         mrequest.type = ORDER_TYPE_BUY;                                        // Buy Order
         mrequest.type_filling = ORDER_FILLING_IOC;                             // Order execution type
         mrequest.deviation=100;                                                // Deviation from current price
         //--- send order
         OrderSend(mrequest,mresult);
         // get the result code
         if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
           {
            Alert("A Buy order has been successfully placed with Ticket#:",mresult.order,"!!");
           }
         else
           {
            Alert("The Buy order request could not be completed -error:",GetLastError());
            ResetLastError();          
            return;
           }
        
     }


//--- Declare bool type variables to hold our Sell Conditions
   bool Sell_Condition_1 = sinal[0] == 1.0;  // MA-8 decreasing downwards


//--- Putting all together
   if(Sell_Condition_1)
    
        {
         // any opened Sell position?
         if(Sell_opened)
           {
            Alert("We already have a Sell position!!!");
            return;    // Don't open a new Sell Position
           }
         ZeroMemory(mrequest);
         mrequest.action=TRADE_ACTION_DEAL;                                // immediate order execution
         mrequest.price = NormalizeDouble(round(latest_price.bid),_Digits);            // latest Bid price
         mrequest.sl = NormalizeDouble(latest_price.bid + STP*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(latest_price.bid - TKP*_Point,_Digits); // Take Profit
         mrequest.symbol = _Symbol;                                          // currency pair
         mrequest.volume = Lot;                                              // number of lots to trade
         mrequest.magic = EA_Magic;                                          // Order Magic Number
         mrequest.type= ORDER_TYPE_SELL;                                     // Sell Order
         mrequest.type_filling = ORDER_FILLING_FOK;                          // Order execution type
         mrequest.deviation=100;                                             // Deviation from current price
         //--- send order
         OrderSend(mrequest,mresult);
         // get the result code
         if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
           {
            Alert("A Sell order has been successfully placed with Ticket#:",mresult.order,"!!");
           }
         else
           {
            Alert("The Sell order request could not be completed -error:",GetLastError());
            ResetLastError();
            return;
           }
        }
    
   return;
  }
//+------------------------------------------------------------------+
 
The code is too long, complicated and inefficient, would rather work with code that can have a higher complexity but rather compact size.
 
PCWalker:
The code is too long, complicated and inefficient, would rather work with code that can have a higher complexity but rather compact size.
Be my guest
 
evertonzn:
Be my guest

Normalize your price like that :

//+------------------------------------------------------------------+
//| Normalize price according to tick size                           |
//+------------------------------------------------------------------+
double normalizePrice(double price)
  {
   double tickSize=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   return(MathRound(price/tickSize)*tickSize);
  }
 
Alain Verleyen:

Normalize your price like that :

//+------------------------------------------------------------------+
//| Normalize price according to tick size                           |
//+------------------------------------------------------------------+
double normalizePrice(double price)
  {
   double tickSize=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   return(MathRound(price/tickSize)*tickSize);
  }
Thank you very much!
 
Alain Verleyen:

Normalize your price like that :

//+------------------------------------------------------------------+
//| Normalize price according to tick size                           |
//+------------------------------------------------------------------+
double normalizePrice(double price)
  {
   double tickSize=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   return(MathRound(price/tickSize)*tickSize);
  }

double NormalizeTick(double num)

  {

   return NormalizeDouble(MathRound(num/ticksize)*ticksize,_Digits);

  }


Does it work that way too?

 
Alain Verleyen #:

Normalize your price like that :

//+------------------------------------------------------------------+
//| Normalize price according to tick size                           |
//+------------------------------------------------------------------+
double normalizePrice(double price)
  {
   double tickSize=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   return(MathRound(price/tickSize)*tickSize);
  }
Thank you!
Reason: