Hi,
I am using the trade context management code found on mql4.com (http://articles.mql4.com/141). My trade context seems to be working fine now but a new problem has sprung up: my orders are sometimes duplicated. Has anyone encountered this issue before? The related code is quoted below:
From start function:
function myOpenOrders():
function Buy():
My EA is only supposed to open a maximum of one order per bar per chart, but sometimes I find that multiple orders have been opened at the exact same time.
If someone could help me out with this I would be very appreciative.
I am using the trade context management code found on mql4.com (http://articles.mql4.com/141). My trade context seems to be working fine now but a new problem has sprung up: my orders are sometimes duplicated. Has anyone encountered this issue before? The related code is quoted below:
From start function:
Inserted Code
if (b) //if buy signal
{
if (DebugMode) Alert(Symbol()," ",Period(),": ", "b = true");
if (OppositeClose) CloseOrders(OP_SELL);
ordersTotal = myOpenOrders(Magic);
RefreshRates();
price = NormalizeDouble(Ask,Digits);
sl = 0;
tp = 0;
ticket = Buy(Symbol(), GetLots(OP_BUY), price, sl, tp, Magic);
if (ticket > -1) { if (DebugMode) Alert(Symbol()," ",Period(),": ", "Buy sent"); }
return(0);
} function myOpenOrders():
Inserted Code
int myOpenOrders(int magic = 0, int type =0)
{
int count = 0;
int total = OrdersTotal();
if (IsTesting() && type==0) { return(total); }
if (total== 0) { return(total); }
for(int k=OrdersTotal()-1; k>=0; k--)
{
if (!OrderSelect(k, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol()) continue;
if (OrderMagicNumber() != magic ) continue;
if (OrderType() != type && type!=0) continue;
count++;
}
return(count);
} function Buy():
Inserted Code
int Buy(string symbol, double lot, double price, double sl, double tp, int magic, string comment="")
{
price = NormalizeDouble(price, Digits);
sl = NormalizeDouble(sl, Digits);
tp = NormalizeDouble(tp, Digits);
lot = NormalizeDouble(lot,LotDigits);
string _lot = DoubleToStr(lot, LotDigits);
string _price = DoubleToStr(price, Digits);
string _sl = DoubleToStr(sl, Digits);
string _tp = DoubleToStr(tp, Digits);
comment = StringConcatenate(OtherComments," #: ",DoubleToStr(Magic,0));
// if (ShowPrintComments)Print("Buy \"", symbol, "\", ", _lot, ", ", _price, ", ", Slippage, ", ", _sl, ", ", _tp, ", ", magic, ", \"", comment, "\"");
if(TradeIsBusy() < 0) { if(DebugMode) Alert(Symbol()," ",Period(),": TradeIsBusy, order delayed"); return(-1); }
RefreshRates();
int res = OrderSend(symbol, OP_BUY, lot, price, Slippage*mt, sl, tp, comment, magic, 0, clBuy);
if (res >= 0) {
TradeIsNotBusy();
if(DebugMode) Alert(Symbol()," ",Period(),": TradeIsNotBusy, order sent");
return (res);
}
else {
Print("Buy failed: ", ErrorDescription(GetLastError()), " (", GetLastError(), ")");
return (-1);
}
} My EA is only supposed to open a maximum of one order per bar per chart, but sometimes I find that multiple orders have been opened at the exact same time.
If someone could help me out with this I would be very appreciative.