Hi, I am relatively new to coding EAs so i apologize if my code is sloppy and if the solution to my problem is basic. However, i have been struggling for weeks, i want my EA to open a buy and a sell on initialization and then as soon as one of them takes profit open another buy and sell and then when a sell for example takes profit i want it to close the previous sell at the open price which will be this sell's take profit, if that makes sense. But what my code is doing is it works for the first 2 trades and then stops opening new orders and if it does then it doesn't close any orders. Can someone please help?
In MT4, when you need to retrieve closed orders - use OrdersHistoryTotal() instead of OrdersTotal().
- Please edit
your (original) post and use the CODE button
(Alt-S)! (For large amounts of code, attach it.)
General rules and best pratices of the Forum. - General - MQL5 programming forum
Messages Editor -
Why did you post your MT4 question in the
Root /
MT5 EA section
instead of the MQL4 section,
(bottom of the Root page?)
General rules and best pratices of the Forum. - General - MQL5 programming forum
Next time post in the correct place. The moderators will likely move this thread there soon. //---->>>>>Open New Orders After TakeProfit<<<<<----\\ for (int b=OrdersTotal()-1;b>=0;b--){ if (OrderSelect(b,SELECT_BY_POS,MODE_HISTORY))
There are not OrdersTotal in history.if (OrderSelect(b,SELECT_BY_POS,MODE_HISTORY) && OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol() && OrderType()==OP_BUY) && OrderClosePrice()>OrderOpenPrice()) if (OrderCloseTime()==TimeCurrent())
An order closed in profit (ignoring Swap and Commission.) Don't you want to be looking at the last closed order? What if you don't get a tick the exact same second when the order closed?for (int bb=OrdersTotal()-1;bb>=0;bb--){ if (OrderSelect(bb,SELECT_BY_POS,MODE_TRADES)) while((OrderType()==OP_BUY) && (OrderSymbol()==Symbol()) && OrderOpenTime()<LastBuy && OrderCloseTime()==0)
You just selected a current order and it's a buy. The OrderCloseTime will always be zero.- Your loop never selects a new order, therefor your while condition never changes.
In MT4, when you need to retrieve closed orders - use OrdersHistoryTotal() instead of OrdersTotal().
@NewTrader20
- Please edit
your (original) post and use the CODE button
(Alt-S)! (For large amounts of code, attach it.)
General rules and best pratices of the Forum. - General - MQL5 programming forum
Messages Editor -
Why did you post your MT4 question in the
Root /
MT5 EA section
instead of the MQL4 section,
(bottom of the Root page?)
General rules and best pratices of the Forum. - General - MQL5 programming forum
Next time post in the correct place. The moderators will likely move this thread there soon. - There are not OrdersTotal in history.
- An order closed in profit (ignoring Swap and Commission.) Don't you want to be looking at the last closed order? What if you don't get a tick the exact same second when the order closed?
- You just selected a current order and it's a buy. The OrderCloseTime will always be zero.
- Your loop never selects a new order, therefor your while condition never changes.
1. As i said im new to this.
2. Sorry didn't because again as i said im new to this.
3. i know that now thanks.
4. No im looking for the last order closed in profit (dont know if that is what you meant) and i do not know how to take Swap and Commission into account. I am still learning all this, if you would be kind enough to show me id appreciate it
5. That is true, I missed that
6. Cool thanks ill change that
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi, I am relatively new to coding EAs so i apologize if my code is sloppy and if the solution to my problem is basic. However, i have been struggling for weeks, i want my EA to open a buy and a sell on initialization and then as soon as one of them takes profit open another buy and sell and then when a sell for example takes profit i want it to close the previous sell at the open price which will be this sell's take profit, if that makes sense. But what my code is doing is it works for the first 2 trades and then stops opening new orders and if it does then it doesn't close any orders. Can someone please help?
My Code:
}