Here's a really weird one

 

I'm working on a trade management EA and it works fine on tester. However, when I put it on a demo account and place it on more than one chart, the various charts try to close each other resulting in pricing errors--these are the errors:

2009.07.09 09:38:02 3A Mgr v1.2 USDCHF,M30: TP Close of GBPUSD ticket no 73318534 failed with error (129): no error
2009.07.09 09:38:02 3A Mgr v1.2 NZDUSD,M30: TP Close of GBPUSD ticket no 73318534 failed with error (129): no error
2009.07.09 09:38:01 3A Mgr v1.2 NZDUSD,M30: TP Close of GBPUSD ticket no 73318534 failed with error (129): no error
2009.07.09 09:38:00 3A Mgr v1.2 USDCHF,M30: TP Close of GBPUSD ticket no 73318534 failed with error (129): no error

Here the EA on the NZD and CHF charts are both trying to close a trade on the GBP chart. Since this is a trade management EA, I don't have a Magic Number to reference--but using standard OrderSelect(i,SELECT_BY_POS) should get me the right one--I'm lost on this one. Here's the code I'm using:

void Check_TP()
{
happy = false;
int totalorders = OrdersTotal();
for(int i=totalorders-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
bool result = false;
if ( OrderSymbol()==Symbol() )
if (OrderType() == OP_BUY && Bid >= hidden_tp)
if(OrderClose(OrderTicket(), NormalizeDouble(Close_Lots, 1), Bid, 5, Red ))
{
happy = true;
if (ShowAlerts) Alert("Profit taken on ", OrderSymbol(), " ticket no ", OrderTicket());
}
else
{
int err=GetLastError();
if (ShowAlerts)
Print("TP Close of ", OrderSymbol(), " ticket no ", OrderTicket()," failed with error (",err,"): ",ErrorDescription(err));
}
if (OrderType() == OP_SELL && Ask <= hidden_tp)
if(OrderClose(OrderTicket(), NormalizeDouble(Close_Lots, 1), Ask, 5, Red ))
{
happy = true;
if (ShowAlerts) Alert("Profit taken on ", OrderSymbol(), " ticket no ", OrderTicket());
}
else
{
int err2=GetLastError();
if (ShowAlerts)
Print("TP Close of ", OrderSymbol(), " ticket no ", OrderTicket()," failed with error (",err2,"): ",ErrorDescription(err));
}
}
return;
}//End of Check_TP Function

 

Hello jcb

", I don't have a Magic Number to reference--but using standard OrderSelect(i,SELECT_BY_POS) should get me the right one"

not true.

Trade pool is universal for Client Terminal / ALL running Experts.

The "right one" is only correct IF the EA selecting the order is the owner of that Trade pool list entry at position indexed by i

Multi EA's must know what they alone are doing and not mess with any other item which is not 100% their own.

Your code achieves exactly what one would expect to happen.

Magic #'s and/or ticket #'s are the only datums which can reliably be considered to achieve the needed compartmentalisation when >1 EA is running...

Suggest, due to your code/stated understanding, that you 'hit the books' some more...

Is fun journey and I wish you well.

 
fbj wrote >>

Hello jcb

", I don't have a Magic Number to reference--but using standard OrderSelect(i,SELECT_BY_POS) should get me the right one"

not true.

Trade pool is universal for Client Terminal / ALL running Experts.

The "right one" is only correct IF the EA selecting the order is the owner of that Trade pool list entry at position indexed by i

Multi EA's must know what they alone are doing and not mess with any other item which is not 100% their own.

Your code achieves exactly what one would expect to happen.

Magic #'s and/or ticket #'s are the only datums which can reliably be considered to achieve the needed compartmentalisation when >1 EA is running...

Suggest, due to your code/stated understanding, that you 'hit the books' some more...

Is fun journey and I wish you well.

Thanks fbj--I appreciate the info. However, I have been "hitting" whatever books I can find and not finding much that is definitive along the way. Do you have any suggestions as to well written and informative books/sources where I can learn these things? This forum is the greatest and has helped whenever I can't find an answer but I would love to be able to have some sort of reference material that explains a lot of the oittle gotchas like that.

Once again--thanks for the answer.

 
jcbwrites wrote >>

Thanks fbj--I appreciate the info. However, I have been "hitting" whatever books I can find and not finding much that is definitive along the way. Do you have any suggestions as to well written and informative books/sources where I can learn these things? This forum is the greatest and has helped whenever I can't find an answer but I would love to be able to have some sort of reference material that explains a lot of the oittle gotchas like that.

Once again--thanks for the answer.

JCB,

While I don't have suggestions on what books or sources are any better than this website, I can show you a code snippet that has worked well for me. This is from an EA running concurrently on six currency pairs allowing a maximum of 1 buy and 1 sell trade on each pair. It may not be the most efficient way to write the routine but it's never failed and it's easy to understand.

orderCount=OrdersTotal();
for(x=0; x<orderCount; x++)
{
orderFound=OrderSelect(x,SELECT_BY_POS,MODE_TRADES);

if(orderFound
&& OrderSymbol() == currPair
&& OrderMagicNumber() == _magicNbr)
{
if(OrderType()==OP_BUY) { currTktBuy=OrderTicket(); }
if(OrderType()==OP_SELL) { currTktSell=OrderTicket(); }
}
}