Error 4051 when 'OrderSelect' function is used as a switch

 

Hi


Could someone help me with a frustrating problem ?


When I use the following command it works perfectly as a switch but always generates Error 4051.

             if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))return;

I can just use GetLastError to clear but in an attempt to learn more about MQL4 I would like to understand why it is generated.


Kind Regards to all.


William

 

Hi,

So the selection of the orders is  invalid :

4051

ERR_INVALID_FUNCTION_PARAMVALUE

Invalid function parameter value

It would be in this way :

for(int i=0; i<OrdersTotal(); i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))continue;
      if(OrderSymbol()==_Symbol)
        {
         //do something 
        }
     }


Regards.
 
Mehrdad Jeddi:

Hi,

So the selection of the orders is  invalid :

4051

ERR_INVALID_FUNCTION_PARAMVALUE

Invalid function parameter value

It would be in this way :


Regards.

Thanks for the response Mehrdad

But apart from the inversion it appears to be the same function arrangement as my original post , I must be missing the point , could you explain the shortcomings of my effort


Regards


Will

 
Rick_Grimes:

Do not duplicate topics

Your other topics have been deleted.

 
Rick_Grimes:

Thanks for the response Mehrdad

But apart from the inversion it appears to be the same function arrangement as my original post , I must be missing the point , could you explain the shortcomings of my effort


Regards


Will

So if you dont have any order,then the selection of order by zero value is invalid,

In loop statement the OrdersTotal will be checked of there is not any opened order.

 
Mehrdad Jeddi:

So if you dont have any order,then the selection of order by zero value is invalid,

In loop statement the OrdersTotal will be checked of there is not any opened order.

Hi Mehrdad

I am just using it to check if I have any active open trades, no loops, if there is a trade it will be in position 0 and the switch instigates a return if there is no trade open the switch will not return and proceed through the code.

I can't see how the parameters are incorrect,  btw the switch does function correctly i.e. returns when trade present otherwise proceeds' to next line. However, it always generates Error 4051.

Currently I just use ResetLastError() to clear.

Regards


Will

 
To have better understanding about error you should call Errordescription function from stdlib library. Int error=GetLastError() then ErrorDescription(error). You shall use OrderSelect with a for loop.
 
Rick_Grimes:

Hi Mehrdad

I am just using it to check if I have any active open trades, no loops, if there is a trade it will be in position 0 and the switch instigates a return if there is no trade open the switch will not return and proceed through the code.

I can't see how the parameters are incorrect,  btw the switch does function correctly i.e. returns when trade present otherwise proceeds' to next line. However, it always generates Error 4051.

Currently I just use ResetLastError() to clear.

Regards


Will

This is not a good way to use OrderSelect at all... This will guarantee that you will only be able to ever run only one EA on only one chart per account. Not good. Furthermore, if you're just mucking around with the strategy tester and want to take the easy way out then just use orderstotal because it does the exact same thing without generating errors and arbitrarily (inefficiently) selecting orders behind the scenes. 

if (OrdersTotal())
   return;
 
Rick_Grimes:

Hi Mehrdad

I am just using it to check if I have any active open trades, no loops, if there is a trade it will be in position 0 and the switch instigates a return if there is no trade open the switch will not return and proceed through the code.

I can't see how the parameters are incorrect,  btw the switch does function correctly i.e. returns when trade present otherwise proceeds' to next line. However, it always generates Error 4051.

Currently I just use ResetLastError() to clear.

Regards


Will

So maybe the error is from another function that has invalid input,

So provide the full code that i check it better.

 
Rick_Grimes:
if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))return;
I can just use GetLastError to clear but in an attempt to learn more about MQL4 I would like to understand why it is generated.
  1. If there are open or pending orders, the OrderSelect selects it, and returns true. Then the if then calls return. Below that line there will always be an error, because no order has been selected.

    Perhaps you meant to write:

    if(!OrderSelect(0,SELECT_BY_POS,MODE_TRADES))return;

  2. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles