20 days average distance from the EMA

 
I need to measure an average 20 days of daily price action from the distance of the 200 day ema on a daily basis. Can anyone help me how I would do this on an indicator with a separate window please???
 
  1. Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help
              urgent help.

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum

  2. Get the EMA, loop through the 20 days prices, sum the absolute value of the differences. Average is sum divided by 20. What's the problem?
 
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2

#property indicator_maximum 100
#property indicator_minimum -100

//--- plot fMA3
#property indicator_label1  "Average distance "
#property indicator_label2  "Price "

#property indicator_type1   DRAW_LINE
#property indicator_color1  clrBlue
#property indicator_width1  4

input int            InpMAShift=0;          // Shift
input ENUM_MA_METHOD InpMAMethod=MODE_EMA;  // Method

#property indicator_style1  STYLE_SOLID
double dLastPrice = -1;

double dMaxPositiveValue = 0;
double dMaxNegativeValue = 0;

//--- indicator buffers
double         fMA3Buffer[];
double         fMA3Buffer2[];

double         fMA3BufferBackup[];

bool bProcessPositive = false;
bool bProcessNegative = false;

input int iDaysForAverage = 20; // How many days to average

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,fMA3Buffer);
   SetIndexBuffer(1,fMA3Buffer2);
      
   IndicatorShortName("SIERI (" + iDaysForAverage +")");
   
   IndicatorSetInteger(INDICATOR_LEVELS,0);
   
   
//---
   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[])
{

   double dCurrentPrice = Open[0];
 
bProcessPositive = false;
bProcessNegative = false;

double dMax = 0;
double dMin = 0;

int i;
int limit=rates_total-prev_calculated;
bool bScale = false;
for(i=0; i<(limit - iDaysForAverage - 1 ); i++)
   {


   double dAveragePrice = 0.0;
   for (int iDays = iDaysForAverage; iDays >= 1; iDays--)
   {     
       double dCloseValOnDay = iMA(NULL,PERIOD_D1,1,0,MODE_SMA,PRICE_CLOSE,(i + iDays));          
       dAveragePrice += NormalizeDouble(dCloseValOnDay,Digits);       
   }

   dAveragePrice = ( (double) (dAveragePrice / (double)iDaysForAverage ));
   
   double fMAOnDay=iMA(NULL,PERIOD_D1,200,InpMAShift,InpMAMethod,PRICE_CLOSE, i);   
   
   double dAverageDistance = dAveragePrice - fMAOnDay;   
    
   
   double pipMultiplier = MathPow(10,MathMod(Digits,2));
   dAverageDistance = dAverageDistance / (pipMultiplier*Point);
   
   
   
   


     fMA3Buffer[i]=dAverageDistance;
     

  
   }   
   
   
   
   
 
      
   return(rates_total);
   
}

bool AreDoublesEqual(double number1,double number2)
{
   if(NormalizeDouble(number1-number2,Digits)==0) 
      return(true);
   else 
      return(false); 
}
//+------------------------------------------------------------------+


double ConvertPips(double pips)
{
   //4 ot 2 decimal currency/instrument pairs hence a 4 digit broker
   if(MathMod(Digits,2)==0){
      return pips/MathPow(10,Digits);
   }
   //5 or 3 decimal currency/instrument  pairs hence a 5 digit broker
   else{
      return NormalizeDouble((pips/MathPow(10,Digits))*10,Digits);
   }
}
 

The problem I have is that I want this to be scaled from 100 / -100 but some of the average distances on say 1000 bars ago are something like 1200 pips etc so the graph on the chart looks off when its all scaled down to 100 because the current chart I am looking at has distance values up to 40 so when its scaled down it squashes it all.