//+------------------------------------------------------------------+
//|                                   YangTrader on Chart MTF v2.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 clrAqua
#property indicator_color2 clrAqua
#property indicator_color3 clrBlue
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 4

input ENUM_TIMEFRAMES TimeFrame = PERIOD_M15;
input int    Periodos    = 14;
input double Multiplier  = 2.5;
input double OVBlevel    = 75;
input double OVSLevel    = 25;
input bool   Interpolate = true;

double CNT[],OVB[],OVS[],TRD[],YAN[];
double ATR,HHV,LLV,SMT;
string indicatorFileName;
bool   returnBars;
bool   calculateValue;

int TimeFramex;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorBuffers(5);
   SetIndexBuffer(0,OVB); SetIndexLabel(0,"Upper level");
   SetIndexBuffer(1,OVS); SetIndexLabel(1,"Lower level");
   SetIndexBuffer(2,TRD); SetIndexLabel(2,"YangTrader");
   SetIndexBuffer(3,CNT);
   SetIndexBuffer(4,YAN);
   indicatorFileName = WindowExpertName();
   returnBars        = TimeFrame==-99;
  
   TimeFramex = TimeFrame;
   TimeFramex         = MathMax(TimeFrame,_Period);
   //IndicatorShortName(timeFrameToString(TimeFrame)+" OnChart YangTrader ("+IntegerToString(Periodos)+")");  
   
   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,y,n,x,limit=Bars;
   if (returnBars) {OVB[0]=MathMin(limit+1,Bars-1); return(0);}
   
   if (TimeFramex == Period())
      {
         for(i=0; i<limit; i++)
            {
               HHV=High[iHighest(NULL,0,MODE_HIGH,Periodos,i)];
               LLV=Low[iLowest(NULL,0,MODE_LOW,Periodos,i)]; 
               YAN[i]=100*(Close[i]-LLV)/(HHV-LLV);
            }
      
         for(i=0; i<limit; i++)
            {
               SMT=iMAOnArray(YAN,limit,3,0,3,i);
               ATR=iATR(NULL,0,Periodos,i);

               CNT[i]=iMA(NULL,0,Periodos,0,0,PRICE_CLOSE,i);
               OVB[i]=CNT[i]+(ATR*(OVBlevel-50)/100)*Multiplier;
               OVS[i]=CNT[i]-(ATR*(50-OVSLevel)/100)*Multiplier;
               TRD[i]=CNT[i]+((SMT-50)*ATR/100)*Multiplier;
            } 
         return(0);
      }
            
   //---------- Multitimeframe ----------------------------------------------------------------------------------
 
   limit=MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
   for (i=limit; i>=0; i--)
      {
         y=iBarShift(NULL,TimeFrame,Time[i]);
         OVB[i]=iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,Periodos,Multiplier,OVBlevel,OVSLevel,0,y);
         OVS[i]=iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,Periodos,Multiplier,OVBlevel,OVSLevel,1,y);
         TRD[i]=iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,Periodos,Multiplier,OVBlevel,OVSLevel,2,y);
             
         if (!Interpolate || y==iBarShift(NULL,TimeFrame,Time[i-1])) continue;
         //datetime time=iTime(NULL,TimeFrame,y);
          datetime time=iTime(NULL,TimeFrame,y);
         for(n=1; i+n < Bars && Time[i+n] >= time; n++) continue;	
         for(x=1; x < n; x++)
            {
               OVB[i+x]=OVB[i]+(OVB[i+n]-OVB[i])*x/n;
               OVS[i+x]=OVS[i]+(OVS[i+n]-OVS[i])*x/n;
               TRD[i+x]=TRD[i]+(TRD[i+n]-TRD[i])*x/n;
            }               
      }
                
   return(rates_total);
  }

/*string sTfTable[] = {"M1","M3","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,3,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}*/

