//+------------------------------------------------------------------+
//|                                                 !OTF Candles.mq4 |
//|                                  Copyright © 2011, John Wustrack |
//|                                        john_wustrack@hotmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, John Wustrack"
#property link      "john_wustrack@hotmail.com"

#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 White
#property indicator_color2 Green
#property indicator_color3 White
#property indicator_color4 Green
#property indicator_color5 Yellow
#property indicator_color6 Blue
#property indicator_color7 Red

#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 1
#property indicator_width6 1
#property indicator_width7 1

#property indicator_style5 STYLE_SOLID
#property indicator_style6 STYLE_DOT
#property indicator_style7 STYLE_DOT


//extern int xi_Other.TF = 15;
extern int xi_Nbr.Candles = 25;
extern int xi_Body.Width = 2;
extern int xi_Offset = 8;

extern int xi_TMA = 56;
extern int xi_ATR.Period = 100;
extern double xd_ATR.Mult = 2.0;

extern color xc_Bull = Green;
extern color xc_Bear = White;

double IB_UpperWick[],IB_LowerWick[],IB_UpperBody[],IB_LowerBody[];
double IB_TMA[];
double IB_TMA.Up[];
double IB_TMA.Dn[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 
   IndicatorBuffers(7);
   
   SetIndexStyle(0,DRAW_HISTOGRAM, 0, 1, xc_Bear);
   SetIndexBuffer(0, IB_UpperWick);
   SetIndexStyle(1,DRAW_HISTOGRAM, 0, 1, xc_Bull);
   SetIndexBuffer(1, IB_LowerWick);
   SetIndexStyle(2,DRAW_HISTOGRAM, 0, xi_Body.Width, xc_Bear);
   SetIndexBuffer(2, IB_UpperBody);
   SetIndexStyle(3,DRAW_HISTOGRAM, 0, xi_Body.Width, xc_Bull);
   SetIndexBuffer(3, IB_LowerBody);

   // TMA
   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,IB_TMA);
   SetIndexStyle(5,DRAW_LINE);
   SetIndexBuffer(5,IB_TMA.Up);
   SetIndexStyle(6,DRAW_LINE);
   SetIndexBuffer(6,IB_TMA.Dn);
      
   SetIndexShift(0, (xi_Nbr.Candles+xi_Offset)-1);
   SetIndexShift(1, (xi_Nbr.Candles+xi_Offset)-1);
   SetIndexShift(2, (xi_Nbr.Candles+xi_Offset)-1);
   SetIndexShift(3, (xi_Nbr.Candles+xi_Offset)-1);
   SetIndexShift(4, (xi_Nbr.Candles+xi_Offset)-1);
   SetIndexShift(5, (xi_Nbr.Candles+xi_Offset)-1);
   SetIndexShift(6, (xi_Nbr.Candles+xi_Offset)-1);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----
   double ld_Low,ld_High,ld_Open,ld_Close;
   
   static int li_Period;
   int limit;
   double ld_Range;
   
   if (Period() != li_Period)
      limit = Bars;
   else 
      limit = xi_Nbr.Candles;
   
   for (int i=limit; i>=0; i--)
       {
       IB_UpperWick[i] = EMPTY_VALUE;
       IB_LowerWick[i] = EMPTY_VALUE;
       IB_UpperBody[i] = EMPTY_VALUE;
       IB_LowerBody[i] = EMPTY_VALUE;
       IB_TMA[i] = EMPTY_VALUE;
       IB_TMA.Up[i] = EMPTY_VALUE;
       IB_TMA.Dn[i] = EMPTY_VALUE;
       }
       
   for (i=xi_Nbr.Candles-1; i>=0; i--)
      {
      ld_Low = iLow(NULL,5,i);
      ld_High = iHigh(NULL,5,i);
      ld_Open = iOpen(NULL,5,i);
      ld_Close = iClose(NULL,5,i);
      
      if (ld_Open<ld_Close)
         {
         IB_UpperWick[i] = ld_Low;   
         IB_LowerWick[i] = ld_High;
         }
      else
         {
         IB_UpperWick[i] = ld_High;
         IB_LowerWick[i] = ld_Low;
         }
      
      IB_UpperBody[i] = ld_Open;
      IB_LowerBody[i] = ld_Close;
      
      // Now fill in the TMA
      IB_TMA[i] = iMA(NULL,5,xi_TMA,0,MODE_LWMA,PRICE_CLOSE,i);
      ld_Range = iATR(NULL,5,xi_ATR.Period,i+10);
      IB_TMA.Up[i] = IB_TMA[i] + (ld_Range * xd_ATR.Mult);
      IB_TMA.Dn[i] = IB_TMA[i] - (ld_Range * xd_ATR.Mult);
         
      }

   li_Period = Period();   
//----
   return(0);
  }
//+------------------------------------------------------------------+