Questions from Beginners MQL5 MT5 MetaTrader 5 - page 864

 
Vladimir Karputov:

You must pass a PRICE to the function, around which the pending orders will be searched for.

In addition to this price, you have to pass two variables:

I give up, do you have a ready-made Expert Advisor that prints the prices of those pending orders I showed with arrows in the screenshot?

I don't understand why this function works without passing any price.

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);

???

 
ilyav:

I give up, don't you have a ready-made Expert Advisor that outputs a print of the prices of those orders that I showed with arrows in the screenshot?

I gave you a ready-made function. You need to pass in it:

  • the current price
  • a variable in which the price of the next Buy stop will be written
  • a variable into which the price of the next Sell stop will be written

What don't you understand?

 
I see. Apparently it's not my thing.
 
ilyav:

I give up, don't you have a ready-made Expert Advisor that would output a print of the prices of those pending orders that I showed with arrows in the screenshot?

I don't understand why this function works without sending any prices.

???

You show unworkable code - at least it does not compile - because it has a mismatch of opening and closing brackets. And secondly = broken logic.

 
Vladimir Karputov:

You are showing unworkable code - at least it doesn't compile - because it has a mismatch of opening and closing brackets. And secondly = broken logic.

I rewrote your code.

But it produces pending orders far from the price

void TradeMode3()
  {
   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()-price1>0.0 && m_order.PriceOpen()-price1<diff_buy_stop)
                 {
                  diff_buy_stop           = m_order.PriceOpen()-price1;
                  price_nearest_buy_stop  = m_order.PriceOpen();
                 
                 }
               Print("buy : ",price_nearest_buy_stop);
                return;  
              }
              
               
            else if(m_order.OrderType()==ORDER_TYPE_SELL_STOP)
              {
               if(price1-m_order.PriceOpen()>0.0 && price1-m_order.PriceOpen()<diff_sell_stop)
                 {
                  diff_sell_stop          = price1-m_order.PriceOpen();
                  price_nearest_sell_stop = m_order.PriceOpen();
                 }
                 Print("sell : ",price_nearest_sell_stop);
                 return;
              }
           }
  }
  
 
 
ilyav:

I rewrote your code.

But it gives out pendants farther away from the price

The prints should come after theOrdersTotal loop.

And what is price1? How do you get it?
 

And in this version, it prints out all the available

//+------------------------------------------------------------------+
//| Calculate all pending orders                                     |
//+------------------------------------------------------------------+
void TradeMode3()
  {
   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()-price1>0.0 && m_order.PriceOpen()-price1<diff_buy_stop)
                 {
                  diff_buy_stop           = m_order.PriceOpen()-price1;
                  price_nearest_buy_stop  = m_order.PriceOpen();
                 }
                 Print("buy : ",price_nearest_buy_stop);
                 
              }
            else if(m_order.OrderType()==ORDER_TYPE_SELL_STOP)
              {
               if(price1-m_order.PriceOpen()>0.0 && price1-m_order.PriceOpen()<diff_sell_stop)
                 {
                  diff_sell_stop          = price1-m_order.PriceOpen();
                  price_nearest_sell_stop = m_order.PriceOpen();
                 }
                 Print("sell : ",price_nearest_sell_stop);
                 
              }
           }
  }
 
Vladimir Karputov:

The prints must come after theOrdersTotal loop.

And what is price1? How do you get it?
//|  Выбран режим торговли 3 ? Тогда торгуем его    

      if(РежимТорговли==3)

        {

        price1=m_symbol.Bid();

        TradeMode3();

And in the header of the EA I have written

double price1;
double price_nearest_buy_stop;
double price_nearest_sell_stop;

If everything is correct, here is the final variant

//+------------------------------------------------------------------+
//| Calculate all pending orders                                     |
//+------------------------------------------------------------------+
void TradeMode3()
  {
   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()-price1>0.0 && m_order.PriceOpen()-price1<diff_buy_stop)
                 {
                  diff_buy_stop           = m_order.PriceOpen()-price1;
                  price_nearest_buy_stop  = m_order.PriceOpen();
                 }

              }
            else if(m_order.OrderType()==ORDER_TYPE_SELL_STOP)
              {
               if(price1-m_order.PriceOpen()>0.0 && price1-m_order.PriceOpen()<diff_sell_stop)
                 {
                  diff_sell_stop          = price1-m_order.PriceOpen();
                  price_nearest_sell_stop = m_order.PriceOpen();
                 }

              }

           }
   Print("Цена ближнего SELL_STOP : ",price_nearest_sell_stop);
   Print("Цена ближнего BUY_STOP : ",price_nearest_buy_stop);
  }
//+------------------------------------------------------------------+

Everything seems to work. Tell me if there are any errors. Thank you very much.

 

Please help me to solve this problem.

Suppose I have a current timeframe M1, I need to know the time of the last bar N of the given bar of any upper TF, the hour may not be 60 minutes, but 45 or any other time, and besides there are weekends, I need to know the date of the last bar of the current TF from any TF.

How to implement it?

 
ilyav:

And in the advisor's header it says

And if everything is correct, here is the final variant

Everything seems to work. Tell me if there are any mistakes. Thank you very much.

Before the line

price1=m_symbol.Bid();

you need to update the quotes:

if(!RefreshRates())
   return;
Reason: