- Error 4108 backtesting Trailing stop EA
- order close error
- By trying to delete pending orders from ticket number I got the error message "OrderDelete error 4108"
-
if(OrderSelect(orderID,SELECT_BY_TICKET) == true){
You selected the ticket. You don't check if it has already closed or not. -
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 to recover, or persistent storage (GV+flush or files) of ticket numbers required.
-
if(orderType == 0){ optimalTakeProfit = NormalizeDouble(Ask + 800*Point,Digits); }else{ optimalTakeProfit = NormalizeDouble(Bid - 800*Point,Digits);
You buy at the Ask and sell at the Bid.- Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using the Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?
- Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger at a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25 - The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
Most brokers with variable spread widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134.
-
orderID = OrderSend(NULL,OP_BUYLIMIT,lotSize,Ask,1,stopLossPrice,takeProfitPrice,NULL,magicNumber);
You can't open a pending order at the market.
Thanks for the reply @William Roeder, from my understanding your suggestion is like this:
1. Before I run the order select I should check the order id if it's closed or not, is that correct?
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 to recover, or persistent storage (GV+flush or files) of ticket numbers required.
2. It is better to use at least a loop or persistent storage to avoid problem like you mention above?
You buy at the Ask and sell at the Bid.
- Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using the Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?
- Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger at a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25 - The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
Most brokers with variable spread widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134.
3. that TP and SL is figurative model. I'm going to use the TP using an indicator but I'm still far from fix that.
You can't open a pending order at the market.
4. I can't use the Ask as the entry price if I using OP_BUYLIMIT?
Sorry for asking to much, I'm still new with mql4.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use