Where are you testing for “when the first open order TP is hit?”
//+------------------------------------------------------------------+ void OnTimer() { if(_StopFlag || !IsConnected()) return; UpdatePanel(); if(!NewBarD) { if(BarTimeD < iTime(NULL, PERIOD_D1, 0)) { NewBarD = true; BarTimeD = iTime(NULL, PERIOD_D1, 0); } } datetime dayStart = int(TimeCurrent() / 86400) * 86400; for(int i = OrdersHistoryTotal() - 1; i >= 0; i--) { if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) return; if(OrderOpenTime() < dayStart) break; if(OrderType() != OP_BUY && OrderType() != OP_SELL) continue; if(OrderMagicNumber() != Magic) continue; GlobalVariableSet(OrderSymbol() + "_stoptrading" + (string)Magic, TimeDayOfYear(TimeCurrent())); } bool checked; if(ATR_TP1_CloseTrades) { for(int i = OrdersTotal() - 1; i >= 0; i--) { if(OrderSelect(i, SELECT_BY_POS)) { if(OrderMagicNumber() != Magic) continue; string symbol = OrderSymbol(); if(GlobalVariableGet(OrderSymbol() + "_stoptrading" + (string)Magic) == TimeDayOfYear(TimeCurrent())) { if(OrderType() == OP_BUY) { checked = OrderClose(OrderTicket(), OrderLots(), MarketInfo(symbol, MODE_BID), 0); } else if(OrderType() == OP_SELL) { checked = OrderClose(OrderTicket(), OrderLots(), MarketInfo(symbol, MODE_ASK), 0); } else { checked = OrderDelete(OrderTicket()); } } } } }
Please see now...
Hi, I need some help please... I would like my EA to close all open orders when the first open order TP is hit - for magic number, symbol...
The code below is closing all trades when any SL / TP hit.
It's pretty simple. Assign a custom code to the comment field or a different magicnumber to this first order. After that, if you don't have this magic number, you can close all orders. Or if there is no special code in the comment section, it's like close all orders.
1. Loop through OrderHistory and see if any MagicNumber trade closed because of TP.
2. If so, close all other orders with the MagicNumber?
-
if(!NewBarD) { if(BarTimeD < iTime(NULL, PERIOD_D1, 0)) { NewBarD = true; BarTimeD = iTime(NULL, PERIOD_D1, 0); } }
Once set true, will always be true.
MT4: New candle - MQL4 programming forum #3 (2014)
MT5: Accessing variables - MQL4 programming forum #3 -
Use not equal. There was a post around 2002, where a server clock was mis-set and then corrected — stopped the EA for days!
- Where do you use it?
Prefer positive logic over negative for(int i = OrdersHistoryTotal() - 1; i >= 0; i--) { if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) return; if(OrderOpenTime() < dayStart) break; if(OrderType() != OP_BUY && OrderType() != OP_SELL) continue; if(OrderMagicNumber() != Magic) continue;
Easier to understand for(int i = OrdersHistoryTotal() - 1; i >= 0; i--) if( OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && OrderMagicNumber() == Magic && OrderOpenTime() >= dayStart && OrderType() <= OP_SELL ){
-
MT4:
-
Do not assume history has only closed orders.
OrderType() == 6, 7 in the history pool? - MQL4 programming forum (2017) -
Do not assume history is ordered by date, it's not.
Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020)
-
-
if(OrderType() == OP_BUY) { checked = OrderClose(OrderTicket(), OrderLots(), MarketInfo(symbol, MODE_BID), 0); } else if(OrderType() == OP_SELL) { checked = OrderClose(OrderTicket(), OrderLots(), MarketInfo(symbol, MODE_ASK), 0);
MT4: You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type to get the close price.
- katiz22: I would like my EA to close all open orders when the first open order TP is hit - for magic number, symbol...You set the GV when you find a closed order, opened today. Let me ask the question a third time:
The code below is closing all trades when any SL / TP hit.
William Roeder #: Where are you testing for “when the first open order TP is hit?”Even andrew implied the same question:
andrew #: Loop through OrderHistory and see if any MagicNumber trade closed because of TP.
- Once set true, will always be true.
MT4: New candle - MQL4 programming forum #3 (2014)
MT5: Accessing variables - MQL4 programming forum #3 -
Use not equal. There was a post around 2002, where a server clock was mis-set and then corrected — stopped the EA for days!
Thanks for this - I was having this issue at times with EA not working but couldnt understand I will review.
- 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 need some help please... I would like my EA to close all open orders when the first open order TP is hit - for magic number, symbol...
The code below is closing all trades when any SL / TP hit.