//downloaded at http://www.forexfabrik.de/topic/2177-steigung-von-ma/#entry44616
//http://forexsystems.ucoz.com/load/mt_4/indicator/50_sma_angle/2-1-0-1
//+------------------------------------------------------------------+
//|                                                      MAAngle.mq4 |
//|                                               original    jpkfox |
//|                                               edited by dariuske |
//| You can use this indicator to measure when the MA angle is       |
//| "near zero". AngleTreshold determines when the angle for the     |
//| EMA is "about zero": This is when the value is between           |
//| [-AngleTreshold, AngleTreshold] (or when the histogram is red).  |
//|   MAMode : 0 = SMA, 1 = EMA, 2 = Smoothed, 3 = Weighted          |
//|   MAPeriod: MA period                                            |
//|   AngleTreshold: The angle value is "about zero" when it is      |
//|     between the values [-AngleTreshold, AngleTreshold].          |      
//|   StartMAShift: The starting point to calculate the              |   
//|     angle. This is a shift value to the left from the            |
//|     observation point. Should be StartEMAShift > EndEMAShift.    | 
//|   StartMAShift: The ending point to calculate the                |
//|     angle. This is a shift value to the left from the            | 
//|     observation point. Should be StartEMAShift > EndEMAShift.    |
//|                                                                  |
//|   Modified by MrPip                                              |
//|       Red for down                                               |
//|       Yellow for near zero                                       |
//|       Green for up                                               |
//|  10/15/05  MrPip                                                 |
//|            Corrected problem with USDJPY and optimized code      |
//|  10/23/05  Added other JPY crosses                               |
//|                                                                  |
//|                                                                  |  
//|                                                                  |  
//|  12/01/07  Dariuske: Changed code for SMA50 (PhilNelSystem)      |
//|  18/01/07  Dariuske: Changed code for multiple mode MA's         |
//+------------------------------------------------------------------+

#property  copyright "jpkfox"
#property  link      "http://www.strategybuilderfx.com/forums/showthread.php?t=15274&page=1&pp=8"
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 4
#property  indicator_color1  LimeGreen
#property  indicator_color2  FireBrick 
#property  indicator_color3 Yellow
#property  indicator_color4 White //MediumSpringGreen

//---- indicator parameters
extern int MAPeriod = 50;
extern int MAMode   = MODE_EMA;
extern int Price    = PRICE_MEDIAN;

extern int SignalMA_Period = 21;
extern int SignalMA_Mode   = MODE_EMA;

extern double AngleTreshold=0.25;
extern int StartMAShift=2;
extern int EndMAShift=0;
extern string Note ="0-SMA 1-EMA 2-SMMA 3-LWMA";

//---- indicator buffers
double UpBuffer[];
double DownBuffer[];
double ZeroBuffer[];
double SignalBuffer[];
double fAngle[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(5);
//---- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,4);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,4);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,4);
   SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,2);


   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);

//---- 3 indicator buffers mapping
   if(!SetIndexBuffer(0,UpBuffer) &&
      !SetIndexBuffer(1,DownBuffer) &&
      !SetIndexBuffer(2,ZeroBuffer))
      Print("cannot set indicator buffers!");
      SetIndexBuffer(3,SignalBuffer);
      SetIndexBuffer(4,fAngle);
      
      SetIndexLabel(0,"Up");
      SetIndexLabel(1,"Down");
      SetIndexLabel(2,"ZERO");
      SetIndexLabel(3,"Signal Ma");
      SetIndexLabel(4,"Angle");
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("MAAngle("+MAPeriod+","+AngleTreshold+","+StartMAShift+","+EndMAShift+")");
   //IndicatorShortName("MAAngle->("+MAPeriod+")( Winkelwert="+AngleTreshold+")");
//---- initialization done
   return(0);
}
//+------------------------------------------------------------------+
//| The angle for EMA                                                |
//+------------------------------------------------------------------+
int start()
{
   double fEndMA, fStartMA;
   double mFactor, dFactor;
   int nLimit, i;
   int nCountedBars;
   double angle;
   int ShiftDif;
   string Sym;
   
   if (MAMode >= 4) MAMode = 0;
 
   if(EndMAShift >= StartMAShift)
   {
      Print("Error: EndMAShift >= StartMAShift");
      StartMAShift = 6;
      EndMAShift = 0;      
   }  
         
   nCountedBars = IndicatorCounted();
//---- check for possible errors
   if(nCountedBars<0) 
      return(-1);
//---- last counted bar will be recounted
   if(nCountedBars>0) 
      nCountedBars--;
   nLimit = Bars-nCountedBars;
   dFactor = 2*3.14159/180.0;
   mFactor = 10000.0;
   Sym = StringSubstr(Symbol(),3,3);
   if (Sym == "JPY") mFactor = 100.0;
   ShiftDif = StartMAShift-EndMAShift;
   mFactor /= ShiftDif; 
//---- main loop
   for(i=0; i<nLimit; i++)
   {
      fEndMA=iMA(NULL,0,MAPeriod,0,MAMode,Price,i+EndMAShift);
      fStartMA=iMA(NULL,0,MAPeriod,0,MAMode,Price,i+StartMAShift);
      // 10000.0 : Multiply by 10000 so that the fAngle is not too small
      // for the indicator Window.
      fAngle[i] = 0;
      fAngle[i] = mFactor * (fEndMA - fStartMA)/2.0;
      //fAngle = MathArctan(fAngle)/dFactor;

      DownBuffer[i] = 0.0;
      UpBuffer[i] = 0.0;
      ZeroBuffer[i] = 0.0;
      
      if(fAngle[i] > AngleTreshold)
         UpBuffer[i] = fAngle[i];
      else if (fAngle[i] < -AngleTreshold)
         DownBuffer[i] = fAngle[i];
      else ZeroBuffer[i] = fAngle[i];
      
   }
   for(i=0; i<nLimit; i++)
   {
      SignalBuffer[i]=0;
      SignalBuffer[i]=iMAOnArray(fAngle,0,SignalMA_Period,0,SignalMA_Mode,i);
   }
   return(0);
  }
//+------------------------------------------------------------------+

