Need Help Fixing Frank_UD EA CloseOrder Error

 
Hi Folks

I keep getting a 4051 error saying the OrderClose syntax is wrong. Can somebody help me out? What would be the syntax to get this script working properly?

invalid ticket for OrderClose function
Error No4051 with Close SELL1






//+------------------------------------------------------------------+
//| ôðàíê_óä. mq4 |
//| Copyright © 2006, Ðàìèëü Ñàôèóëëîâè÷ Èðãèçîâ |
//| popcorn@aaanet. ru |
//+------------------------------------------------------------------+
#define m 20050611
//----
extern int tp = 65;
extern int sh = 41;
//----
datetime lastt;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int kol_buy()
{
int kol_ob = 0;
//----
for(int i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
break;
//----
if(OrderType() == OP_BUY)
kol_ob++;
}
return(kol_ob);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int kol_sell()
{
int kol_os = 0;
//----
for(int i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
break;
//----
if(OrderType() == OP_SELL)
kol_os++;
}
return(kol_os);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
int slip, i, ii, tic, total, kk, gle;
double lotsi = 0.0;
bool sob = false, sos = false, scb = false, scs = false;
int kb, kb_max = 0;
kb = kol_buy() + 1;
double M_ob[11][8];
ArrayResize(M_ob,kb);
int ks = 0, ks_max = 0;
ks = kol_sell() + 1;
double M_os[11][8];
ArrayResize(M_os,ks);
ArrayInitialize(M_ob, 0.0);
int kbi = 0;
//----
for(i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
break;
//----
if(OrderSymbol() == Symbol() && OrderType() == OP_BUY)
{
kbi++;
M_ob[kbi][0] = OrderTicket();
M_ob[kbi][1] = OrderOpenPrice();
M_ob[kbi][2] = OrderLots();
M_ob[kbi][3] = OrderType();
M_ob[kbi][4] = OrderMagicNumber();
M_ob[kbi][5] = OrderStopLoss();
M_ob[kbi][6] = OrderTakeProfit();
M_ob[kbi][7] = OrderProfit();
}
}
M_ob[0][0] = kb;
double max_lot_b = 0.0;
//----
for(i = 1; i < kb; i++)
if(M_ob[i][2] > max_lot_b)
{
max_lot_b = M_ob[i][2];
kb_max = i;
}
double buy_lev_min = M_ob[kb_max][1];
ArrayInitialize(M_os,0.0);
int ksi = 0;
//----
for(i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
break;
//----
if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
{
ksi++;
M_os[ksi][0] = OrderTicket();
M_os[ksi][1] = OrderOpenPrice();
M_os[ksi][2] = OrderLots();
M_os[ksi][3] = OrderType();
M_os[ksi][4] = OrderMagicNumber();
M_os[ksi][5] = OrderStopLoss();
M_os[ksi][6] = OrderTakeProfit();
M_os[ksi][7] = OrderProfit();
}
}
M_os[0][0] = ks;
double max_lot_s = 0.0;
//----
for(i = 1;i < ks; i++)
if(M_os[i][2] > max_lot_s)
{
max_lot_s = M_os[i][2];
ks_max = i;
}
double sell_lev_max = M_os[ks_max][1];
//----
if(Bars < 100 || IsTradeAllowed() == false)
return(0);
sob = (kol_buy() < 1 || buy_lev_min - sh*Point > Ask) &&
AccountFreeMargin() > AccountBalance()*0. 5;
sos = (kol_sell() < 1 || sell_lev_max + sh*Point < Bid) &&
AccountFreeMargin() > AccountBalance()*0. 5;
//----
if(M_ob[kb_max][2] > 0.0)
scb = M_ob[kb_max][7] / (M_ob[kb_max][2]*10) > tp;
//----
if(M_os[ks_max][2] > 0.0)
scs = M_os[ks_max][7] / (M_os[ks_max][2]*10) > tp;
kk = 0;
ii = 0;
//----
if(scb)
{
while(kol_buy() > 0 && kk < 3)
{
for(i = 1; i <= kb; i++)
{
ii = M_ob[i][0];
//----
if(!OrderClose(ii, M_ob[i][2], Bid,slip, White))
{
gle = GetLastError();
kk++;
Print("Error No", gle, "with Close buy", kk);
Sleep(6000);
RefreshRates();
}
}
kk++;
}
}
kk = 0;
ii = 0;
//----
if(scs)
{
while(kol_sell() > 0 && kk < 3)
{
for(i = 1; i <= ks; i++)
{
ii = M_os[i][0];
//----
if(!OrderClose(ii, M_os[i][2], Ask, slip, White))
{
gle = GetLastError();
kk++;
Print("Error No", gle, "with Close sell", kk);
Sleep(6000);
RefreshRates();
}
}
kk++;
}
}
kk = 0;
tic = -1;

//----
if(sob)
{
if(max_lot_b == 0.0)
lotsi = 0.1;
else
lotsi = 2.0*max_lot_b;
//----
while(tic == -1 && kk < 3)
{
tic = OrderSend(Symbol(), OP_BUY, lotsi, Ask, slip, 0, Ask + (tp + 25)*Point,
" ", m, 0, Yellow);
Print("tic_buy=", tic);
//----
if(tic==-1)
{
gle = GetLastError();
kk++;
Print("Error No", gle, "with buy", kk);
Sleep(6000);
RefreshRates();
}
}
lastt = CurTime();
return;
}
tic = -1;
kk = 0;
//----
if(sos)
{
if(max_lot_s == 0.0)
lotsi = 0.1;
else
lotsi = 2.0*max_lot_s;
//----
while(tic == -1 && kk < 3)
{
tic = OrderSend(Symbol(), OP_SELL, lotsi, Bid, slip, 0, Bid - (tp + 25)*Point,
" ", m, 0, Red);
Print("tic_sell=", tic);
//----
if(tic == -1)
{
gle = GetLastError();
kk++;
Print("Error No", gle, "with sell", kk);
Sleep(6000);
RefreshRates();
}
}
lastt = CurTime();
return;
}
}
//+------------------------------------------------------------------+
 

Are you still working on this problem? I've been looking at this EA as well and it looks promising to me. If so, please reply or email me: kevin4x@gmail.com

Take it easy.

Kevin

mindriot:
Hi Folks

I keep getting a 4051 error saying the OrderClose syntax is wrong. Can somebody help me out? What would be the syntax to get this script working properly?

invalid ticket for OrderClose function
Error No4051 with Close SELL1


 

Print out the values that you are sending with your order, as well as the bid and ask at that time, see what is going wrong.

 

As phy suggested...

11:17:04 Frank_UD EA GBPJPY,H1: loaded successfully
11:17:58 Frank_UD EA inputs: tp=65; sh=41;
11:17:58 2007.09.10 00:00 Frank_UD EA GBPJPY,H1: open #1 buy 0.10 GBPJPY at 229. 34 tp: 230.24 ok
11:17:58 2007.09.10 00:00 Frank_UD EA GBPJPY,H1: *** Lots=0.1 Ask=229.34 Slippage=0 Ask+tp+25*Point=230.24 MagicNumber=20050611 ***
11:17:58 2007.09.10 00:00 Frank_UD EA GBPJPY,H1: open #2 sell 0.10 GBPJPY at 229. 23 tp: 228.33 ok
11:17:58 2007.09.10 00:00 Frank_UD EA GBPJPY,H1: *** Lots=0.1 Bid=229.23 Slippage=0 Bid-(tp+25)*Point=228.33 MagicNumber=20050611 ***
11:17:58 2007.09.10 00:11 Frank_UD EA GBPJPY,H1: open #3 buy 0.20 GBPJPY at 228. 91 tp: 229.81 ok
11:17:58 2007.09.10 00:11 Frank_UD EA GBPJPY,H1: *** Lots=0.2 Ask=228.91 Slippage=0 Ask+tp+25*Point=229.81 MagicNumber=20050611 ***
11:17:58 2007.09.10 01:04 Frank_UD EA GBPJPY,H1: close #2 sell 0.10 GBPJPY at 229. 23 tp: 228.33 at price 228.51
11:17:58 2007.09.10 01:04 Frank_UD EA GBPJPY,H1: Error No0with Close sell1
11:17:58 2007.09.10 01:04 Frank_UD EA GBPJPY,H1: invalid ticket for OrderClose function
11:17:58 2007.09.10 01:04 Frank_UD EA GBPJPY,H1: OrderClose error 4051
11:17:58 2007.09.10 01:04 Frank_UD EA GBPJPY,H1: *** OrderClose - ii=0 M_os=0 Ask=228.51 Slippage=0 ***
11:17:58 2007.09.10 01:04 Frank_UD EA GBPJPY,H1: Error No4002with Close sell2
11:17:58 2007.09.10 01:04 Frank_UD EA GBPJPY,H1: open #4 buy 0.40 GBPJPY at 228. 49 tp: 229.39 ok
11:17:58 2007.09.10 01:04 Frank_UD EA GBPJPY,H1: *** Lots=0.4 Ask=228.49 Slippage=0 Ask+tp+25*Point=229.39 MagicNumber=20050611 ***
11:17:58 2007.09.10 01:05 Frank_UD EA GBPJPY,H1: open #5 sell 0.10 GBPJPY at 228. 41 tp: 227.51 ok
11:17:58 2007.09.10 01:05 Frank_UD EA GBPJPY,H1: *** Lots=0.1 Bid=228.41 Slippage=0 Bid-(tp+25)*Point=227.51 MagicNumber=20050611 ***
11:18:00 2007.09.10 02:01 Tester: order #5 is closed
11:18:00 2007.09.10 02:01 Tester: order #4 is closed
11:18:00 2007.09.10 02:01 Tester: order #3 is closed
11:18:00 2007.09.10 02:01 Tester: order #1 is closed

This line if(!OrderClose(ii,M_os[i][2], Ask, slip, White)) doesn't make sense, sending the close order via a not equal statement. Correct me if I'm wrong but this EA uses a 2 dimensional array in M_os[i][2].

Also which pair and timeframe does this EA work best?

 
Hello guys. I coded a 'work around' to prevent this error from happening in the EA. The error occurs when values of 0 ( ii & M_os[i][2] ) are passed to the OrderClose function. Not exactly sure why this is happening. Maybe 0 vals in the array, etc. There's a disconnect somewhere, I'm still tracking it down. Anyway, to get around the error I added the following if statement(s) to the EA. I'm not sure if the error is affecting the EA negatively either. It almost looks like it might not be. I've tested this on (5m, 15m, and 1hr) time frames. It looks to me like the sweet spot is 15m charts. . . AUDUSD, EURGBP, EURJPY, EURUSD, USDJPY. I'm still gathering data. Hopefully this helps.
if (ii != 0 && M_ob[i][0] != 0)
{
if(!OrderClose(ii, M_ob[i][2], Bid,slip, White))
{
gle = GetLastError();
kk++;
Print("Error No", gle, "with Close buy", kk);
Sleep(6000);
RefreshRates();
}
}
if (ii != 0 && M_os[i][2] != 0)
{
if(!OrderClose(ii,M_os[i][2], Ask, slip, White))
{
gle = GetLastError();
kk++;
Print("Error No", gle, "with Close sell", kk);
Sleep(6000);
RefreshRates();
}
}
 

Great work kevin, and yes the array is returning zero as per the journal above which is causing the error. Made your changes and working fine however what's with the disconnect issue?

This EA looks interesting...

 

Not sure where the disconnect is. Still looking at it. Also, I might have hit another snag. ARGHH!! When I get the code fixed / tested, I'll upload the EA.

Kevin

Superion:

Great work kevin, and yes the array is returning zero as per the journal above which is causing the error. Made your changes and working fine however what's with the disconnect issue?

This EA looks interesting...

 
mindriot:
Hi Folks

I keep getting a 4051 error saying the OrderClose syntax is wrong. Can somebody help me out? What would be the syntax to get this script working properly?

invalid ticket for OrderClose function
Error No4051 with Close SELL1






//+------------------------------------------------------------------+
//| ôðàíê_óä. mq4 |
//| Copyright © 2006, Ðàìèëü Ñàôèóëëîâè÷ Èðãèçîâ |
//| popcorn@aaanet. ru |
//+------------------------------------------------------------------+
#define m 20050611
//----
extern int tp = 65;
extern int sh = 41;
//----
datetime lastt;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int kol_buy()
{
int kol_ob = 0;
//----
for(int i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
break;
//----
if(OrderType() == OP_BUY)
kol_ob++;
}
return(kol_ob);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int kol_sell()
{
int kol_os = 0;
//----
for(int i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
break;
//----
if(OrderType() == OP_SELL)
kol_os++;
}
return(kol_os);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
int slip, i, ii, tic, total, kk, gle;
double lotsi = 0.0;
bool sob = false, sos = false, scb = false, scs = false;
int kb, kb_max = 0;
kb = kol_buy() + 1;
double M_ob[11][8];
ArrayResize(M_ob,kb);
int ks = 0, ks_max = 0;
ks = kol_sell() + 1;
double M_os[11][8];
ArrayResize(M_os,ks);
ArrayInitialize(M_ob, 0.0);
int kbi = 0;
//----
for(i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
break;
//----
if(OrderSymbol() == Symbol() && OrderType() == OP_BUY)
{
kbi++;
M_ob[kbi][0] = OrderTicket();
M_ob[kbi][1] = OrderOpenPrice();
M_ob[kbi][2] = OrderLots();
M_ob[kbi][3] = OrderType();
M_ob[kbi][4] = OrderMagicNumber();
M_ob[kbi][5] = OrderStopLoss();
M_ob[kbi][6] = OrderTakeProfit();
M_ob[kbi][7] = OrderProfit();
}
}
M_ob[0][0] = kb;
double max_lot_b = 0.0;
//----
for(i = 1; i < kb; i++)
if(M_ob[i][2] > max_lot_b)
{
max_lot_b = M_ob[i][2];
kb_max = i;
}
double buy_lev_min = M_ob[kb_max][1];
ArrayInitialize(M_os,0.0);
int ksi = 0;
//----
for(i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
break;
//----
if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
{
ksi++;
M_os[ksi][0] = OrderTicket();
M_os[ksi][1] = OrderOpenPrice();
M_os[ksi][2] = OrderLots();
M_os[ksi][3] = OrderType();
M_os[ksi][4] = OrderMagicNumber();
M_os[ksi][5] = OrderStopLoss();
M_os[ksi][6] = OrderTakeProfit();
M_os[ksi][7] = OrderProfit();
}
}
M_os[0][0] = ks;
double max_lot_s = 0.0;
//----
for(i = 1;i < ks; i++)
if(M_os[i][2] > max_lot_s)
{
max_lot_s = M_os[i][2];
ks_max = i;
}
double sell_lev_max = M_os[ks_max][1];
//----
if(Bars < 100 || IsTradeAllowed() == false)
return(0);
sob = (kol_buy() < 1 || buy_lev_min - sh*Point > Ask) &&
AccountFreeMargin() > AccountBalance()*0. 5;
sos = (kol_sell() < 1 || sell_lev_max + sh*Point < Bid) &&
AccountFreeMargin() > AccountBalance()*0. 5;
//----
if(M_ob[kb_max][2] > 0.0)
scb = M_ob[kb_max][7] / (M_ob[kb_max][2]*10) > tp;
//----
if(M_os[ks_max][2] > 0.0)
scs = M_os[ks_max][7] / (M_os[ks_max][2]*10) > tp;
kk = 0;
ii = 0;
//----
if(scb)
{
while(kol_buy() > 0 && kk < 3)
{
for(i = 1; i <= kb; i++)
{
ii = M_ob[i][0];
//----
if(!OrderClose(ii, M_ob[i][2], Bid,slip, White))
{
gle = GetLastError();
kk++;
Print("Error No", gle, "with Close buy", kk);
Sleep(6000);
RefreshRates();
}
}
kk++;
}
}
kk = 0;
ii = 0;
//----
if(scs)
{
while(kol_sell() > 0 && kk < 3)
{
for(i = 1; i <= ks; i++)
{
ii = M_os[i][0];
//----
if(!OrderClose(ii, M_os[i][2], Ask, slip, White))
{
gle = GetLastError();
kk++;
Print("Error No", gle, "with Close sell", kk);
Sleep(6000);
RefreshRates();
}
}
kk++;
}
}
kk = 0;
tic = -1;

//----
if(sob)
{
if(max_lot_b == 0.0)
lotsi = 0.1;
else
lotsi = 2.0*max_lot_b;
//----
while(tic == -1 && kk < 3)
{
tic = OrderSend(Symbol(), OP_BUY, lotsi, Ask, slip, 0, Ask + (tp + 25)*Point,
" ", m, 0, Yellow);
Print("tic_buy=", tic);
//----
if(tic==-1)
{
gle = GetLastError();
kk++;
Print("Error No", gle, "with buy", kk);
Sleep(6000);
RefreshRates();
}
}
lastt = CurTime();
return;
}
tic = -1;
kk = 0;
//----
if(sos)
{
if(max_lot_s == 0.0)
lotsi = 0.1;
else
lotsi = 2.0*max_lot_s;
//----
while(tic == -1 && kk < 3)
{
tic = OrderSend(Symbol(), OP_SELL, lotsi, Bid, slip, 0, Bid - (tp + 25)*Point,
" ", m, 0, Red);
Print("tic_sell=", tic);
//----
if(tic == -1)
{
gle = GetLastError();
kk++;
Print("Error No", gle, "with sell", kk);
Sleep(6000);
RefreshRates();
}
}
lastt = CurTime();
return;
}
}
//+------------------------------------------------------------------+
 
Hi send me the EA Thanks speedy@proi.co.za
Reason: