modyifing orders

 

hi guys , im writing function to move stop loss to break even and close percentage of the order im geting errors in the ordermodify and orderclose opreator when i print the resuilts in journal i get the orderlot()=0.0 before i even issue the orderclose commandm any help will be apriciated thanks

void be_cl(int percentage){
int i;
for(i=OrdersTotal()-1; i>=0; i--){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
  {
   if(OrderSymbol()==Symbol() && OrderType()==OP_BUY)
     {
      if(Bid-OrderOpenPrice()>(OrderOpenPrice()-OrderStopLoss()))
        {
      OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Blue);
      Print("Moving to break even");
      Print("closing ",OrderLots()*(percentage/100));
      OrderClose(OrderTicket(),OrderLots()*(percentage/100),Bid,3,clrYellow);
       }
     }
    if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
     {
      if((OrderStopLoss()-OrderOpenPrice())<OrderOpenPrice()-Ask)
        {
      OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Blue);
      Print("Moving to break even");
      Print("closing ",OrderLots()*(percentage/100));
      OrderClose(OrderTicket(),OrderLots()*(percentage/100),Ask,3,clrYellow);
      }
     }
  }
}
}

the journel output

2020.04.26 02:07:55.701 2020.04.01 01:00:00  MACD_EMA AUDUSD,M30: OrderModify error 1
2020.04.26 02:07:55.701 2020.04.01 00:30:00  MACD_EMA AUDUSD,M30: OrderClose error 4051
2020.04.26 02:07:55.701 2020.04.01 00:30:00  MACD_EMA AUDUSD,M30: invalid lots number for OrderClose function
2020.04.26 02:07:55.701 2020.04.01 00:30:00  MACD_EMA AUDUSD,M30: closing 0.0
2020.04.26 02:07:55.701 2020.04.01 00:30:00  MACD_EMA AUDUSD,M30: Moving to break even
 
mo798ua: im writing function to move stop loss to break even and close percentage of the order 
  1. First problem is not closing multiple times. Move to BE before closing so you know you already did it.
  2.       OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Blue);
    What do you think that line does? ERR_NO_RESULT
    You Server
    Change the SL to X It is at X!
    Change the SL to X It is at X!
    Change the SL to X You are insane
    Insanity: doing the same thing over and over again and expecting different results.
              Unknown
    Compute the new value, then check that you are moving the existing value at least a tick. PIP, Point, or Tick are all different in general.
              What is a TICK? - MQL4 programming forum

  3. OrderClose(OrderTicket(),OrderLots()*(percentage/100),Ask,3,clrYellow);
    You can't just use OrderLots()/2 because that is not a multiple of LotStep, and you can't close or have remaining less than MinLot. See my code.
  4. You also must check if you have already done it, to avoid repeated closing.
 
William Roeder:
  1. First problem is not closing multiple times. Move to BE before closing so you know you already did it.
  2. What do you think that line does? ERR_NO_RESULT
    You Server
    Change the SL to X It is at X!
    Change the SL to X It is at X!
    Change the SL to X You are insane
    Compute the new value, then check that you are moving the existing value at least a tick. PIP, Point, or Tick are all different in general.
              What is a TICK? - MQL4 programming forum

  3. You can't just use OrderLots()/2 because that is not a multiple of LotStep, and you can't close or have remaining less than MinLot. See my code.
  4. You also must check if you have already done it, to avoid repeated closing.

your answer is so confusing are you sure you read my code i moves sl to be before closing , im not doing OrderLots/2 im using OrderLots*percentage*100, i got your idea of cheacking wether i already modified or closed partially the order but how i go a long with that is ther a built function to cheackthat??

 
if(Bid-OrderOpenPrice()>(OrderOpenPrice()-OrderStopLoss()))

Think about what this condition outputs once the SL has been moved to BE.


void be_cl(int percentage)
//
Print("closing ",OrderLots()*(percentage/100));

percentage and 100 are both ints and so the result of the division will be an int, as it will be less than 1, it will be 0.

Change your code to

Print("closing ",OrderLots()*(percentage/100.0));

And take note of what William posted.

You can't just use OrderLots()/2 because that is not a multiple of LotStep, and you can't close or have remaining less than MinLot. See my code.

the 2 is not important as it applies to any multiplication or division of OrderLots().

 
Keith Watford:

Think about what this condition outputs once the SL has been moved to BE.


percentage and 100 are both ints and so the result of the division will be an int, as it will be less than 1, it will be 0.

Change your code to

Ok so let me see if get your point, when multiplying with percent*100 it converts to int , so i need to make to change it to 100.0 so it be double , right?
 
mo798ua:
Ok so let me see if get your point, when multiplying with percent*100 it converts to int , so i need to make to change it to 100.0 so it be double , right?

Yes

Reason: