//+------------------------------------------------------------------+
//|                                         miro1360_TDI_Dev_1.0.mq4 |
//|                                         Copyright 2016, miro1360 |
//|                                                 miro1360@azet.sk |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, miro1360"
#property link      "miro1360@azet.sk"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 6
#property indicator_plots   5
#property indicator_maximum 100
#property indicator_minimum 0
//--- plot TDI price
#property indicator_color1  clrLimeGreen
//--- plot TDI signal
#property indicator_color2  clrOrangeRed
//--- plot TDI market
#property indicator_color3  clrGold
//--- plot High
#property indicator_color4  clrDodgerBlue
//--- plot Low
#property indicator_color5  clrDodgerBlue
//--- input parameters
input string   TSIsettings="*** TDI ***";
input int      RSI_Period=14;
input int      MA1_Period=2;
input int      MA1_Mode=0;
input int      MA2_Period=7;
input int      MA2_Mode=0;
input int      MA3_Period=34;
input int      MA3_Mode=0;
input string   DeviationSettings="*** Deviation ***";
input int      DeviationPeriod=20;
input string   DeviationInfo1="H1 period and standard pairs=0.9, DAX=0.00009, XAUUSD=0.0009, USDX=0.009";
input string   DeviationInfo2="for higher period use smaller number, lower period greater number";
input double   Deviation=0.9;
input int      DeviationMode=0;
input string   VisualSettings="*** Graphics ***";
input int      LineWidthPrice=2;
input color    LineColorPrice=clrLimeGreen;
input int      LineWidthSignal=2;
input color    LineColorSignal=clrOrangeRed;
input int      LineWidthMarket=1;
input color    LineColorMarket=clrGold;
input int      LineWidthHigh=1;
input color    LineColorHigh=clrDodgerBlue;
input int      LineWidthLow=1;
input color    LineColorLow=clrDodgerBlue;
//--- indicator buffers
double         TDIBuffer[];
double         TDIsBuffer[];
double         TDIvBuffer[];
double         HighBuffer[];
double         LowBuffer[];
double         RSIBuffer[];

string shortName;
double digits1=10000;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,TDIBuffer);
   SetIndexBuffer(1,TDIsBuffer);
   SetIndexBuffer(2,TDIvBuffer);
   SetIndexBuffer(3,HighBuffer);
   SetIndexBuffer(4,LowBuffer);
   SetIndexBuffer(5,RSIBuffer);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,LineWidthPrice,LineColorPrice);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,LineWidthSignal,LineColorSignal);
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,LineWidthMarket,LineColorMarket);
   SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,LineWidthHigh,LineColorHigh);
   SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,LineWidthLow,LineColorLow);
   SetIndexLabel(0,"TDIprice");
   SetIndexLabel(1,"TDIsignal");
   SetIndexLabel(2,"TDImarket");
   SetIndexLabel(3,"TDIhigh");
   SetIndexLabel(4,"TDIlow");
   
   SetLevelValue(0,50);
   SetLevelValue(1,68);
   SetLevelValue(2,32);
   SetLevelStyle(STYLE_DOT,1,clrDimGray);
   shortName = "TDI"+"("+(string)RSI_Period+")("+(string)MA1_Period+")("+(string)MA2_Period+")("+(string)MA3_Period+")";
   IndicatorShortName(shortName);
   
   if(StringFind(Symbol(),"JPY",0)>-1) digits1=100;
   
   
//---
   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 limit=rates_total-prev_calculated-1;
   for(int i=limit;i>=0;i--)
   {
      RSIBuffer[i]=iRSI(Symbol(),Period(),RSI_Period,PRICE_CLOSE,i);
   }
   for(int i=limit;i>=0;i--)
   {
      TDIBuffer[i]=iMAOnArray(RSIBuffer,0,MA1_Period,0,MA1_Mode,i);
   }
   for(int i=limit;i>=0;i--)
   {
      TDIsBuffer[i]=iMAOnArray(TDIBuffer,0,MA2_Period,0,MA2_Mode,i);
   }
   for(int i=limit;i>=0;i--)
   {
      TDIvBuffer[i]=iMAOnArray(TDIsBuffer,0,MA3_Period,0,MA3_Mode,i);
      HighBuffer[i]=TDIvBuffer[i]+digits1*iStdDev(Symbol(),Period(),DeviationPeriod,0,DeviationMode,PRICE_CLOSE,i)*Deviation;
      LowBuffer[i]=TDIvBuffer[i]-digits1*iStdDev(Symbol(),Period(),DeviationPeriod,0,DeviationMode,PRICE_CLOSE,i)*Deviation;
   }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

