I ended up switching up my technique:
int GetOrdersCount(int Type = -1)
{
int count = 0;
for(int i = 0; i < OrdersTotal(); i++)
{
// already closed
if(OrderSelect(i, SELECT_BY_POS) == false) continue;
// not current symbol
if(OrderSymbol() != Symbol()) continue;
if(OrderType() == Type || Type == -1)
{
count++;
}
}
return (count);
}
int CheckTicketDistance()
{
int sticket=GetOrdersCount();
OrderSelect(sticket-1,SELECT_BY_POS);
if(TimeCurrent()-OrderOpenTime()<7200) return(1);
return(0);
}
Ticket is being selected by position so I had to subtract 1 since the pool starts at 0. Works great!
Hi mixtermind
Beware of the sequence of orders in the list of orders. There has been a bit of discussion about this recently (https://www.mql5.com/en/forum/123505) and it seems it is not guaranteed whether the most recent order is first or last in the list. Your code may work, but it may be vunerable to failing later. Also, it relies on there being no orders from other EAs, I think.
Perhaps you should add a check for a magic number.
Perhaps you could change the logic so that when an order is successful, record it in a boolean variable, e.g. bool OrderExists = True. Whenever an order is about to be placed you could check this variable to avoid repeat orders.
Cheers
Jellybean
- 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've got a system that I'm trying to program that uses the Gann Hi-Low indicator which repaints the current bar(bar[0])..Anyway, I've haven't been able to nail the crossovers because of the repaint. Instead I'm trying to put in a delay between trades so that if the bars are too close following the crossover they don't all alert. I'm using the code below but it's not working. I'm still getting orders opening before 7200 seconds (2 hours). Can somebody explain to me why this isn't working:
3 2009.09.09 01:00 buy 2 9.00 1.6524 1.6470 1.6612 0.00 10000.00
4 2009.09.09 02:00 buy 3 8.00 1.6534 1.6470 1.6642 0.00 10000.00
int ReturnFirstTicket(int Magic)
{
// Takes an input of MagicNumber
int icnt, itotal, iTicket;
itotal=OrdersTotal();
for(icnt=0;icnt<itotal;icnt++)
{ // order loop boundary
OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
// check for opened position, symbol & MagicNumber
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) // Open orders for pair on chart and the right Magic Number
{
iTicket = OrderTicket();
break;
}
} // order loop boundary
return(iTicket);
}
int start
{
int ticket=ReturnFirstTicket(Magic);
OrderSelect(0,SELECT_BY_POS);
if(TimeCurrent()-OrderOpenTime()<7200) return(0);
.............
...........................
.............
return(0);
}