New to custom indicator, graphs looking strange

 

hi everyone i am new to custom indicator scripting, so i made a custom indicator that shows the difference in price on a graph in a separated window and i also plan to do add another one that shows the average price difference in the same window

The first graph (price difference) i drew was okay, however once i add in the average graph it messes up the whole thing and the graph just looks strange.

//+------------------------------------------------------------------+
//|                                                     Overlay3.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
//--- input parameters
double Buf_0[], Buf_1[];
extern string    Symbol1="EURUSD";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
SetIndexBuffer(0,Buf_0);         // Assigning an array to a buffer
SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);// Line style
SetIndexBuffer(1,Buf_1);         // Assigning an array to a buffer
SetIndexStyle (1,DRAW_LINE,STYLE_SOLID,2);// Line style
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  int i,
  
  Counted_bars;
   int    counted_bars=IndicatorCounted();
//----
 
   
   

 i=Bars-Counted_bars-1;           // Index of the first uncounted
   while(i>=0)                      // Loop for uncounted bars
     {
     double price1 = iClose(Symbol(),0,i);
     double price2 = iClose(Symbol1,0,i);
     double price3 = MathAbs(price1 - price2);
     double price4 = (price4 + price3)/i;

     
     
      Buf_0[i]=price3;
      Buf_1[i]=price4;
    

      i--;                          // Calculating index of the next bar
     }


//----
   return(0);
  }
//+------------------------------------------------------------------+
 
 double price4 = (price4 + price3)/i;

You are adding 2 prices together, then dividing by the bar shift. That is not an average

 
GumRai:

You are adding 2 prices together, then dividing by the bar shift. That is not an average


thanks!! just realised that.

is it a better idea to use iMA to draw a graph of average price difference or doing it manually is better?

 
     double price1 = iClose(Symbol(),0,i);
     double price2 = iClose(Symbol1,0,i);
You can't do that in the tester. The Symbol() pair is adjusted Bar zero is the current tester bar. But OTHER pairs are not shifted in the tester.
     double price1 = iClose(Symbol(),0,i);
     double price2 = iClose(Symbol1,0,iBarShift(Symbol1,0,Time[i]);
Also make sure you have bars in chart set high enough to get the symbol1 data.
Reason: