//+------------------------------------------------------------------+
//|                                             Angle of average.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property  copyright "mladen"
#property  link      "www.forex-station.com"

#property  indicator_separate_window
#property  indicator_buffers 4
#property indicator_label1  "Angle of average up"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrBlue
#property indicator_width1  5

#property indicator_label2  "Angle of average down"
#property indicator_type2   DRAW_HISTOGRAM
#property indicator_color2  clrRed
#property indicator_width2  5

#property indicator_label3  "Angle of average neutral"
#property indicator_type3   DRAW_HISTOGRAM
#property indicator_color3  clrBlack

#property indicator_label4  "Angle of average"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrBlack
#property indicator_width4  2
#property strict



enum maTypes
{
   ma_sma,  // Simple moving average
   ma_ema,  // Exponential moving average
   ma_smma, // Smoothed moving average
   ma_lwma, // Linear weighted moving average
};

input int                MAPeriod         =  55;         // Average period
input ENUM_MA_METHOD     MAType           = MODE_EMA;    // Average type to use
input ENUM_APPLIED_PRICE MAAppliedPrice   = PRICE_CLOSE; // Price to use
input double             AngleLevel       = 0;           // Angle treshold level
input int                AngleBars        = 1;           // Bars used for angle normalization

double Buffer1[],Buffer2[],Buffer3[],Buffer4[],state[],avg[];
string avgNames[] = {"SMA","EMA","SMMA","LWMA"};

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int OnInit()
{
   IndicatorBuffers(6);
   SetIndexBuffer(0,Buffer1,INDICATOR_DATA); 
   SetIndexBuffer(1,Buffer2,INDICATOR_DATA); 
   SetIndexBuffer(2,Buffer3,INDICATOR_DATA); 
   SetIndexBuffer(3,Buffer4,INDICATOR_DATA);
   SetIndexBuffer(4,state);
   SetIndexBuffer(5,avg);
      SetLevelValue(0, AngleLevel);
      SetLevelValue(1,-AngleLevel);
      
      IndicatorSetInteger(INDICATOR_LEVELS,2);
      IndicatorSetDouble( INDICATOR_LEVELVALUE,0,AngleLevel);
      IndicatorSetInteger(INDICATOR_LEVELSTYLE,0,STYLE_DOT);
      IndicatorSetInteger(INDICATOR_LEVELCOLOR,0,clrMediumOrchid);  
      IndicatorSetDouble( INDICATOR_LEVELVALUE,1,-AngleLevel);
      IndicatorSetInteger(INDICATOR_LEVELSTYLE,1,STYLE_DOT);
      IndicatorSetInteger(INDICATOR_LEVELCOLOR,1,clrMediumOrchid);  
   
      //
      //
      //
      //
      //
      
   string  MAsType = avgNames[MAType];
   IndicatorSetString(INDICATOR_SHORTNAME,MAsType+" angle ("+(string)MAPeriod+","+(string)AngleLevel+","+(string)AngleBars+")");
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason){  } 

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=fmin(rates_total-prev_calculated+1,rates_total-1); 

   //
   //
   //
   //
   //
   
   for(i=limit; i>=0; i--)
   {
      avg[i] = iMA(NULL,0,MAPeriod,0,MAType,MAAppliedPrice,i);
      double range  = iATR(NULL,0,AngleBars*20,i);
      double change = (i<rates_total-AngleBars) ? avg[i]-avg[i+AngleBars] : 0;
      double angle  = (range != 0) ? MathArctan(change/(range*AngleBars))*180.0/M_PI : 0.00;

      //
      //
      //
      //
      //
      
         Buffer1[i] = EMPTY_VALUE;
         Buffer2[i] = EMPTY_VALUE;
         Buffer3[i] = angle;
         Buffer4[i] = angle;
         state[i]   = 0;
            if (angle >  AngleLevel) { Buffer1[i] = angle; Buffer3[i] = EMPTY_VALUE; state[i] =  1;}
            if (angle < -AngleLevel) { Buffer2[i] = angle; Buffer3[i] = EMPTY_VALUE; state[i] = -1;}
   }
return(rates_total);
}
