Closing all orders EA?

 

Hi,

I've been working on a system and I'm in need of an EA that will close all orders (active and pending) as soon as a profit target is hit (lets say 30 pips, or an $ value). I'll be running a set of EAs for my testing so this EA should close all orders regardless of magic number.

Anyone seen such an EA?

Thanks a lot.

 

extern int profit=30; //profit USD

int mgc=1234;

double pft;

int total=OrdersTotal();

for(int cnt=0;cnt<total;cnt++) {

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderMagicNumber()==mgc) pft=pft+OrderProfit();

}

if(pft>profit) {

for(cnt=0;cnt<total;cnt++) {

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderMagicNumber()==mgc) {

if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),Bid,3,White); else

if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,3,White); else

OrderDelete(OrderTicket());

}

}

}

 

This EA seems to work fine for me in closing all orders regardless of the magic number, active and pending, when the profit level is reached.

The default profit value is $30. You can set it to any $ value you wish externally.

Files:
 

Closing EA

The EAs in this thread work well too

https://www.mql5.com/en/forum/176207

John

 

Thanks a million guys!

 

equity %

is it possible to add % of equity in addition to $ or pip values?..

 
 

Hi,

Quite interesting but how could this EA define that it need to CLOSE for OPEN BUY or to CLOSE OPEN SELL POSITION . What I means is this EA should CLOSE OPEN BUY or OPEN SELL POSITION ?

Thank you.

 

Hi,

Quite interesting but how could this EA Code define which one it should CLOSE.

OPEN BUY POSITION or OPEN SELL POSITION.

Thank you.

 

Not Sure, I requested this EA but I do not code myself. I have been using this and it seems to work ok. This just closes a trade when the EAs cross. thx

 

Hi,fxgroup.

>Hi,

>Quite interesting but how could this EA Code define which one it should >CLOSE.

>OPEN BUY POSITION or OPEN SELL POSITION.

>

>Thank you.

I seem that this EA will close both OPEN BUY and OPEN SELL.

And it will close all of the same pair.

exp. EUR/USD 15M :This EA EUR/USD 4H : manual BUY

I guess this EA will close both of them.

but may be I misunderstanding.

Reason: