OrderOpenPrice returns 0

 

Hi,

I tried to get the open price of an order so that I can base on that value to place a new order. For example 100 pip away from that price. I use the OrderOpenPrice() function but the value returns 0. Please help me with this issue.

Here is my code:

if (OrdersTotal() < 2)
   {
      int buyTicket = OrderSend(Symbol(),OP_BUY, lot, Ask, 3, Ask-300*_Point, Ask+150*_Point, NULL, 0, 0, Green);
      double entryPrice = OrderOpenPrice();
      Comment("Ask price was: " + entryPrice);
   }

Thank you very much for your help.

 

You have to select the order with orderselect function first.

OrderSelect(buyTicket,SELECT_BY_TICKET);

      double entryPrice = OrderOpenPrice();
      Comment("Ask price was: " + entryPrice);
 
  1. You can not use any Trade Functions until you first select an order.

  2. int buyTicket = OrderSend(Symbol(),OP_BUY, lot, Ask, 3, Ask-300*_Point, Ask+150*_Point, NULL, 0, 0, Green);

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    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.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

 
Thank you all. I get it now :)
 
Marco vd Heijden #:

You have to select the order with orderselect function first.

Hi,


Can you please help with this one too I used your script but my sell order won't open?


void OnTick(){



if(Close[1] > Open[1] && OrdersTotal() == 0) {int buyTicket = OrderSend(_Symbol, OP_BUY, 0.1, Ask, 3, Ask - 600*_Point, Ask + 400*_Point, "BUY", 0, 0, clrGreen);

OrderSelect(buyTicket,SELECT_BY_TICKET);
double entryPrice = OrderOpenPrice();
Print("The first entry price is: ", entryPrice - 200*_Point);}



if(OrdersTotal() ==  1 && Bid == or <= (entryPrice - 200*_Point)){OrderSend(_Symbol, OP_SELL, 0.2, Bid, 3, Bid + 400*_Point, Bid - 600*_Point, "Sell", 0, 0, clrRed);}


}



Many Thanks

 
William Roeder #:
  1. You can not use any Trade Functions until you first select an order.

  2. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    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.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

Hi


Can you please help with this, my sell order won't trigger any idea why?


void OnTick(){



if(Close[1] > Open[1] && OrdersTotal() == 0) {int buyTicket = OrderSend(_Symbol, OP_BUY, 0.1, Ask, 3, Ask - 600*_Point, Ask + 400*_Point, "BUY", 0, 0, clrGreen);

OrderSelect(buyTicket,SELECT_BY_TICKET);
double entryPrice = OrderOpenPrice();
Print("The first entry price is: ", entryPrice - 200*_Point);}



if(OrdersTotal() ==  1 && Bid <= (entryPrice - 200*_Point)){OrderSend(_Symbol, OP_SELL, 0.2, Bid, 3, Bid + 400*_Point, Bid - 600*_Point, "Sell", 0, 0, clrRed);}


}


Many Thanks

 
24061512 #: Can you please help with this, my sell order won't trigger any idea why?
  1. Please edit your post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)  Remove the duplicate post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2. 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)

  3. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)