Request for help on 'break" function

 

Good afternoon,


I have the following question. I want  the code to determine the deviation of an envelope line that is between the upper and lower envelopes with different parameters.

I used the explanation on this page: https://book.mql4.com/operators/break

The code I have compiles without errors, so it looks like I'm doing something wrong at the logical level.

double DevStep,Deviation;
   double M15UppEnv  = iEnvelopes(NULL, PERIOD_M5,63,1,0,PRICE_CLOSE,0.063,MODE_UPPER,i+1); // prijs van de M15-upper envelope deviatie 0,063 bar 0
   double M15LowEnv  = iEnvelopes(NULL, PERIOD_M5,63,1,0,PRICE_CLOSE,0.063,MODE_LOWER,i+1); // prijs van de M15-lower envelope deviatie 0,063 bar 0

   for (DevStep = 0.125; DevStep <=3.000; DevStep++)
      
   {
      
         if (iEnvelopes(NULL, PERIOD_M5,1008,1,0,PRICE_CLOSE,DevStep,MODE_UPPER,i+1) > M15LowEnv
         && (iEnvelopes(NULL, PERIOD_M5,1008,1,0,PRICE_CLOSE,DevStep,MODE_UPPER,i+1) < M15UppEnv))
         
         
         break;
      
      Deviation = DevStep;
      
   }
      
   
     ExtMapBuffer1[i] = (1+Deviation/100)*iMA(NULL,0,MA_Period,0,MA_Method,Applied_Price,i);
     ExtMapBuffer2[i] = (1-Deviation/100)*iMA(NULL,0,MA_Period,0,MA_Method,Applied_Price,i);
          
   
     
//---- done
   return(0);
  }
Operator 'break' - Operators - MQL4 Tutorial
Operator 'break' - Operators - MQL4 Tutorial
  • book.mql4.com
In some cases, for example, when programming some cycle operations, it could become necessary to break the execution of a cycle before its condition becomes false. In order to solve such problems, you should use the operator 'break'. Format of the Operator 'break' The operator 'break' consists of only one word and ends in character...
 
         //If you want the current value
         if (iEnvelopes(NULL, PERIOD_M5,1008,1,0,PRICE_CLOSE,DevStep,MODE_UPPER,i+1) > M15LowEnv
         && (iEnvelopes(NULL, PERIOD_M5,1008,1,0,PRICE_CLOSE,DevStep,MODE_UPPER,i+1) < M15UppEnv))
         {
         Deviation = DevStep;
         break;
         }

 

Hi Lorentzos, thanks for your swift reaction.

I applied the changes you recommended, compilation OK, but the line is not drawn. Did I make an other error in this peace of code?

 
   for (int iDevStep = 125; iDevStep <=3000; iDevStep++)  
   {
         DevStep=iDevStep;
         DevStep=DevStep/1000;
         if (iEnvelopes(NULL, PERIOD_M5,1008,1,0,PRICE_CLOSE,DevStep,MODE_UPPER,i+1) > M15LowEnv
         && (iEnvelopes(NULL, PERIOD_M5,1008,1,0,PRICE_CLOSE,DevStep,MODE_UPPER,i+1) < M15UppEnv))
         {
         Deviation = DevStep;
         break;
         }      
         Deviation = DevStep;      
   }

Try this , sorry

 

After your first comment I checked the code and saw that e "for" function was missing, so I added it and tha the line was drawn, only not at the envelope level but at the EMA level.

When I added your second comment, the line was not drawn anymore.

The code looks like this now

double DevStep,Deviation;
   double M15UppEnv  = iEnvelopes(NULL, PERIOD_M5,63,1,0,PRICE_CLOSE,0.063,MODE_UPPER,i+1); // prijs van de M15-upper envelope deviatie 0,063 bar 0
   double M15LowEnv  = iEnvelopes(NULL, PERIOD_M5,63,1,0,PRICE_CLOSE,0.063,MODE_LOWER,i+1); // prijs van de M15-lower envelope deviatie 0,063 bar 0

   for (DevStep = 0.125; DevStep <=3.000; DevStep++)
      
   {
      
         if (iEnvelopes(NULL, PERIOD_M5,1008,1,0,PRICE_CLOSE,DevStep,MODE_UPPER,i+1) > M15LowEnv
         && (iEnvelopes(NULL, PERIOD_M5,1008,1,0,PRICE_CLOSE,DevStep,MODE_UPPER,i+1) < M15UppEnv))
         
      {  
         Deviation = DevStep;
         break;
      }
      
      Deviation = DevStep;
      
      
   }
   
   for( i=0; i<limit; i++)
      
   {
     ExtMapBuffer1[i] = (1+Deviation/100)*iMA(NULL,0,MA_Period,0,MA_Method,Applied_Price,i);
     ExtMapBuffer2[i] = (1-Deviation/100)*iMA(NULL,0,MA_Period,0,MA_Method,Applied_Price,i);
          
   }
     
//---- done
   return(0);
  }
 
Okay . Are you trying to optimize something for each bar ? 
 

No, the sole purpose of the indicator is to draw an envelope line that is between the values of an other upper and lower envelope.

To do that de codes needs to establish the Deviation based on the steps "DevStep"

Your first comment certainly helped me the line gets drawn now, but not yet at the right level.

 
i get it . MT4?
 
Yes
 

I notice that the lines that are drawn only react on the max DevStep value. When I change that value from 3.000 to 1.000 the lines jump to an other level, but not the 1.000 level.

I cannot explain this right now.


I have to go now, may I call on you later?


Thanks for your help, enjoy your day


best regards, Robert

 
Yes its interesting .

Try this : 

//+------------------------------------------------------------------+
//|                                                    Envelopes.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots   4
//--- plot OriginalUpper
#property indicator_label1  "OriginalUpper"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrWhite
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot OriginalLower
#property indicator_label2  "OriginalLower"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrWhite
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot SeekUpper
#property indicator_label3  "SeekUpper"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrDodgerBlue
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- plot SeekLower
#property indicator_label4  "SeekLower"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrCrimson
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1

input ENUM_MA_METHOD MA_Method=MODE_SMA;
input ENUM_APPLIED_PRICE Applied_Price=PRICE_CLOSE;
input int MA_Period=20;

//--- indicator buffers
double         OriginalUpperBuffer[];//M15
double         OriginalLowerBuffer[];//M15
double         SeekUpperBuffer[];
double         SeekLowerBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,OriginalUpperBuffer);
   SetIndexBuffer(1,OriginalLowerBuffer);
   SetIndexBuffer(2,SeekUpperBuffer);
   SetIndexBuffer(3,SeekLowerBuffer);
   
//---
   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 begin=rates_total-prev_calculated;
   if(begin>100) begin=begin-10;
//--- the main loop of calculations
   for(int i=begin; i>=0 && !IsStopped(); i--)
     {
     double DevStep=0.125;
     double Deviation=0;
     double M15UppEnv  = iEnvelopes(NULL, PERIOD_M15,63,1,0,PRICE_CLOSE,0.063,MODE_UPPER,i+1); // prijs van de M15-upper envelope deviatie 0,063 bar 0
     double M15LowEnv  = iEnvelopes(NULL, PERIOD_M15,63,1,0,PRICE_CLOSE,0.063,MODE_LOWER,i+1); // prijs van de M15-lower envelope deviatie 0,063 bar 0
     //seek 
     int seek_step=25;
     for(int seek=125;seek<=3000;seek+=seek_step)
     {
       DevStep=seek;
       DevStep=DevStep/1000;
       double env=iEnvelopes(NULL,PERIOD_M5,108,1,0,PRICE_CLOSE,DevStep,MODE_UPPER,i+1);
       if(env>M15LowEnv&&env<M15UppEnv)
         {
         Deviation = DevStep;
         break;
         }
         Deviation=DevStep;
     }
     //seek ends here 
     OriginalUpperBuffer[i]=M15UppEnv;
     OriginalLowerBuffer[i]=M15LowEnv;
     double ma=iMA(NULL,0,MA_Period,0,MA_Method,Applied_Price,i+1);
     SeekUpperBuffer[i]=(1+Deviation/100)*ma;
     SeekLowerBuffer[i]=(1-Deviation/100)*ma;
     }
//---   
//--- return value of prev_calculated for next call
   return(rates_total);
  }