Error in normalizing prices

 

Hi guys,

i had problems with one of my EAs, after normalizing prices i still got outputs like this:

I dont know why this is there, considering all prices are normalized.Ill send my code, if someone could help!

Thanks

      double stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL)*point;
      double bid=NormalizeDouble(MarketInfo(Symbol(),MODE_BID),Digits);
      double ask=NormalizeDouble(MarketInfo(Symbol(),MODE_ASK),Digits);      
      //====== =========== ============ =======================
      if(!AreThereOrders() && rsi0>rsi_buylevel && stoch0>stoch_buylevel && maFast_0>maSlow_0 && maFast_1<maSlow_1) // Order 1 setup
        {
         for(int i=OrdersTotal()-1;i>=0;i--)
           {
            if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
               continue;
            if(OrderMagicNumber()==magic && OrderSymbol()==Symbol() && OrderType()==OP_SELL)
              {
               bool close=OrderClose(OrderTicket(),OrderLots(),Ask,slippage,clrNONE);
              }
           }
//          RefreshRates(); 
         if(CheckMoneyForTrade(Symbol(),LotSize,0)==false) return;
         //========================================================
         SL=NormalizeDouble(bid-stoploss*point,Digits);
         if(stoploss*point<stoplevel)SL=NormalizeDouble(bid-stoplevel,Digits);
         if(stoploss==0 && stoploss==0)SL=0;
         TP=NormalizeDouble(bid+takeprofit*point,Digits);
         if(takeprofit*point<stoplevel)TP=NormalizeDouble(bid+stoplevel,Digits);
         if(takeprofit==0 && stoplevel==0)TP=0;
         //======================================================
         ticket=OrderSend(Symbol(),OP_BUY,NormalizeLots(LotSize),Ask,slippage*pips2points,0,0,NULL,magic,0,clrGreen);
   //      RefreshRates();
         if(ticket>0)
           {
            Print("Order Opened BUY  !!");
           }
         else
            Print("Error occured BUY  not sent! Error code :",GetLastError());
        }
      //--- check for short position (SELL) possibility
      else if(!AreThereOrders() && rsi0<rsi_selllevel && stoch0<stoch_selllevel && maFast_0<maSlow_0 && maFast_1>maSlow_1)
        {
         for(int i=OrdersTotal()-1;i>=0;i--)
           {
            if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
               continue;
            if(OrderMagicNumber()==magic && OrderSymbol()==Symbol() && OrderType()==OP_BUY)
              {
               bool close=OrderClose(OrderTicket(),OrderLots(),Bid,slippage,clrNONE);
              }
           }
 //         RefreshRates(); 
         if(CheckMoneyForTrade(Symbol(),LotSize,1)==false) return;
        //======================================================== 
         SL=NormalizeDouble(ask+stoploss*point,Digits);
         if(stoploss*point<stoplevel)SL=NormalizeDouble(ask+stoplevel,Digits);
         if(stoploss==0 && stoplevel==0)SL=0;
         TP=NormalizeDouble(ask-takeprofit*point,Digits);
         if(takeprofit*point<stoplevel)TP=NormalizeDouble(ask-stoplevel,Digits);
         if(takeprofit==0 && stoplevel==0)TP=0;         
         //=======================================================================
         ticket=OrderSend(Symbol(),OP_SELL,NormalizeLots(LotSize),Bid,slippage*pips2points,0,0,NULL,magic,0,clrRed);
 //        RefreshRates();
         if(ticket>0)
           {
            Print("Order Opened SELL !!");
           }
         else
            Print("Error occured SELL  not sent! Error CODE:",GetLastError());
        }
     }
  //------------   
  Print(TP," TP ",SL," SL ");
   Modify();
 
Stanislav Ivanov:

Hi guys,

i had problems with one of my EAs, after normalizing prices i still got outputs like this:

I dont know why this is there, considering all prices are normalized.Ill send my code, if someone could help!

Thanks

It is happening when printing a Normalized Double. Use DoubleToString() to avoid this when using Print().
Here it is from the documentation.
"Please note that when output to Journal using the Print() function, a normalized number may contain a greater number of decimal places than you expect..."

https://docs.mql4.com/convert/normalizedouble

Also you don't need to Normalize Bid/Ask etc, they are already Normalized.
NormalizeDouble - Conversion Functions - MQL4 Reference
NormalizeDouble - Conversion Functions - MQL4 Reference
  • docs.mql4.com
Calculated values of StopLoss, TakeProfit, and values of open prices for pending orders must be normalized with the accuracy, the value of which can be obtained by Digits(). Please note...
 

Well, I removed all Normalize Doubles, but this still appears, and the tester returns 4501(because it pput the TP and SL in OrderModify).Ive accounted for stoplevel, price decimals, refresh rate, and so on, but nothing! I think it could be because of this(of the many decimals, so that price>price)

any one, any ideas how to solve it?

Thanks

      double stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL)*point;
      //====== =========== ============ =======================
      if(!AreThereOrders() && rsi0>rsi_buylevel && stoch0>stoch_buylevel && maFast_0>maSlow_0 && maFast_1<maSlow_1) // Order 1 setup
        {
         for(int i=OrdersTotal()-1;i>=0;i--)
           {
            if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
               continue;
            if(OrderMagicNumber()==magic && OrderSymbol()==Symbol() && OrderType()==OP_SELL)
              {
               bool close=OrderClose(OrderTicket(),OrderLots(),Ask,slippage,clrNONE);
              }
           }
          RefreshRates(); 
         if(CheckMoneyForTrade(Symbol(),LotSize,0)==false) return;
         //========================================================
         SL=Bid-stoploss*point;
         if(stoploss*point<stoplevel)SL=Bid-stoplevel;
         if(stoploss==0 && stoploss==0)SL=0;
         TP=Bid+takeprofit*point;
         if(takeprofit*point<stoplevel)TP=Bid+stoplevel;
         if(takeprofit==0 && stoplevel==0)TP=0;
         //======================================================
         ticket=OrderSend(Symbol(),OP_BUY,NormalizeLots(LotSize),Ask,slippage*pips2points,0,0,NULL,magic,0,clrGreen);
         RefreshRates();
         if(ticket>0)
           {
            Print("Order Opened BUY  !!");
           }
         else
            Print("Error occured BUY  not sent! Error code :",GetLastError());
        }
      //--- check for short position (SELL) possibility
      else if(!AreThereOrders() && rsi0<rsi_selllevel && stoch0<stoch_selllevel && maFast_0<maSlow_0 && maFast_1>maSlow_1)
        {
         for(int i=OrdersTotal()-1;i>=0;i--)
           {
            if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
               continue;
            if(OrderMagicNumber()==magic && OrderSymbol()==Symbol() && OrderType()==OP_BUY)
              {
               bool close=OrderClose(OrderTicket(),OrderLots(),Bid,slippage,clrNONE);
              }
           }
          RefreshRates(); 
         if(CheckMoneyForTrade(Symbol(),LotSize,1)==false) return;
        //======================================================== 
         SL=Ask+stoploss*point;
         if(stoploss*point<stoplevel)SL=Ask+stoplevel;
         if(stoploss==0 && stoplevel==0)SL=0;
         TP=Ask-takeprofit*point;
         if(takeprofit*point<stoplevel)TP=Ask-stoplevel;
         if(takeprofit==0 && stoplevel==0)TP=0;         
         //=======================================================================
         ticket=OrderSend(Symbol(),OP_SELL,NormalizeLots(LotSize),Bid,slippage*pips2points,0,0,NULL,magic,0,clrRed);
         RefreshRates();
         if(ticket>0)
           {
            Print("Order Opened SELL !!");
           }
         else
            Print("Error occured SELL  not sent! Error CODE:",GetLastError());
        }
     }
 
Stanislav Ivanov:

Well, I removed all Normalize Doubles, but this still appears, and the tester returns 4501(because it pput the TP and SL in OrderModify).Ive accounted for stoplevel, price decimals, refresh rate, and so on, but nothing! I think it could be because of this(of the many decimals, so that price>price)

any one, any ideas how to solve it?

Thanks

Don't remove all the NormalizeDouble(), I said you don't need to use it for Ask and Bid.
and as mentioned in the Documentation use DoubleToString() when printing a Normalized Double.

Change the last line only of the Original code you used.
Print(DoubleToString(TP,Digits)," TP ",DoubleToString(SL,Digits)," SL ");


Alternative(better) function for NormalizeDouble() https://www.mql5.com/en/forum/146370#comment_3693988

Trailing Bar Entry EA
Trailing Bar Entry EA
  • 2013.08.16
  • www.mql5.com
Hello, I have written an EA that trails one bar high or low and opens 2 positions. After that, it should halt and do nothing more...
 

Well Ive normalize them back, but this appeared.Just to see i set Double ToStr with 16decimal places to see if it will be only 0000 after the fifth sign, but apparently not! Could this be causing 4501 error (invalid param value)?

Thanks 


 
Stanislav Ivanov:

Well Ive normalize them back, but this appeared.Just to see i set Double ToStr with 16decimal places to see if it will be only 0000 after the fifth sign, but apparently not! Could this be causing 4501 error (invalid param value)?

Thanks 


I don't think so. I think you are referring to error 130 (trade server). I don't think so. But also check out that alternative function, NormalizePrice(), in the link I gave above.

 
Lakshan Perera:

I don't think so. I think you are referring to error 130 (trade server). I don't think so. But also check out that alternative function, NormalizePrice(), in the link I gave above.

Thanks mate

 
But can the SL/TP be modified for pips and also normalized with the Normalized Price f-n ?
 
Stanislav Ivanov:
But can the SL/TP be modified for pips and also normalized with the Normalized Price f-n ?
Yes, NormalizePrice() will round off any price you passed to it up to the correct digits of the symbol.
 
  1. Floating point has infinite number of decimals, it's your not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
              Double-precision floating-point format - Wikipedia, the free encyclopedia

    See also The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum

    Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

  2. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is usually wrong, as it is in this case.

Reason: