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

 
Artyom Trishkin:

What calculations?

Last_Arrow_Buy_volume = Buf_Arrow_Buy[n];
Last_Arrow_Buy_index  = n;

Got it, question removed, I heard from you and realised that declaring a variable in the OnTick() area is bad, and calculations can be done inside the loop as well.

Done, everything works thanks, now I just need to get the prices. Thank you very much.

void OnTick()
  {
   if (CopyBuffer(CrossAD, 1, 0, period_find, Buf_Arrow_Buy) != period_find)
      {  
         Print("НЕ удалось правильно скопировать данные из 1-го буфера индикатора iCrossAD, error code %d",GetLastError());
         return;
      }
         for(int n=0; n<(int)period_find; n++)
            {
               if(Buf_Arrow_Buy[n]!=EMPTY_VALUE)
               {
                  Last_Arrow_Buy_volume = Buf_Arrow_Buy[n];
                  Last_Arrow_Buy_index  = n;
                  Print("Last_Arrow_Buy_volume = ",Last_Arrow_Buy_volume,", Last_Arrow_Buy_index = ",Last_Arrow_Buy_index);
                  break;
               }   
            }
         
   if (CopyBuffer(CrossAD, 2, 0, period_find, Buf_Arrow_Sell) != period_find)
      {  
         Print("НЕ удалось правильно скопировать данные из 2-го буфера индикатора iCrossAD, error code %d",GetLastError());
         return;
      }
         for(int n=0; n<(int)period_find; n++)
            {
               if(Buf_Arrow_Sell[n]!=EMPTY_VALUE)
               {
                  Last_Arrow_Sell_volume = Buf_Arrow_Sell[n];
                  Last_Arrow_Sell_index  = n;
                  Print("Last_Arrow_Sell_volume = ",Last_Arrow_Sell_volume,", Last_Arrow_Sell_index = ",Last_Arrow_Sell_index);
                  break;
               }
            }
      
Comment("-------------------------", 
         "\n Last_Arrow_Buy_volume     = ",Last_Arrow_Buy_volume,
         "\n Last_Arrow_Buy_index        = ",Last_Arrow_Buy_index,
         "\n ---------------------- ",
         "\n Last_Arrow_Sell_volume     = ",Last_Arrow_Sell_volume,
         "\n Last_Arrow_Sell_index        = ",Last_Arrow_Sell_index
         ); 
  }
 
Sergey Voytsekhovsky:

Got it, question removed, I heard from you and realised that declaring a variable in the OnTick() area is bad, and calculations can be done inside the loop as well.

Done, everything works thanks, now I just need to get the prices. Thank you very much.

It's not calculations inside the loop, it's getting the necessary data, which is what this loop is designed for.

But calculations inside the loop should be avoided.

In fact, you need to reconsider your approach right away. You are getting data in the loop. You interrupt the loop after the first encountered data. All of this is entirely within the concept of a function - to return the requested result.

Make the whole loop into a function and return the number of the bar if it is found or WRONG_VALUE - if it is not found.

then: n=Func();

and then use n for its intended purpose if it is not equal to WRONG_VALUE. Name the function with a "talking" name. And the code in OnTick() will be shorter and the logic will be clearer - it won't be overloaded with anything unnecessary.

 
Artyom Trishkin:

Form the whole loop into a function, and return bar number from it if found, or WRONG_VALUE if not found.

Then: n=Func();

and then use n as intended, if it is not equal to WRONG_VALUE. Give the function a "telling" name. And the code in OnTick() will be shorter and the logic will be clearer - it won't be overloaded with unnecessary things.

You live and learn. Very interesting, I'll try it in the evening. In the meantime, as a report and with great gratitude to you, I got the prices from..... It was a good day :-))

void OnTick()
  {
   if (CopyBuffer(CrossAD, 1, 0, period_find, Buf_Arrow_Buy) != period_find)
      {  
         Print("НЕ удалось правильно скопировать данные из 1-го буфера индикатора iCrossAD, error code %d",GetLastError());
         return;
      }
         for(int n=0; n<(int)period_find; n++)
            {
               if(Buf_Arrow_Buy[n]!=EMPTY_VALUE)
               {
                  Last_Arrow_Buy_volume = iOpen(_Symbol,_Period,n);
                  Last_Arrow_Buy_index  = n;
                  Print("Last_Arrow_Buy_volume = ",Last_Arrow_Buy_volume,", Last_Arrow_Buy_index = ",Last_Arrow_Buy_index);
                  break;
               }   
            }
         
   if (CopyBuffer(CrossAD, 2, 0, period_find, Buf_Arrow_Sell) != period_find)
      {  
         Print("НЕ удалось правильно скопировать данные из 2-го буфера индикатора iCrossAD, error code %d",GetLastError());
         return;
      }
         for(int n=0; n<(int)period_find; n++)
            {
               if(Buf_Arrow_Sell[n]!=EMPTY_VALUE)
               {
                  Last_Arrow_Sell_volume = iOpen(_Symbol,_Period,n);
                  Last_Arrow_Sell_index  = n;
                  Print("Last_Arrow_Sell_volume = ",Last_Arrow_Sell_volume,", Last_Arrow_Sell_index = ",Last_Arrow_Sell_index);
                  break;
               }
            }
      
Comment("-------------------------", 
         "\n Last_Arrow_Buy_volume     = ",Last_Arrow_Buy_volume,
         "\n Last_Arrow_Buy_index        = ",Last_Arrow_Buy_index,
         "\n ---------------------- ",
         "\n Last_Arrow_Sell_volume     = ",Last_Arrow_Sell_volume,
         "\n Last_Arrow_Sell_index        = ",Last_Arrow_Sell_index
         ); 
  }
 

Guys who can help with this question https://www.mql5.com/ru/forum/160683/page845#comment_11741857 - has the comment already gone far? There is also a question how to create a loop that can work in either direction, if you need to run incremental if you need to run decremental? Here is a sample scheme of how it would work.

If (something happened) {assigning values to variables} then the loop will be called and it will contain a body, the calculations of which will depend on the values of the loop

for( depending on the variables above increment or decrement)

{ body }

There is no way to duplicate the condition, it has to be specified once in the loop. Is it possible to do this.

 

Good evening again. I haven't made it into a function yet, but I have a new question. In the attached picture you can see that there is a new signal (Arrow), but in the comments you can see that the EA does not accept it, it still considers the one from 11 candles ago as the Ultimate Up Arrow.

https://www.mql5.com/ru/charts/10181812/eurusd-h1-alpari-international

График EURUSD, H1, 2019.05.20 18:00 UTC, Alpari International, MetaTrader 5, Real
График EURUSD, H1, 2019.05.20 18:00 UTC, Alpari International, MetaTrader 5, Real
  • www.mql5.com
Символ: EURUSD. Период графика: H1. Брокер: Alpari International. Торговая платформа: MetaTrader 5. Режим торговли: Real. Дата: 2019.05.20 18:00 UTC.
 
Sergey Voytsekhovsky:

11 candles ago.

It turns out that the indicator has already given a new signal (it is running parallel to the EA on the chart), there are no events in the EA other than OnTick, and the EA does not see the signal.

 
Sergey Voytsekhovsky:

Good evening again. I haven't made it into a function yet, but I have a new question. In the attached picture you can see that there is a new signal (Arrow), but in the comments you can see that the EA does not accept it, it still considers the one from 11 candles ago as the Ultimate Up Arrow.

https://www.mql5.com/ru/charts/10181812/eurusd-h1-alpari-international

When finding a non-empty buffer value, print in the log the time corresponding to bar n. You will see which bar it finds. It may well be that you are reading the buffer backwards.

 

Please advise why the EA does not see the arrow, it is already in the indicator buffer, judging by the data window, the EA interrogates OnTick, what else does it need? Full code above.


 
Artyom Trishkin:

When finding a non-empty buffer value, print in the log the time corresponding to bar n. It will show which bar it finds. It may well be that you are reading the buffer backwards.

OK, done, the time shows the correct time, the opening time of the candle on which the arrow is set. Code attached, picture attached.

void OnTick()
  {
   if (CopyBuffer(CrossAD, 1, 0, period_find, Buf_Arrow_Buy) != period_find)
      {  
         Print("НЕ удалось правильно скопировать данные из 1-го буфера индикатора iCrossAD, error code %d",GetLastError());
         return;
      }
         for(int n=0; n<(int)period_find; n++)
            {
               if(Buf_Arrow_Buy[n]!=EMPTY_VALUE)
               {
                  Last_Arrow_Buy_volume = iOpen(_Symbol,_Period,n);
                  Last_Arrow_Buy_time   = iTime(_Symbol,0,n);
                  Last_Arrow_Buy_index  = n;
                  Print("Last_Arrow_Buy_volume = ",Last_Arrow_Buy_volume,", Last_Arrow_Buy_index = ",Last_Arrow_Buy_index,", Last_Arrow_Buy_time = ",Last_Arrow_Buy_time);
                  break;
               }   
            }
         
   if (CopyBuffer(CrossAD, 2, 0, period_find, Buf_Arrow_Sell) != period_find)
      {  
         Print("НЕ удалось правильно скопировать данные из 2-го буфера индикатора iCrossAD, error code %d",GetLastError());
         return;
      }
         for(int n=0; n<(int)period_find; n++)
            {
               if(Buf_Arrow_Sell[n]!=EMPTY_VALUE)
               {
                  Last_Arrow_Sell_volume = iOpen(_Symbol,_Period,n);
                  Last_Arrow_Sell_time   = iTime(_Symbol,0,n);
                  Last_Arrow_Sell_index  = n;
                  Print("Last_Arrow_Sell_volume = ",Last_Arrow_Sell_volume,", Last_Arrow_Sell_index = ",Last_Arrow_Sell_index,", Last_Arrow_Buy_time = ",Last_Arrow_Buy_time);
                  break;
               }
            }
      
Comment("-------------------------", 
         "\n Last_Arrow_Buy_volume     = ",Last_Arrow_Buy_volume,
         "\n Last_Arrow_Buy_index        = ",Last_Arrow_Buy_index,
         "\n ---------------------- ",
         "\n Last_Arrow_Sell_volume     = ",Last_Arrow_Sell_volume,
         "\n Last_Arrow_Sell_index        = ",Last_Arrow_Sell_index
         ); 
  }


 

if you can't see the log in the picture, here are some extreme entries


2019.05.20 21:42:52.601 Test_iCustom (EURUSD,H1) Last_Arrow_Sell_volume = 1.12112, Last_Arrow_Sell_index = 56.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:42:53.824 Test_iCustom (EURUSD,H1) Last_Arrow_Buy_volume = 1.11544, Last_Arrow_Buy_index = 11.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:42:53.824 Test_iCustom (EURUSD,H1) Last_Arrow_Sell_volume = 1.12112, Last_Arrow_Sell_index = 56.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:43:00.819 Test_iCustom (EURUSD,H1) Last_Arrow_Buy_volume = 1.11544, Last_Arrow_Buy_index = 11.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:43:00.819 Test_iCustom (EURUSD,H1) Last_Arrow_Sell_volume = 1.12112, Last_Arrow_Sell_index = 56.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:43:03.021 Test_iCustom (EURUSD,H1) Last_Arrow_Buy_volume = 1.11544, Last_Arrow_Buy_index = 11.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:43:03.021 Test_iCustom (EURUSD,H1) Last_Arrow_Sell_volume = 1.12112, Last_Arrow_Sell_index = 56.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:43:03.621 Test_iCustom (EURUSD,H1) Last_Arrow_Buy_volume = 1.11544, Last_Arrow_Buy_index = 11.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:43:03.622 Test_iCustom (EURUSD,H1) Last_Arrow_Sell_volume = 1.12112, Last_Arrow_Sell_index = 56.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:43:10.696 Test_iCustom (EURUSD,H1) Last_Arrow_Buy_volume = 1.11544, Last_Arrow_Buy_index = 11.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:43:10.696 Test_iCustom (EURUSD,H1) Last_Arrow_Sell_volume = 1.12112, Last_Arrow_Sell_index = 56.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00


Reason: