Can´t find the reason why OrderModify Error 4051 is happening - page 2

 

Thank you guys for the quick replies..

 I have change the FOR loop to:

  v_cnt = OrdersTotal();

for(int pos=v_cnt-1;pos>=0;pos--) { 

And results changed with no errors.. and got the selected order correctly :)

2014.12.09 16:31:23.640 2014.03.28 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 0 - Position : 0 Total Trades : 4
2014.12.09 16:31:23.640 2014.03.28 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 0 - Position : 1 Total Trades : 4
2014.12.09 16:31:23.640 2014.03.28 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 0 - Position : 2 Total Trades : 4
2014.12.09 16:31:23.640 2014.03.28 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 0 - Position : 3 Total Trades : 4
2014.12.09 16:31:23.640 2014.03.28 00:00  TESTIN~1 EURGBP-,Daily: open #9 buy 0.10 EURGBP- at 0.82709 tp: 0.83184 ok
2014.12.09 16:31:23.640 2014.03.28 00:00  TESTIN~1 EURGBP-,Daily: Alert:              ###   --  New BAR  (DOWN) --   ### : 5
2014.12.09 16:31:23.375 2014.03.27 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 0 - Position : 0 Total Trades : 3
2014.12.09 16:31:23.375 2014.03.27 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 0 - Position : 1 Total Trades : 3
2014.12.09 16:31:23.375 2014.03.27 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 0 - Position : 2 Total Trades : 3
2014.12.09 16:31:23.375 2014.03.27 00:00  TESTIN~1 EURGBP-,Daily: open #8 buy 0.10 EURGBP- at 0.83115 tp: 0.83627 ok
2014.12.09 16:31:23.375 2014.03.27 00:00  TESTIN~1 EURGBP-,Daily: Alert:              ###   --  New BAR  (DOWN) --   ### : 4
2014.12.09 16:31:23.125 2014.03.26 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 0 - Position : 0 Total Trades : 2
2014.12.09 16:31:23.125 2014.03.26 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 0 - Position : 1 Total Trades : 2
2014.12.09 16:31:23.125 2014.03.26 00:00  TESTIN~1 EURGBP-,Daily: open #7 buy 0.10 EURGBP- at 0.83639 tp: 0.83903 ok
2014.12.09 16:31:23.125 2014.03.26 00:00  TESTIN~1 EURGBP-,Daily: Alert:              ###   --  New BAR  (DOWN) --   ### : 3
2014.12.09 16:31:22.984 2014.03.25 16:12  Tester: take profit #5 at 0.83421 (0.83415 / 0.83421)
2014.12.09 16:31:22.984 2014.03.25 16:11  Tester: take profit #6 at 0.83429 (0.83423 / 0.83429)
2014.12.09 16:31:22.796 2014.03.25 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 0 - Position : 0 Total Trades : 3
2014.12.09 16:31:22.796 2014.03.25 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 1 - Position : 1 Total Trades : 3
2014.12.09 16:31:22.796 2014.03.25 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 1 - Position : 2 Total Trades : 3
2014.12.09 16:31:22.796 2014.03.25 00:00  TESTIN~1 EURGBP-,Daily: open #6 sell 0.10 EURGBP- at 0.83859 tp: 0.83429 ok
2014.12.09 16:31:22.796 2014.03.25 00:00  TESTIN~1 EURGBP-,Daily: Alert:           ###   --  New BAR (UP) --   ### : 4
2014.12.09 16:31:22.515 2014.03.24 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 0 - Position : 0 Total Trades : 2
2014.12.09 16:31:22.515 2014.03.24 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 1 - Position : 1 Total Trades : 2
2014.12.09 16:31:22.515 2014.03.24 00:00  TESTIN~1 EURGBP-,Daily: open #5 sell 0.10 EURGBP- at 0.83601 tp: 0.83421 ok
2014.12.09 16:31:22.515 2014.03.24 00:00  TESTIN~1 EURGBP-,Daily: Alert:           ###   --  New BAR (UP) --   ### : 3
2014.12.09 16:31:22.328 2014.03.21 10:38  Tester: take profit #4 at 0.83649 (0.83649 / 0.83655)
2014.12.09 16:31:22.265 2014.03.21 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 0 - Position : 0 Total Trades : 2
2014.12.09 16:31:22.265 2014.03.21 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 0 - Position : 1 Total Trades : 2
2014.12.09 16:31:22.265 2014.03.21 00:00  TESTIN~1 EURGBP-,Daily: open #4 buy 0.10 EURGBP- at 0.83474 tp: 0.83649 ok
2014.12.09 16:31:22.265 2014.03.21 00:00  TESTIN~1 EURGBP-,Daily: Alert:              ###   --  New BAR  (DOWN) --   ### : 2
2014.12.09 16:31:22.078 2014.03.20 13:47  Tester: take profit #1 at 0.83415 (0.83409 / 0.83415)
2014.12.09 16:31:21.921 2014.03.20 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 1 - Position : 0 Total Trades : 2
2014.12.09 16:31:21.921 2014.03.20 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 0 - Position : 1 Total Trades : 2
2014.12.09 16:31:21.921 2014.03.20 00:00  TESTIN~1 EURGBP-,Daily: open #3 buy 0.10 EURGBP- at 0.83624 tp: 0.83968 ok
2014.12.09 16:31:21.921 2014.03.20 00:00  TESTIN~1 EURGBP-,Daily: Alert:              ###   --  New BAR  (DOWN) --   ### : 1
2014.12.09 16:31:21.875 2014.03.19 22:26  Tester: take profit #2 at 0.83607 (0.83601 / 0.83607)
2014.12.09 16:31:21.546 2014.03.19 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 1 - Position : 0 Total Trades : 2
2014.12.09 16:31:21.546 2014.03.19 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 1 - Position : 1 Total Trades : 2
2014.12.09 16:31:21.546 2014.03.19 00:00  TESTIN~1 EURGBP-,Daily: open #2 sell 0.10 EURGBP- at 0.83967 tp: 0.83607 ok
2014.12.09 16:31:21.546 2014.03.19 00:00  TESTIN~1 EURGBP-,Daily: Alert:           ###   --  New BAR (UP) --   ### : 2
2014.12.09 16:28:48.109 2014.03.18 00:00  TESTIN~1 EURGBP-,Daily: On Success: - Type : 1 - Position : 0 Total Trades : 1
2014.12.09 16:28:48.109 2014.03.18 00:00  TESTIN~1 EURGBP-,Daily: open #1 sell 0.10 EURGBP- at 0.83672 tp: 0.83415 ok
2014.12.09 16:28:48.109 2014.03.18 00:00  TESTIN~1 EURGBP-,Daily: Alert:           ###   --  New BAR (UP) --   ### : 1
2014.12.09 16:28:48.093 TESTIN~1 inputs: BuyLots=0.1; SellLots=0.1; Barsize_min=20; Barsize_max=60; Spread=10; Take_profit_level=100; high_low=1; 

 

I am wondering if I can use the same for loop for an inner FOR loops inside the Main FOR loop, one FOR loop for buy orders and one FOR loop for sell orders (All inside the main loop)..

So that I can modify the takeprofit level depending on market conditions that I set.

The main issue that I am going to be having, how to identify the closed orders while still going through the main loop?

Let say I have 5 orders open, and by the time the code is going through the second order the 4th order got closed because simply it reached takeprofit level, now the total order has changed to 4 order instead of 5. So the order count is not in sync anymore with the orderselect!

Simply I just want to modify the orders that are still open. 

 

Kind regards,

 

I have same invalit ticket for OrderModify function : OrderModify erro 4051 

Guys anyone can help .....?

 
billy9663:

  OrderSelect(ticket1,SELECT_BY_POS);

  1. 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 2019.05.06
              Messages Editor

  2. A ticket is not a position. Make up your mind how you are selecting orders.

    Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum 2012.05.20
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 25 March 2014

  3. 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 and order count:

    1. 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
    2. 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
 
William Roeder:
  1. 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 2019.05.06
              Messages Editor

  2. A ticket is not a position. Make up your mind how you are selecting orders.

    Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum 2012.05.20
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 25 March 2014

  3. 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 and order count:

    1. 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
    2. 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
Tq Bro 
 
Roberto Dasso:

Hi guys,

I just can´t find why my EA is showing this error when I use the Trailing Stop option.

I am attaching here the code where I call the function Trailing Stop and then the function itself

After that I am showing the messsages from the Alerts That I have inserted in the code

In short, The StopLoss of the Selected Order is the same as the new Stop Loss that the trailing Stop is calculating

If I am right, I have specifically coded that in such case the EA will not run the OrderModify function

But the Error 4051 is happening even though the OrderModify function is not being called

Well, at least that is my deduction

Will someone please check what is wrong with my code?

This is the Trailing Stop function

so, after I check that Modify==false, there is a break instruction, so the EA should get out of the while(true) loop and should NOT execute the OrderModify instruction, but somehow it is beeing called and if my new SL is equal to the OrderStopLoss then error 4051 is understandable. But why is the OrderModify instruction being executed, will someone please explain? 

here are the messages when I backtested the EA:

 What I see here is that the new SL (variable SL) is 1.3418 and the OrderStopLoss (variable loss) is exactly 1.3418

That is why the condition if(SL<loss-pips2dbl) under the case 1: (sell) is not met.

That is why the message "Modify=false" is being showed

That is why the Answer (A)=false is being showed

So then why is the EA showing "Occurred error 4051"?

Maybe the int Error=GetLastError is not being initialized ? So it is showing this error just because it was the last error and not because this error is happening over and over? 

Thanks in advance for your help

Hi I was having this error and commented this line of my code : //OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderTakeProfit(), 0, Blue);.. try I hope this help, I'm Not an expert

Reason: