When we OrderClose
a position partially (say you buy at 2 lots but only close for 1 Lot), it will open a new ticket number, but how do we know that ticket number specifically?
Asked
Active
Viewed 1,942 times
2

william007
- 17,375
- 25
- 118
- 194
2 Answers
1
Need to loop over all tickets, your new ticket has same magic number and comment, as well as all other entry parameters. Since new ticket is open, its ticket number is larger then the closed one. As a result, the idea is to filter all open orders by symbol and magic, then find the largest ticket with same entry time as the just closed one.

user3666197
- 1
- 6
- 50
- 92

Daniel Kniaz
- 4,603
- 2
- 14
- 20
1
Script
int cou;
///////////////////////////////////////////
void OnStart()
{
ticket_search_start_f();
}
///////////////////////////////////////////
void ticket_search_start_f()
{
int tick_start;
int tick_curr;
int str_start;
for (int i=OrdersTotal()-1; i>=0; i--)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
str_start=StringFind(OrderComment(),"from #");
if(str_start!=-1)
{
tick_curr=OrderTicket();
tick_start=ticke_search_f(int(StringSubstr(OrderComment(),str_start+6)));
Alert("Ticket:"+string(tick_curr)," Start ticket:"+string(tick_start)," Parts:",cou);
}
}
}
////////////////////////////////////////////////////////////////////////
int ticke_search_f(int ticke)
{
//no order
if(!OrderSelect(ticke,SELECT_BY_TICKET)) return(-1);
//parts
int tick=ticke;
int tick_fin=ticke;
cou=1;
while(tick!=-1)
{
tick=comme_search_f(tick);
if(tick!=-1)
{
tick_fin=tick;
cou++;
}
}
//return ticket of first closed part
return(tick_fin);
}
////////////////////////////////////////////////////////
int comme_search_f(int tick_s)
{
int tick=-1;
for (int i=OrdersHistoryTotal()-1; i>=0; i--)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
if(StringFind(OrderComment(),string(tick_s))!=-1 && StringFind(OrderComment(),"from")==-1)
{
tick=OrderTicket();
break;
}
}
return(tick);
}

eevviill
- 11
- 1