why is my OrderStopLoss and OrderTakeProfit set to zero?

 

Hi, I have the following code..

orderS = OrderSelect(buyTicket,SELECT_BY_TICKET);
bCount++;                             // B2
bPrice[bCount] = OrderOpenPrice();    // bPrice[2]
bTicket[bCount] = buyTicket;          // bTicket[2]
buyAvgPrice = (bPrice[1] + bPrice[2]) / 2;
orderM = OrderModify(buyTicket, OrderOpenPrice(), OrderStopLoss(), 2 * buyAvgPrice - sPrice[1] + (takeProfitPips + 30) * Point, 0, Green);
                   
// Adjust take profit and stop loss levels for other deals
orderS = OrderSelect(bTicket[1],SELECT_BY_POS);    // adjust B1 t/p level
orderM = OrderModify(bTicket[1], OrderOpenPrice(), OrderStopLoss(), 2 * buyAvgPrice - sPrice[1] + (takeProfitPips + 30) * Point, 0, Green);
   
orderS = OrderSelect(sTicket[1],SELECT_BY_POS);    // adjust S1 s/l level
orderM = OrderModify(sTicket[1], OrderOpenPrice(), 2 * buyAvgPrice - sPrice[1] + (takeProfitPips + 20) * Point, OrderTakeProfit(), 0, Green);
return;

I have problems with the second and third OrderModify command..

in the 2nd OrderModify, I do have an existing s/l level in bTicket[1], however when this this command was executed, the s/l level was set to zero.

in the 3rd OrderModify, I do have an existing t/p level in sTicket[1], however when this command was executed, the t/p level was set to zero.


May I know why and how can I keep the existing s/l and t/p levels?

 

My bad... i realise i should SELECT_BY_TICKET instead of SELECT_BY_POS

sigh.. took me so much time to see this.. ha!

 
having said that, does changing to SELECT_BY_TICKET solve the s/l and t/p issues?
 

I see you're doing this:

OrderSelect(bTicket[1])

It seems to me you're hard coding the array index/position. Shouldn't you do more or less this instead?

OrderSelect(bTicket[bCount])
OrderSelect(bTicket[(bCount + 1)])
 

Yes I am hardcoding this at the moment cos I only have 2 buy tickets and 2 sell tickets max..

It seems that the OrderModify can pick up the correct ticket but it is the s/l and t/p set to zero that I am concerned about.

 
orderS = OrderSelect(
orderM = OrderModify(
  1. 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.
              return value of OrderSend should be checked - Expert Advisors and Automated Trading - MQL5 programming forum

  2. You select by ticket. Where do you check if the order has already closed?

  3. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static / global ticket variables will have been lost. You will have an open order / position but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?

    Use a OrderSelect / Position select loop on the first tick, or persistent storage (GV+flush or files) of ticket numbers required.