//+------------------------------------------------------------------+
//|                                                2bars_MML_MTF.mq4 |
//|                                            Copyright © 2016, RFX |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "RFX skype:rapidfx"
#property version   "0.9b"
#property description "2 Bars MML's MTF"

enum aTF {
   M1    = 1,
   M5    = 5,
   M15   = 15,
   M30   = 30,
   H1    = 60,
   H4    = 240,
   D1    = 1440,
   WK    = 10080,
   MN = 43200
};
input aTF ChartTF = D1;
enum aStyle
{
   solid = 0,
   dash = 1,
   dot = 2,
   dashdot = 3,
   dashdotdot = 4
};
input aStyle PitchforkStyle = solid;
// extern bool ShowPitchfork = true;
extern color PitchforkColorA = clrViolet;
extern color PitchforkColorB = clrRoyalBlue;
extern bool Ray = false;

// extern int LevelWidth = 1;
int PeriodBars;
string TFdesc;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicator buffers mapping  
//---- drawing settings
//----
//---- name for DataWindow
//---- initialization done   
   PeriodBars = ChartTF/Period();
      switch(ChartTF) {
      case 1:  TFdesc = "M1"; break;
      case 5:  TFdesc = "M5"; break;
      case 15: TFdesc = "M15"; break;
      case 30: TFdesc = "M30"; break;
      case 60: TFdesc = "H1"; break;
      case 240: TFdesc = "H4"; break;
      case 1440: TFdesc = "D1"; break;
      case 10080: TFdesc = "Wk"; break;
      case 43200: TFdesc = "Mn"; break;
      default: TFdesc = "XX";
   }

   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   ObjectDelete("A_Pitch_"+TFdesc);
   ObjectDelete("B_Pitch_"+TFdesc);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   double AHigh, ALow, AOpen, AClose, BHigh, BLow, BOpen, BClose;
   double ARange, BRange, Ratio;
   datetime ATime, BTime, CTime, ATimeHigh, ATimeLow, BTimeHigh, BTimeLow;
   int idxA, idxB,idxC,Abar,Bbar;
   string Name, Ridx, Rgap, Rdir, Pos;
   
   AHigh = iHigh(NULL,ChartTF,2);
   ALow  = iLow(NULL,ChartTF,2);
   ATime = iTime(NULL, ChartTF,2);
   AOpen = iOpen(NULL,ChartTF,2);
   AClose = iClose(NULL,ChartTF,2);
   BHigh = iHigh(NULL,ChartTF,1);
   BLow = iLow(NULL,ChartTF,1);
   BOpen = iOpen(NULL,ChartTF,1);
   BClose = iClose(NULL,ChartTF,1);
   BTime = iTime(NULL, ChartTF, 1);
   CTime = iTime(NULL, ChartTF, 0);
   // ------------ calc table
   ARange = AHigh - ALow;
   BRange = BHigh - BLow;
   Ratio = ARange/BRange;
   if(Ratio > 1) Ridx ="A"; // A bar > B bar
   if(Ratio > 0.85 && Ratio <=1) Ridx ="B"; // A ~= B
   if(Ratio <= 0.85) Ridx ="C"; // A < B
   Rgap = "2"; // default
   if(AHigh < BHigh) Rgap ="1";
   if(AHigh > BHigh) Rgap="3";
   if(AOpen > AClose && BOpen > BClose) Rdir ="a";
   if(AOpen > AClose && BOpen < BClose) Rdir ="b";
   if(AOpen < AClose && BOpen > BClose) Rdir ="c";
   if(AOpen < AClose && BOpen < BClose) Rdir ="d";
   Pos = Ridx+Rgap+Rdir;
   Comment("2Bar[",TFdesc,"]: ",Pos);
   //-------- calc table end
   
   if(Period() < ChartTF) {
   // ------------
      idxC = iBarShift(NULL, 0,CTime);
      idxA = iBarShift(NULL, 0,ATime);
      Abar = iHighest(NULL,0,MODE_HIGH,PeriodBars,PeriodBars+idxC);
      AHigh = iHigh(NULL,0,Abar);
      ATimeHigh = iTime(NULL, 0,Abar);
      Abar = iLowest(NULL,0,MODE_LOW,PeriodBars,PeriodBars+idxC);
      ALow = iLow(NULL,0,Abar);
      ATimeLow = iTime(NULL, 0,Abar);
   
      idxB = iBarShift(NULL, 0,BTime);
      Bbar = iHighest(NULL,0,MODE_HIGH,PeriodBars,idxC);
      BHigh = iHigh(NULL,0,Bbar);
      BTimeHigh = iTime(NULL, 0,Bbar);
      Bbar = iLowest(NULL,0,MODE_LOW,PeriodBars,idxC);
      BLow = iLow(NULL,0,Bbar);
      BTimeLow = iTime(NULL, 0,Bbar);
      // Comment("2Bar[",TFdesc,"]: ",Pos);
   // ------------
     ///// --------------
      Name = "A_Pitch_"+TFdesc;
            ObjectDelete(Name);
            ObjectCreate(0,Name,OBJ_PITCHFORK,0,
                         ATimeHigh,AHigh,
                         BTimeHigh,BHigh,
                         BTimeLow,BLow);
            ObjectSet(Name, OBJPROP_STYLE, PitchforkStyle);
            ObjectSet(Name,OBJPROP_COLOR,PitchforkColorA);
            ObjectSet(Name,OBJPROP_RAY,Ray);
      Name = "B_Pitch_"+TFdesc;
            ObjectDelete(Name);
            ObjectCreate(0,Name,OBJ_PITCHFORK,0,
                         ATimeLow,ALow,
                         BTimeHigh,BHigh,
                         BTimeLow,BLow);
            ObjectSet(Name, OBJPROP_STYLE, PitchforkStyle);
            ObjectSet(Name,OBJPROP_COLOR,PitchforkColorB);
            ObjectSet(Name,OBJPROP_RAY,Ray);
   }
   if(Period() == ChartTF) {
      Comment("2Bar[",TFdesc,"]: ",Pos);
      Name = "A_Pitch_"+TFdesc;
            ObjectDelete(Name);
            ObjectCreate(0,Name,OBJ_PITCHFORK,0,
                         ATime,AHigh,
                         BTime,BHigh,
                         BTime,BLow);
            ObjectSet(Name, OBJPROP_STYLE, PitchforkStyle);
            ObjectSet(Name,OBJPROP_COLOR,PitchforkColorA);
            ObjectSet(Name,OBJPROP_RAY,Ray);
      Name = "B_Pitch_"+TFdesc;
            ObjectDelete(Name);
            ObjectCreate(0,Name,OBJ_PITCHFORK,0,
                         ATime,ALow,
                         BTime,BHigh,
                         BTime,BLow);
            ObjectSet(Name, OBJPROP_STYLE, PitchforkStyle);
            ObjectSet(Name,OBJPROP_COLOR,PitchforkColorB);
            ObjectSet(Name,OBJPROP_RAY,Ray);     
    }
     return(0);
  }
