Navigation:Home > Content >

5i-AdaptiveCyberCycle-Div3.mq4

Time: 2014-04-13 | Download file:5i-AdaptiveCyberCycle-Div3.mq4

#property copyright "© 2008-2011 BJF Trading Group"
#property link      "www.iticsoftware.com"

#define vers   "3.2"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Orange
#property indicator_color2 Blue
#property indicator_width1  1
#property indicator_width2  1
#property indicator_style1  STYLE_SOLID
#property indicator_style2  STYLE_SOLID


extern string _tmp1_ = " --- Common ---";
//Set 4, 5 or 0
//4: set 4 if your broker provides 4-digit quotes for EURUSD pair and 2-digit quotes for USDJPY pair
//5: set 5 if your broker provides 5-digit quotes for EURUSD pair and 3-digit quotes for USDJPY pair
//0: auto detect
extern int  AccDigits = 0;
extern bool UseOpenBar = false;


extern string _tmp2_ = " --- AdaptiveCyberCycle ---";
extern double Alpha = 0.007;


extern string _tmp3_ = " --- Divergence peaks ---";
extern int Chart.LeftTop.BarsL = 3;
extern int Chart.LeftTop.BarsR = 2;
extern int Chart.RightTop.BarsL = 2;
extern int Chart.RightTop.BarsR = 2;

extern int Ind.LeftTop.BarsL = 5;
extern int Ind.LeftTop.BarsR = 5;
extern int Ind.RightTop.BarsL = 3;
extern int Ind.RightTop.BarsR = 2;

extern int LeftTops.MaxBarsDiff = 10;
extern int RightTops.MaxBarsDiff = 5;


extern string _tmp4_ = " --- Levels ---";
extern double BullishDiv.ResetAbove = 100000.0;
extern double BearishDiv.ResetBelow = -100000.0;


extern string _tmp5_ = " --- Filters ---";
extern int MaxBars = 2000;
extern int DivWidth.MinBars = 15;
extern int DivWidth.MaxBars = 100;
extern int DivHeight.MinPips = 1;
extern int DivHeight.MaxPips = 450;


extern string _tmp6_ = " --- Graphic ---";
extern color clBullishDiv = DeepSkyBlue;
extern color clBearishDiv = OrangeRed;

extern int Chart.BullishDiv.ArrowCode1 = 233;
extern int Chart.BullishDiv.ArrowCode2 = 32;
extern int Chart.BearishDiv.ArrowCode1 = 234;
extern int Chart.BearishDiv.ArrowCode2 = 32;

extern int Chart.BullishDiv.ArrowSize1 = 1;
extern int Chart.BullishDiv.ArrowSize2 = 1;
extern int Chart.BearishDiv.ArrowSize1 = 1;
extern int Chart.BearishDiv.ArrowSize2 = 1;

//M1;M5;M15;M30;H1;H4;D1;W1;MN1
extern string Chart.Arrow.shift_all_TF = "5;5;10;10;15;20;70;200;300";
extern string Chart.Line.shift_all_TF = "0;0;0;0;0;0;0;0;0";

extern int Chart.Line.style = STYLE_DOT;
extern int Chart.Line.width = 1;
extern int Ind.Line.style = STYLE_SOLID;
extern int Ind.Line.width = 1;


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//
double CP_DeltaPhase[];
double CP_CPeriod[];
double CP_Smooth[];
double CP_Cycle[];
double CP_Q1[];
double CP_I1[];
double CP_InstPeriod[]; 

int drawBegin = 8;
int median = 5; 
//

double Cycle[];
double Trigger[];
double Smooth[];

double chart_right_fr[], chart_left_fr[];
double ind_right_fr[], ind_left_fr[];

int Chart_Arrow_shift[];
int Chart_Line_shift[];

string prefix;
string short_name;

double kIT = 0.236;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void init() 
{
  IndicatorBuffers(7);
    
  SetIndexStyle(0, DRAW_LINE);
  SetIndexStyle(1, DRAW_LINE);
  SetIndexStyle(2, DRAW_NONE);
  
  SetIndexDrawBegin(0, drawBegin);
  SetIndexDrawBegin(1, drawBegin);
  
  SetIndexBuffer(0, Cycle);
  SetIndexBuffer(1, Trigger);
  SetIndexBuffer(2, Smooth);
    
  SetIndexBuffer(3, chart_right_fr);
  SetIndexBuffer(4, chart_left_fr);
  SetIndexBuffer(5, ind_right_fr);
  SetIndexBuffer(6, ind_left_fr);

  SetIndexEmptyValue(3, 0.0); 
  SetIndexEmptyValue(4, 0.0);
  SetIndexEmptyValue(5, 0.0);
  SetIndexEmptyValue(6, 0.0);
  
  //
  ArraySetAsSeries(CP_DeltaPhase, true);
  ArraySetAsSeries(CP_CPeriod, true);
  ArraySetAsSeries(CP_Smooth, true);
  ArraySetAsSeries(CP_Cycle, true);
  ArraySetAsSeries(CP_Q1, true);
  ArraySetAsSeries(CP_I1, true);
  ArraySetAsSeries(CP_InstPeriod, true);
  //

  short_name = "BJF AdaptiveCyberCycle Div(" + DoubleToStr(Alpha, 4) + ")";
  IndicatorShortName(short_name);
  
  SetIndexLabel(0, "Cycle");
  SetIndexLabel(1, "Trigger");
  
  SetLevelValue(0, BullishDiv.ResetAbove);
  SetLevelValue(1, BearishDiv.ResetBelow);
  
    
  prefix = WindowExpertName();
  prefix = str_replace(prefix, "AdaptiveCyberCycle", "JE_ACC");
      
  clear();
  
  split_i(Chart_Arrow_shift, Chart.Arrow.shift_all_TF, ";");
  split_i(Chart_Line_shift, Chart.Line.shift_all_TF, ";");
}

void deinit()
{
  clear();
}

double fInd(int ind)
{
  return (Cycle[ind]);
}

double PointEx()
{
  if (AccDigits == 0)
  {
    if (Digits == 2 || Digits == 4) return (Point);
    if (Digits == 3 || Digits == 5) return (10.0*Point);

    return (Point);
  }
  
  if (AccDigits == 4)
  {
    return (Point);
  }

  if (AccDigits == 5)
  {
    return (10.0*Point);
  }
  
  return (Point);
}

void start() 
{  
  AdaptiveCyberCycle();
  
  int win_ind = WindowFind(short_name);

  //-----
  
  int counted_bars = IndicatorCounted();
  if (counted_bars > 0) counted_bars--;
  int limit = Bars-counted_bars;
  if (limit > 1) clear();
  
  //-----
  
  int i, j, k;
  double w;
  string obj_name;

  int chart_fr.i, ind_fr.i;
  int chart_fr.j, ind_fr.j;
    
  datetime time1, time2;
  double price1, price2;
  
  int x1, x2;
  double y1, y2, a, b, f;

  int shift = 0;
  if (!UseOpenBar)
  {
    shift = 1;
  }
  
  
  clear();
  
  for (i=0; i < Bars; i++)
  {
    chart_right_fr[i] = 0.0;
    chart_left_fr[i] = 0.0;
    
    ind_right_fr[i] = 0.0;
    ind_left_fr[i] = 0.0;
  }
  
  //-----
  
  limit = MaxBars;  
  for (i=0; i < limit; i++)
  {
    chart_right_fr[i] = chart_is_fr(MODE_UPPER, Chart.RightTop.BarsL, Chart.RightTop.BarsR, i);
    chart_left_fr[i] = chart_is_fr(MODE_UPPER, Chart.LeftTop.BarsL, Chart.LeftTop.BarsR, i);
    
    ind_right_fr[i] = ind_is_fr(MODE_UPPER, Ind.RightTop.BarsL, Ind.RightTop.BarsR, i);
    ind_left_fr[i] = ind_is_fr(MODE_UPPER, Ind.LeftTop.BarsL, Ind.LeftTop.BarsR, i);
  }
  
  //-----
  
  for (i=0; i < limit; i++)
  {
    chart_fr.i = i + Chart.RightTop.BarsR + shift;
    ind_fr.i = i + Ind.RightTop.BarsR + shift;
    
    if (chart_right_fr[chart_fr.i] == 0 && ind_right_fr[ind_fr.i] == 0) continue;

    for (k=0; k < RightTops.MaxBarsDiff; k++)
    {
      if (chart_right_fr[chart_fr.i] != 0)
      {
        ind_fr.i = chart_fr.i + k;
        if (ind_right_fr[ind_fr.i] != 0 && ind_fr.i - Ind.RightTop.BarsR - shift >= i)
        {
          break;
        }
      }

      if (ind_right_fr[ind_fr.i] != 0)
      {
        chart_fr.i = ind_fr.i + k;
        if (chart_right_fr[chart_fr.i] != 0 && chart_fr.i - Chart.RightTop.BarsR - shift >= i)
        {
          break;
        }
      }
    }
    
    if (chart_right_fr[chart_fr.i] == 0 || ind_right_fr[ind_fr.i] == 0) continue;
    
    //-----
    
    int j0 = MathMax(chart_fr.i, ind_fr.i) + 1;
    for (j=j0; j < limit; j++)
    {
      chart_fr.j = j;
      ind_fr.j = j;
     
      if (chart_fr.j - Chart.LeftTop.BarsR - shift < i + 1) continue;
      if (ind_fr.j - Ind.LeftTop.BarsR - shift < i + 1) continue;


      if (chart_left_fr[chart_fr.j] == 0 && ind_left_fr[ind_fr.j] == 0) continue;

    
      for (k=0; k < LeftTops.MaxBarsDiff; k++)
      {
        if (chart_left_fr[chart_fr.j] != 0)
        {
          ind_fr.j = chart_fr.j + k;
          if (ind_left_fr[ind_fr.j] != 0 && ind_fr.j - Ind.LeftTop.BarsR - shift >= i + 1)
          {
            break;
          }
        }

        if (ind_left_fr[ind_fr.j] != 0)
        {
          chart_fr.j = ind_fr.j + k;
          if (chart_left_fr[chart_fr.j] != 0 && chart_fr.j - Chart.LeftTop.BarsR - shift >= i + 1)
          {
            break;
          }
        }
      }


      if (chart_fr.j-chart_fr.i > DivWidth.MaxBars) break;
      
      if (chart_left_fr[chart_fr.j] == 0 || ind_left_fr[ind_fr.j] == 0) continue;

      
      //-----
      
      if (chart_left_fr[chart_fr.j] > chart_right_fr[chart_fr.i]) break;

      if (ind_left_fr[ind_fr.j] < ind_right_fr[ind_fr.i]) continue;

      
      if (chart_fr.j-chart_fr.i < DivWidth.MinBars) continue;
      if (ind_fr.j-ind_fr.i < DivWidth.MinBars) continue;

      w = MathAbs(chart_left_fr[chart_fr.j] - chart_right_fr[chart_fr.i]);
      
      if (w < DivHeight.MinPips*PointEx()) continue; 
      if (w > DivHeight.MaxPips*PointEx()) continue;
      
      //-----

      int chart.bar = -1;
      for (k = chart_fr.i+1; k < chart_fr.j; k++)
      {
        if (High[k] > High[chart_fr.i] + 1*PointEx())
        {
          chart.bar = k;
          break;
        }
      }
      
      if (chart.bar >= 0) break;
      
      //-----
      
      
      x1 = ind_fr.j;
      y1 = fInd(x1);
      x2 = ind_fr.i;
      y2 = fInd(x2);
      a = (y2-y1)/(x2-x1);
      b = y1 - a*x1;
      
      int ind.bar = -1;
      for (k = ind_fr.i; k < ind_fr.j; k++)
      {
        double ind.val = fInd(k);
        if (ind.val > ind_left_fr[ind_fr.j])
        {
          ind.bar = k;
          break;
        }


        f = a*k + b;
        if (fInd(k) > f + kIT*MathAbs(y2-y1))
        {
          ind.bar = k;
          break;
        }


        if (ind.val < BearishDiv.ResetBelow)
        {
          ind.bar = k;
          break;
        }
      }
      
      if (ind.bar >= 0) break;
      
            
      //-----
            
      obj_name = prefix + "_chart_ua1_" + Time[i];
      time1 = Time[i];
      price1 = High[i] + 1.3*Chart_Arrow_shift[ITF(Period())]*PointEx();
      
      if (ObjectFind(obj_name) == -1)
      {
        ObjectCreate(obj_name, OBJ_ARROW, 0, time1, price1);
      }
      else
      {
        ObjectMove(obj_name, 0, time1, price1);
      }
      
      ObjectSet(obj_name, OBJPROP_WIDTH, Chart.BearishDiv.ArrowSize1);
      ObjectSet(obj_name, OBJPROP_COLOR, clBearishDiv);
      ObjectSet(obj_name, OBJPROP_ARROWCODE, Chart.BearishDiv.ArrowCode1);

      //-----
      
      obj_name = prefix + "_chart_ua2_" + Time[chart_fr.j];
      time1 = Time[chart_fr.j];
      price1 = High[chart_fr.j] + 1.3*Chart_Arrow_shift[ITF(Period())]*PointEx();
      
      if (ObjectFind(obj_name) == -1)
      {
        ObjectCreate(obj_name, OBJ_ARROW, 0, time1, price1);
      }
      else
      {
        ObjectMove(obj_name, 0, time1, price1);
      }
      
      ObjectSet(obj_name, OBJPROP_WIDTH, Chart.BearishDiv.ArrowSize2);
      ObjectSet(obj_name, OBJPROP_COLOR, clBearishDiv);
      ObjectSet(obj_name, OBJPROP_ARROWCODE, Chart.BearishDiv.ArrowCode2);

      //-----
      
      obj_name = prefix + "_chart_ul_" + Time[chart_fr.j] + "_" + Time[chart_fr.i];
      time1 = Time[chart_fr.j];
      price1 = High[chart_fr.j] + Chart_Line_shift[ITF(Period())]*PointEx();
      time2 = Time[chart_fr.i];
      price2 = High[chart_fr.i] + Chart_Line_shift[ITF(Period())]*PointEx();
      
      if (ObjectFind(obj_name) == -1)
      {
        ObjectCreate(obj_name, OBJ_TREND, 0, time1, price1, time2, price2);
      }
      else
      {
        ObjectMove(obj_name, 0, time1, price1);
        ObjectMove(obj_name, 1, time2, price2);
      }
              
      ObjectSet(obj_name, OBJPROP_RAY, false);
      ObjectSet(obj_name, OBJPROP_COLOR, clBearishDiv);
      ObjectSet(obj_name, OBJPROP_STYLE, Chart.Line.style);
      ObjectSet(obj_name, OBJPROP_WIDTH, Chart.Line.width);

      //-----
      
      obj_name = prefix + "_ind_ul_" + Time[ind_fr.j] + "_" + Time[ind_fr.i];
      time1 = Time[ind_fr.j];
      price1 = ind_left_fr[ind_fr.j];
      time2 = Time[ind_fr.i];
      price2 = ind_right_fr[ind_fr.i];

      if (win_ind != -1)
      {
        if (ObjectFind(obj_name) == -1)
        {
          ObjectCreate(obj_name, OBJ_TREND, win_ind, time1, price1, time2, price2);
        }
        else
        {
          ObjectMove(obj_name, 0, time1, price1);
          ObjectMove(obj_name, 1, time2, price2);
        }

        ObjectSet(obj_name, OBJPROP_RAY, false);
        ObjectSet(obj_name, OBJPROP_COLOR, clBearishDiv);
        ObjectSet(obj_name, OBJPROP_STYLE, Ind.Line.style);
        ObjectSet(obj_name, OBJPROP_WIDTH, Ind.Line.width);
      }
    }
  }
 
  
  //-----
  
  
  for (i=0; i < Bars; i++)
  {  
    chart_right_fr[i] = 0.0;
    chart_left_fr[i] = 0.0;
    
    ind_right_fr[i] = 0.0;
    ind_left_fr[i] = 0.0;
  }

  limit = MaxBars;
  for (i=0; i < limit; i++)
  {
    chart_right_fr[i] = chart_is_fr(MODE_LOWER, Chart.RightTop.BarsL, Chart.RightTop.BarsR, i);
    chart_left_fr[i] = chart_is_fr(MODE_LOWER, Chart.LeftTop.BarsL, Chart.LeftTop.BarsR, i);
    
    ind_right_fr[i] = ind_is_fr(MODE_LOWER, Ind.RightTop.BarsL, Ind.RightTop.BarsR, i);
    ind_left_fr[i] = ind_is_fr(MODE_LOWER, Ind.LeftTop.BarsL, Ind.LeftTop.BarsR, i);
  }



  for (i=0; i < limit; i++)
  {
    chart_fr.i = i + Chart.RightTop.BarsR + shift;
    ind_fr.i = i + Ind.RightTop.BarsR + shift;
    
    if (chart_right_fr[chart_fr.i] == 0 && ind_right_fr[ind_fr.i] == 0) continue;

    for (k=0; k < RightTops.MaxBarsDiff; k++)
    {
      if (chart_right_fr[chart_fr.i] != 0)
      {
        ind_fr.i = chart_fr.i + k;
        if (ind_right_fr[ind_fr.i] != 0 && ind_fr.i - Ind.RightTop.BarsR - shift >= i)
        {
          break;
        }
      }

      if (ind_right_fr[ind_fr.i] != 0)
      {
        chart_fr.i = ind_fr.i + k;
        if (chart_right_fr[chart_fr.i] != 0 && chart_fr.i - Chart.RightTop.BarsR - shift >= i)
        {
          break;
        }
      }
    }
    
    if (chart_right_fr[chart_fr.i] == 0 || ind_right_fr[ind_fr.i] == 0) continue;
    
    //-----
    
    j0 = MathMax(chart_fr.i, ind_fr.i) + 1;
    for (j=j0; j < limit; j++)
    {
      chart_fr.j = j;
      ind_fr.j = j;
     
      if (chart_fr.j - Chart.LeftTop.BarsR - shift < i + 1) continue;
      if (ind_fr.j - Ind.LeftTop.BarsR - shift < i + 1) continue;


      if (chart_left_fr[chart_fr.j] == 0 && ind_left_fr[ind_fr.j] == 0) continue;

    
      for (k=0; k < LeftTops.MaxBarsDiff; k++)
      {
        if (chart_left_fr[chart_fr.j] != 0)
        {
          ind_fr.j = chart_fr.j + k;
          if (ind_left_fr[ind_fr.j] != 0 && ind_fr.j - Ind.LeftTop.BarsR - shift >= i + 1)
          {
            break;
          }
        }

        if (ind_left_fr[ind_fr.j] != 0)
        {
          chart_fr.j = ind_fr.j + k;
          if (chart_left_fr[chart_fr.j] != 0 && chart_fr.j - Chart.LeftTop.BarsR - shift >= i + 1)
          {
            break;
          }
        }
      }


      if (chart_fr.j-chart_fr.i > DivWidth.MaxBars) break;
      
      if (chart_left_fr[chart_fr.j] == 0 || ind_left_fr[ind_fr.j] == 0) continue;

      
      //-----

      if (chart_left_fr[chart_fr.j] < chart_right_fr[chart_fr.i]) break;

      if (ind_left_fr[ind_fr.j] > ind_right_fr[ind_fr.i]) continue;

      
      if (chart_fr.j-chart_fr.i < DivWidth.MinBars) continue;
      if (ind_fr.j-ind_fr.i < DivWidth.MinBars) continue;

      w = MathAbs(chart_left_fr[chart_fr.j] - chart_right_fr[chart_fr.i]);
      
      if (w < DivHeight.MinPips*PointEx()) continue; 
      if (w > DivHeight.MaxPips*PointEx()) continue;
      
      //-----

      chart.bar = -1;
      for (k = chart_fr.i+1; k < chart_fr.j; k++)
      {
        if (Low[k] < Low[chart_fr.i] - 1*PointEx())
        {
          chart.bar = k;
          break;
        }
      }
      
      if (chart.bar >= 0) break;

      
      //-----
      
      x1 = ind_fr.j;
      y1 = fInd(x1);
      x2 = ind_fr.i;
      y2 = fInd(x2);
      a = (y2-y1)/(x2-x1);
      b = y1 - a*x1;

      ind.bar = -1;
      for (k = ind_fr.i; k < ind_fr.j; k++)
      {
        ind.val = fInd(k);
        if (ind.val < ind_left_fr[ind_fr.j])
        {
          ind.bar = k;
          break;
        }
        
        
        f = a*k + b;
        if (fInd(k) < f - kIT*MathAbs(y2-y1))
        {
          ind.bar = k;
          break;
        }


        if (ind.val > BullishDiv.ResetAbove)
        {
          ind.bar = k;
          break;
        }
      }
      
      if (ind.bar >= 0) break;
      
            
      //-----
            
      obj_name = prefix + "_chart_la1_" + Time[i];
      time1 = Time[i];
      price1 = Low[i] - 1.0*Chart_Arrow_shift[ITF(Period())]*PointEx();
      
      if (ObjectFind(obj_name) == -1)
      {
        ObjectCreate(obj_name, OBJ_ARROW, 0, time1, price1);
      }
      else
      {
        ObjectMove(obj_name, 0, time1, price1);
      }
      
      ObjectSet(obj_name, OBJPROP_WIDTH, Chart.BullishDiv.ArrowSize1);
      ObjectSet(obj_name, OBJPROP_COLOR, clBullishDiv);
      ObjectSet(obj_name, OBJPROP_ARROWCODE, Chart.BullishDiv.ArrowCode1);

      //-----
      
      obj_name = prefix + "_chart_la2_" + Time[chart_fr.j];
      time1 = Time[chart_fr.j];
      price1 = Low[chart_fr.j] - 1.0*Chart_Arrow_shift[ITF(Period())]*PointEx();
      
      if (ObjectFind(obj_name) == -1)
      {
        ObjectCreate(obj_name, OBJ_ARROW, 0, time1, price1);
      }
      else
      {
        ObjectMove(obj_name, 0, time1, price1);
      }
      
      ObjectSet(obj_name, OBJPROP_WIDTH, Chart.BullishDiv.ArrowSize2);
      ObjectSet(obj_name, OBJPROP_COLOR, clBullishDiv);
      ObjectSet(obj_name, OBJPROP_ARROWCODE, Chart.BullishDiv.ArrowCode2);

      //-----
      
      obj_name = prefix + "_chart_ll_" + Time[chart_fr.j] + "_" + Time[chart_fr.i];
      time1 = Time[chart_fr.j];
      price1 = Low[chart_fr.j] - Chart_Line_shift[ITF(Period())]*PointEx();
      time2 = Time[chart_fr.i];
      price2 = Low[chart_fr.i] - Chart_Line_shift[ITF(Period())]*PointEx();
      
      if (ObjectFind(obj_name) == -1)
      {
        ObjectCreate(obj_name, OBJ_TREND, 0, time1, price1, time2, price2);
      }
      else
      {
        ObjectMove(obj_name, 0, time1, price1);
        ObjectMove(obj_name, 1, time2, price2);
      }
              
      ObjectSet(obj_name, OBJPROP_RAY, false);
      ObjectSet(obj_name, OBJPROP_COLOR, clBullishDiv);
      ObjectSet(obj_name, OBJPROP_STYLE, Chart.Line.style);
      ObjectSet(obj_name, OBJPROP_WIDTH, Chart.Line.width);

      //-----
      
      obj_name = prefix + "_ind_ll_" + Time[ind_fr.j] + "_" + Time[ind_fr.i];
      time1 = Time[ind_fr.j];
      price1 = ind_left_fr[ind_fr.j];
      time2 = Time[ind_fr.i];
      price2 = ind_right_fr[ind_fr.i];

      if (win_ind != -1)
      {
        if (ObjectFind(obj_name) == -1)
        {
          ObjectCreate(obj_name, OBJ_TREND, win_ind, time1, price1, time2, price2);
        }
        else
        {
          ObjectMove(obj_name, 0, time1, price1);
          ObjectMove(obj_name, 1, time2, price2);
        }

        ObjectSet(obj_name, OBJPROP_RAY, false);
        ObjectSet(obj_name, OBJPROP_COLOR, clBullishDiv);
        ObjectSet(obj_name, OBJPROP_STYLE, Ind.Line.style);
        ObjectSet(obj_name, OBJPROP_WIDTH, Ind.Line.width);
      }
    }
  }
}  

