Download MetaTrader 5
To add comments, please log in or register
Read the interviews of the Automated Trading Championship participants
m_shafiei2006
147
m_shafiei2006 2016.04.29 01:13 

Hi


I get 2 errors on this function

#property strict
#import "stdlib.ex4"
#import


void    CloseLongTicket()
        {
           for(int i=OrdersTotal()-1;i>=0;i--)
           if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true)
              {
                 if(OrderType() == OP_BUY)
                 bool order = OrderClose(OrderTicket(),OrderLots(),Bid,3,clrRed);
                 if (order == false)
                 Alert("Close Long Error = ",ErrorDescription(GetLastError()));
              }
           return;
        }



which is :

1.'order' - undeclared identifier

2.'ErrorDescription' - function not defined


can anybody explain what are these errors and how can I fix them ? and is return; necessary or not ?

Keith Watford
Moderator
7986
Keith Watford 2016.04.29 01:36  

For the error description, I use

#include <stdlib.mqh>


void    CloseLongTicket()
        {
           for(int i=OrdersTotal()-1;i>=0;i--)
           if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true)
              {
                 if(OrderType() == OP_BUY)
                 {
                 bool order = OrderClose(OrderTicket(),OrderLots(),Bid,3,clrRed);
                 if (order == false)
                 Alert("Close Long Error = ",ErrorDescription(GetLastError()));
                 }
              }
           return;
        }

You need the {} as highlighted

m_shafiei2006
147
m_shafiei2006 2016.04.29 01:42  

thanks,


and is return; necessary ? compile with and without return;


i ask this because i get confused whit this code ! it gives me error too.


int     BuyPosIndex()
        {
           for(int i=OrdersTotal()-1;i>=0;i--)
           {
              if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true)
              {
                 if(OrderType() == OP_BUY)
                 return(i);
              }
           }   
           return(i);
        }
Keith Watford
Moderator
7986
Keith Watford 2016.04.29 02:08  

return is necessary except for a void function.

int     BuyPosIndex()
  {
   int i;
   for(i=OrdersTotal()-1;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(OrderType()==OP_BUY)
            return(i);
        }
     }
   return(i);
  }

You need to declare int i before the loop or it is only local to the loop

Otherwise, you could just return(-1) for the final return

m_shafiei2006
147
m_shafiei2006 2016.04.29 02:11  
thank you very much.
whroeder1
13641
whroeder1 2016.04.29 12:43  
This does nothing
use #include <stdlib.mqh>
#import "stdlib.ex4"
#import
#import "stdlib.ex4"
string ErrorDescription(int error_code);
int    RGB(int red_value,int green_value,int blue_value);
bool   CompareDoubles(double number1,double number2);
string DoubleToStrMorePrecision(double number,int precision);
string IntegerToHexString(int integer_number);
/
To add comments, please log in or register