bool User defined function, help

 

Hi, 

I am very newbie with mql5 and I wrote a function to return bool value. It seems I am missing some parenthesis but I don't

bool isSlBuyNear ()
{
bool slBuyNear = true;
double slBuyDis = calSLBuyDist();
double validSlDist = updateSlDist;

if ( slBuyDis < validSlDist)

  slBuyNear = true;
  else
  slBuyNear = false;
 
  return (slBuyNear) 
}


knew where exactly :) 

 
  1. No semicolon at the end of return.

  2. Your code
    bool slBuyNear = true;
    if ( slBuyDis < validSlDist)
    
      slBuyNear = true;
      else
      slBuyNear = false;
     
    return (slBuyNear);
    Simplified
              Increase Order after stoploss - MQL4 programming forum #1.3 2017.05.29
    return slBuyDis < validSlDist;
    

 

hehehe. I like it.. 

No errors now!

Thank you so much.. 

 

Hi, I am trying to build an auto SL EA. The idea is so simple. EA should look for any open order and update its SL to make it always away from current price by certain pips. This EA should not open or close any order by itself but it just updates the SL of already opened orders. 

Now, EA could detect orders and update only Sell orders. There is no error generated. However, EA could not update the SL of buy orders. 

I would appreciate to show me why is that. 

Later, I am trying to make EA select only orders with certain magic numbers. :). Hopefully, I could!

Here is the code ( My apology that

input bool enableSlBlaster=true;//Enable SL Blaster
input bool selectAllOrders = true;// Select All Orders
input int selectMagicOrders = 111;
enum selectordertypeenum {Buy,Sell,Both};
input selectordertypeenum selectordertype= Buy;// Select Order Type

input string slConditions="SL conditions";     //SL Conditioins
input double stopLossDist = 1500.0;// SL pip distance (points)
input int checkSeconds = 60;// Check SL every (seconds)
input double updateSlDist = 1000.0;// Update SL distance(points)
input double maximumSlDist = 7000.0;// Maximum SL distance(points)

input bool sMagic=222;//Use Magic Numbers

int mypoint;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
  EventSetTimer(checkSeconds); 
if(MarketInfo(Symbol(), MODE_DIGITS)==3||MarketInfo(Symbol(), MODE_DIGITS)==5)
  
   mypoint=10;
   else mypoint=1;
   return(INIT_SUCCEEDED);
  
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
EventKillTimer(); 
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTimer()
  {
   UpdateSl ();
  }
//+------------------------------------------------------------------+
double calSLBuyDist()
{
   double orderOpen = 0;
   double orderSL   = 0; 
   double calSlDist = 0;
   double slBuyDist = 0;
   double calSLBuyDistNorm = 0;
   
   
   for(int i=OrdersTotal()-1; i>=0; i--)
      if(OrderSelect(i,SELECT_BY_POS) )
         { 
         
           
           
            if(OrderType() == OP_BUY)
               {
                  orderOpen = OrderOpenPrice();
                  orderSL   = OrderStopLoss ();
                  calSlDist = ((orderOpen - orderSL)/mypoint);
                  calSLBuyDistNorm = NormalizeDouble (calSlDist,2); 
               }
            slBuyDist = calSLBuyDistNorm;
         }
   return(slBuyDist);
}

//+------------------------------------------------------------------+
double calSLSellDist()
{
   double orderOpen = 0;
   double orderSL   = 0; 
   double calSlDist = 0;
   double slBuyDist = 0;
   double calSLSellDistNorm = 0;
   
   
   for(int i=OrdersTotal()-1; i>=0; i--)
      if(OrderSelect(i,SELECT_BY_POS) )
         { 
         
           
           
            if(OrderType() == OP_SELL)
               {
                  orderOpen = OrderOpenPrice();
                  orderSL   = OrderStopLoss ();
                  calSlDist = orderOpen + orderSL;
                  calSLSellDistNorm = NormalizeDouble (calSlDist,2); 
               }
            slBuyDist = calSLSellDistNorm;
         }
   return(slBuyDist);
}

//+------------------------------------------------------------------+
/* simplified by William in mql5 code forum
bool isSlBuyNear ()
{

double slBuyDis = calSLBuyDist(); 
double validSlDist = updateSlDist;
return slBuyDis < validSlDist; 
 
}
*/
//+------------------------------------------------------------------+

bool isSlBuyNear ()
{
bool slBuyNear = true;
double slBuyDis = calSLBuyDist();
double validSlDist = updateSlDist;

if ( slBuyDis < validSlDist)

  slBuyNear = true;
  else
  slBuyNear = false;
 
  return (slBuyNear); 
}

//+------------------------------------------------------------------+

bool isSlSellNear ()
{
bool slSellNear = true;
double slSellDis = calSLSellDist();
double validSlDist = updateSlDist;

if ( slSellDis < validSlDist)

  slSellNear = true;
  else
  slSellNear = false;
 
  return (slSellNear); 
}
//+------------------------------------------------------------------+
void UpdateSl()
  {
int      cnt         =  OrdersTotal();
for (int i=cnt-1; i>=0; i--) 
{
   if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
   {
      if (isSlSellNear ()|| isSlBuyNear ())
            if ((OrderType()==ORDER_TYPE_BUY || OrderType()==ORDER_TYPE_SELL)) 
            {
            
         if (enableSlBlaster) {          
            double   stopLoss       =  mypoint*stopLossDist*SymbolInfoDouble(OrderSymbol(), SYMBOL_POINT);
            double   stopLossPrice  =  (OrderType()==ORDER_TYPE_BUY) ?
                                       OrderOpenPrice()-stopLoss :
                                       OrderOpenPrice()+stopLoss;
            stopLossPrice           =  NormalizeDouble(stopLossPrice, (int)SymbolInfoInteger(OrderSymbol(), SYMBOL_DIGITS));
            if (OrderModify(OrderTicket(), OrderOpenPrice(), stopLossPrice, OrderTakeProfit(), OrderExpiration(),clrRed)) {}
         }
      }
   }
}
   
  }

I pasted all the code because I do not knew where exactly is the problem)

 
A new trial.. I know it very messy!. But I am trying :)
//+------------------------------------------------------------------+
//|                                               AutoSL Blaster.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input bool enableSlBlaster=true;//Enable SL Blaster
input bool selectAllOrders = true;// Select All Orders
input int selectMagicOrders = 111;
enum selectordertypeenum {Buy,Sell,Both};
input selectordertypeenum selectordertype= Buy;// Select Order Type

input string slConditions="SL conditions";     //SL Conditioins
input double stopLossDist = 1500.0;// SL pip distance (points)
input int checkSeconds = 60;// Check SL every (seconds)
input double updateSlDist = 1000.0;// Update SL distance(points)
input double maximumSlDist = 7000.0;// Maximum SL distance(points)

input bool sMagic=222;//Use Magic Numbers

int mypoint=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
  EventSetTimer(checkSeconds); 
if(MarketInfo(Symbol(), MODE_DIGITS)==3||MarketInfo(Symbol(), MODE_DIGITS)==5)
  
   mypoint=10;
   else mypoint=1;
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
EventKillTimer(); 
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+ 
  void OnTick ()
  {
  Comment (CalSLSellDist(),"     ",  CalSLBuyDist (),"     ", isSlSellNear (),"     ",isSlBuyNear () );
  }
//+------------------------------------------------------------------+
//| Expert Timer function                                             |
//+------------------------------------------------------------------+
void OnTimer()
  {
  if (isSlBuyNear ())
   UpdateBuySl ();
  if (isSlSellNear ()) 
   UpdateSellSl ();
   
  }
//+------------------------------------------------------------------+
double CalSLBuyDist()
{
   
   double currSlDist = 0;
   double slBuyDist = 0;

   for(int i=OrdersTotal()-1; i>=0; i--)
      if(OrderSelect(i,SELECT_BY_POS) )
         { 
            if(OrderType() == OP_BUY)
               {  
                  double current = MarketInfo("",MODE_ASK);
                  double orderSL   = OrderStopLoss (); 
                  
                  currSlDist = NormalizeDouble ((MathAbs((current - orderSL)/Point)),2);
               }
            slBuyDist = currSlDist;
         }
   return(slBuyDist);
}

