OrderSend error 138 during BackTesting
for OrderSend I use this function
//+------------------------------------------------------------------+ void Buy(double lot,int magic) { if(OrderSend(Symbol(),OP_BUY,lot,Ask,3,0,0,NULL,magic,0,clrNONE)) { Print(Symbol()+" Buy @ "+Ask); }else{ Print("Buy Open ERROR"); } } //+------------------------------------------------------------------+ void Sell(double lot,int magic) { if(OrderSend(Symbol(),OP_SELL,lot,Bid,3,0,0,NULL,magic,0,clrNONE)) { Print(Symbol()+" Sell @ "+Bid); }else{ Print("Sell Open ERROR"); } } //+------------------------------------------------------------------+
for OrderClose I use
//+------------------------------------------------------------------+ void CloseBuy(int magic) { for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber()==magic) if(OrderSymbol()==Symbol()) if(OrderType()==OP_BUY) if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrNONE)) { Print(OrderTicket()+" "+Symbol()+" Buy Closed @ "+OrderClosePrice()); }else{ Print("Buy Close Error"); } } } //+------------------------------------------------------------------+ void CloseSell(int magic) { for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber()==magic) if(OrderSymbol()==Symbol()) if(OrderType()==OP_SELL) if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrNONE)) { Print(OrderTicket()+" "+Symbol()+" Sell Closed @ "+OrderClosePrice()); }else{ Print("Sell Close Error"); } } } //+------------------------------------------------------------------+
for counting orders
int totalBuys(int magic) { double total=0; for(int t=0; t<OrdersTotal(); t++) { if(OrderSelect(t,SELECT_BY_POS,MODE_TRADES)==true) { if(OrderMagicNumber()==magic) if(OrderSymbol()==Symbol()) { if(OrderType()==OP_BUY) total++; } } } return(total); } //+------------------------------------------------------------------+ int totalSells(int magic) { double total=0; for(int t=0; t<OrdersTotal(); t++) { if(OrderSelect(t,SELECT_BY_POS,MODE_TRADES)==true) { if(OrderMagicNumber()==magic) if(OrderSymbol()==Symbol()) { if(OrderType()==OP_SELL) total++; } } } return(total); } //+------------------------------------------------------------------+
useful functions
int test=OrderSend(_CROSS,OP_BUY,0.01,Ask,5,0,0,"",0,0,clrGreen);
-
Check your return codes for errors and report them.
What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles - Do not trade multiple currencies in one EA.
- You can't use any {MT4: predefined variables, MT5: predefined variables,} can't use the tester, must poll (not OnTick,) and usually other problems, e.g. A problem with iBarShift - MQL4 and MetaTrader 4 - MQL4 programming forum- Page 2
- You must handle History {MT4: 4066/4073 errors: Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum , MT5: Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5.}
- Code it to trade the chart pair only. Look at the others if you must. (Don't assume that Time[i] == iTime(otherPair,TF, i) always use iBarShift.)
- Then put it on other charts to trade the other pairs. Done.
Hi Guys,
I'm testing my EA and I'm getting always the error 138 each time I run OrderSend function.
I edited the EA in order to just call OrderSend on each onTick event:
RefreshRates();
int test=OrderSend(_CROSS,OP_BUY,0.01,Ask,5,0,0,"",0,0,clrGreen);
return;
In the code I only declare variables and call OrderSend in this way but no order is being created...
I searched a lot in the forum but I didn't find a suitable solution...
This is very strange...
Thanks to support!