Create a custom indicator

 

Hi, I'm trying for the first time to write an indicator,

I have a function that works, like a sort of "density" calculation, but converting it in similar indicator still not working...maybe i still miss some passages,

function


double density(string currency, int timeframe, int period, int shift){

double index_max;
double index_min;
double val_max;
double val_min;
int val_dif;
double  tot_volume;
double val_max_each;
double val_min_each;
int val_dif_each;
double val_dif_each_tot;



index_max  = iHighest(0,timeframe,MODE_HIGH,period,shift);
index_min  = iLowest(0,timeframe,MODE_LOW,period,shift);
val_max  = iHigh(0,timeframe,index_max);
val_min  = iLow(0,timeframe,index_min);
val_dif = (val_max - val_min)/_Point;
tot_volume = val_dif*period;

for(i=0; i < period; i++){
val_max_each  = iHigh(0,timeframe,i);
val_min_each  = iLow(0,timeframe,i);

val_dif_each = (val_max_each - val_min_each)/_Point;

val_dif_each_tot = val_dif_each_tot + val_dif_each;

}

double val_perc_tot =  NormalizeDouble((val_dif_each_tot/tot_volume)*100,2);

Print("tot_volume: ",tot_volume," val_dif_each_tot: ",val_dif_each_tot," val_perc_tot: ",val_perc_tot);

}


indicator


#property description "Density"
#property strict

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//--- input parameter
input int InpDensPeriod=30;  // Momentum Period
//--- buffers
  double ExtDensBuffer[];
   double index_max;
   double index_min;
   double val_max;
   double val_min;
   int val_dif;
   double  tot_volume;
   double val_max_each;
   double val_min_each;
   int val_dif_each;
   double val_dif_each_tot;
   int i;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   string short_name;
//--- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtDensBuffer);
//--- name for DataWindow and indicator subwindow label
   short_name="Density("+IntegerToString(InpDensPeriod)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//--- check for input parameter
   if(InpDensPeriod<=0)
     {
      Print("Wrong input parameter Density Period=",InpDensPeriod);
      return(INIT_FAILED);
     }
//---
   SetIndexDrawBegin(0,InpDensPeriod);
//--- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Momentum                                                         |
//+------------------------------------------------------------------+
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[]
                            
                
                )
  {
      index_max  = iHighest(0,0,MODE_HIGH,InpDensPeriod,0);
      index_min  = iLowest(0,0,MODE_LOW,InpDensPeriod,0);
      val_max  = iHigh(0,0,index_max);
      val_min  = iLow(0,0,index_min);
      val_dif = (val_max - val_min)/_Point;
      tot_volume = val_dif*InpDensPeriod;
      
      for(i=0; i < InpDensPeriod; i++){
      val_max_each  = iHigh(0,0,i);
      val_min_each  = iLow(0,0,i);
      
      val_dif_each = (val_max_each - val_min_each)/_Point;
      
      val_dif_each_tot = val_dif_each_tot + val_dif_each;
      
      }

   double val_perc_tot =  NormalizeDouble((val_dif_each_tot/tot_volume)*100,2);
   
   Print("val_perc_tot:",val_perc_tot);
   return(val_perc_tot);
  }
//+------------------------------------------------------------------+
 
 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 i, limit;
        
   if (prev_calculated < 0)
       return (- 1 );
   if (prev_calculated == 0)
      limit = rates_total - InpDensPeriod;
   else 
      limit = rates_total - prev_calculated + 1;
        
   for (i = limit; i >= 0 ; i--)
      ExtDensBuffer[i] = density(Symbol(), PERIOD_CURRENT, InpDensPeriod, i);
        
   return (rates_total);
}
//+------------------------------------------------------------------+ 
double density( string currency, int timeframe, int period, int shift){
.
.
.
.

Since you already have a function, you can just call it like this.

 
   for (i = limit; i >= 0 ; i--)
      ExtDensBuffer[i] = density(Symbol(), PERIOD_CURRENT, InpDensPeriod, i);
Don't mix apples and oranges.
 
whroeder1 :
Don't mix apples and oranges .
What is the problem?
 
the problem is that I want to see the graph. 
 
ale mariani:
the problem is that I want to see the graph. 
Files:
Density.mq4  4 kb
 
Naguisa Unada:
i will try and i will tell you. THANKS A LOT
 

Naguisa, it works thank you so much, and I like it.


The result should be in percentage....but i'll fix it no problem! :)

Reason: