Help coding a custom indicator

 

I have spent a few days exploring MT4 language and I have hit a wall regarding programming something.

SIDE NOTE::::>

I tried using arrays as INT arrays but I could not get it working so I made all arrays double. If anybody knows why  the up[] and dn[] cannot be INT arrays please tell me .

BACK to PROBLEM::::>


I wanted to plot the up BARS count versus the down BARS count over a designed period ( 20) .

I started by successfully plotting a one for the days when close> open denoted by a  value of 1 : labelled  array up[] and a value of -1 when days were c< o  :  labelled array dn[]. [please see code below- full code at very end ]

Now I am trying to total the past 20 bars of each array but having real difficulty.

so I successfuly printed the follwing arrays

double up[]  // equals when a bar finishes with OPEN > CLOSE and has a 1 in each location for true result

double dn[] // equals when a bar finishes with OPEN < CLOSE and has a -1 in each location for true result

double  eq[] // if OPEN == CLOSE .

Here is a snipet of  what I did to try and create the 20 period sum of the array.

//-set up for loop limits//////////////////////////////
   int counted_bars=IndicatorCounted();
   int i,j,k;
  
   if(counted_bars > 0)
   counted_bars--;

   int limit = Bars-counted_bars;

////////////////////////////////////////////////

   int limitupdn = limit  - 20 ;  / limit the calculation 20 bars from the end of array as it is a 20 day period summation         
             for( k=0; k < limitupdn ; k++) // loop to store in new array
             {
                        for( j=0; j < 20; j++) // loop to add up 20 days of array values
                        {
                                                  upma[k]  += up[k+j];
                        }
             }

A loop Example:

so @ k = 0  the first array entry  upma[0] += up[0+ ( j loops 0 to 19)]   // ie loops and adds up[0] through to up[19] summated .

so @ k = 1  the first array entry  upma[1] += up[1+ ( j loops 0 to 19)]   // ie loops and adds up[1] through to up[20]summated .

So upma[] sums 20 periods back of up[].

I tried this but it gave me really large values and I am at a loss what I am doing wrong.


Help would be greatly appreciated.

Full code is below: Cut and PASTE and run COMPILER and ADD TO a CHART

:::::::::::8<:::::::::::::::::::::8<:::::::::::::::::::::8<:::::::::::::::::::::8<:::::::::::::::::::::8<:::::::::::::::::::::8<:::::::::::::::::::::8<:::::::::::::::::::::8<::::::::::

//+------------------------------------------------------------------+
//|                                              zMF_basics_UPDN.mq4 |
//|                                                              moz |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+

///////////////////////////////////////////////////////////////
//     __________TESTED USING MINUTE TIME FRAMES___________
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////


#property copyright "moz"
#property link      "http://www.metaquotes.net"
#property indicator_separate_window
#property indicator_buffers 5       // Amount of buffers


#property indicator_color1 White
#property indicator_color2 Yellow
#property indicator_color3 Orange
#property indicator_color4 Green

 
int upp ;
double tempu;
double up[] ;
double dn[] ;
double eq[] ;
double upma[] ;

 
                    
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(4);
   IndicatorShortName("up"+up[0]);
   SetIndexDrawBegin(1,dn);
   IndicatorDigits(Digits+1);
     
   SetIndexBuffer(0,up);         // Assigning the array to the buffer
   SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);// Line style
   SetIndexLabel (0,"up");

   SetIndexBuffer(1,dn);         // Assigning the array to the buffer
   SetIndexStyle (1,DRAW_LINE,STYLE_SOLID,2);// Line style
   SetIndexLabel (1,"dn");

   SetIndexBuffer(2,eq);         // Assigning the array to the buffer
   SetIndexStyle (2,DRAW_LINE,STYLE_SOLID,2);// Line style
   SetIndexLabel (2,"eq");
 
   SetIndexBuffer(3,upma);         // Assigning the array to the buffer
   SetIndexStyle (3,DRAW_LINE,STYLE_SOLID,2);// Line style
   SetIndexLabel (3,"upma");
 
 //  SetIndexBuffer(4,dn);         // Assigning the array to the buffer
 //  SetIndexStyle (4,DRAW_LINE,STYLE_SOLID,2);// Line style
  // SetIndexLabel (4,"dnma");

   
 
//---- indicators
 
   //----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
 //----------------------------------------



//----------------------------------------
   int counted_bars=IndicatorCounted();
   int i,j,k;
   
   if(counted_bars > 0)
   counted_bars--;
   int limit = Bars-counted_bars;
   
     
   for( i=0; i<limit; i++)                      // Cycle for the uncounted bars
     {
                        
            
            if  ( iClose(NULL,PERIOD_M1,i) > iOpen(NULL,PERIOD_M1,i ) )
              {
                    if  ( iClose(NULL,PERIOD_M1,i) == iOpen(NULL,PERIOD_M1,i ) )
                              {
                                 up[i] = 0;
                                 dn[i] = 0;
                                 eq[i] = 1;  
                              }
                    else
                              {
                                 up[i] = 1;
                                 dn[i] = 0;
                                 eq[i] = 0;  
                        
                              }
               }
            else
                  {
                       up[i] = 0;
                       dn[i] = -1;
                       eq[i] = 0;
                  }   
                   
    }
 //////////////////// UNCOMMENT THIS SECTION TO ENABLE THE SUMMATION  Array
 //////////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////////////////////////////////////
   
 /*  
   int limitupdn = limit  - 20 ;           
             for( k=0; k < limitupdn ; k++)
             {
                        for( j=0; j < 20 ; j++)
                        {
                        upma[k]  += up[k+j];}
                         }
 
*/

//----
   return(0);
  }
//+------------------------------------------------------------------+
 
dasser:

I have spent a few days exploring MT4 language and I have hit a wall regarding programming something.

I wanted to plot the up BARS versus the down BARS count over a designed period.

I started by successfully plotting a one on the days when close> open denoted by a  value of 1 : labelled  array up[] and a value of -1 when days were c< o  :  labelled array dn[]. [please see code below- full code at very end ]

Now I am trying to total the past 20 days of each array but having real difficulty.

so I successfuly printed the follwing arrays

<CODE REMOVED>

Please edit your post . . .    please use the   SRC   button to post code: How to use the   SRC   button.
 
so I successfuly printed the follwing arrays

double up[]  // equals when a bar finishes with OPEN > CLOSE and has a 1 in each location for true result

double dn[] // equals when a bar finishes with OPEN < CLOSE and has a -1 in each location for true result

double  eq[] // if OPEN == CLOSE .

Here is a snipet of  what I did to try and create the 20 period sum of the array.

//-set up for loop limits//////////////////////////////
   int counted_bars=IndicatorCounted();
   int i,j,k;
  
   if(counted_bars > 0)
   counted_bars--;

   int limit = Bars-counted_bars;

////////////////////////////////////////////////

   int limitupdn = limit  - 20 ;  / limit the calculation 20 bars from the end of array as it is a 20 day period summation         
             for( k=0; k < limitupdn ; k++) // loop to store in new array
             {
                        for( j=0; j < 20; j++) // loop to add up 20 days of array values
                        {
                                                  upma[k]  += up[k+j];
                        }
             }

A loop Example:

so @ k = 0  the first array entry  upma[0] += up[0+ ( j loops 0 to 19)]   // ie loops and adds up[0] through to up[19] summated .

so @ k = 1  the first array entry  upma[1] += up[1+ ( j loops 0 to 19)]   // ie loops and adds up[1] through to up[20]summated .

So upma[] sums 20 periods back of up[].

I tried this but it gave me really large values and I am at a loss what I am doing wrong.


Help would be greatly appreciated.

Full code is below: Cut and PASTE and run COMPILER and ADD TO a CHART


//+------------------------------------------------------------------+
//|                                              zMF_basics_UPDN.mq4 |
//|                                                              moz |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+

///////////////////////////////////////////////////////////////
//     __________TESTED USING MINUTE TIME FRAMES___________
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////


#property copyright "moz"
#property link      "http://www.metaquotes.net"
#property indicator_separate_window
#property indicator_buffers 5       // Amount of buffers


#property indicator_color1 White
#property indicator_color2 Yellow
#property indicator_color3 Orange
#property indicator_color4 Green

 
int upp ;
double tempu;
double up[] ;
double dn[] ;
double eq[] ;
double upma[] ;

 
                    
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(4);
   IndicatorShortName("up"+up[0]);
   SetIndexDrawBegin(1,dn);
   IndicatorDigits(Digits+1);
     
   SetIndexBuffer(0,up);         // Assigning the array to the buffer
   SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);// Line style
   SetIndexLabel (0,"up");

   SetIndexBuffer(1,dn);         // Assigning the array to the buffer
   SetIndexStyle (1,DRAW_LINE,STYLE_SOLID,2);// Line style
   SetIndexLabel (1,"dn");

   SetIndexBuffer(2,eq);         // Assigning the array to the buffer
   SetIndexStyle (2,DRAW_LINE,STYLE_SOLID,2);// Line style
   SetIndexLabel (2,"eq");
 
   SetIndexBuffer(3,upma);         // Assigning the array to the buffer
   SetIndexStyle (3,DRAW_LINE,STYLE_SOLID,2);// Line style
   SetIndexLabel (3,"upma");
 
 //  SetIndexBuffer(4,dn);         // Assigning the array to the buffer
 //  SetIndexStyle (4,DRAW_LINE,STYLE_SOLID,2);// Line style
  // SetIndexLabel (4,"dnma");

   
 
//---- indicators
 
   //----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
 //----------------------------------------



//----------------------------------------
   int counted_bars=IndicatorCounted();
   int i,j,k;
   
   if(counted_bars > 0)
   counted_bars--;
   int limit = Bars-counted_bars;
   
     
   for( i=0; i<limit; i++)                      // Cycle for the uncounted bars
     {
                        
            
            if  ( iClose(NULL,PERIOD_M1,i) > iOpen(NULL,PERIOD_M1,i ) )
              {
                    if  ( iClose(NULL,PERIOD_M1,i) == iOpen(NULL,PERIOD_M1,i ) )
                              {
                                 up[i] = 0;
                                 dn[i] = 0;
                                 eq[i] = 1;  
                              }
                    else
                              {
                                 up[i] = 1;
                                 dn[i] = 0;
                                 eq[i] = 0;  
                        
                              }
               }
            else
                  {
                       up[i] = 0;
                       dn[i] = -1;
                       eq[i] = 0;
                  }   
                   
    }
 //////////////////// UNCOMMENT THIS SECTION TO ENABLE THE SUMMATION  Array
 //////////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////////////////////////////////////
   
 /*  
   int limitupdn = limit  - 20 ;           
             for( k=0; k < limitupdn ; k++)
             {
                        for( j=0; j < 20 ; j++)
                        {
                        upma[k]  += up[k+j];}
                         }
 
*/

//----
   return(0);
  }
//+------------------------------------------------------------------+


 
Visuals
 

I think i would have chosen

SetIndexStyle(0, DRAW_HISTOGRAM, STYLE_SOLID, 2);

with

#property indicator_minimum 0.0
#property indicator_maximum 1.0

 color green up    color red down

 

 

instead of  


SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);
 
  1. dasser: If anybody knows why  the up[] and dn[] cannot be INT arrays please tell me .

    What is the type of the second argument of SetIndexBuffer?


  2. up[i] = 0;
    dn[i] = 0;
    Why are you setting them to zero when you haven't specified an empty value?
 
WHRoeder:
  1. dasser: If anybody knows why  the up[] and dn[] cannot be INT arrays please tell me .

    What is the type of the second argument of SetIndexBuffer?


  2. Why are you setting them to zero when you haven't specified an empty value?

1. I am not sure exactly what you are asking . I set these arrays to Double type. originally I set them as Int up[] but the code never produced a line. I tried debugging but could not work out why when I changed to Double everything seemd ok , however , instead of values 1,2,3,4 etc I had , 1.000000 ,2.0000000 which is untidy.

with SetIndexbuffer  I then set that as 

SetIndexBuffer(0,up); 

Have I missed something ?

2. I only want the Array up[] to store if the Bar is UP ( value = 1 ) . The other Bars will be denoted by ( values 0 ) for equal bar and or Down Bar.

How do I specify an empty value ? and do I need to specify an empty value ? Doesn't declaring the array Double up[] automatically set to zero ?


Thanks

 
dasser:

1. I am not sure exactly what you are asking . I set these arrays to Double type. originally I set them as Int up[] but the code never produced a line. I tried debugging but could not work out why when I changed to Double everything seemd ok , however , instead of values 1,2,3,4 etc I had , 1.000000 ,2.0000000 which is untidy.

with SetIndexbuffer  I then set that as 

SetIndexBuffer(0,up); 

Have I missed something ?

2. I only want the Array up[] to store if the Bar is UP ( value = 1 ) . The other Bars will be denoted by ( values 0 ) for equal bar and or Down Bar.

How do I specify an empty value ? and do I need to specify an empty value ? Doesn't declaring the array Double up[] automatically set to zero ?


Thanks

If you are reffering to :

if  ( iClose(NULL,PERIOD_M1,i) == iOpen(NULL,PERIOD_M1,i ) )
                              {
                                 up[i] = 0;
                                 dn[i] = 0;
                                 eq[i] = 1;  
                              }
This loop is active when the bar open and close are equal values so it is nether a up or down bar and therefore arrayas up and dn must be zero values.
 
deVries:

I think i would have chosen

with

 color green up    color red down

 

 

instead of  


 

 

 

 
dasser:



I like the max and min commands and colours are good too .. so I changed it :

It did not seem to make a difference for my graph .. not sure why . The values need to be from

-1 to 1 because dn[]  is between 0 to -1 and I actually made them -2 to 2 but I had no success changing the grid.

#property indicator_minimum -2.0
#property indicator_maximum 2.0

#property indicator_color1 Green 
#property indicator_color2 Red 
 
dasser:

1. I am not sure exactly what you are asking . I set these arrays to Double type. originally I set them as Int up[] but the code never produced a line. I tried debugging but could not work out why when I changed to Double everything seemd ok , however , instead of values 1,2,3,4 etc I had , 1.000000 ,2.0000000 which is untidy.

with SetIndexbuffer  I then set that as  SetIndexBuffer(0,up); 

Have I missed something ?

2. I only want the Array up[] to store if the Bar is UP ( value = 1 ) . The other Bars will be denoted by ( values 0 ) for equal bar and or Down Bar.

How do I specify an empty value ? and do I need to specify an empty value ? Doesn't declaring the array Double up[] automatically set to zero ?

  1. I asked you simple question,
    What is the type of the second argument of SetIndexBuffer?
    what does the manual say. Let me post it and highlight the question and answer in red
    bool SetIndexBuffer( int index, double array[])
    But you couldn't bother to look at the links posted.

  2. No they are not set to zero, they are indicator buffers. Why didn't you look at what the manual says. Again, let me post it and highlight the question and answer in red
    void SetIndexEmptyValue( int index, double value)
    Sets drawing line empty value. Empty values are not drawn or shown in the DataWindow. By default, empty value is EMPTY_VALUE. 
    But you couldn't bother to look at the links posted.
  3. RTFM and the links
Reason: