returning ticket number of the nth position...

 

Hi Everyone!


I am trying to create a script that will produce the ticket number given the nth sequence number of a long or short position (in the current open trades). When I put BuyPositionTicket(10), I want it to return the 10th buy position that was opened. While when I put in SellPositionTicket(10), I want the 10th sell position to be returned. I've written the script below to do just that, but I need you guys to help me figure out where or why it's not working. I printed blue the part of the script where I think the problem is. Also please let me know if there is a better/easier way to accomplish the task. Thanks in advanced and happy trading!! =)






extern int MagicNo = 27;

//this determines the BUY sequence given a ticket number
double BuyTicketPosition(double TicketNum)
{
double TPos=1;
for(int k=OrdersTotal()-1;k>=0;k--)
{
if(OrderSelect(k,SELECT_BY_POS,MODE_TRADES)== true && OrderMagicNumber() == MagicNo && OrderType() == OP_BUY)
{
if(OrderTicket()<TicketNum)
{
TPos = TPos+1;
}
}
}
return(TPos);
}

//this determines the BUY sequence given a ticket number
double SellTicketPosition(double TicketNum)
{
double TPos=1;
for(int k=OrdersTotal()-1;k>=0;k--)
{
if(OrderSelect(k,SELECT_BY_POS,MODE_TRADES)== true && OrderMagicNumber() == MagicNo && OrderType() == OP_SELL)
{
if(OrderTicket()<TicketNum)
{
TPos = TPos+1;
}
}
}
return(TPos);
}

//this determines the ticket number given a BUY sequence
double BuyPositionTicket(double PosNum)
{
for(int k=OrdersTotal()-1;k>=0;k--)
{
OrderSelect(k,SELECT_BY_POS,MODE_TRADES);
if(OrderMagicNumber() == MagicNo && OrderType() == OP_BUY && PosNum == BuyTicketPosition(OrderTicket()))return(OrderTicket());
else return(0);
}
}

//this determines the ticket number given a SELL sequence
double SellPositionTicket(double PosNum)
{
for(int k=OrdersTotal()-1;k>=0;k--)
{
OrderSelect(k,SELECT_BY_POS,MODE_TRADES);
if(OrderMagicNumber() == MagicNo && OrderType() == OP_SELL && PosNum == SellTicketPosition(OrderTicket()))return(OrderTicket());
else return(0);
}
}
void start()
{
Comment( "BUY seq1: ",BuyPositionTicket(1),
"\nSELL seq1: ",SellPositionTicket(1),
"\nBUY seq2: ",BuyPositionTicket(2),
"\nSELL seq2: ",SellPositionTicket(2),
"\nBUY seq3: ",BuyPositionTicket(3),
"\nSELL seq3: ",SellPositionTicket(3),
);
}

 
heyarn:

Hi Everyone!


I am trying to create a script that will produce the ticket number given the nth sequence number of a long or short position (in the current open trades). When I put BuyPositionTicket(10), I want it to return the 10th buy position that was opened. While when I put in SellPositionTicket(10), I want the 10th sell position to be returned. I've written the script below to do just that, but I need you guys to help me figure out where or why it's not working. I printed blue the part of the script where I think the problem is. Also please let me know if there is a better/easier way to accomplish the task. Thanks in advanced and happy trading!! =)






extern int MagicNo = 27;

//this determines the BUY sequence given a ticket number
double BuyTicketPosition(double TicketNum)
{
double TPos=1;
for(int k=OrdersTotal()-1;k>=0;k--)
{
if(OrderSelect(k,SELECT_BY_POS,MODE_TRADES)== true && OrderMagicNumber() == MagicNo && OrderType() == OP_BUY)
{
if(OrderTicket()<TicketNum)
{
TPos = TPos+1;
}
}
}
return(TPos);
}

//this determines the BUY sequence given a ticket number
double SellTicketPosition(double TicketNum)
{
double TPos=1;
for(int k=OrdersTotal()-1;k>=0;k--)
{
if(OrderSelect(k,SELECT_BY_POS,MODE_TRADES)== true && OrderMagicNumber() == MagicNo && OrderType() == OP_SELL)
{
if(OrderTicket()<TicketNum)
{
TPos = TPos+1;
}
}
}
return(TPos);
}

//this determines the ticket number given a BUY sequence
double BuyPositionTicket(double PosNum)
{
for(int k=OrdersTotal()-1;k>=0;k--)
{
OrderSelect(k,SELECT_BY_POS,MODE_TRADES);
if(OrderMagicNumber() == MagicNo && OrderType() == OP_BUY && PosNum == BuyTicketPosition(OrderTicket()))return(OrderTicket());
else return(0);
}
}

//this determines the ticket number given a SELL sequence
double SellPositionTicket(double PosNum)
{
for(int k=OrdersTotal()-1;k>=0;k--)
{
OrderSelect(k,SELECT_BY_POS,MODE_TRADES);
if(OrderMagicNumber() == MagicNo && OrderType() == OP_SELL && PosNum == SellTicketPosition(OrderTicket()))return(OrderTicket());
else return(0);
}
}
void start()
{
Comment( "BUY seq1: ",BuyPositionTicket(1),
"\nSELL seq1: ",SellPositionTicket(1),
"\nBUY seq2: ",BuyPositionTicket(2),
"\nSELL seq2: ",SellPositionTicket(2),
"\nBUY seq3: ",BuyPositionTicket(3),
"\nSELL seq3: ",SellPositionTicket(3),
);
}

A little help? Anyone? Please? I just want it to return the ticket number of the nth buy or sell position..

 

Collect your open buy and sell ticket numbers

put them in two arrays buy[] and sell[]

sort the arrays by ticket number

the ticket in index 9 should be your "10th" ticket

 

What do you mean by nth?

Do you mean:

-nth after a certain date/time?

-nth from the start of history?

-nth most recent?


CB

 
cloudbreaker:

What do you mean by nth?

Do you mean:

-nth after a certain date/time?

-nth from the start of history?

-nth most recent?


CB

Hi,


Sorry for my late reply! I meant chronologically from all the currently open positions. (if n = 2, then the ticket number would be the 2nd smallest number).. It would be great if you could point me in the right direction =)

 
phy:

Collect your open buy and sell ticket numbers

put them in two arrays buy[] and sell[]

sort the arrays by ticket number

the ticket in index 9 should be your "10th" ticket

Phy,


Thanks for your input.. I looked in the help file for arrays, but might need some time to grasp it and figure out how to sort them. Would it be possible for you to give me a short example if you already have one. I learn well from breaking things apart then putting them back together. Thanks again! =)


Arn

 

Don't use ticket numbers to find anything. Use ticket numbers to do something to a trade only *after* you have found your trade. If you want to target specific trades, use their positions in the list.

Jon