Sure this has been covered before but would like to revisit it. Would like to confirm ideas or get new ideas
How do you handle requotes in MT4 within an EA?
Of course in manual trading in MT4 you can set an additional price differential (max slippage) and quickly re-click or resubmit an order manually as soon as you get a requote and usually it will get filled on the 2nd or 3rd try.
But on an EA, unless you have effective error checking and code to resubmit the order, your EA is dead on that trading cycle and could completely mess up your current trading algo's cycle thus turning a winning strategy into a losing one because of unfilled orders.
The simplest solution is to widen the spread by increasing the slippage on the ordersend, however I feel that once broker gets a feel for how much you are willing to give up on the slippage they will add a buffer to the spread whenever they can get away with it.
I want to keep a tighter slippage range and keep hitting the server at the offer until the order gets taken. Of course in fast moving markets this could take several attempts just like in manual trading, but with an EA it would only take mlliseconds or seconds at the longest.. I want the EA to do the work and need to have the orders filled exactly when the algo says trade.
How do you handle it?
Many say, use a "while" loop. This sounds good but if you have an EA that can have multiple open orders and hedges, an improperly coded while loop has the potential of submitting multiple trades when there should only be one for that magic number. For example if there is a delay in the reply from the server, your EA could end up submitting multiple trades until it gets a fill.
I have not tested the following code on a live market yet, intend to do so next week. However would like to get some opinions/feedback from some others who have tackled this problem of handling requotes in a live EA.
Here is the code that tests well, but as we know live trading in a fast moving market like the DOW will be the real test...
Do you see any potential hazards here? Or non useful code? The GV part is for another part of the EA and gets written after server returns a ticket number for the open trade.
How do you handle requotes in MT4 within an EA?
Of course in manual trading in MT4 you can set an additional price differential (max slippage) and quickly re-click or resubmit an order manually as soon as you get a requote and usually it will get filled on the 2nd or 3rd try.
But on an EA, unless you have effective error checking and code to resubmit the order, your EA is dead on that trading cycle and could completely mess up your current trading algo's cycle thus turning a winning strategy into a losing one because of unfilled orders.
The simplest solution is to widen the spread by increasing the slippage on the ordersend, however I feel that once broker gets a feel for how much you are willing to give up on the slippage they will add a buffer to the spread whenever they can get away with it.
I want to keep a tighter slippage range and keep hitting the server at the offer until the order gets taken. Of course in fast moving markets this could take several attempts just like in manual trading, but with an EA it would only take mlliseconds or seconds at the longest.. I want the EA to do the work and need to have the orders filled exactly when the algo says trade.
How do you handle it?
Many say, use a "while" loop. This sounds good but if you have an EA that can have multiple open orders and hedges, an improperly coded while loop has the potential of submitting multiple trades when there should only be one for that magic number. For example if there is a delay in the reply from the server, your EA could end up submitting multiple trades until it gets a fill.
I have not tested the following code on a live market yet, intend to do so next week. However would like to get some opinions/feedback from some others who have tackled this problem of handling requotes in a live EA.
Here is the code that tests well, but as we know live trading in a fast moving market like the DOW will be the real test...
Do you see any potential hazards here? Or non useful code? The GV part is for another part of the EA and gets written after server returns a ticket number for the open trade.
Inserted Code
void fBuy1() {result = OrderSend(Symbol(),OP_BUY,Lots,MarketInfo(Symbol(),MODE_ASK),Slippage,0,0,"Open LONG MN1",MN1,0,Blue); if ((result == -1) && ((GetLastError() == 129) || (GetLastError() == 135) || (GetLastError() == 138))) fBuy1(); if (result > 0) {GlobalVariableSet("BuyValue1",iOpen(Symbol(),0,0)+(Slippage*Point)); Print("MN1 BUY SHOULD BE OPEN. Ticket #",IntegerToString(result,Digits)," >> OpenTrades: ", OrdersTotal(), " , Last: ", DoubleToStr(Close[0], Digits), ", AcctMarginLevelInPerc: ", NormalizeDouble(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL),0), ", AcctMarginInEquity: ", NormalizeDouble(AccountInfoDouble(ACCOUNT_MARGIN),0)); ResetLastError();} if ((result == -1) && ((GetLastError() != 0) && (GetLastError() != 129) && (GetLastError() != 135) && (GetLastError() != 138))) Print("Open Buy MN1 ERROR #", GetLastError());