Download MetaTrader 5

Ciclo while help me!

To add comments, please log in or register
fly7680
538
fly7680 2016.08.12 08:05 

hello everybody, I'm trying to learn while loop and in this my code I would like the RSI, when it comes to 70/30, must await the descent / ascent to 50 then give me back the Buffer / Cue Ball.

i--; It is correct for the increase of a future candle?

The code does not give me errors but does not work, you could kindly help me?

Thank you


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

#property indicator_type1 DRAW_ARROW
#property indicator_width1 1
#property indicator_color1 Yellow
#property indicator_label1 "Sell"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 1
#property indicator_color2 Yellow
#property indicator_label2 "Buy"

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




extern string RSI       =" Setting RSI";
extern int Period       = 2;
extern double RSI_Max   = 70;
extern double RSI_Min   = 30;

datetime time_alert; //used when sending alert
extern bool Audible_Alerts = true;
double myPoint; //initialized in OnInit




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

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexArrow(0, 159);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexArrow(1, 159);
   //initialize myPoint
   Comment("Copyright © 2016, Trade60Secondi.com");
   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, 0);
      ArrayInitialize(Buffer2, 0);
     }
   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(iRSI(NULL, PERIOD_CURRENT, Periodo, PRICE_CLOSE, 1+i) > RSI_Max //Relative Strength Index > fixed value
      )
      while (iRSI(NULL, PERIOD_CURRENT, Periodo, PRICE_CLOSE, 1+i) > 50)
      i--;
        {                         
         Buffer1[i] = High[i] + 1 * myPoint; 
        }
        
      
        
      //Indicator Buffer 2
      if(iRSI(NULL, PERIOD_CURRENT, Periodo, PRICE_CLOSE, 1+i) < RSI_Min //Relative Strength Index < fixed value
      )
      while (iRSI(NULL, PERIOD_CURRENT, Periodo, PRICE_CLOSE, 1+i) < 50)
      i--;
        {                                        
         Buffer2[i] = Low [i] - 1 * myPoint; 
        }
        
         
      }
      
  return(0);
  } 
   
  
   
//+------------------------------------------------------------------+
fly7680
538
fly7680 2016.08.12 19:42  
A little help please...
Fernando Carreiro
2290
Fernando Carreiro 2016.08.12 20:11  

To be honest, I really do not understand what it is you are trying a accomplish!

All I can say, is that your code is probably not correct in terms of coding syntax. The "while" loop is not set out correctly and maybe it should be more like this:

while( iRSI(NULL, PERIOD_CURRENT, Periodo, PRICE_CLOSE, 1+i) < 50 )
{                                        
   i--;
   Buffer2[i] = Low [i] - 1 * myPoint; 
}

However, even thou the above code is more syntactically correct, I don't understand the coding logic or what it is you are trying to achieve.

fly7680
538
fly7680 2016.08.12 20:26  

RSI must be above 70 (RSI Max), then with the while loop I want to raise a candle until the RSI drops to 50 so I get out of the while loop and then have the buffer1.

sorry for my English!!!

inserting i--; inside the braces solve?


// Indicator Buffer 1
      if(iRSI(NULL, PERIOD_CURRENT, Periodo, PRICE_CLOSE, 1+i) > RSI_Max //1) First condition
      )
      while (iRSI(NULL, PERIOD_CURRENT, Periodo, PRICE_CLOSE, 1+i) > 50) //2) Second condition whit cycle while
      
        { 
         i--;                        
         Buffer1[i] = High[i] + 1 * myPoint; //2) when the while loop condition is not met, then the RSI is below the value of 50 out Buffer 1
        }
        
      
        
      //Indicator Buffer 2
      if(iRSI(NULL, PERIOD_CURRENT, Periodo, PRICE_CLOSE, 1+i) < RSI_Min //Relative Strength Index < fixed value
      )
      while (iRSI(NULL, PERIOD_CURRENT, Periodo, PRICE_CLOSE, 1+i) < 50)
      
        { 
         i--;                                       
         Buffer2[i] = Low [i] - 1 * myPoint; 
        }
        
         
      }
      
  return(0);
  } 
fly7680
538
fly7680 2016.08.12 20:28  
My MT4 get stuck with this code
fly7680
538
fly7680 2016.08.12 20:39  

1) RSI > 70

2)I look the while loop RSI < 50

3) Out Buffer 1


where am I wrong?


thank you

Fernando Carreiro
2290
Fernando Carreiro 2016.08.12 20:41  
fly7680: inserting i--; inside the braces solve? My MT4 get stuck with this code

No, it will not solve your problem. All I did, was show you a possible correct construct for a "while" loop. That is all!

As for the rest, I also said that your code does not make sense to me. I don't understand your code and also I don't understand what it is you are trying to achieve!

fly7680
538
fly7680 2016.08.12 21:31  
      while ( Condition )                                  // Header of the cycle operator
      One operator, the cycle body                  // Cycle body is one operator

My Condition is iRSI(NULL, PERIOD_CURRENT, Periodo, PRICE_CLOSE, 1+i) > 50
Cylce body is one operator is i--;

I do not understand where I'm wrong, the while loop looks correct!

I would wait with a while loop that the condition is no longer fulfilled for the buffer output

Fernando Carreiro
2290
Fernando Carreiro 2016.08.12 23:31  
fly7680:
      while ( Condition )                                  // Header of the cycle operator
      One operator, the cycle body                  // Cycle body is one operator

My Condition is iRSI(NULL, PERIOD_CURRENT, Periodo, PRICE_CLOSE, 1+i) > 50
Cylce body is one operator is i--;

I do not understand where I'm wrong, the while loop looks correct!

I would wait with a while loop that the condition is no longer fulfilled for the buffer output

If it looks correct, why did you place the next statement inside a code block? Is it supposed to execute only once after the loop or many times inside the loop?

{                         
   Buffer1[i] = High[i] + 1 * myPoint; 
}

Also, why are you decremented "i" in the "while" loops, when it is clearly being controlled by the the parent "for" loop?

for(int i = limit-1; i >= 0; i--)
fly7680
538
fly7680 2016.08.13 07:40  
I may have mixed up the "for" loop and while. Eliminating the "for" loop syntax of the while loop should be correct right?
fly7680
538
fly7680 2016.08.13 11:38  
I do not understand this step .. sorry
I declare this: Int a=bars; and operator Cylce body is a--;

Ok? 
12
To add comments, please log in or register