View Single Post
  #1 (permalink)  
Old 10-24-2007, 06:39 AM
el cid's Avatar
el cid el cid is online now
Senior Member
 
Join Date: Nov 2006
Posts: 970
el cid is an unknown quantity at this point
multiple order send by error

Hi Folks

The EAS can place more than the desired number of trades .I was using a single chart on platform and only one EA but the EA placed 3 orders inplace of one trade.

How can the order send function be tightened and the code improved to prevent more than the required number of orders being placed?

Should there be a time gap between orders ?

Should every order sent be checked on log and should resending should be frozen for 10 minutes and should resending only be valid only after checking filled orders(total open trades) show trade as not executed?

Could anyone advise please

Here is what the broker stated in email

Hi

I wrote to a broker and asked why the orders were duplicated

Hi,



Sorry for the delay in reply. We were waiting fro our programmer to come back to us.



He said that expert advisor does not have any errors in its code.



What might be have happened:



1. the “UseTF2” parameter is TRUE, that’s why the EA uses the second timeframe in addition to the first one.



2. the expert was attached to two charts instead of one that is why it produces signals twice. To check this you would need to carefully look through log files. If log files contacin a record that expert produced two signals one by one then chance are that expert is attached on two charts.







He also gave you a couple of advices:



Sometimes in the source code (for example, in the CloseAllOrders function) the return value of the trading functions (OrderModify, OrderSend etc.) is not checked. So, if an error occurs, there will be no error messages in the log file and it could be difficult to understand why the trading operation has failed.



Sometimes, when several EAs start trading at the same time, trading functions may fail because the trading context is busy and only one EA is allowed to trade at the moment. So, other EAs will not open positions/modify orders etc. and the signal will be missed. To avoid this, you can either use WaitUntilTradingIsAllowed() function or StartTrading()/StopTrading() functions.





Option 1.







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



//| WaitUntilTradingIsAllowed() returns: |



//| 0 – if trade context is not busy |



//| 1 – if EA has been stopped |



//| 2 – if client terminal’s setting don’t allow EAs |



//| to trade |



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



int WaitUntilTradingIsAllowed()



{



if (IsTradeAllowed()) return(0);







while (!IsStopped())



{



if (!IsExpertEnabled()) return(2);







if (!IsTradeContextBusy())



{



RefreshRates();



return(0);



}







Sleep(100);



}







return(1);



}











Example:







void start()

{







If(WaitUntilTradingIsAllowed()==0)



{



int t = OrderSend(…);







}







}











Option 2.







Please note:



a) It’s not a good idea to use the functions below if you attach other EAs to your trading account or trade manually.



b) I recommend that you call StopTrading() in the init() function of your EA to cleanup “critical section” variable (GLOB_VAR_NAME).











#define GLOB_VAR_NAME "TradeIsAllowed"







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



//| StartTrading() returns: |



//| |



//| 0 – if this EA can trade |



//| 1 - if EA has been stopped |



//| 2 – if client terminal’s setting don’t allow EAs to trade |



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



int StartTrading()



{



if (IsTesting()) return(0);







int LastError;







while (!IsStopped())



{



if (!IsExpertEnabled()) return(2);







if (GlobalVariableCheck(GLOB_VAR_NAME)) break;







LastError = GetLastError();



if (LastError!=0)



{



Print("StartTrading():GlobalVariableCheck(“,GLOB_V AR_NAME, ”) failed with the error code ", LastError);



Sleep(100);



continue;



}







if (GlobalVariableSet(GLOB_VAR_NAME, 0)>0) break;







LastError = GetLastError();



Print("StartTrading():GlobalVariableSet(“,GLOB_VAR _NAME, ”) failed with the error code ", LastError);



Sleep(100);



}







while (!IsStopped())



{



if (!IsExpertEnabled()) return(2);







if (GlobalVariableSetOnCondition(GLOB_VAR_NAME, 1, 0))



{



RefreshRates();



return(0);



}







Sleep(100);



}







return(1);



}







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



//| StopTrading() returns: |



//| |



//| 0 – if other EAs can trade |



//| 1 - if EA has been stopped |



//| 2 – if client terminal’s setting don’t allow EAs to trade |



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



int StopTrading()



{



if (IsTesting()) return(0);







int LastError;







while (!IsStopped())



{



if (!IsExpertEnabled()) return(2);







if (GlobalVariableSet(GLOB_VAR_NAME, 0)>0)



{



return(0);



}







LastError = GetLastError();



Print("StopTrading():GlobalVariableSet(“,GLOB_VAR_ NAME, ”) failed with the error code ", LastError);







Sleep(100);



}







return(1);



}















Example:







void start()

{







If(StartTrading()==0)



{



int t = OrderSend(…);



StopTrading();







}







}







Please use the source code above as guidance only and amend it to suit your requirements.



Should you have any questions feel free to get back in touch.



Best regards,



Helpdesk / Customer Support
Reply With Quote