- Trading Principles - Trade - MetaTrader 5 for iPhone
- Trading Principles - Trade - MetaTrader 5 for Android
- Exchange Execution - Opening and Closing Positions - Trade - MetaTrader 5 for iPhone
Looks like your EA is placing multiple pending orders for each bar close instead of just one pending order per signal.
If you want to have only one position opened at a time, you need to check if there are positions already opened for that symbol/EA. You could create a function like this:
int HasPosition(string symbol, ulong ea_magic) { int total_positions = PositionsTotal(); // get the total opened positions for(int i = 0; i < total_positions; ++i) // loop through the list { ulong ticket = PositionGetTicket(i); // get the ticket of the position at index i ulong position_magic = PositionGetInteger(POSITION_MAGIC); // get the ea_magic of the position at index i if(position_magic == ea_magic) // if the position magic matches our ea magic { string position_symbol = PositionGetString(POSITION_SYMBOL); if(position_symbol == symbol) // if the position symbol matches our symbol { ENUM_POSITION_TYPE pos_type = PositionGetInteger(POSITION_TYPE); // get the position direction if(pos_type == POSITION_TYPE_BUY) { return 1; // if it is a long position, return 1 } else if (pos_type == POSITION_TYPE_SELL) { return 2; // if it is a short position, return 2 } } } } return 0; // if there're no positions for the current symbol/ea magic, return 0 }
For example, if this function returns 0, there're no positions opened for the current symbol/ea, so you could create a logic to open a new one at this point.
Also, there're a few errors to be corrected and improvements to be done with your code. Note that you're calling int ma_handle = iMA(Symbol(), 0, InpEMAPeriod, 0, MODE_EMA, PRICE_CLOSE); every tick. This is fundamentally wrong - you only need one handle of an indicator in the EA. Also, you're not releasing this handle, causing a huge memory leak and performance issues. Store the handle in a global variable and call iMA inside the OnInit function, then just copy the values to the buffer.
ps: this code was not tested.
if (price > ema[0])
You are looking at a signal (opening one order per tick). Act on a change of signal.
Too many orders - MQL4 programming forum #1 (2017)
Hi
Also, you can create ma handle on the init function, handles should be created once and here you repeat this on every new candle.
As for the multiple trades: you can check change of signal only (as William Roeder wrote: so compare current price and last price with current ema and last ema) or simply check if there are already opened trades in the market (only check signal if there are no other trades opened yet – but here you limit the number of trades to maximum one at the same time – and I’m not sure if this is what you want).
Best Regards
If you want to have only one position opened at a time, you need to check if there are positions already opened for that symbol/EA. You could create a function like this:
For example, if this function returns 0, there're no positions opened for the current symbol/ea, so you could create a logic to open a new one at this point.
Also, there're a few errors to be corrected and improvements to be done with your code. Note that you're calling int ma_handle = iMA(Symbol(), 0, InpEMAPeriod, 0, MODE_EMA, PRICE_CLOSE); every tick. This is fundamentally wrong - you only need one handle of an indicator in the EA. Also, you're not releasing this handle, causing a huge memory leak and performance issues. Store the handle in a global variable and call iMA inside the OnInit function, then just copy the values to the buffer.
ps: this code was not tested.
Using PositionsTotal won't work for this specific problem since they are operating with pending orders. OrdersTotal() would solve the issue though
oh, yeah, i misunderstood the problem, sry. i thought it was opening multiple positions at a time. The logic still serves, though, because only checking if there's a pending order may cause the EA to actually detect placed orders to other EAs/symbols.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use