Questions from Beginners MQL5 MT5 MetaTrader 5 - page 865

 
Aleksey Vyazmikin:

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 do I do this?

You specify the bar number, symbol name and required timeframe:

//+------------------------------------------------------------------+ 
//| Get Time for specified bar index                                 | 
//+------------------------------------------------------------------+ 
datetime iTime(const int index,string symbol=NULL,ENUM_TIMEFRAMES timeframe=PERIOD_CURRENT)
  {
   if(symbol==NULL)
      symbol=Symbol();
   if(timeframe==0)
      timeframe=Period();
   datetime Time[1];
   datetime time=0; // datetime "0" -> D'1970.01.01 00:00:00'
   int copied=CopyTime(symbol,timeframe,index,1,Time);
   if(copied>0)
      time=Time[0];
   return(time);
  }

Check (just in case) - if the function returns time D'1970.01.01 00:00:00' - then there is an error.

 

I have encountered a problem with a sell order not being placed

2018.05.23 12:07:55.002 Trades  '50250084': market sell 0.01 EURUSD tp: 1.17397
2018.05.23 12:07:55.112 Trades  '50250084': accepted market sell 0.01 EURUSD tp: 1.17397
2018.05.23 12:07:55.113 Trades  '50250084': market sell 0.01 EURUSD tp: 1.17397 placed for execution
2018.05.23 12:07:55.114 Trades  '50250084': rejected market sell 0.01 EURUSD tp: 1.17397 (rejected)

Do I understand correctly that the broker accepted the order and then rejected it?

On what grounds then and why not? There are no more errors in the logs. Before this error there were more than 50 orders with no errors. After this error we have not registered any errors either.

Is it possible to get rid of this problem?

 
Vladimir Karputov:

Specify bar number, symbol name and required timeframe:

Check (just in case) - if the function returns time D'1970.01.01 00:00:00' - then there is an error.

I use this function, is it worse than the one you suggest?

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
datetime iTime(string symbol,int tf,int index)
  {
   if(index < 0) return(-1);
   ENUM_TIMEFRAMES timeframe=TFMigrate(tf);
   datetime Arr[];
   if(CopyTime(symbol,timeframe,index,1,Arr)>0)
      return(Arr[0]);
   else return(-1);
  }

The problem is that if the current timeframe is 1 minute, I want to know the number of the last bar of the current timeframe in the selected timeframe, but a lot of time has passed (a gap because of the weekend or because the evening session closed at 23-50, and the next opened only at 10-00), then I return 09:59 and not 23:49:00, I request the time so

datetime         StopDt=iTime(Symbol(),TF_iDeltaP,_Shift-1)-60;

That's why I asked, how to get this time of the last bar with any combinations of TFs?

 
Aleksey Vyazmikin:

...

The problem is that if I have a current timeframe of 1 minute, I want to know the number of the last bar of the current timeframe in the selected timeframe, ...

At the highlighted phrase my brain exploded.

 
Vladimir Karputov:

At the highlighted phrase my brain exploded.

There's a typo there, I need the date of the last bar of the current timeframe, which refers to the selected bar of the upper timeframe.

Is that clear?
 
Aleksey Vyazmikin:

There's a typo there, I need the date of the last bar of the current timeframe, which refers to the selected bar of the upper timeframe.

Is that clear?

No, it's not clear. My brain keeps bubbling and exploding :). One bar belongs to its own timeframe only.

 
Aleksey Vyazmikin:

There's a typo there, I need the date of the last bar of the current timeframe, which refers to the selected bar of the upper timeframe.

Is this clear?

If I understood your request correctly, then:

1. Determine the closing time of the desired bar of the upper timeframe.

2. Calculate the time of the bar of the low TF which closes together with the bar of the high TF.

3. On this timeframe ask for the bar of the younger TF (the request is not strict) - get the bar closest to the left on the timeline - the one you need.

4. Check, if necessary, that this bar is within the bar of the upper TF (exclude the variant when the bars of the junior TF are not present in the bar of the senior TF for some reason).

 
Vladimir Karputov:

No, it doesn't. The brain keeps bubbling and exploding :). One bar belongs to its timeframe only.

Ok, moving on to visualisation of thoughts :)

In white I showed where we are making the request, in yellow the bar time we should get.

Is it clearer? If not, please ask questions.

 
Aleksey Vyazmikin:

OK, moving on to visualisation of thoughts :)

White shows where we are making the request, yellow the bar time we should get.

Is this clearer? If not, please ask questions to clarify.

1. The picture is small - next time put 2m by 2m :)

2. Again, I do not understand what you mean: you've shown me a chart on some timeframe. What is the problem to request bar number N on this timeframe?

 
Vladimir Karputov:

1. The picture is small - next time put 2 metres by two at once :)

2. I don't understand the thought again - you showed a chart on some timeframe. What is the problem requesting a bar number N on this timeframe?

1. I can do more - no problem ;)

2. The timeframe shown in the top left corner is M5. I cannot ask for bar number N as I do not know how to know it exactly, especially if the shift is not 1 bar of upper TF, but more bars.

I will try for a shift of one bar the following algorithm:

1. Find out how many bars have passed since the beginning of the current upper TF

2. Shift the obtained number by 1 bar and find out the time using iTime function

Reason: