Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 501

 
dimkh87:
Afternoon. Can you please advise whether MT4 and MT5 have the possibility to overlay the charts of one instrument with different timeframes, and the indicators for these timeframes? For example, I want to display in one window 3 charts of EUR/USD pair with TFs: day, 4hour and hour, and also overlay the sliding indicators of these TFs. Is it possible? I will be glad for any help.

What to do with the timeline? Or draw bars from smaller timescales a few bars wide?

 
Вадим Мотеюнас:

Gentlemen, please advise, I took a function from Kim which returns the bar number of the last position opened or -1.

I call this function and write a condition

I have looked through Print and see that this function return -1, at first I thought there was no order but after a while the order reappeared and on the same bar a deal was opened after the stop.

static datetime time_open = 0;
if(Open[2]>Close[2] && Open[1]>Close[1] && Close[1]<Low[2])
     {
      Comment("цена входа = ",DoubleToStr(Low[2],Digits));
      if( Open[0] != time_open )
         if(NumberOfBarOpenLastPos("",0,-1,-1)!=0)//вызвал здесь
            ticket=OrderSend(Symbol(),OP_SELLLIMIT,0.1,Low[2],2,0,0,magic,0);

      if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
        {
         time_open = Open[0];
         TP=NormalizeDouble(OrderOpenPrice()-tp*Point,Digits);
         SL=NormalizeDouble(OrderOpenPrice()+sl*Point,Digits);
         modify=OrderModify(ticket,OrderOpenPrice(),SL,TP,0);
       }
      }
 
Konstantin Nikitin:
error indicatingif( iOpen[0] != time_open )'iOpen' - undeclared identifier

 
Вадим Мотеюнас:
error is written pointing toif( iOpen[0] != time_open )'iOpen' - undeclared identifier

Kim has all the functions working! You didn't apply it correctly.

 
Vitaly Muzichenko:

Kim has all the functions working! You didn't apply it correctly.

No one is arguing, I just want to understand how I misapplied it.

 
Вадим Мотеюнас:

No one is arguing, I would like to understand how I am misapplying it

Print everything - it's the right thing to do to identify mistakes and shortcomings:

Print( NumberOfBarOpenLastPos("",0,-1,-1) );
 
Vitaly Muzichenko:

Print everything - this is the best solution to identify errors and deficiencies:

I was saying that it returns -1, i.e. the last bar at which the order was opened is not found by the function, I am worried that I am not passing the right parameters

 
int NumberOfBarOpenLastPos(string sym="",int tf=0,int op=-1,int mn=-1)
  {
   datetime oot;
   int      i,k=OrdersTotal();

   if(sym=="") sym=Symbol();
   for(i=0; i<k; i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==sym)
           {
            if(OrderType()==OP_BUY || OrderType()==OP_SELL)
              {
               if(op<0 || OrderType()==op)
                 {
                  if(mn<0 || OrderMagicNumber()==mn)
                    {
                     if(oot<OrderOpenTime()) oot=OrderOpenTime();//что означает это строка?что текущее время раньше цены открытия ордера, это как?
                    }
                 }
              }
           }
        }
     }
 
Вадим Мотеюнас:

The line means that you go through all the positions, and choose/remember the newest one, and work with it. This is in case the positions are not in order, but scattered.

 

The function goes through and selects the newest one by time, i.e. the last one

int NumberOfBarOpenLastPos(string sym="",int tf=0,int op=-1,int mn=-1)
  {
   datetime oot=0; // Инициализируем нулём
   int      i,k=OrdersTotal();

   if(sym=="") sym=Symbol();
   for(i=0; i<k; i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==sym)
           {
            if(OrderType()==OP_BUY || OrderType()==OP_SELL)
              {
               if(op<0 || OrderType()==op)
                 {
                  if(mn<0 || OrderMagicNumber()==mn)
                    {
                     if(oot<OrderOpenTime()) oot=OrderOpenTime(); // Ищем последнюю позицию
                    }
                 }
              }
           }
        }
     }
   return(iBarShift(sym, tf, oot, True));
  }

Next, we have a selected one, and we substitute it in the functioniBarShift(sym, tf, oot ,true);

The function returns the bar number by time. That's all.

Unfortunately, I can't check it, but when I used it, it worked fine, as well as all functions published here by I. Kim