//+------------------------------------------------------------------+
//|                                                     TmaSlope.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, zznbrm"


//Edited by shahrooz "sh.sadeghi.me@gmail.com"                          
//---- indicator settings
#property indicator_separate_window
//#property  indicator_buffers 8 
#property  indicator_level1 0.5
#property  indicator_level2 -0.5
#property  indicator_buffers 5 
#property indicator_color1 RoyalBlue
#property indicator_color2 Red
#property indicator_color3 NULL
#property indicator_color4 Black
#property indicator_color5 Black


#property indicator_width1 4
#property indicator_width2 4
#property indicator_width3 NULL
#property indicator_width4 0  
#property indicator_width5 0 


//---- input parameters
extern int eintPeriod = 56;
extern double edblHigh1 = 0.5;

extern double edblLow1 = -0.5;


extern color      Font_Color              = Black;
extern int        H_Pos               = 1;
extern int        V_Pos                = 25;
extern int        Corner                 = 3;
extern int        Font_Size               = 14;
extern int        Font_Size_text               = 14;
extern int        H_Pos_text               = 1;
extern int        V_Pos_text               = 1;

extern int Text_Corner=3;
extern int H_Pos_M15=1;
extern int V_Pos_M15=50;
extern int        Font_Size_M15               = 12;
extern int Corner_M15=3;
extern color      Font_Color_M15              = Black;
//---- indicator buffers
double gadblUp1[];
double gadblUp2[];
double gadblDn1[];
double gadblDn2[];
double gadblExt[];
double gadblSlope[];
double arrowup[];
double arrowdown[];
//double gadblTma[];
//double gadblPrev[];

double TICK;
bool AdditionalDigit;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{              
   //IndicatorBuffers( 8 );    
   IndicatorBuffers( 5 );
   IndicatorDigits( 5 );
   IndicatorShortName( "TmaSlope_Ed" );
   AdditionalDigit = MarketInfo(Symbol(), MODE_MARGINCALCMODE) == 0 && MarketInfo(Symbol(), MODE_PROFITCALCMODE) == 0 && Digits % 2 == 1;
   TICK = getTick();
      
   SetIndexBuffer( 0, gadblUp1 );    SetIndexLabel( 0, NULL );       SetIndexStyle( 0, DRAW_HISTOGRAM );

   SetIndexBuffer( 1, gadblDn1 );    SetIndexLabel( 1, NULL );       SetIndexStyle( 1, DRAW_HISTOGRAM );

   //SetIndexBuffer( 2, gadblExt );    SetIndexLabel( 2, NULL );       SetIndexStyle( 2, DRAW_HISTOGRAM );
   SetIndexBuffer( 2, gadblSlope );  SetIndexLabel( 2, "TMA Slope" );    SetIndexStyle( 2, DRAW_NONE );
   SetIndexBuffer( 3, arrowup );     SetIndexLabel( 3, NULL );       SetIndexStyle( 3, DRAW_ARROW ); SetIndexArrow(3,233);
   SetIndexBuffer( 4, arrowdown );   SetIndexLabel( 4, NULL );       SetIndexStyle( 4, DRAW_ARROW ); SetIndexArrow(4,234);  
      
   SetIndexEmptyValue( 0, 0.0 );
   SetIndexEmptyValue( 1, 0.0 );
   SetIndexEmptyValue( 2, 0.0 );
   SetIndexEmptyValue( 3, 0.0 );
   SetIndexEmptyValue( 4, 0.0 );
  

          
   return( 0 );
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   return( 0 );
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int counted_bars = IndicatorCounted();
   if ( counted_bars < 0 ) return(-1);
   if ( counted_bars > 0 ) counted_bars--;
               
   int intLimit = MathMin( Bars - 1, Bars - counted_bars + eintPeriod );
   
   double dblTma, dblPrev;
    double dblTmaM15, dblPrevM15;
   
   for( int inx = intLimit; inx >= 0; inx-- )
   {   
      //gadblTma[inx] = calcTma( inx );
      //gadblPrev[inx] = calcTma( inx+1 );
      //gadblSlope[inx] = ( gadblTma[inx] - gadblPrev[inx] ) / TICK;
      dblTma = calcTma( inx );
      dblPrev = calcTma( inx+1 );
      gadblSlope[inx] = ( dblTma - dblPrev ) / TICK;
      
      gadblUp1[inx] = 0.0;
      gadblExt[inx]=0.0;
      gadblDn1[inx] = 0.0;
    
      arrowdown[inx]=0.0;
      arrowup[inx]=0.0;
      
     // if ( gadblSlope[inx] >= edblHigh1 )      gadblUp1[inx] = gadblSlope[inx]; 
    //  else 
      if ( gadblSlope[inx] >= 0.0 )         gadblUp1[inx] = gadblSlope[inx];
     // else if ( gadblSlope[inx] >= edblLow1 )  gadblExt[inx] = gadblSlope[inx];
      else                                     gadblDn1[inx] = gadblSlope[inx];
   
   if(gadblSlope[inx]>=0 && gadblSlope[inx]<gadblSlope[inx+1] && Close[inx]>Open[inx] && gadblSlope[inx+2]<=gadblSlope[inx+1]) arrowdown[inx]=gadblSlope[inx]+0.1*gadblSlope[inx]; 
   if(gadblSlope[inx]<0 && gadblSlope[inx]>gadblSlope[inx+1] && Close[inx]<Open[inx]&& gadblSlope[inx+2]>=gadblSlope[inx+1]) arrowup[inx]=gadblSlope[inx]+(0.1*gadblSlope[inx]); 

     string tt=DoubleToStr(gadblSlope[inx],2);

    ObjectCreate("label",OBJ_LABEL,WindowFind("TmaSlope_Ed"),0,0);
    ObjectSet("label",OBJPROP_XDISTANCE,H_Pos);
    ObjectSet("label",OBJPROP_YDISTANCE,V_Pos);
    ObjectSet("label",OBJPROP_CORNER,Corner);
    ObjectSetText("label"," "+tt+" ",Font_Size,"Arial",Font_Color);
    
    dblTmaM15 = calcTmaM15( 0 );
      dblPrevM15 = calcTmaM15( 1 );
      double hh = ( dblTmaM15 - dblPrevM15 ) / TICK;

string jj=DoubleToStr(hh,2);
    ObjectCreate("label M15",OBJ_LABEL,WindowFind("TmaSlope_Ed"),0,0);
    ObjectSet("label M15",OBJPROP_XDISTANCE,H_Pos_M15);
    ObjectSet("label M15",OBJPROP_YDISTANCE,V_Pos_M15);
    ObjectSet("label M15",OBJPROP_CORNER,Corner_M15);
   ObjectSetText("label M15","M15= "+jj+" ",Font_Size_M15,"Arial",Font_Color_M15);
   string sObjName="InfoBar1";
   
  
   if (gadblSlope[0] >= edblHigh1)
      ObjectSetText(sObjName, "Buy Only", Font_Size_text, "Verdana", YellowGreen);
   
   else if (gadblSlope[0] <= edblLow1)
      ObjectSetText(sObjName, "Sell Only", Font_Size_text, "Verdana", Red);
   
   else 
      ObjectSetText(sObjName, "Ranging", Font_Size_text, "Verdana", Black);
      
   
   string name1 = "InfoBar1";
   

   
   ObjectCreate(sObjName, OBJ_LABEL,WindowFind("TmaSlope_Ed"), 0, 0);
   ObjectSet(sObjName, OBJPROP_CORNER, Text_Corner);
   ObjectSet(sObjName, OBJPROP_XDISTANCE, H_Pos_text);//left to right
   ObjectSet(sObjName, OBJPROP_YDISTANCE, V_Pos_text);//top to bottom

     }
   return( 0 );
}

//+------------------------------------------------------------------+
//| getTick()                                                        |
//+------------------------------------------------------------------+
double getTick() {
    double tick = MarketInfo(Symbol(), MODE_TICKSIZE);
    if (AdditionalDigit) {
        tick *= 10;
    }    
    return (tick);
}

//+------------------------------------------------------------------+
//| calcTma()                                                        |
//+------------------------------------------------------------------+
double calcTma( int inx )
{
   double dblSum  = (eintPeriod+1)*Close[inx];
   double dblSumw = (eintPeriod+1);
   int jnx, knx;
         
   for ( jnx = 1, knx = eintPeriod; jnx <= eintPeriod; jnx++, knx-- )
   {
      dblSum  += ( knx * Close[inx+jnx] );
      dblSumw += knx;

      if ( jnx <= inx )
      {
         dblSum  += ( knx * Close[inx-jnx] );
         dblSumw += knx;
      }
   }
   
   return( dblSum / dblSumw );
}
 
 double calcTmaM15( int inx )
{
   double dblSum  = (eintPeriod+1)*iClose(Symbol(),15,inx);
   double dblSumw = (eintPeriod+1);
   int jnx, knx;
         
   for ( jnx = 1, knx = eintPeriod; jnx <= eintPeriod; jnx++, knx-- )
   {
      dblSum  += ( knx * iClose(Symbol(),15,inx+jnx) );
      dblSumw += knx;

      if ( jnx <= inx )
      {
         dblSum  += ( knx * iClose(Symbol(),15,inx-jnx) );
         dblSumw += knx;
      }
   }
   
   return( dblSum / dblSumw );
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 



   