//+------------------------------------------------------------------+
double CalSLSellDist()
{
   double currSlDist=0;
   double slBuyDist=0;
   
   for(int i=OrdersTotal()-1; i>=0; i--)
      if(OrderSelect(i,SELECT_BY_POS) )
         { 
           
            if(OrderType() == OP_SELL)
               {
                 double current = MarketInfo("",MODE_BID);
                 double orderSL   = OrderStopLoss (); 
                  currSlDist = NormalizeDouble ((MathAbs((current - orderSL)/Point)),2);
               }
            slBuyDist = currSlDist;
         }
   return(slBuyDist);
}

//+------------------------------------------------------------------+
/* simplified by William in mql5 code forum
bool isSlBuyNear ()
{

double slBuyDis = calSLBuyDist(); 
double validSlDist = updateSlDist;
return slBuyDis < validSlDist; 
}
*/
//+------------------------------------------------------------------+

bool isSlBuyNear ()
{
bool slBuyNear = true;
double slBuyDis = CalSLBuyDist();
double closest = updateSlDist;
double farthest = maximumSlDist;

if ( slBuyDis < closest || slBuyDis > farthest)

  slBuyNear = true;
  else
  slBuyNear = false;
 
  return (slBuyNear); 
}
//+------------------------------------------------------------------+

bool isSlSellNear ()
{
bool slSellNear = true;
double slSellDis = CalSLSellDist();
double closest = updateSlDist;
double farthest = maximumSlDist;

if ( slSellDis < closest|| slSellDis > farthest )

  slSellNear = true;
  else
  slSellNear = false;
 
  return (slSellNear); 
}
//+------------------------------------------------------------------+
void UpdateBuySl()
  {
int      cnt         =  OrdersTotal();
for (int i=cnt-1; i>=0; i--) 
{
   if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
   {
        if (OrderType()==ORDER_TYPE_BUY)
            {
         if (enableSlBlaster) 
         {          
            double   stopLoss       =  mypoint*stopLossDist*SymbolInfoDouble(OrderSymbol(), SYMBOL_POINT);
            double   stopLossPrice  =  OrderOpenPrice()-stopLoss;
                                       
            stopLossPrice           =  NormalizeDouble(stopLossPrice, (int)SymbolInfoInteger(OrderSymbol(), SYMBOL_DIGITS));
            if (OrderModify(OrderTicket(), OrderOpenPrice(), stopLossPrice, OrderTakeProfit(), OrderExpiration(),clrRed)) {}
         }
      }
   }
}
   return;
  }
  //+------------------------------------------------------------------+ 
  
  void UpdateSellSl()
  {
int      cnt         =  OrdersTotal();
for (int i=cnt-1; i>=0; i--) 
{
   if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
   {
        if (OrderType()==ORDER_TYPE_SELL)
            {
         if (enableSlBlaster) {          
            double   stopLoss       =  mypoint*stopLossDist*SymbolInfoDouble(OrderSymbol(), SYMBOL_POINT);
            double   stopLossPrice  =  OrderOpenPrice()+stopLoss;                                       
            stopLossPrice           =  NormalizeDouble(stopLossPrice, (int)SymbolInfoInteger(OrderSymbol(), SYMBOL_DIGITS));
            if (OrderModify(OrderTicket(), OrderOpenPrice(), stopLossPrice, OrderTakeProfit(), OrderExpiration(),clrRed)) {}
         }
      }
   }
}
   return;
  }
 
  1.         if (OrderType()==ORDER_TYPE_BUY)
            if (OrderType()==ORDER_TYPE_SELL)

    No such OrderTypes in MT4.
              Order Properties - Trade Constants - Constants, Enumerations and Structures - MQL4 Reference


  2. NormalizeDouble(stopLossPrice, (int)SymbolInfoInteger(OrderSymbol(), SYMBOL_DIGITS));

    You used NormalizeDouble, It's use is usually wrong.

    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.06.07

    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.08.25)

      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.02.16

    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.08.17
                Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum 2012.01.02

    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.08.17

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

    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.08.03