Questions from Beginners MQL5 MT5 MetaTrader 5 - page 863

 
Can you show the code that prints the price of the nearest buy stop and the nearest sell stop ?
 
ilyav:
Can you show the code that prints the price of the nearest buy stop and the nearest sell stop?

Just the price? What is it for? And decipher the word "nearest" - to whom, what ...

 
Vladimir Karputov:

Just the price? What is it for? And decipher the word "nearest" - to whom, what ...

Just the price.

Look.

We now have 10 buy stops 100 pips away from the current price.

We now have 10 sell stops 100 pips away from the current price.

I need to display the price of the nearest buy stop and the nearest sell stop relative to the current price in Print.

I tried to do it but it did not work.

The price of the nearest buy stop is output correctly, but the price of the nearest sell stop is output for some reason.

This functionm_order.PriceOpen(); always displays the buy stop price. Although I ask it to give me the price of the sell stop after the buy stop request.

This is done for further logic-

I divide by 2 the price of the nearest buy stop and the price of the nearest sell stop.

The Expert Advisor will continue its work based on the obtained figure.

The full function is

//+------------------------------------------------------------------+
void TradeMode3()
  {
   int TotalGridBuyOrders=0;
   Print(__FUNCTION__);
   for(int i=OrdersTotal()-1;i>=0;i--) // returns the number of current orders
      if(m_order.SelectByIndex(i)) // selects the pending order by index for further access to its properties
         if(m_order.Symbol()==m_symbol.Name() && m_order.Magic()==m_magic && m_order.OrderType()==ORDER_TYPE_BUY_STOP)
            TotalGridBuyOrders++;
   Print("Количество buy ордеров grid ",TotalGridBuyOrders);
   if(TotalGridBuyOrders>=1)
     {
      grid_buy_price_memory=m_order.PriceOpen();
      Print("Цена ближайшего buy grid ордера: ",grid_buy_price_memory);

      int TotalGridSellOrders=0;
      Print(__FUNCTION__);
      for(int i=OrdersTotal()-1;i>=0;i--) // returns the number of current orders
         if(m_order.SelectByIndex(i)) // selects the pending order by index for further access to its properties
            if(m_order.Symbol()==m_symbol.Name() && m_order.Magic()==m_magic && m_order.OrderType()==ORDER_TYPE_SELL_STOP)
               TotalGridSellOrders++;
      Print("Количество sell ордеров grid ",TotalGridSellOrders);
      if(TotalGridSellOrders>=1)
        {
         grid_sell_price_memory=m_order.PriceOpen();
         Print("Цена ближайшего sell grid ордера: ",grid_sell_price_memory);
        }
     }
  }
//+------------------------------------------------------------------+
 

Now I've tried changing the code.

//+------------------------------------------------------------------+
void TradeMode3()
  {
     {
      int TotalGridBuyOrders=0;
      Print(__FUNCTION__);
      for(int i=OrdersTotal()-1;i>=0;i--) // returns the number of current orders
         if(m_order.SelectByIndex(i)) // selects the pending order by index for further access to its properties
            if(m_order.Symbol()==m_symbol.Name() && m_order.Magic()==m_magic && m_order.OrderType()==ORDER_TYPE_BUY_STOP)
               TotalGridBuyOrders++;
      Print("Количество buy ордеров grid ",TotalGridBuyOrders);
      if(TotalGridBuyOrders>=1)
        {
         double grid_buy_price_memory=m_order.PriceOpen();
         Print("Цена ближайшего buy grid ордера: ",grid_buy_price_memory);
        }
     }
     {
      int TotalGridSellOrders=0;
      Print(__FUNCTION__);
      for(int i=OrdersTotal()-1;i>=0;i--) // returns the number of current orders
         if(m_order.SelectByIndex(i)) // selects the pending order by index for further access to its properties
            if(m_order.Symbol()==m_symbol.Name() && m_order.Magic()==m_magic && m_order.OrderType()==ORDER_TYPE_SELL_STOP)
               TotalGridSellOrders++;
      Print("Количество sell ордеров grid ",TotalGridSellOrders);
      if(TotalGridSellOrders>=1)
        {
         double grid_sell_price_memory=m_order.PriceOpen();
         Print("Цена ближайшего sell grid ордера: ",grid_sell_price_memory);
        }
     }
  }
//+------------------------------------------------------------------+

No change. Outputs only the buy stop price

 
ilyav:

Now I've tried changing the code.

No change. It only displays the buy stop price.

Let's write the code first. I would start by saying that the Buy Stop and Sell Stop search function needs to pass the price - otherwise how do you search? ...

Also, we need to clarify: "nearest" - above or below the price? Or is it just the difference between the set price and the pending order?

 
Vladimir Karputov:

Let's write it up first. I would start by saying that the Buy Stop and Sell Stop search function needs to pass the price - otherwise how do you search? ...

This is how I do it.

for(int i=OrdersTotal()-1;i>=0;i--) // returns the number of current orders
         if(m_order.SelectByIndex(i)) // selects the pending order by index for further access to its properties
            if(m_order.Symbol()==m_symbol.Name() && m_order.Magic()==m_magic && m_order.OrderType()==ORDER_TYPE_SELL_STOP)
               TotalGridSellOrders++;

The function perfectly outputs the current number of sell stops.

Next, we have selected the closest Sell Stop. We ask to display its price and print it

double grid_sell_price_memory=m_order.PriceOpen();    
Print("Цена ближайшего sell grid ордера: ", grid_sell_price_memory);

It is written in the help.

The COrderInfo class

PriceOpen

Gets the order price.

doublePriceOpen()const

Returned value

Order open price.

Note

The order should be pre-selected for access using the Select(by ticket) orSelectByIndex(by index) methods.


What is wrong? We have selected the order. The price of the installation is requested.

Or am I misunderstanding something ? Why do I get a buy stop price if I do not do it right?

 
Vladimir Karputov:

Also, we need to clarify: is the "nearest" the top or bottom of the price? Or is it just the difference between the set price and the pending order?

Here is the price of which buy stop and sell stop I need

 
ilyav:

Here is the price of which Buy Stop and Sell Stop I need

This function searches for the nearest above Buy Stop from the price "price" and the nearest below Sell Stop from the price "price":

//+------------------------------------------------------------------+
//| Calculate all pending orders                                     |
//+------------------------------------------------------------------+
void CalculateAllPendingOrders(const double price,double &price_nearest_buy_stop,double &price_nearest_sell_stop)
  {
   price_nearest_buy_stop  = 0;
   price_nearest_sell_stop = 0;
   double diff_buy_stop    = DBL_MAX;
   double diff_sell_stop   = DBL_MAX;

   for(int i=OrdersTotal()-1;i>=0;i--) // returns the number of current orders
      if(m_order.SelectByIndex(i))     // selects the pending order by index for further access to its properties
         if(m_order.Symbol()==m_symbol.Name() && m_order.Magic()==m_magic)
           {
            if(m_order.OrderType()==ORDER_TYPE_BUY_STOP)
              {
               if(m_order.PriceOpen()-price>0.0 && m_order.PriceOpen()-price<diff_buy_stop)
                 {
                  diff_buy_stop           = m_order.PriceOpen()-price;
                  price_nearest_buy_stop  = m_order.PriceOpen();
                 }
              }
            else if(m_order.OrderType()==ORDER_TYPE_SELL_STOP)
              {
               if(price-m_order.PriceOpen()>0.0 && price-m_order.PriceOpen()<diff_sell_stop)
                 {
                  diff_sell_stop          = price-m_order.PriceOpen();
                  price_nearest_sell_stop = m_order.PriceOpen();
                 }
              }
           }
  }
 

Now another problem)

I used to call my function like this -

//|  Выбран режим торговли 3 ? Тогда торгуем его    
      if(РежимТорговли==3)
        {
         TradeMode3();
        }

Inserted your code and changed my function to

 //|  Выбран режим торговли 3 ? Тогда торгуем его    
      if(РежимТорговли==3)
        {
        CalculateAllPendingOrders();
        }
     

Now there is an error when compiling(

'CalculateAllPendingOrders' - wrong parameters count
 
ilyav:

Now another problem)

I used to call my function like this -

Inserted your code and changed my function to

Now there is an error when compiling it(

You have to pass a PRICE to the function, around which the search for pending orders will be performed.

Besides this price you have to pass two variables:

void CalculateAllPendingOrders(const double price,double &price_nearest_buy_stop,double &price_nearest_sell_stop)