problem about using a loop

 

Hello


Due to many conditions in this code,

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 3
#property indicator_color1 0xFFAA00
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 3
#property indicator_color2 0xFB00FF
#property indicator_label2 "Sell"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | loop @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexArrow(0, 241);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   SetIndexArrow(1, 242);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, EMPTY_VALUE);
      ArrayInitialize(Buffer2, EMPTY_VALUE);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      
      //Indicator Buffer 1
      if(iDeMarker(NULL, PERIOD_CURRENT, 14, i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 1+i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 2+i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 3+i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 4+i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 5+i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 6+i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 7+i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 8+i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 9+i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 10+i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 11+i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 12+i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 13+i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 14+i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 15+i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 16+i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 17+i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 18+i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 19+i) > 0.20
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 20+i) > 0.20
      
      )
        {
         Buffer1[i] = Low[i]; //Set indicator value at Candlestick Low
        }
      else
        {
         Buffer1[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 2
      if(iDeMarker(NULL, PERIOD_CURRENT, 14, i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 1+i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 2+i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 3+i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 4+i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 5+i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 6+i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 7+i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 8+i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 9+i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 10+i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 11+i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 12+i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 13+i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 14+i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 15+i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 16+i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 17+i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 18+i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 19+i) < 0.80
      && iDeMarker(NULL, PERIOD_CURRENT, 14, 20+i) < 0.80
      )
        {
         Buffer2[i] = High[i]; //Set indicator value at Candlestick High
        }
      else
        {
         Buffer2[i] = EMPTY_VALUE;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+



I have tried to shorten these previous conditions by loop as you see in the next code:



#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 3
#property indicator_color1 0xFFAA00
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 3
#property indicator_color2 0xFB00FF
#property indicator_label2 "Sell"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | loop @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexArrow(0, 241);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   SetIndexArrow(1, 242);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, EMPTY_VALUE);
      ArrayInitialize(Buffer2, EMPTY_VALUE);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      
      //Indicator Buffer 1
      for(int j = 0; j<= 20; j++)
      {
         if(iDeMarker(NULL, PERIOD_CURRENT, 14, j + i) > 0.20)
         {
            Buffer1[i] = Low[i]; //Set indicator value at Candlestick Low
         }
         else
         {
            Buffer1[i] = EMPTY_VALUE;
         }
      }
      
      //Indicator Buffer 2
      for(int jj = 0; jj<= 20; jj++)
      {
         if(iDeMarker(NULL, PERIOD_CURRENT, 14, jj + i) < 0.80)
         {
            Buffer2[i] = High[i]; //Set indicator value at Candlestick High
         }
         else
         {
            Buffer2[i] = EMPTY_VALUE;
         }
     }
   }
   return(rates_total);
  }
//+------------------------------------------------------------------+



But it is not working.


Can anyone tell me what is the mistake please?

 
Please don't double post! You already have a thread open on this subject! I've answered there.
Loop HELP, it is easy for experts
Loop HELP, it is easy for experts
  • 2022.02.12
  • www.mql5.com
Hi I want to create a loop, but it just work for the first candle...
 
Fernando Carreiro #:
Please don't double post! You already have a thread open on this subject! I've answered there.

I try to write it more clearly

 
#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 3
#property indicator_color1 0xFFAA00
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 3
#property indicator_color2 0xFB00FF
#property indicator_label2 "Sell"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | loop @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexArrow(0, 241);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   SetIndexArrow(1, 242);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, EMPTY_VALUE);
      ArrayInitialize(Buffer2, EMPTY_VALUE);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      
      //Indicator Buffer 1
      bool bLow = true, bHigh = true;

for( int j = 0; j <= 20; j++ )
{
   bLow  &&= iDeMarker( _Symbol, _Period, 14, i + j ) > 0.20;
   bHigh &&= iDeMarker( _Symbol, _Period, 14, i + j ) < 0.80;
};

Buffer1[ i ] = bLow  ? Low[  i ] : EMPTY_VALUE;
Buffer2[ i ] = bHigh ? High[ i ] : EMPTY_VALUE;
         
     
   
   return(rates_total);
  }
//+------------------------------------------------------------------+

This is what happened after I read your comment :)

 
Ahmed Abd El Aziz #: This is what happened after I read your comment :)

Close your loop braces {...} properly.

 

Untested, uncompiled:

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[])
{
   int
      nLookback = 20,
      nlimit    = rates_total - fmax( prev_calculated,  nLookback ) - 1;

   if( prev_calculated < 1 )
   {
      ArrayInitialize( Buffer1, EMPTY_VALUE );
      ArrayInitialize( Buffer2, EMPTY_VALUE );
   };

   for( int i = nLimit; i >= 0; i-- )
   {
      bool bLow = true, bHigh = true;

      for( int j = 0; j <= nLookback; j++ )
      {
         bLow  &&= iDeMarker( _Symbol, _Period, 14, i + j ) > 0.20;
         bHigh &&= iDeMarker( _Symbol, _Period, 14, i + j ) < 0.80;
      };

      Buffer1[ i ] = bLow  ? low[  i ] : EMPTY_VALUE;
      Buffer2[ i ] = bHigh ? high[ i ] : EMPTY_VALUE;
   };

   return(rates_total);
};

In MT4/MQL4, buffers are alread as series. No need to set them AsSeries.

 
Fernando Carreiro #:

Untested, uncompiled:

In MT4/MQL4, buffers are alread as series. No need to set them AsSeries.

look my friend,

I have very little experience in this field

and i don't even reach 1% of your experience,

So I request you to modify this code as you see fit to solve this problem

Because when you ask me to do this, I can't understand you because of the language differences between us and my lack of experience


But when I see the modified code then I will know the difference and what should always be done in these cases

 

Ahmed Abd El Aziz #:

so I request you to modify this code as you see fit to solve this problem

Because when you ask me to do this, I can't understand you because of the language differences between us and my lack of experience

MT4: Learn to code it.
MT5: Begin learning to code it.

If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.

  • Or pay (Freelance) someone to code it. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2019)

  • We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
              No free help (2017)

     
    Ahmed Abd El Aziz #: look my friend, I have very little experience in this field and i don't even reach 1% of your experience, So I request you to modify this code as you see fit to solve this problem

    Because when you ask me to do this, I can't understand you because of the language differences between us and my lack of experience. But when I see the modified code then I will know the difference and what should always be done in these cases

    I'm not your friend. I'm a total stranger to you and you to me. The forum is for helping users who want to learn to code in MQL, not to code it for them.

    If you want someone to code the whole thing for you, then I direct your attention to the Freelance section. Hire someone and pay them to do it for you.

    Trading applications for MetaTrader 5 to order
    Trading applications for MetaTrader 5 to order
    • 2022.02.15
    • www.mql5.com
    The largest freelance service with MQL5 application developers
     
    Fernando Carreiro #:

    I'm not your friend. I'm a total stranger to you and you to me. The forum is for helping users who want to learn to code in MQL, not to code it for them.

    If you want someone to code the whole thing for you, then I direct your attention to the Freelance section. Hire someone and pay them to do it for you.

    When you edit this code i will really learn how to solve this kind of problems and i will not ask about it again.


    This is my way of learning because of my bad English language.


    But everyone in this site is trying as hard as possible to search for benefit, and this is an abhorrent sometimes


    Thank you strangers.

     
    Ahmed Abd El Aziz #: When you edit this code i will really learn how

    When you fix your code, you will really learn how.

    There are no slaves here, waiting to code for you.

    Reason: