Download MetaTrader 5

Lost in the code

To add comments, please log in or register
Learn operation principles of Trading Signals for MetaTrader.
247743
20
247743 2014.01.15 13:39 

What is going on?

This is an Easylanguage port of John Ehlers MESA Stochastic indicator on page 22 of S&C Jan 2014

Am I using the buffers as arrays correct? Are the arrays initialized to 0.0?

The Code:

<REMOVED>

Here is a picture of the output.

Please help

Ubzen
5394
Ubzen 2014.01.15 13:46  
247743
20
247743 2014.01.15 13:48  
output
247743
20
247743 2014.01.15 13:53  
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Yellow
#property indicator_minimum 0
#property indicator_maximum 1
//--- input parameters
extern int       Length=20;
//--- buffers
double ExtMapBuffer1[];
double hp[];
double flit[];
double stoc[];
double alpha=0.0,a1=0.0,b1=0.0,c1=0.0,c2=0.0,c3=0.0,h=0.0,l=0.0,pi=3.14159,q=0.0;
int count=0,i=0,o=0,Counted_bars,j=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1, hp);
   SetIndexBuffer(2, flit);
   SetIndexBuffer(3, stoc);
   
   alpha=(MathCos(.707*(2*pi)/48) + MathSin(.707*(2*pi)/48)-1)/MathCos(.707*(2*pi)/48);
   a1 = MathExp(-1.414*pi/10);
   b1= 2*a1*MathCos(1.414*pi/10);
   c2=b1;
   c3=-a1*a1;
   c1= 1-c2-c3;
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+


int start()
  {
   Counted_bars=IndicatorCounted();
   Print("Counted_bars " + Counted_bars);
//----
   if(Bars<=20) return(0);
   i=Bars-Counted_bars-1;           // Index of the first uncounted
   j=i;
   
   while(i>=0)                      // Loop for uncounted bars
   
   {   
      if(i <= j-Length)
      {  
         hp[i]=(1-alpha/2)*(1-alpha/2)*(Close[i]-2*Close[i+1]+Close[i+2])+2*(1-alpha)*hp[i+1]-(1-alpha)*(1-alpha)*hp[i+2];
         flit[i]=c1*(hp[i]+hp[i+1])/2+c2*flit[i+1]+c3*flit[i+2];
         h=flit[i];
         l=flit[i];
         for (count=0; count < Length; count++)
         {
            if(flit[count]>h) h=flit[count];
            if(flit[count]<l) l=flit[count];
         }
         q = h-l;
         if (q==0) q=1;
         stoc[i]=(flit[i]-l)/q;
         ExtMapBuffer1[i]=c1*(stoc[i] + stoc[i+1])/2+c2*ExtMapBuffer1[i+1]+c3*ExtMapBuffer1[i+2];
        
      }
       i--;                          // Calculating index of the next bar
   }
//----
   return(0);
  }
247743
20
247743 2014.01.15 13:54  
sorry about that ...newbe
whroeder1
14517
whroeder1 2014.01.15 16:23  
mdantzis: Am I using the buffers as arrays correct? Are the arrays initialized to 0.0?

The Code: <REMOVED>

   i=Bars-Counted_bars-1;           // Index of the first uncounted
   j=i;
   while(i>=0){                     // Loop for uncounted bars
      if(i <= j-Length){
         hp[i]=(1-alpha/2)*(1-alpha/2)*(Close[i]-2*Close[i+1]+Close[i+2])
              +2*(1-alpha)*hp[i+1]-(1-alpha)*(1-alpha)*hp[i+2];
  1. Next time use SRC and edit your original post.
  2. Arrays are initialized to EMPTY_VALUE not zero unless you use SetIndexEmptyValue.
  3. Your maximum look back 2 bars so adjust your loop so you do not look at Close[Bars] (does not exist.)
    if(counted_bars < 2) counted_bars = 2; // lookBack
       i=Bars-Counted_bars-1;           // Index of the first uncounted
    

247743
20
247743 2014.01.15 23:04  

thanks, i made the changes.

I initialized the four arrays first 20 elements to 0.0 & made the change for the nonexistent Close[i+2]. however i get the same result, why?

thanks

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Yellow
#property indicator_minimum 0
#property indicator_maximum 1
//--- input parameters
extern int       Length=20;
//--- buffers
double ExtMapBuffer1[];
double hp[];
double flit[];
double stoc[];
double alpha=0.0,a1=0.0,b1=0.0,c1=0.0,c2=0.0,c3=0.0,h=0.0,l=0.0,pi=3.14159,q=0.0;
int count=0,i=0,o=0,Counted_bars,j=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1, hp);
   SetIndexBuffer(2, flit);
   SetIndexBuffer(3, stoc);
   
   alpha=(MathCos(.707*(2*pi)/48) + MathSin(.707*(2*pi)/48)-1)/MathCos(.707*(2*pi)/48);
   a1 = MathExp(-1.414*pi/10);
   b1= 2*a1*MathCos(1.414*pi/10);
   c2=b1;
   c3=-a1*a1;
   c1= 1-c2-c3;
   
   for (i =0;i<=Length;i++)
   {
      ExtMapBuffer1[i]=0.0;
      hp[i]=0.0;
      flit[i]=0.0;
      stoc[i]=0.0;
 }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+


int start()
  {
   Counted_bars=IndicatorCounted();
   Print("Counted_bars " + Counted_bars);
//----
   if(Bars<=20) return(0);
   if(Counted_bars < 2) Counted_bars = 2; // lookBack
   i=Bars-Counted_bars-1;           // Index of the first uncounted        // Index of the first uncounted
   j=i;
   
   while(i>=0)                      // Loop for uncounted bars
   
   {   
      if(i <= j-Length)
      {  
         hp[i]=(1-alpha/2)*(1-alpha/2)*(Close[i]-2*Close[i+1]+Close[i+2])+2*(1-alpha)*hp[i+1]-(1-alpha)*(1-alpha)*hp[i+2];
         flit[i]=c1*(hp[i]+hp[i+1])/2+c2*flit[i+1]+c3*flit[i+2];
         h=flit[i];
         l=flit[i];
         for (count=0; count < Length; count++)
         {
            if(flit[count]>h) h=flit[count];
            if(flit[count]<l) l=flit[count];
         }
         q = h-l;
         if (q==0) q=1;
         stoc[i]=(flit[i]-l)/q;
         ExtMapBuffer1[i]=c1*(stoc[i] + stoc[i+1])/2+c2*ExtMapBuffer1[i+1]+c3*ExtMapBuffer1[i+2];
        
      }
       i--;                          // Calculating index of the next bar
   }
//----
   return(0);
  }

nonexistent Close[i+2, however i get the same result.

whroeder1
14517
whroeder1 2014.01.16 02:51  
mdantzis: I initialized the four arrays first 20 elements to 0.0 & made the change for the nonexistent Close[i+2]. however i get the same result, why?

No you didn't. The Buffers haven't been allocated yet. Like it says in ArrayInitialize

It is not recommended to initialize index buffers in the custom indicator init() function as such functions are initialized automatically with an "empty value" at allocation and re-allocation of buffers.
Why didn't you use SetIndexEmptyValue.
247743
20
247743 2014.01.16 05:33  

if i understand you correctly, the SetIndexEmptyValue will set the empty value to 0.0, however if the true value of the calculation is 0.0 it won't display "Sets drawing line empty value. Empty values are not drawn or shown in the DataWindow". i tried to initialize the first 20 values of my arrays in the start function with a flag to run only once. the first time through the 0.0 displayed, however the next time start ran the 0.0 was gone and -1.#IND0000 is displayed.

the reason i need to initialize the first 20 values is because the loop to find the lowest and highest price of the last 20 bars, yet they don't exist at the start of the run.

additionally, am i using the 1-3 buffers correctly? they are not for display just to hold intermediate calculations per bar. should i just use user defined arrays and if so how do i constantly increase the arrays sizes for each up coming bar in the future?

any other ideas?

thanks

To add comments, please log in or register