Is there something wrong with my code? if OrderCloseTime() != 0

 

I have been using this piece of code for a long time:


if (OrderSelect(orderTicket, SELECT_BY_TICKET) == true) {
   if (OrderCloseTime() != 0)   {
        isTradeRunning = false;
        orderProfit = OrderProfit() + OrderCommission();
        return;
   }
}


It always worked.

Now I have it in a new EA.
I close the order manually (clicking the x) or it hits stoploss, the order closes, and visual debug (isTradeRunning variable) tells me that the EA thinks the order is still running.

What am I doing wrong? Is that method unreliable for some reason?

 
whoowl :

I have been using this piece of code for a long time:



It always worked.

Now I have it in a new EA.
I close the order manually (clicking the x) or it hits stoploss, the order closes, and visual debug (isTradeRunning variable) tells me that the EA thinks the order is still running.

What am I doing wrong? Is that method unreliable for some reason?

You can't get a healthy answer without posting the whole code.

 
if (OrderSelect(orderTicket, SELECT_BY_TICKET) == true) {
  1. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?

    Use a OrderSelect / Position select loop on the first tick, or persistent storage (GV+flush or files) of ticket numbers required.


  2. If the select fails, your code silently also fails.

    Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum (2012)
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles (2014)

 
whoowl:

I have been using this piece of code for a long time:



It always worked.

Now I have it in a new EA.
I close the order manually (clicking the x) or it hits stoploss, the order closes, and visual debug (isTradeRunning variable) tells me that the EA thinks the order is still running.

What am I doing wrong? Is that method unreliable for some reason?

As a side note and for future reference, the 

OrderSelect(...)

function is a bool, if you're looking for a bool function to return true, you can just do the following:

if(OrderSelect(...)){

        //Do a function

}

I don't really know why the literature appends the "== true" on the end but it's not necessary, I've never appended an "==true" to the end of bool function.

Essentially this is saying:

if((Something == true) == true)


If I were you, I would start with printing the error code and see what comes up:

bool    isTradeRunning;
double	orderProfit;
int     errorCode               = GetLastError();
int	orderTicket		= OrdersTotal()-1;


if (OrderSelect(orderTicket, SELECT_BY_TICKET)){

   if (OrderCloseTime() != 0){

                isTradeRunning = false;

                        orderProfit = OrderProfit() + OrderCommission();
        
                                return;
   }
} else {

        PrintFormat("Order not selected. Error %d",errorCode);

}

Also, if you want people to be able to replicate this, you ideally want to declare all variable by their type. I appreciate you would have done this in your actual code but the idea is for people on here to be able to copy and paste your code straight into their MetaEditor, replicate the problem, and find what the problem is.

If we were to copy and paste your code as is, it will not recognise your variables because you have not declared the variable types in your snippet. 

Lastly, whenever your EA is not doing what you expect, ALWAYS, print your error whenever a bool returns false.

Hope you find a solution :)  

 

You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and “if long entry” is an incomplete sentence.

 
int	orderTicket		= OrdersTotal()-1;


if (OrderSelect(orderTicket, SELECT_BY_TICKET)){

OrdersTotal is a count, not a ticket. Code will always fail.

 
William Roeder #:

You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and “if long entry” is an incomplete sentence.

Yes, you explained that much better than me :)