Hello,
I am not an Empty4 coder but I've got a code related question that I am hoping to resolve.
If I've got an EA that hedges, is there a way to program it so that when the EA closes the hedge that it closes the positions in profit first? The reason I ask this is that if a trade is in a massive hedge and the EA decides to close the positions that are in negative pips first, there is a possibility that it could blow and account before realizing that there was an overcoming amount of profit on the positive side of the hedge.
Thanks for your attention.
Daniel
Hedging - Close open positions in profit first
- keydcuk
- Trader
- Posts: 104
- Joined: Tue Dec 04, 2012 8:01 pm
- SteveHopwood
- Owner
- Posts: 9904
- Joined: Tue Nov 15, 2011 8:43 am
- Location: Misterton - an insignificant village in England. Very pleasant to live in.
Hedging - Close open positions in profit first
You can use my FIFO code, assuming that the oldest trades are likely to be the most profitable. Sing out if you need any help.

Read the effing manual, ok?
Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.
I still suffer from OCCD. Good thing, really.
Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.
To see The Weekly Roundup of stuff you guys might have missed Click here
My special thanks to Thomas (tomele) for all the incredible work he does here.
Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.
I still suffer from OCCD. Good thing, really.
Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.
To see The Weekly Roundup of stuff you guys might have missed Click here
My special thanks to Thomas (tomele) for all the incredible work he does here.
- keydcuk
- Trader
- Posts: 104
- Joined: Tue Dec 04, 2012 8:01 pm
Hedging - Close open positions in profit first
Hi Steve,SteveHopwood » Sun Mar 20, 2016 12:46 pm wrote:You can use my FIFO code, assuming that the oldest trades are likely to be the most profitable. Sing out if you need any help.
Thanks for your reply.
FIFO might solve part of my problem as if it is in a hedge, it might close some sells and some buys in an alternate fashion of some sort rather than choosing to close all the buys first or all the sells first, but ideally I would like the EA to identify that once the basket TP has been hit, that it closes the profitables first before the losers.
If there is no solution to this, where might I find your FIFO code?
Thanks,
Daniel
- SteveHopwood
- Owner
- Posts: 9904
- Joined: Tue Nov 15, 2011 8:43 am
- Location: Misterton - an insignificant village in England. Very pleasant to live in.
Hedging - Close open positions in profit first
Yes, you are correct. The biggest losers will also close first, alternating with the biggest winners.
There is a solution and I will add it to GW - I am going to reopen that thread later. I will add it now and point you to the correct line numbers so you can adapt it to your code.

There is a solution and I will add it to GW - I am going to reopen that thread later. I will add it now and point you to the correct line numbers so you can adapt it to your code.
Read the effing manual, ok?
Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.
I still suffer from OCCD. Good thing, really.
Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.
To see The Weekly Roundup of stuff you guys might have missed Click here
My special thanks to Thomas (tomele) for all the incredible work he does here.
Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.
I still suffer from OCCD. Good thing, really.
Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.
To see The Weekly Roundup of stuff you guys might have missed Click here
My special thanks to Thomas (tomele) for all the incredible work he does here.
- keydcuk
- Trader
- Posts: 104
- Joined: Tue Dec 04, 2012 8:01 pm
Hedging - Close open positions in profit first
Thanks Steve,SteveHopwood » Sun Mar 20, 2016 1:43 pm wrote:Yes, you are correct. The biggest losers will also close first, alternating with the biggest winners.
There is a solution and I will add it to GW - I am going to reopen that thread later. I will add it now and point you to the correct line numbers so you can adapt it to your code.
I try not to be a pain to you. I do enjoy this forum website and am only trying to contribute in a positive way. It is not my intentions to upset you. I do get you when you talk about the filtering of the posts etc... I will try to be more switched on to what is appropriate to post as well as where to post.
Thanks for being so reasonable and forgiving with me.
Daniel
- SteveHopwood
- Owner
- Posts: 9904
- Joined: Tue Nov 15, 2011 8:43 am
- Location: Misterton - an insignificant village in England. Very pleasant to live in.
Hedging - Close open positions in profit first
This version will not actually work on accounts whose broker demands true FIFO, but those traders are not using the ea anyhow because they cannot hedge.
My Fifo stuff works by storing ticket numbers in an array. Here we need two of them as we want the profitable side of the position closing first, so:

My Fifo stuff works by storing ticket numbers in an array. Here we need two of them as we want the profitable side of the position closing first, so:
- 143-146 declare two arrays, one for OP_BUY.... and one for OP_SELL... order types.
- 2295-5 initialises them to zero length.
- 2328+ resizes the relevant array and stores the order ticket in the new element.
- 2476+ sorts the arrays so that the tickets are saved in descending order, and so are saved in the order they were sent.
- 1201+ is a modified version of the original hedge position take profit closure. It assumes that the sell side is losing if the buy side is in profit, and so closes the buys first. Vice versa if the sell side is in profit.
- 2074 is void CloseAllTrades(int type). This needed modifying so it would use the appropriate array.
Code: Select all
void CountOpenTrades()
{
//Not all these will be needed. Which ones are depends on the individual EA.
int OpenTrades = 0;
//FIFO ticket resize
ArrayResize(FifoTicketBuys, 0);
ArrayResize(FifoTicketSells, 0);
if (OrdersTotal() == 0) return;
//Iterating backwards through the orders list caters more easily for closed trades than iterating forwards
for (int cc = OrdersTotal() - 1; cc >= 0; cc--)
{
//Ensure the trade is still open
if (!OrderSelect(cc, SELECT_BY_POS, MODE_TRADES) ) continue;
//Ensure the EA 'owns' this trade
if (OrderSymbol() != Symbol() ) continue;
if (OrderMagicNumber() != MagicNumber) continue;
if (OrderCloseTime() > 0) continue;
//All conditions passed, so carry on
OpenTrades++;
//Store ticket numbers for FIFO
if (OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP)
{
ArrayResize(FifoTicketBuys, OpenTrades + 1);
FifoTicketBuys[OpenTrades] = OrderTicket();
}//if (OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP)
else
{
ArrayResize(FifoTicketSells, OpenTrades + 1);
FifoTicketSells[OpenTrades] = OrderTicket();
}//else
}//for (int cc = OrdersTotal() - 1; cc >= 0; cc--)
}//End void CountOpenTrades();
You do not have the required permissions to view the files attached to this post.
Read the effing manual, ok?
Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.
I still suffer from OCCD. Good thing, really.
Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.
To see The Weekly Roundup of stuff you guys might have missed Click here
My special thanks to Thomas (tomele) for all the incredible work he does here.
Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.
I still suffer from OCCD. Good thing, really.
Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.
To see The Weekly Roundup of stuff you guys might have missed Click here
My special thanks to Thomas (tomele) for all the incredible work he does here.
- keydcuk
- Trader
- Posts: 104
- Joined: Tue Dec 04, 2012 8:01 pm
Hedging - Close open positions in profit first
Brilliant!
Thanks for your help Steve. I genuinely appreciate it.
Daniel
Thanks for your help Steve. I genuinely appreciate it.
Daniel
- pascalx
- Trader
- Posts: 20
- Joined: Mon Mar 28, 2016 2:40 pm
Hedging - Close open positions in profit first
Managing your buys and sells in a container is paramount. I would advice to reduce calls to ArrayResize() though. I don't know its implementation, but it is very likely that it will allocate memory every time. If you plan on running the EA for long time, I suggest to allocate as much as possible on initialization and reduce allocation on runtime. You definitely want to avoid memory fragmentation. It will also benefit performance.
- hopfi2k
- Trader
- Posts: 35
- Joined: Tue Nov 15, 2011 11:03 pm
- Location: Germany
Hedging - Close open positions in profit first
pascalx,
while you are right on ArrayResize(). I nevertheless would like to point out to the documentations of recent MQL4 version (>900), that support a third parameter reserved_size preventing most of the in memory allocations and fragmentation: https://docs.mql4.com/array/arrayresize.
Cheers,
Andy
while you are right on ArrayResize(). I nevertheless would like to point out to the documentations of recent MQL4 version (>900), that support a third parameter reserved_size preventing most of the in memory allocations and fragmentation: https://docs.mql4.com/array/arrayresize.
Cheers,
Andy
- pascalx
- Trader
- Posts: 20
- Joined: Mon Mar 28, 2016 2:40 pm
Hedging - Close open positions in profit first
I know. My container classes make use of that too to reduce allocations.