//+------------------------------------------------------------------+
//|                                           TMA Без понтов S&N.mq4 |
//|                           TMA Без понтов от Ttomas с понтами S&N |
//|                                                 TradeLikeAPro.ru |
//+------------------------------------------------------------------+
#property copyright "Ttomas, S&&N"
#property link      "TradeLikeAPro.ru"
#property version   "1.00"
#property description "TMA Без понтов от Ttomas с понтами S&&N"

#property strict
#property indicator_chart_window
#property indicator_buffers    5
extern int     TMAPeriod=56;                    // TMAPeriod
extern int     TmaAtrPeriod=100;                // TmaAtrPeriod
extern double  TmaBandSize=2;                   // TmaBandSize
extern int     PercentWidth    = 20;            // Ширина средней полосы, % от ширины канала
extern int     NumberOf2Bar    = 10;            // Номер 2-й точки линии наклона
extern int     KoefPrivedenija = 30;            // Коэффициент приведения осей (для разного масштаба осей)
extern string  Maska           = "%m (%a)";     // Маска данных:(%m-ширина канала, a% - угол наклона)
extern int     Xshift          = 3;             // Смещение надписи по оси Х (число баров)
extern int     Yshift          = 0;             // Смещение надписи по оси Y (пипсов)

double Buf[], Buf1[], Buf2[], BufMdUp[], BufMdDn[];
int OnInit()
  {
   SetIndexBuffer(0,Buf);
   SetIndexStyle(0,DRAW_LINE,0,1,clrYellow);
   SetIndexBuffer(1,Buf1);
   SetIndexStyle(1,DRAW_LINE,0,1,clrRed);
   SetIndexBuffer(2,Buf2);
   SetIndexStyle(2,DRAW_LINE,0,1,clrBlue);
   SetIndexBuffer(3,BufMdDn);
   SetIndexStyle(3,DRAW_LINE,STYLE_DASH,1,clrRed);
   SetIndexBuffer(4,BufMdUp);
   SetIndexStyle(4,DRAW_LINE,STYLE_DASH,1,clrBlue);
   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[])
  {
   int i,limit;
   if(rates_total<=TMAPeriod)
      return(0);
   limit=rates_total-prev_calculated;
   if(prev_calculated>0)
      limit++;
//--- macd counted in the 1-st buffer
   for(i=0; i<limit; i++)
   {
   if (i>=Bars-TMAPeriod) continue;
    double  range = iATR(NULL,0,TmaAtrPeriod,i)*TmaBandSize;
    double dblSum  = (TMAPeriod+1)*close[i];
    double dblSumw = (TMAPeriod+1);
    int jnx, knx;
         
    for ( jnx = 1, knx = TMAPeriod; jnx <= TMAPeriod; jnx++, knx-- )
      {
      dblSum  += ( knx * close[i+jnx] );
      dblSumw += knx;      
      
      if ( jnx <= 0 )
      {         
         if (time[i-jnx] > time[0])
         {
            //Print (" TimeFrameValue ", TimeFrameValue , " inx ", inx," jnx ", jnx, " iTime(Symbol(),TimeFrameValue,inx-jnx) ", TimeToStr(iTime(Symbol(),TimeFrameValue,inx-jnx)), " Time[0] ", TimeToStr(Time[0])); 
            continue;
         }
         dblSum  += ( knx * close[i-jnx] );
         dblSumw += knx;
         }
      }
    double TMA=dblSum / dblSumw;
    Buf[i]=NormalizeDouble(TMA,Digits);
    Buf1[i]=NormalizeDouble(TMA-range,Digits);
    Buf2[i]=NormalizeDouble(TMA+range,Digits);
    
    double Delta = +(Buf2[i]-Buf1[i])*PercentWidth/100/2;   
    BufMdUp[i] = Buf[i]+Delta;  
    BufMdDn[i] = Buf[i]-Delta;
    }
   Label();
   
   return(rates_total);
  }
//+------------------------------------------------------------------+

void Label()
{
  string mask = Maska;
   // Distance to mid
   double DistPts = MathAbs(Buf2[0] - Buf1[0]);
   double DistPips = Convert_2_Pips(DistPts);
   ObjectCreate("!Mid",OBJ_TEXT,0,0,0);
   ObjectSet("!Mid",OBJPROP_TIME1,Time[0]+(Xshift*Period()*60));
   ObjectSet("!Mid",OBJPROP_PRICE1,Buf[0]+ Yshift*Point);   
   StringReplace(mask,"%a",IntegerToString(GetAngle()));
   StringReplace(mask,"%m",DoubleToStr(DistPips,0));
   ObjectSetText("!Mid",mask,10,"Arial",Yellow);  
}

int GetAngle()
{
   double x1 = -((int)(Time[0]-Time[NumberOf2Bar]))/(KoefPrivedenija+Period()/2);
   double x2 = 0;     
   double y1 = Buf[NumberOf2Bar];
   double y2 = Buf[0];
      
   double k  = (y2-y1)/(x2-x1)/Point;
   
   return (int)(MathArctan(k)*180/3.1415926535);
}

//+------------------------------------------------------------------+
//| convert to pips                                                  |
//+------------------------------------------------------------------+
double Convert_2_Pips(double pd_Points)
  {
   double pd_Pips=pd_Points/Point;  // Default - no conversion
 	if (Digits == 5 || (Digits == 3 && StringFind(Symbol(), "JPY") != -1)) pd_Pips=pd_Points/Point/10;
 	if (Digits == 6 || (Digits == 4 && StringFind(Symbol(), "JPY") != -1)) pd_Pips=pd_Points/Point/100;
   return(pd_Pips);
  }
//+------------------------------------------------------------------+
//| get the pips decimal places                                      |
//+------------------------------------------------------------------+
int Get_Pips_Decimal()
  {
   int pi_PipsDecimal = 0;  // Default - no decimals
 	if (Digits == 5 || (Digits == 3 && StringFind(Symbol(), "JPY") != -1)) pi_PipsDecimal = 1;
 	if (Digits == 6 || (Digits == 4 && StringFind(Symbol(), "JPY") != -1)) pi_PipsDecimal = 2;
   return(pi_PipsDecimal);
  }