Have you looked up the meaning of 4051:Invalid function parameter
value. So try to change the values of OrderModify(..), like price, SL, and profit.
Check the minimal difference (MODE_STOPLEVEL and MODE_FREEZELEVEL)
Maybe instead of price use either OrderOpenPrice() or 0 as you don't change a pending order, ....Hi gooly,
There is another section in my code where I modify the initial Sl and TP values for a recently opened trade.
Might it be possible that the error is being generated THERE and that the sentence Error=GetLastError() is receiving the Error generated on that part of the code?
BTW, thanks for your answer , I will check the parameters' values on both OrderModify funtions.
best regards. JHF
So then why is the EA showing "Occurred error 4051"?
That is the last error that happened . . . it may be completely somewhere else in your code . . . and completely unrelated to your Trailing Stop function, if you want to check this then at the start of your Trailing Stop function add . . . ResetLastError();
Part of the issue is that your trailing stop function returns False even if there was no failure . . . then you go looking for an error . . . why ? you need to fix this design flaw.
That is the last error that happened . . . it may be completely somewhere else in your code . . . and completely unrelated to your Trailing Stop function, if you want to check this then at the start of your Trailing Stop function add . . . ResetLastError();
Part of the issue is that your trailing stop function returns False even if there was no failure . . . then you go looking for an error . . . why ? you need to fix this design flaw.
Hi everyone,
I am getting the same kind of error message but with openselect function (OrderSelect returned error of 4051):
Can anyone help please?
Here is copy of my journal:
2014.12.08 15:25:41.375 2014.03.25 00:00 Testing_loop_v1 EURGBP-,Daily: OrderSelect returned error of 4051
2014.12.08 15:25:41.375 2014.03.25 00:00 Testing_loop_v1 EURGBP-,Daily: Type : 1 - Position : 2 Total Trades : 3
2014.12.08 15:25:41.375 2014.03.25 00:00 Testing_loop_v1 EURGBP-,Daily: Type : 1 - Position : 1 Total Trades : 3
2014.12.08 15:25:41.375 2014.03.25 00:00 Testing_loop_v1 EURGBP-,Daily: Type : 0 - Position : 0 Total Trades : 3
2014.12.08 15:25:41.375 2014.03.25 00:00 Testing_loop_v1 EURGBP-,Daily: open #6 sell 0.10 EURGBP- at 0.83859 tp: 0.83429 ok
2014.12.08 15:25:41.375 2014.03.25 00:00 Testing_loop_v1 EURGBP-,Daily: Alert: ### -- New BAR (UP) -- ### : 4
2014.12.08 15:25:41.093 2014.03.24 00:00 Testing_loop_v1 EURGBP-,Daily: OrderSelect returned error of 4051
2014.12.08 15:25:41.093 2014.03.24 00:00 Testing_loop_v1 EURGBP-,Daily: Type : 1 - Position : 1 Total Trades : 2
014.12.08 15:25:41.093 2014.03.24 00:00 Testing_loop_v1 EURGBP-,Daily: Type : 0 - Position : 0 Total Trades : 2
2014.12.08 15:25:41.093 2014.03.24 00:00 Testing_loop_v1 EURGBP-,Daily: open #5 sell 0.10 EURGBP- at 0.83601 tp: 0.83421 ok
2014.12.08 15:25:41.093 2014.03.24 00:00 Testing_loop_v1 EURGBP-,Daily: Alert: ### -- New BAR (UP) -- ### : 3
2014.12.08 15:25:40.906 2014.03.21 10:38 Tester: take profit #4 at 0.83650 (0.83650 / 0.83657)
2014.12.08 15:25:40.843 2014.03.21 00:00 Testing_loop_v1 EURGBP-,Daily: OrderSelect returned error of 4051
2014.12.08 15:25:40.843 2014.03.21 00:00 Testing_loop_v1 EURGBP-,Daily: Type : 0 - Position : 1 Total Trades : 2
2014.12.08 15:25:40.843 2014.03.21 00:00 Testing_loop_v1 EURGBP-,Daily: Type : 0 - Position : 0 Total Trades : 2
2014.12.08 15:25:40.843 2014.03.21 00:00 Testing_loop_v1 EURGBP-,Daily: open #4 buy 0.10 EURGBP- at 0.83475 tp: 0.83650 ok
2014.12.08 15:25:40.843 2014.03.21 00:00 Testing_loop_v1 EURGBP-,Daily: Alert: ### -- New BAR (DOWN) -- ### : 2
2014.12.08 15:25:40.656 2014.03.20 13:47 Tester: take profit #1 at 0.83415 (0.83408 / 0.83415)
2014.12.08 15:25:40.500 2014.03.20 00:00 Testing_loop_v1 EURGBP-,Daily: OrderSelect returned error of 4051
2014.12.08 15:25:40.500 2014.03.20 00:00 Testing_loop_v1 EURGBP-,Daily: Type : 0 - Position : 1 Total Trades : 2
2014.12.08 15:25:40.500 2014.03.20 00:00 Testing_loop_v1 EURGBP-,Daily: Type : 1 - Position : 0 Total Trades : 2
2014.12.08 15:25:40.500 2014.03.20 00:00 Testing_loop_v1 EURGBP-,Daily: open #3 buy 0.10 EURGBP- at 0.83625 tp: 0.83969 ok
2014.12.08 15:25:40.500 2014.03.20 00:00 Testing_loop_v1 EURGBP-,Daily: Alert: ### -- New BAR (DOWN) -- ### : 1
2014.12.08 15:25:40.437 2014.03.19 22:26 Tester: take profit #2 at 0.83607 (0.83600 / 0.83607)
2014.12.08 15:25:40.125 2014.03.19 00:00 Testing_loop_v1 EURGBP-,Daily: OrderSelect returned error of 4051
2014.12.08 15:25:40.125 2014.03.19 00:00 Testing_loop_v1 EURGBP-,Daily: Type : 1 - Position : 1 Total Trades : 2
2014.12.08 15:25:40.125 2014.03.19 00:00 Testing_loop_v1 EURGBP-,Daily: Type : 1 - Position : 0 Total Trades : 2
2014.12.08 15:25:40.125 2014.03.19 00:00 Testing_loop_v1 EURGBP-,Daily: open #2 sell 0.10 EURGBP- at 0.83967 tp: 0.83607 ok
2014.12.08 15:25:40.125 2014.03.19 00:00 Testing_loop_v1 EURGBP-,Daily: Alert: ### -- New BAR (UP) -- ### : 2
2014.12.08 15:25:36.718 2014.03.18 00:00 Testing_loop_v1 EURGBP-,Daily: OrderSelect returned error of 4051
2014.12.08 15:25:36.718 2014.03.18 00:00 Testing_loop_v1 EURGBP-,Daily: Type : 1 - Position : 0 Total Trades : 1
2014.12.08 15:25:36.718 2014.03.18 00:00 Testing_loop_v1 EURGBP-,Daily: open #1 sell 0.10 EURGBP- at 0.83672 tp: 0.83415 ok
2014.12.08 15:25:36.718 2014.03.18 00:00 Testing_loop_v1 EURGBP-,Daily: Alert: ### -- New BAR (UP) -- ### : 1
And here is part my code:
v_total_trades = OrdersTotal();
for(int pos=0;pos<=v_cnt;pos++)
{
v_total_trades = OrdersTotal();
if(OrderSelect(pos, SELECT_BY_POS)==true)
{
// I am just displaying a message for now so I can find the problem,
Print("Type : ", OrderType() + " - Position : ", pos + " Total Trades : " + v_total_trades);
}
else
Print("OrderSelect returned error of ",GetLastError());
} // End of FOR Loop
-
Play videoPlease edit your post.
For large amounts of code, attach it.
for(i=0; i<=OrdersTotal(); i++) // Cycle searching in orders { if(OrderSelect(i,SELECT_BY_POS)==true)
There are OrdersTotal() count of orders. Order positions are [0 .. OrdersTotal()-1] // I am just displaying a message for now so I can find the problem, Print("Type : ", OrderType() + " - Position : ", pos + " Total Trades : " + v_total_trades); } else Print("OrderSelect returned error of ",GetLastError());
You print pos and v_total on success. Why don't you print them on failure so you find out why?
-
Play videoPlease edit your post.
For large amounts of code, attach it.
There are OrdersTotal() count of orders. Order positions are [0 .. OrdersTotal()-1] - You print pos and v_total on success. Why don't you print them on failure so you find out why?
v_cnt = 0; v_cnt = OrdersTotal(); for(int pos=0;pos<=v_cnt;pos++) { v_total_trades = OrdersTotal(); if(OrderSelect(pos, SELECT_BY_POS)==true) { //if (your_modify_order_condition && OrdersTotal!=0) // This way he just modifies orders that are still open. Print("On Success: - Type : ", OrderType() + " - Position : ", pos + " Total Trades : " + v_total_trades); } else Print("OrderSelect returned error of ",GetLastError()); Print("On Failure: - Type : ", OrderType() + " - Position : ", pos + " Total Trades : " + v_total_trades); } // End of FOR Loop
Thank you for the reply..
Here is the journal after updating to code to include the values on failure and success:
2014.12.09 13:56:15.734 2014.03.20 00:00 Testing_loop_v1 EURGBP-,Daily: OrderSelect returned error of 40512014.12.09 13:56:12.234 Testing_loop_v1 inputs: BuyLots=0.1; SellLots=0.1; Barsize_min=20; Barsize_max=60; Spread=10; Take_profit_level=100; high_low=1;
1) form a habit to count down OrdersTotal()!!
v_cnt = OrdersTotal(); for(int pos=v_cnt-1;pos>=0;pos--) { ...
2) E.g. TotalOrders() = 4; Please count now how many order do you request in your code if you loop from pos=0 to pos<=4.
- 2014.12.09 13:56:15.468 2014.03.19 00:00 Testing_loop_v1 EURGBP-,Daily: Failure: - Type : 0 - Position : 2 Total Trades : 2Asked and previously answered
2014.12.09 13:56:15.468 2014.03.19 00:00 Testing_loop_v1 EURGBP-,Daily: OrderSelect returned error of 4051There are OrdersTotal() count of orders. Order positions are [0 .. OrdersTotal()-1]There are two trades, at positions zero and one. There is no position 2 - You must count down when closing/deleting in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum

- 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 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