int CyclePeriod()
{
  int size = ArraySize(CP_DeltaPhase);
  if (size < Bars)
  {
    size = Bars;    
    ArrayResize(CP_DeltaPhase, size);
    ArrayResize(CP_CPeriod, size);
    ArrayResize(CP_Smooth, size);
    ArrayResize(CP_Cycle, size);
    ArrayResize(CP_Q1, size);
    ArrayResize(CP_I1, size);
    ArrayResize(CP_InstPeriod, size);
  }

  //-----
  
  if (Bars <= drawBegin) return (0);
  int countedBars = IndicatorCounted();
  if (countedBars < 0) return (-1);
  if (countedBars > 0) countedBars--;
  int s, limit = Bars - countedBars - 1;
  for (s = limit; s >= 0; s--) {
      Smooth[s] = (P(s) + 2.0 * P(s + 1) + 2.0 * P(s + 2) + P(s + 3)) / 6.0;
      Cycle[s] = (1.0 - 0.5 * Alpha) * (1.0 - 0.5 * Alpha) * (Smooth[s] - 2 * Smooth[s + 1] + Smooth[s + 2]) 
          + 2.0 * (1.0 - Alpha) * Cycle[s + 1]
          - (1.0 - Alpha) * (1.0 - Alpha) * Cycle[s + 2];
      if (s > Bars - 8) {
          Cycle[s] = (P(s) - 2.0 * P(s + 1) + P(s + 2)) / 4.0;           
      }
      CP_Q1[s] = (0.0962 * Cycle[s] + 0.5769 * Cycle[s + 2] - 0.5769 * Cycle[s + 4] - 0.0962 * Cycle[s + 6])
          * (0.5 + 0.08 * CP_InstPeriod[s + 1]);
      CP_I1[s] = Cycle[s + 3];
      if (CP_Q1[s] != 0.0 && CP_Q1[s + 1] != 0.0) {
          CP_DeltaPhase[s] = (CP_I1[s] / CP_Q1[s] - CP_I1[s + 1] / CP_Q1[s + 1]) 
              / (1.0 + CP_I1[s] * CP_I1[s + 1] / (CP_Q1[s] * CP_Q1[s + 1]));
      }
      CP_DeltaPhase[s] = MathMax(0.1, CP_DeltaPhase[s]);
      CP_DeltaPhase[s] = MathMin(1.1, CP_DeltaPhase[s]);
      double MedianDelta, DC;
      double M[];
      ArrayResize(M, median);
      ArrayCopy(M, CP_DeltaPhase, 0, s, median);
      ArraySort(M);
      if (median % 2 == 0) {
          MedianDelta = (M[median / 2] + M[(median / 2) + 1]) / 2.0;
      } else {
          MedianDelta = M[median / 2];
      }
      if (MedianDelta == 0.0) {
          DC = 15.0;
      } else {
          DC = 6.28318 / MedianDelta + 0.5;
      }
      CP_InstPeriod[s] = 0.33 * DC + 0.67 * CP_InstPeriod[s + 1];
      CP_CPeriod[s] = 0.15 * CP_InstPeriod[s] + 0.85 * CP_CPeriod[s + 1];
  }

  return (0);
}

int AdaptiveCyberCycle()
{
  CyclePeriod();
  
  //-----
  
  if (Bars <= drawBegin) return (0);
  int countedBars = IndicatorCounted();
  if (countedBars < 0) return (-1);
  if (countedBars > 0) countedBars--;
  int s, limit = Bars - countedBars - 1;
  for (s = limit; s >= 0; s--) 
  {
    Smooth[s] = (P(s) + 2.0 * P(s + 1) + 2.0 * P(s + 2) + P(s + 3)) / 6.0;
    
    double period = CP_CPeriod[s];
    double alpha1 = 2.0 / (period + 1.0);
    
    Cycle[s] = (1.0 - 0.5 * alpha1) * (1.0 - 0.5 * alpha1) * (Smooth[s] - 2.0 * Smooth[s + 1] 
        + Smooth[s + 2]) + 2.0 * (1.0 - alpha1) * Cycle[s + 1] - (1.0 - alpha1) * Cycle[s + 2];
        
    if (s > Bars - 8) {
      Cycle[s] = (P(s) - 2.0 * P(s + 1) + P(s + 2)) / 4.0;
    }
    
    Trigger[s] = Cycle[s + 1];
  }
  
  return (0);
}

double P(int index) 
{
  return ((High[index] + Low[index]) / 2.0);
}


void clear()
{
  int total = ObjectsTotal();  
  for (int i=total-1; i >= 0; i--) 
  {
    string name = ObjectName(i);
    if (StringFind(name, prefix) == 0) ObjectDelete(name);
  }
}


double chart_is_fr(int mode, int left_period, int right_period, int shift)
{
  int l = 0;
  int r = 0;

  if (mode == MODE_UPPER)
  {
    for (int i=1; i <= left_period; i++)
    {
      if (shift+i >= Bars) break;
      
      if (High[shift+i] <= High[shift]) l++;
    }

    for (i=1; i <= right_period; i++)
    {
      if (shift-i < 0) break;
      
      if (High[shift-i] < High[shift]) r++;
    }
    
    if (l == left_period && r == right_period) return (High[shift]);
  }


  if (mode == MODE_LOWER)
  {
    for (i=1; i <= left_period; i++)
    {
      if (shift+i >= Bars) break;
      
      if (Low[shift+i] >= Low[shift]) l++;
    }

    for (i=1; i <= right_period; i++)
    {
      if (shift-i < 0) break;
      
      if (Low[shift-i] > Low[shift]) r++;
    }
    
    if (l == left_period && r == right_period) return (Low[shift]);
  }
  
  return (0);
}


double ind_is_fr(int mode, int left_period, int right_period, int shift)
{
  int l = 0;
  int r = 0;
  
  double ind.val, ind.val_l, ind.val_r;
  
  ind.val = fInd(shift);

  if (mode == MODE_UPPER)
  {
    for (int i=1; i <= left_period; i++)
    {
      if (shift+i >= Bars) break;
          
      ind.val_l = fInd(shift+i);
      if (ind.val_l <= ind.val) l++;
    }

    for (i=1; i <= right_period; i++)
    {
      if (shift-i < 0) break;

      ind.val_r = fInd(shift-i);
      if (ind.val_r < ind.val) r++;
    }
    
    if (l == left_period && r == right_period) return (ind.val);
  }
  

  if (mode == MODE_LOWER)
  {
    for (i=1; i <= left_period; i++)
    {
      if (shift+i >= Bars) break;
      
      ind.val_l = fInd(shift+i);
      if (ind.val_l >= ind.val) l++;
    }

    for (i=1; i <= right_period; i++)
    {
      if (shift-i < 0) break;
      
      ind.val_r = fInd(shift-i);
      if (ind.val_r > ind.val) r++;
    }
    
    if (l == left_period && r == right_period) return (ind.val);
  }
  
  return (0);
}

void split_i(int& arr[], string str, string sym) 
{
  ArrayResize(arr, 0);
  string item;
  int pos, size;
  
  int len = StringLen(str);
  for (int i=0; i < len;) 
  {
    pos = StringFind(str, sym, i);
    if (pos == -1) pos = len;
    
    item = StringSubstr(str, i, pos-i);
    item = StringTrimLeft(item);
    item = StringTrimRight(item);
    
    size = ArraySize(arr);
    ArrayResize(arr, size+1);
    arr[size] = StrToInteger(item);
    
    i = pos+1;
  }
}

string str_replace(string subject, string search, string replace) 
{
  string left_part = "";
  string right_part = "";

  for (;;) 
  {
    int pos = StringFind(subject, search);
    if (pos == -1) break;
    
    left_part = "";
    right_part = "";
    
    if (pos > 0) left_part = StringSubstr(subject, 0, pos);
    if (pos+1 < StringLen(subject)) right_part = StringSubstr(subject, pos+StringLen(search));

    subject = left_part + replace + right_part;
  }

  return (subject);
}

int ITF(int period)
{
  if (period == PERIOD_M1)  return (0);
  if (period == PERIOD_M5)  return (1);
  if (period == PERIOD_M15) return (2);
  if (period == PERIOD_M30) return (3);
  if (period == PERIOD_H1)  return (4);
  if (period == PERIOD_H4)  return (5);
  if (period == PERIOD_D1)  return (6);
  if (period == PERIOD_W1)  return (7);
  if (period == PERIOD_MN1) return (8);
  
  return (4);
}

Recommend