for(int i = 0; i < OrdersTotal(); i++) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) if(OrderType() == OP_BUY && TrallPrice + TrallingStart * Point <= Bid && OrderMagicNumber() == Magic && OrderSymbol() == Symbol() && TrailingSwitch == 1) { res = false; res = OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrallingStop * Point, OrderTakeProfit(), Green); if(res == true) { TrallPrice = Bid ; TrailingSwitch = 2; } }
- Drop your useless variables (TrallPrice, TralingSwitch, TrallingStart)
- The word is trail or trailing; not Trall. Be consistant at least.
- Don't copy and paste code, put it one place or make a variable.
-
for(int i = OrdersTotal()-1; i >= 0; --i) if( OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && && OrderMagicNumber() == Magic && OrderSymbol() == Symbol() ){ if(OrderType() == OP_BUY){ double SL = MathMax(OrderOpenPrice(), OrderClosePrice() ) - TrallingStop * Point; if(SL > OrderStopLoss() + _Point) res = OrderModify(OrderTicket(), OrderOpenPrice(), SL, OrderTakeProfit(), Green); }
The MathMax means don't trail until BE or better. - In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
- For non-FIFO (non-US brokers), (or the EA only opens one order per symbol,) you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
Loops and Closing or Deleting Orders - MQL4 programming forum - For In First Out (FIFO rules — US brokers,) and you (potentially) process multiple orders per symbol, you must find the earliest order (count up,) close it, and on a successful operation, reprocess all positions.
CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1 № 11 ACCOUNT_FIFO_CLOSE - and check OrderSelect in case earlier positions were deleted.
What are Function return values ? How do I use them ? - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles - and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().
- For non-FIFO (non-US brokers), (or the EA only opens one order per symbol,) you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello, m8
I created my own EA.
My EA will open orders twice per signal.
And they should close with trailing stop....
When I did backtesting, they opened at the same time.. However, when they closed orders.. they did not close at the same time.. only first-order closed properly.. but the second one did not close together.. How can I fix it ??
What I would like to fix is that
ex) 01:23 1.1000 Buy Open (order number 1)
01:23 1.1000 Buy Open (order number 2)
01:24 1.1100 modify (order number 1)
01:24 1.1100 modify (order number 2)
01:26 1.1200 s/l (order number 1)
01:26 1.1200 s/l (order number 2)
However, currently, my EA showing that
ex) 01:23 1.1000 Buy Open (order number 1)
01:23 1.1000 Buy Open (order number 2)
01:24 1.1100 modify (order number 1)
01:24 1.1100 modify (order number 2)
01:26 1.1200 s/l (order number 1)
01:26 1.1200 modify (order number 2)
01:27 1.1300 modify (order number 2)
01:29 1.1400 s/l (order number 2)
Something like this.....
As you can see, open orders at the same time(23:40)... but different close and time(#1 : 1:51 ~ 1:58, then close at 2:18 | #2 : 2:26 ~ 2:39 then close at 2:47)... I want them open & close at the same time !!
These are my parts of code below
Any solutions??
Cheers,
좆같네 ㅅㅂ..