Can't avoid OrderModify error 1, even if parameters change!

 

Hi guys!

I've already read topics in this forum about this error and implemented my code.

However, I can't understand why error 1 is still presented, even though occasionally.

Here is my code (I use two ticket: "0" to identify buystop and "1" to sellstop)


void TrailingPendingOrder()

   {

         if(OrdersTotal() > 1){

            double valEntry, valSL, valTP;

            

            //BuyStop

            valEntry = Bid + pipsPendingDist*Point*p;

            valSL = valEntry - pipsPendingSL*Point*p;

            valTP = valEntry + pipsPendingTP*Point*p;

            

            if(OrderSelect(ticket[0],SELECT_BY_TICKET) && valEntry != OrderOpenPrice()){  //isn't it enough?

               if(!OrderModify(ticket[0], valEntry, valSL, valTP, 0, clrYellow)){

                     Print("Error TPO for Buy, ", GetLastError());

               }

            }

               

            //SellStop

            valEntry = Ask - pipsPendingDist*Point*p;

            valSL = valEntry + pipsPendingSL*Point*p;

            valTP = valEntry - pipsPendingTP*Point*p;

            

            if(OrderSelect(ticket[1],SELECT_BY_TICKET) && valEntry != OrderOpenPrice()){  //isn't it enough?

               if(!OrderModify(ticket[1], valEntry, valSL, valTP, 0, clrYellow)){

                     Print("Error TPO for Sell, ", GetLastError());

               }

            }

         }

      }
 

That is a no result "error"

Order open price can not be changed, hence you do not need to check that at all. Compare sl and tp prior to attempt to change them -  if they are the same no need for order modify (and hence there should not be error 1 any more)

 
Mladen Rakic:

That is a no result "error"

Order open price can not be changed, hence you do not need to check that at all. Compare sl and tp prior to attempt to change them -  if they are the same no need for order modify (and hence there should not be error 1 any more)


Thanks Mladen

a question..

if I  modify a buystop, changing the price i want it to be triggered..

does it don't generate a new open price?

example:

buy stop: open price 1,2345

than order modify

modified buy stop: open price 1,2350

?

 
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. You are selecting by ticket. You need to also check if the order is open (vs pending) and/or closed.

  3. //BuyStop
    valEntry = Bid + pipsPendingDist*Point*p;
    valSL = valEntry - pipsPendingSL*Point*p;
    
    //SellStop
    valEntry = Ask - pipsPendingDist*Point*p;
    valSL = valEntry + pipsPendingSL*Point*p;
    You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3
    • The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)

 
whroeder1:
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. You are selecting by ticket. You need to also check if the order is open (vs pending) and/or closed.

  3. You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3
    • The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)

Thanks whroeder1

i've changed my code, but the error persists.

ea knows that if ordertotal > 1, the 2 orders are pending (OCO deletion if one stop order is triggered)

Do you have any other idea of why it happens?

void TrailingPendingOrder()
   {
         if(OrdersTotal() > 1){
            double valEntry, valSL, valTP;
            
            //BuyStop
            valEntry = Ask + pipsPendingDist*Point*p;
            valSL = Bid + pipsPendingDist*Point*p - pipsPendingSL*Point*p;
            valTP = Bid + pipsPendingDist*Point*p + pipsPendingTP*Point*p;
            
            if(OrderSelect(ticket[0],SELECT_BY_TICKET) && valEntry != OrderOpenPrice()){
               if(!OrderModify(ticket[0], valEntry, valSL, valTP, 0, clrYellow)){
                     Print("Error TPO for Buy, ", GetLastError());
               }
            }
               
            //SellStop
             valEntry = Bid - pipsPendingDist*Point*p;
             valSL = Ask - pipsPendingDist*Point*p + pipsPendingSL*Point*p;
             valTP = Ask - pipsPendingDist*Point*p - pipsPendingTP*Point*p;
            
            if(OrderSelect(ticket[1],SELECT_BY_TICKET) && valEntry != OrderOpenPrice()){
               if(!OrderModify(ticket[1], valEntry, valSL, valTP, 0, clrYellow)){
                     Print("Error TPO for Sell, ", GetLastError());
               }
            }
         }
   }
 

What part of "You need to also check if the order is open" was unclear?

What part of "Order open price can not be changed, hence you do not need to check that at all" was unclear?

 
whroeder1:

What part of "You need to also check if the order is open" was unclear?

What part of "Order open price can not be changed, hence you do not need to check that at all" was unclear?

1 - if an order is open, TrailingPendingOrder function is not executed, because OrdersTotal must be more than 1 (clearly I can't have more than 2 pending orders on the chart at the same time) and the OCO deletion function immediately delete the other not-opened order (you can't see here the oco function, but I swear, I have coded it and it works well)..


2- I don't understand why open price can't be changed, as I asked previously to Mladen. 

if I move a buy stop from a price to another, before being triggered (obviously), the second open price is not different from the first?


3 - OrderModify error 1 happens occasionaly. Most of the times the OrderModify function is called, it does its job. 


I really just want to understand where I'm wrong, be patient :D

Anyway, I made a try following your suggestions. Is that right?

(I'm backtesting on a m1 chart, could it be the reason why? unclean data?)


void TrailingPendingOrder()
   {
         if(OrdersTotal() > 1){
            double valEntry, valSL, valTP;
            
            //BuyStop
            valEntry = Ask + pipsPendingDist*Point*p;
            valSL = Bid + pipsPendingDist*Point*p - pipsPendingSL*Point*p;
            valTP = Bid + pipsPendingDist*Point*p + pipsPendingTP*Point*p;
            
            if(OrderSelect(ticket[0],SELECT_BY_TICKET) && OrderProfit() == 0 && valSL != OrderStopLoss()){
               if(!OrderModify(ticket[0], valEntry, valSL, valTP, 0, clrYellow)){
                     Print("Error TPO for Buy, ", GetLastError());
               }
            }
               
            //SellStop
             valEntry = Bid - pipsPendingDist*Point*p;
             valSL = Ask - pipsPendingDist*Point*p + pipsPendingSL*Point*p;
             valTP = Ask - pipsPendingDist*Point*p - pipsPendingTP*Point*p;
            
            if(OrderSelect(ticket[1],SELECT_BY_TICKET) && OrderProfit() == 0 && valSL != OrderStopLoss()){
               if(!OrderModify(ticket[1], valEntry, valSL, valTP, 0, clrYellow)){
                     Print("Error TPO for Sell, ", GetLastError());
               }
            }
         }
   }
 
PierFX:

1 - if an order is open, TrailingPendingOrder function is not executed, because OrdersTotal must be more than 1 (clearly I can't have more than 2 pending orders on the chart at the same time) and the OCO deletion function immediately delete the other not-opened order


2- I don't understand why open price can't be changed, as I asked previously to Mladen.  if I move a buy stop from a price to another, before being triggered (obviously), the second open price is not different from the first?

  1. You open two pending orders, OrdersTotal is two. One opens, OrdersTotal is two. You close the pending order (OCO,) OrdersTotal is one.
  2. You can only move pending order prices. Once the order opens, that is the price bought it at. You can move the buy stop price all you want, you haven't yet bought it. You can't buy milk at $2 and then tomorrow say I'd rather buy that milk for $1. You already bought it yesterday.
 
whroeder1:
  1. You open two pending orders, OrdersTotal is two. One opens, OrdersTotal is two. You close the pending order (OCO,) OrdersTotal is one.
  2. You can only move pending order prices. Once the order opens, that is the price bought it at. You can't buy milk at $2 and then tomorrow say I'd rather buy that milk for $1. You already bought it.

1- in the code, OCO is called before TrailingPO. Hence..

I open two pending orders, OrdersTotal is two. One opens, OCO delete the other one. TrailingPO is called, but OrdersTotal is not > 1. TrailingPO is not executed. Am I wrong?

2- OrderOpenPrice function returns only the price of open orders? Or even of pending orders?

 

PierFX:

1 Am I wrong?

2- OrderOpenPrice function returns only the price of open orders? Or even of pending orders?

    1. You have to make up your mind.
      PierFX: OrdersTotal must be more than 1
      PierFX: but OrdersTotal is not > 1.

      There are no mind readers here.

    2. How are we supposed to know

      PierFX: but OrdersTotal is not > 1. TrailingPO is not executed.

      We can't see code you don't post.

    3. Your code breaks the moment you open any other order (other EAs, other charts, manual trading.)
      Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
                Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

  1. Both, just as OrdersTotal returns the total count (open or pending.)
 
whroeder1:
  1. You have to make up your mind.

    There are no mind readers here.

  2. Both, just as OrdersTotal returns the total count (open or pending.)

Don't be rude, I'm trying to understand, sincerely. And I appreciate your help!

try to read again my first posted code

1- when OrdersTotal is more than one, TrailingPendingOrders is executed entirely, when is not more than one, not. (but i've got the error message when i have two pending orders on the chart!)

----1a - the error message is inside the condition if(OrdersTotal() > 1), and not at the same level..so when the error is printed (as shown in the example attached), the condition OrdersTotal() > is true, that is to say...I still have two pending orders.

2- why i can't use OrderOpenPrice function in my first posted code as check, considering that I'm using TrailingPendingOrders function only when i have two pending orders?


I've got the previous misunderstanding..

I wrote: "1- in the code, OCO is called before TrailingPO. Hence..I open two pending orders, OrdersTotal is two. One opens, OCO delete the other one. TrailingPO is called, but OrdersTotal is not > 1. TrailingPO is not executed. Am I wrong?"

The right sequence should be: I open two pending orders, OrdersTotal is two. OCO is called, but no open orders, so it is not executed. TrailingPO is called, the two pending orders should be modified accordingly to the price (but sometimes it doesn't happen - error message here). One pending order finally opens, OCO delete the other one. TrailingPO is called again, but OrdersTotal is not > 1. TrailingPO is not executed now. Am I wrong?


attached, the error message in the meta tester journal (it is just an example)

Files:
Reason: