i compile an ea to detect orders opened and closed, so i set two arrays to record the order's feature, and compare the
difference between them,and can know if an order is open or closed .but i cannot get right result .
Please post your code using the SRC option (just above where you type your message)
Using . . .
OrderSelect(cnt1, SELECT_BY_POS, MODE_TRADES);
. . . will never select Closed orders
MODE_TRADES (default)- order selected from trading pool(opened and pending orders),MODE_HISTORY - order selected from history pool (closed and cancelled order).
i compile an ea to detect orders opened and closed
- Detect open orders
for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if ( OrderSelect(pos, SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == magic.number // my magic number && OrderSymbol() == Symbol() ){ // and my pair.
- Detect newly closed orders
static datetime lastClose; datetime lastClosePrev = lastClose; for(int pos=0; pos < OrdersHistoryTotal(); pos++) if ( OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY) // Only orders w/ && OrderCloseTime() > lastClosePrev // not yet processed, && OrderMagicNumber() == magic.number // my magic number && OrderSymbol() == Symbol() // and my pair. && OrderType() <= OP_SELL){// Avoid cr/bal forum.mql4.com/32363#325360 lastClose = OrderCloseTime();
Please post your code using the SRC option (just above where you type your message)
Using . . .
. . . will never select Closed orders
MODE_TRADES (default)- order selected from trading pool(opened and pending orders),MODE_HISTORY - order selected from history pool (closed and cancelled order).
thank you
but i can not change code using SRC option,
i check orders opened or closed just compare two status of opening orders,maybe it is not efficient method .
thank you
but i can not change code using SRC option,
You can if you use the edit option at the top right of your post . . . and do this . . .
int torders=0; int data0[50][3]; int data1[50][3]; int tticket=0; int ssymbol=0; int ttype=0; int mmagic=0; string isymbol=""; int init() { for (int cnt0 = 0; cnt0 <50; cnt0++) { data0[cnt0][0]=0; data0[cnt0][1]=0; data0[cnt0][2]=0; data1[cnt0][0]=0; data1[cnt0][1]=0; data1[cnt0][2]=0; } return(0); } int deinit() { return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { torders=OrdersTotal(); if(torders>0) { for (int cnt = 0; cnt <50; cnt++) { data1[cnt][0]=data0[cnt][0];data1[cnt][1]=data0[cnt][1];data1[cnt][2]=data0[cnt][2]; } for (int cnt1 = 0; cnt1 < torders; cnt1++) { OrderSelect(cnt1, SELECT_BY_POS, MODE_TRADES); tticket=OrderTicket(); isymbol=OrderSymbol(); ttype=OrderType(); mmagic=OrderMagicNumber(); ssymbol=100; if(isymbol== "EURUSD") ssymbol=1; if(isymbol== "GBPUSD") ssymbol=2; data0[cnt1][0]=ssymbol; data0[cnt1][1]=ttype; data0[cnt1][2]=tticket; } //////open///////////////////////// for (int cnt2 = 0; cnt2 < 50; cnt2++) { int xxx=0; for (int cnt3 = 0; cnt3 < 50; cnt3++) { if(data0[cnt2][2]!=data1[cnt3][2]) {xxx++;} } if(xxx==50 && data0[cnt2][2]>0) { Print("order open");} } ///////close/////////////////////////////// for (int cnt4 = 0; cnt4 < 50; cnt4++) { int yyy=0; for (int cnt5 = 0; cnt5 < 50; cnt5++) { if(data1[cnt4][2] != data0[cnt5][2]) yyy++; } if(yyy==50 && data1[cnt4][2]>0) { Print("order closed"); } } ////////////////////////////////////////////////////// } return(0); } //+------------------------------------------------------------------+
to select Open or Pending trades do this . . .
OrderSelect(cnt1, SELECT_BY_POS, MODE_TRADES);
to select closed or cancelled trades do this . . .
OrderSelect(cnt1, SELECT_BY_POS, MODE_HISTORY);
thanks to all
may i do it through my method?
if it has mathical erro ?
i use my method to avoid visit MT4 sever frequrenly,so i can get something from my method .
thanks to all may i do it through my method? if it has mathical erro ? i use my method to avoid visit MT4 sever frequrenly,so i can get something from my method .
I have told you what your problem is, WHRoeder has given you some code to use . . . if you choose to ignore us both that is fine . . . . but at least read and understand this thoroughly.
https://docs.mql4.com/trading/OrderSelect or this https://docs.mql4.com/ru/trading/OrderSelect
- Detect open orders
- Detect newly closed orders
very nice,i have tested it .
thanks
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
i compile an ea to detect orders opened and closed, so i set two arrays to record the order's feature, and compare the
difference between them,and can know if an order is open or closed .but i cannot get right result .
int torders=0;
int data0[50][3];
int data1[50][3];
int tticket=0; int ssymbol=0; int ttype=0; int mmagic=0; string isymbol="";
int init()
{
for (int cnt0 = 0; cnt0 <50; cnt0++)
{
data0[cnt0][0]=0; data0[cnt0][1]=0; data0[cnt0][2]=0;
data1[cnt0][0]=0; data1[cnt0][1]=0; data1[cnt0][2]=0;
}
return(0);
}
int deinit() { return(0); }
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
torders=OrdersTotal();
if(torders>0)
{
for (int cnt = 0; cnt <50; cnt++)
{
data1[cnt][0]=data0[cnt][0];data1[cnt][1]=data0[cnt][1];data1[cnt][2]=data0[cnt][2];
}
for (int cnt1 = 0; cnt1 < torders; cnt1++)
{
OrderSelect(cnt1, SELECT_BY_POS, MODE_TRADES);
tticket=OrderTicket(); isymbol=OrderSymbol(); ttype=OrderType(); mmagic=OrderMagicNumber();
ssymbol=100;
if(isymbol== "EURUSD") ssymbol=1;
if(isymbol== "GBPUSD") ssymbol=2;
data0[cnt1][0]=ssymbol; data0[cnt1][1]=ttype; data0[cnt1][2]=tticket;
}
//////open/////////////////////////
for (int cnt2 = 0; cnt2 < 50; cnt2++)
{
int xxx=0;
for (int cnt3 = 0; cnt3 < 50; cnt3++)
{
if(data0[cnt2][2]!=data1[cnt3][2]) {xxx++;}
}
if(xxx==50 && data0[cnt2][2]>0) { Print("order open");}
}
///////close///////////////////////////////
for (int cnt4 = 0; cnt4 < 50; cnt4++)
{
int yyy=0;
for (int cnt5 = 0; cnt5 < 50; cnt5++)
{
if(data1[cnt4][2] != data0[cnt5][2]) yyy++;
}
if(yyy==50 && data1[cnt4][2]>0) { Print("order closed"); }
}
//////////////////////////////////////////////////////
}
return(0);
}
//+------------------------------------------------------------------+
when i open one order,i can see many print inform,"order opened"
and i never closed it,i can see "order closed"
thanks