Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - page 2

 

Hello! I have an issue with my ModifyOrder function.

I believe it is something to do with the calculation of digits for SL/TP...for All fx currency works, for XAU,XAG works, for XTI XNG doesn't not work .For XTI is opening the order but then ModifyOrder comes with error and order is left without SL/TP, and for XNG the same. But for all JPY pairs works :

2021.11.11 23:04:37.173 TEST 3 VRT-2 XTIUSD,Daily: open #188694311 sell 0.04 XTIUSD at 80.15 ok

2021.11.11 23:04:37.173 TEST 3 VRT-2 XTIUSD,Daily: invalid takeprofit for OrderModify function

2021.11.11 23:04:37.173 TEST 3 VRT-2 XTIUSD,Daily: Alert: Modify order #188694311: Error 4051 - invalid function parameter value

2021.11.11 23:04:37.173 TEST 3 VRT-2 XTIUSD,Daily: Price: 0.0, SL: 85.14, TP: -19.85, Expiration: 1970.01.01 00:00:00



2021.11.11 23:00:05.761 TEST 3 VRT-2 XNGUSD,Daily: open #188692649 sell 0.10 XNGUSD at 5.200 ok

2021.11.11 23:00:05.761 TEST 3 VRT-2 XNGUSD,Daily: invalid takeprofit for OrderModify function

2021.11.11 23:00:05.761 TEST 3 VRT-2 XNGUSD,Daily: Alert: Modify order #188692649: Error 4051 - invalid function parameter value

2021.11.11 23:00:05.761 TEST 3 VRT-2 XNGUSD,Daily: Price: 0.0, SL: 5.999, TP: -4.8, Expiration: 1970.01.01 00:00:00




Below is my code 



//Stoploss Calculation in points
   int stoploss;


   if(StopLoss==0)
     {
      stoploss=(int)(ATR*ATRMultiplier/_Point);
     }
   else(stoploss=0);


   int SL= StopLoss+stoploss;

//TakeProfit Calculation
   int takeprofit;

   if(TakeProfit==0)
     {
      takeprofit=(int)(ATR/_Point);
     }
   else(takeprofit=0);

   int TP=TakeProfit+takeprofit;

// Money management
   double lotSize = FixedLotSize;
   if(UseMoneyManagement == true)
     {
      lotSize = MoneyManagement(_Symbol,FixedLotSize,RiskPercent,SL);
     }

// Orders OpenFilters 
//
//

int ticket = OrderSend(_Symbol,OP_BUY,lotSize,Ask,Slippage,0,0,"Buy Order",MagicNumber,0,clrGreen);
             GVSet("tp",PP);
             ModifyStopsByPoints(ticket,SL,TP);

And then OrderModify:


bool ModifyStopsByPoints(int pTicket, int pStopPoints, int pProfitPoints = 0, int pMinPoints = 10)
{
   if(pStopPoints == 0 && pProfitPoints == 0) return false;
   
   bool result = OrderSelect(pTicket,SELECT_BY_TICKET);
   
   if(result == false)
   {
      Print("Modify stops: #",pTicket," not found!");
      return false;
   }
   
   double orderType = OrderType();
   double orderOpenPrice = OrderOpenPrice();
   string orderSymbol = OrderSymbol();
   
   double stopLoss = 0;
   double takeProfit = 0;
   
   if(orderType == OP_BUY)
   {
      stopLoss = BuyStopLoss(orderSymbol,pStopPoints,orderOpenPrice);
      if(stopLoss != 0) stopLoss = AdjustBelowStopLevel(orderSymbol,stopLoss,pMinPoints);
      
      takeProfit = BuyTakeProfit(orderSymbol,pProfitPoints,orderOpenPrice);
      if(takeProfit != 0) takeProfit = AdjustAboveStopLevel(orderSymbol,takeProfit,pMinPoints);
   }
   else if(orderType == OP_SELL)
   {
      stopLoss = SellStopLoss(orderSymbol,pStopPoints,orderOpenPrice);
      if(stopLoss != 0) stopLoss = AdjustAboveStopLevel(orderSymbol,stopLoss,pMinPoints);
      
      takeProfit = SellTakeProfit(orderSymbol,pProfitPoints,orderOpenPrice);
      if(takeProfit != 0) takeProfit = AdjustBelowStopLevel(orderSymbol,takeProfit,pMinPoints);
   }
   
   result = ModifyOrder(pTicket,0,stopLoss,takeProfit);
   return(result);
}


bool ModifyOrder(int pTicket, double pPrice, double pStop = 0, double pProfit = 0, datetime pExpiration = 0, color pArrow = clrOrange)
{
  
   
	bool result = false;
	
		
		result = OrderModify(pTicket, pPrice, pStop, pProfit, pExpiration, pArrow);}
		
 
And one more question : if I wright like this : 
double stoplosss=Ask-at; // at=ATR
double stoploss=NormalizeDouble(stoplosss,_Digits);

stoploss will be 0.7302000000000001 if I put on CADCHF 

                       127.447 on EURJPY


But if I put like this 

long digits = SymbolInfoInteger(_Symbol,SYMBOL_DIGITS);
long Digit= digits==5 ? 4 : 2;

double stoplosss=Ask-at;
double stoploss=NormalizeDouble(stoplosss,Digit);

stoploss will be 0.7302 if I put on CADCHF 

                       127.447 on EURJPY


Why _Digits is giving so many digits on CADCHF . Because it looks like Normalizing is not working with _Digits

 
Daniel Cioca # it looks like Normalizing is not working with _Digits
  1. NormalizeDouble returns a double.

    Floating-point has an infinite number of decimals, it's you, not understanding floating-point and that some numbers can't be represented exactly. (like 1/10.)
              Double-precision floating-point format - Wikipedia

    See also The == operand. - MQL4 programming forum (2013)

    If you want to see the correct number of digits, convert it to a string with the correct/wanted accuracy.
              question about decima of marketinfo() - MQL4 programming forum (2016)

  2. You used NormalizeDouble, It's use is usually wrong, as it is in your case.

    1. Floating point has a 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 programming forum (2013)

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

    3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on non-currencies.
                On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum (2011)

      And abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum (2012)

    4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on non-currencies. So do it right.
                Trailing Bar Entry EA - MQL4 programming forum (2013)
                Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012)

    5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong.
                Do it right. (2013)

    6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
                MT4:NormalizeDouble - MQL5 programming forum (2017)
                How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum (2017)

    7. Prices you get from the terminal are already correct (normalized).

    8. PIP, Point, or Tick are all different in general.
                What is a TICK? - MQL4 programming forum (2014)

 
Ok thanks! Read all that already, still not clarified… the bottom line is… if SL /TP price is with 10 digits… it will affect OrderModify to function correctly? 
Reason: