Newbie Problem: NormalizeDouble

 

Hello! First of all, I am a total Newbie to Mql4 and I just started like 3 days ago to discover the possibilities of an EA.

Please bare with me, I really do appreciate any kind of help, that I can get. I was reading here in the forum and in the manual alot about the

NormalizeDouble function, after I am getting the ongoing error OrderSend 4051. I kind of figured out, that this has to do with the Lot Value and maybe the Bid Value given back,

my EA is currently just for NZDJPY, M5 and only short.

 

I simply cant figure out on my own how to put the NormalizeDouble function correctly, and where. Thank you for your help! I know its anything else than a proper or a clean code, maybe even wrong, but as

I said, please bare with me, I am very green :-) 

 

//global extern variables 
extern int RSI_top = 80;
extern int MagicNumber = 813371337;
extern int RSIPeriod =7;
extern double SLpercent = 0.15,TPpercent = 0.35;
extern double LotValue = 1.5;
extern double MAlong = 200;
extern double MAshort = 50;

//global variables
datetime PeriodStart;
double StopLoss,TakeProfit;
bool NewPeriod,ShortSignal,OrderModified;
int ShortOrder;
double ShortBid;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   PeriodStart = Time[0];
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

//check if new period started
   if(PeriodStart != Time[0])
      {
      NewPeriod = true;
      PeriodStart = Time[0];
      }
   else NewPeriod = false;
   
            //Get market data
            double RSItop = iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,2);
            double RSItopPRE = iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,3);
            double RSIcurrent = iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,1);
            double MAlong1 = iMA(NULL,0,MAlong,8,MODE_SMA,PRICE_CLOSE,1);
            double MAshort1 = iMA(NULL,0,MAshort,8,MODE_SMA,PRICE_CLOSE,1);
            double MAshort2 = iMA(NULL,0,MAshort,8,MODE_SMA,PRICE_CLOSE,16);
  
//trading signals 
   if(NewPeriod == true)
      {
      //generating short signal
            if (RSItopPRE>=RSI_top && RSItop>=RSI_top && MAshort1>=MAlong1 && MAshort1<=MAshort2)
               {
               ShortSignal = true;
               }
            else ShortSignal = false;
      }
      else
         {
         ShortSignal = false;
         }
      
//max open orders
    if (OrdersTotal() < 3)
       {   
         //open short position
             if (ShortSignal == true)
                  {
                  LotValue = NormalizeDouble(LotValue,1);
                  ShortOrder = OrderSend(Symbol(),OP_SELL,LotValue,Bid,10,0,0,"forex_short",MagicNumber,0,Green);
                   }
             else Print("Error OrderSend : ",GetLastError()); 
        }
    
//Stop Loss Short
   if(OrderSelect(ShortOrder,SELECT_BY_TICKET)==true)
      {
      if(OrderCloseTime()==0 && OrderStopLoss()==0)
         {
         StopLoss=NormalizeDouble(OrderOpenPrice()*(1+(SLpercent/100)),Digits);
         OrderModified = OrderModify(OrderTicket(),OrderOpenPrice(),StopLoss,OrderTakeProfit(),0,Yellow);
         }
      else Print("Error Order Modify SL : ",GetLastError()); 
      }
 
//Take Profit Short
   if(OrderSelect(ShortOrder,SELECT_BY_TICKET)==true)
      {
         if(OrderCloseTime()==0 && OrderTakeProfit()==0)
            {
            TakeProfit=NormalizeDouble(OrderOpenPrice()/(1+(TPpercent/100)),Digits);
            OrderModified = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit,0,Orange);
            }
         else Print("Error Order Modify TP : ",GetLastError()); 
       }
       }  
 
tradale:

Hello! First of all, I am a total Newbie to Mql4 and I just started like 3 days ago to discover the possibilities of an EA.

Please bare with me, I really do appreciate any kind of help, that I can get. I was reading here in the forum and in the manual alot about the

NormalizeDouble function, after I am getting the ongoing error OrderSend 4051. I kind of figured out, that this has to do with the Lot Value and maybe the Bid Value given back,

my EA is currently just for NZDJPY, M5 and only short.

 

I simply cant figure out on my own how to put the NormalizeDouble function correctly, and where. Thank you for your help! I know its anything else than a proper or a clean code, maybe even wrong, but as

I said, please bare with me, I am very green :-) 

 

 

4051

ERR_INVALID_FUNCTION_PARAMVALUE

Invalid function parameter value

https://forum.mql4.com/43325 

  1. to make a lot size. NormalizeDouble(x, 1) ASSUMES LOTSTEP is 0.1 it fails for any other. Use MathFloor(x/lotStep)*lotStep.

Hope that helps.  

 
DeanDeV:

4051

ERR_INVALID_FUNCTION_PARAMVALUE

Invalid function parameter value

https://forum.mql4.com/43325 

  1. to make a lot size. NormalizeDouble(x, 1) ASSUMES LOTSTEP is 0.1 it fails for any other. Use MathFloor(x/lotStep)*lotStep.

Hope that helps.  


Ok I dont reallz know how to and where to put this, I am assuming somewhere here:

 

//max open orders
    if (OrdersTotal() < 3)
       {   
         //open short position
             if (ShortSignal == true)
                  {
                  ShortOrder = OrderSend(Symbol(),OP_SELL,LotValue,Bid,5,0,0,"short_forex",MagicNumber,0,Green);
                  }
             else Print("Error OrderSend : ",GetLastError()); 
        }

 

I am getting this error by the way not in the tester and not in the code, when I implement it into a chart it starts immediately and ongoing 2 times or more per second ....on all tested charts so far. First it gives back after activating in demo mode is Order Send Error: 0, then it starts.... even if there is nothing to

be sent yet! 

error4051 

 
  1. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
  2.              if (ShortSignal == true)
                      {
                      LotValue = NormalizeDouble(LotValue,1);
                      ShortOrder = OrderSend(Symbol(),OP_SELL,LotValue,Bid,10,0,0,"forex_short",MagicNumber,0,Green);
                       }
                 else Print("Error OrderSend : ",GetLastError()); 
    Do not print GetLastError unless you have an error; only if OrderSend fails. If ShortSignal is false, GLE is meaningless.
 
WHRoeder:
  1. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it.It's use is always wrong
  2. Do not print GetLastError unless you have an error; only if OrderSend fails. If ShortSignal is false, GLE is meaningless.
That solved the whole problem! Kind of silly, but its logic now! Thank you very much!!!!