Request for help on 'break" function - page 3

 
ReactoFX:
Yes, that is the idea, but although the code seems right, it does not execute right

Great , might i ask about the 1008 to 63 ? why this pair ?

Great = We are indeed checking the upper line only , add another clause in the if statement for the lower line.

 
Also , when it does not find a match , it should use the previous DevStep correct ?
 

These are just the MA settings I use.

Adding the lower line now will not change the functioning of the calculation. First I need the code to be working correctly and than I can add the lower line condition.

 

These are just the MA settings I use.

Adding the lower line now will not change the functioning of the calculation. First I need the code to be working correctly and than I can add the lower line condition.

 
Adding the Lower Line will change the "idea" of how it operates , regardless of whether or not the code is correct .
The idea with the lower line is closer to what you are trying to accomplish so i would do that first .


//+------------------------------------------------------------------+
//|                                                    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;
enum ds_use
{
ds_use_valid=0,//Only if found
ds_use_last=1,//Last loop value
ds_use_closest=2//Closest possible - if not found
};
input ds_use use_last_value=ds_use_valid;
//--- 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);
  }
  double previous_dev_step=-1;
//+------------------------------------------------------------------+
//| 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 CDevStep=0.125;//closest dev step
     double minimum=0;
     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=125;
     for(int seek=125;seek<=3000;seek+=seek_step)
     {
       DevStep=seek;
       DevStep=DevStep/1000;
       double env_up=iEnvelopes(NULL,PERIOD_M5,108,1,0,PRICE_CLOSE,DevStep,MODE_UPPER,i+1);
       double env_dw=iEnvelopes(NULL,PERIOD_M5,108,1,0,PRICE_CLOSE,DevStep,MODE_LOWER,i+1);
       if(env_up>M15LowEnv&&env_up<M15UppEnv&&env_dw>M15LowEnv&&env_dw<M15UppEnv)
         {
         Deviation = DevStep;
         break;
         }
         //closest calcs
         double dist_up=env_up-M15UppEnv;
              dist_up=MathAbs(dist_up);
         double dist_dw=env_dw-M15LowEnv;
              dist_dw=MathAbs(dist_dw);
         double dist=dist_up+dist_dw;
         if(seek==125)
         {
         minimum=dist;
         CDevStep=DevStep;
         }
         if(seek>125)
         {
         if(dist<minimum) 
           {
           minimum=dist;
           CDevStep=DevStep;           
           }
         }
         //closest calcs end here          
         Deviation=DevStep;
         if(seek==3000&&use_last_value==ds_use_valid) Deviation=previous_dev_step;
         if(seek==3000&&use_last_value==ds_use_closest) Deviation=CDevStep;

     }
     //seek ends here 
     OriginalUpperBuffer[i]=M15UppEnv;
     OriginalLowerBuffer[i]=M15LowEnv;
      //if value is valid 
      if(Deviation!=-1)
      {
      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;
      }
      previous_dev_step=Deviation;
      //if value is valid ends here 
     }
//---   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   
  }
//+------------------------------------------------------------------+
 

Hi Lorentzos,


Thanks for the work your putting into my request.

The code you send can draws two lines of period 63, but not the lines of the 1008 period, and its the last one that is required. As the code is now, it will never work though because the logic in  Line 102 excludes every possibility.

The upper envelope and the lower envelope of a slow period (1008) can never be between the fast period envelopes at the same time.


It is like I explained before, just one of the two slow envelopes either the upper or the lower will be between the fast envelopes at any given time. That is why I road that once the code for the upper slow envelope is working, the code for the lower slow envelope is easily added.

 

Hi Lorentzos,


I made some changes to your code, and guess what,..... it is working.

Thanks a lot for the help you gave me.


I wish you a very nice evening and prosperous trading.


Robert

 
ReactoFX:

Hi Lorentzos,


I made some changes to your code, and guess what,..... it is working.

Thanks a lot for the help you gave me.


I wish you a very nice evening and prosperous trading.


Robert

No problem . Enjoy :)

Reason: