Restarting EA with current open trade

 

Is there anything special I need to do with an EA when restarting if there is a current trade open on the currency pair?

 
dwmcqueen:
Is there anything special I need to do with an EA when restarting if there is a current trade open on the currency pair?

It's the responsibility of the EA to check for open orders and re-initialise any variables that may need updating. So yes, a lot needs to be done........

 

Is there any 'template code' that accomplishes at least 90% of this?

 
dwmcqueen:
Is there any 'template code' that accomplishes at least 90% of this?

It depends on the complexity of the EA. You will at least have to scan all open trades, locate the trade with your magic number, get it's trade ticket and check if the SL and TP variables need updating...........

 

My EA is basic - just needs to know if trade is there. No trailing stop or anything.

I have language like this:

int trade;

for(trade=OrdersTotal()-1;trade>=0;trade--)

{

if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)==false)

continue;

if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)

continue;

if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)

if(OrderType()==OP_BUY)

OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Blue);

}//for

Will that catch previously opened trades?

 

The following when called will detect the number of open and pending orders.

int CountTrades()

{

int count=0;

int trade;

for(trade=OrdersTotal()-1;trade>=0;trade--)

{

OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);

if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)

continue;

if(OrderSymbol()==Symbol() && OrderMagicNumber()== MagicNumber)

{

if((OrderType()==OP_SELL) || (OrderType()==OP_SELLSTOP) || (OrderType()==OP_SELLLIMIT) ||

(OrderType()==OP_BUY) || (OrderType()==OP_BUYSTOP) || (OrderType()==OP_BUYLIMIT))

count++;

}

}//for

return(count);

}

Reason: