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

 
m-r LSV:

Am I reading this right?

if(rates_total - prev_calculated == 1)
 Print("Новый бар");
 
Taras Slobodyanik:

Thank you, I have this now

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
   {
   fin=iTime(Symbol(), PERIOD_M1 ,0);
   if(last==fin) return(rates_total);
   last=fin;
   
   if(rates_total - prev_calculated == 1)
   Print("Новый бар");


return(rates_total);

	          
 

Can you please tell me the direction of the last closed candle when opening a bar?

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
   {
   fin=iTime(Symbol(), PERIOD_M1 ,0);
   if(last==fin) return(rates_total);
   last=fin;
   
   if(rates_total - prev_calculated == 1)
   //Print("Новый бар");
   
   if(open[1] > close[1])
   {
      Print("Dn");
   }
   else if(open[1] < close[1])
   {
      Print("Up");
   }

return(rates_total);

This code always shows the same message, no matter what candle was closed.
I understand that I need to re-calculate the candlestick through a loop ?
Please advise, people !

 
Taras Slobodyanik:

Why would you mock an inexperienced programmer like that?

What if a missed story is loaded and the difference is greater than 1? We don't really care, let's skip one bar... let's think about it, one more, one less. And without explanations why should we give the code? See what you get in the end?

 
Alexey Viktorov:

Why would you mock an inexperienced programmer like that?

What if a missed story is loaded and the difference is greater than 1? We don't really care, let's skip one bar... let's think about it, one more, one less. And without explanations why should we give the code? See what you get in the end?

Alexei, tell me the right way. Please.
 
Alexey Viktorov:

Why would you mock an inexperienced programmer like that?

What if a missed story is loaded and the difference is greater than 1? We don't really care, let's skip one bar... let's think about it, one more, one less. And without explanations why should we give the code? See what you get in the end?

It's obvious.)

If it's loaded, it'll be more than 1 and it's not a new bar, why keep track of it, there's no such thing in the problem.

 
m-r LSV:

Can you please tell me the direction of the last closed candle when opening a bar?

This code always shows the same message, no matter what candle was closed.
I understand that I need to re-calculate the candlestick through a loop ?
Please advise, people !

because there are no brackets.

if(rates_total - prev_calculated == 1)
   {
   //Print("Новый бар");
   if(open[1] > close[1])
        {
        Print("Dn");
        }
   else if(open[1] < close[1])
        {
        Print("Up");
        }
   }
 
m-r LSV:

Am I reading this right?

Yes, that's right.

m-r LSV:

The opening of the candle is delayed by 9-12 seconds.
Could you please tell me if this is the first tick?

It is just extremely important for me to make calculations, and perform any actions on the first tick of a new candle.


A new candle is formed on the first tick.

To check, you can ask for the ticks

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
 {
  MqlTick tick[];
  if(prev_calculated > 0 && rates_total > prev_calculated)
   {
    if(CopyTicks(_Symbol, tick, COPY_TICKS_ALL, 0, 2) <= 0)
      Print("ERR ", GetLastError());
    else
      ArrayPrint(tick);
   }

  return(rates_total);
 }

and see that the last tick belongs to the current candle and the previous tick, respectively, to the previous candle.

2021.06.02 17:50:59.311 00 (EURUSD,M1)                   [time]   [bid]   [ask] [last] [volume]    [time_msc] [flags] [volume_real]
2021.06.02 17:50:59.311 00 (EURUSD,M1)  [0] 2021.06.02 17:50:58 1.22005 1.22007 0.0000        0 1622656258958       4       0.00000
2021.06.02 17:50:59.311 00 (EURUSD,M1)  [1] 2021.06.02 17:51:00 1.22004 1.22010 0.0000        0 1622656260655       6       0.00000
2021.06.02 17:51:58.741 00 (EURUSD,M1)                   [time]   [bid]   [ask] [last] [volume]    [time_msc] [flags] [volume_real]
2021.06.02 17:51:58.741 00 (EURUSD,M1)  [0] 2021.06.02 17:51:59 1.22021 1.22026 0.0000        0 1622656319887       6       0.00000
2021.06.02 17:51:58.741 00 (EURUSD,M1)  [1] 2021.06.02 17:52:00 1.22020 1.22025 0.0000        0 1622656320080       6       0.00000

For example, on my demo it opens without any delays.

But on a less liquid pair, there is a 1 second delay

2021.06.02 17:55:59.742 00 (CADJPY,M1)                   [time]  [bid]  [ask] [last] [volume]    [time_msc] [flags] [volume_real]
2021.06.02 17:55:59.742 00 (CADJPY,M1)  [0] 2021.06.02 17:55:59 90.881 90.885  0.000        0 1622656559379       2         0.000
2021.06.02 17:55:59.742 00 (CADJPY,M1)  [1] 2021.06.02 17:56:01 90.882 90.887  0.000        0 1622656561076       6         0.000
 
Taras Slobodyanik:

it's obvious)

If it's loaded, it'll be more than 1, and it's not a New Bar, why keep track of it, there's no such thing in the task.

Where is it written that along with the new bar there can be no history subloading?
 
Taras Slobodyanik:

because there are no brackets

Didn't help :(

2021.06.02 17:54:09.465 Test (Volatility 10 Index,M1)      Up
2021.06.02 17:55:09.439 Test (Volatility 10 Index,M1)      Up
2021.06.02 17:56:09.686 Test (Volatility 10 Index,M1)      Up
2021.06.02 17:57:09.471 Test (Volatility 10 Index,M1)      Up
2021.06.02 17:58:09.586 Test (Volatility 10 Index,M1)      Up
Reason: