Trading Loops?

 

Hi have written some code which attempts to make an OrderSend() to open an order. After the OrderSend() the code checks for the GetLatestError() and if an error is found (ie > 0) then it tries again. My concern with this is that a situation may arise where the trade has actually gone through successfully but still reports an error. Further calls to open an order keep being fired off until I have an unfathomably large position.

Does anyone have any experience with avoiding this situation? Or, can we assume that if GetLatestError() > 0 then the trade hasn't gone through?

 
stewart:

Hi have written some code which attempts to make an OrderSend() to open an order. After the OrderSend() the code checks for the GetLatestError() and if an error is found (ie > 0) then it tries again. My concern with this is that a situation may arise where the trade has actually gone through successfully but still reports an error. Further calls to open an order keep being fired off until I have an unfathomably large position.

Does anyone have any experience with avoiding this situation? Or, can we assume that if GetLatestError() > 0 then the trade hasn't gone through?

Don't test GetLastError() test the value returned by OrderSend. If OrderSend returns -1 it has failed, ( read the Documentation ) then use GetLast Error() and report the error to the Log.

There is an example in this thread: What are Function return values ? How do I use them ?

 

Thanks for that Raptor.

Is it possible that OrderSend() could return true when a ticket was still created? What about for a partial fill?

 

OrderSend() NEVER returns true. OrderSend() NEVER returns false. It ONLY returns the ticket number or -1.

Once you get a ticket number, you can orderSelect() and find out the actual OrderLots().

 
stewart:

Thanks for that Raptor.

Is it possible that OrderSend() could return true when a ticket was still created? What about for a partial fill?

I gave you a link, read the Documentation, why didn't you read it ?
 

Sorry about that - I was thinking of OrderClose() which returns a boolean indicating success or not. Apologies again for the wrong info as OrderSend() returns an int being ticket number if successful.

So, back to the original question, it seems as WHRoeder suggests that a partial fill could be found by comparing the number of lots you passed into OrderOpen() with those returned by OrderLots() after the ticket is selected using OrderSelect(). If the two don't match then it is a partial fill. This is the only way I can think of for detecting a partial fill. I was kind of expecting it would indicate a ticket number and an error. Sorry about that - I'm reasonably new to MQL4.

Reason: