Author: Alexandr Sokolov
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
MCC
ÿþ#property copyright    "Alexandr Sokolov"

#property link         "https://www.mql5.com/en/users/asokolov7"

#property version      "1.00"

#property description  "Market Change Cycles"

#property strict

#property indicator_separate_window

#property indicator_buffers 5



//--- CD5@K --------------------------------------------------------

double  KV_S[],  //;O >A=>2=>3> A8;L=>3> 7=0G5=8O 8=48:0B>@0

        KV_W[],  //;O >A=>2=>3> A;01>3> 7=0G5=8O 8=48:0B>@0

        DV[],    //;O A83=0;L=>9 ;8=88 8=48:0B>@0

        VHigh[], //;O 2KAH59 ;8=88 8=48:0B>@0

        VLow[];  //;O =87H59 ;8=88 8=48:0B>@0



//--- !B0B8G5A:85 ?5@5<5==K5 ----------------------------------------

static double  vhigh, //;O ?5@540G8 2KAH59 F5=K High 2 @0<:0E ?5@8>40

               vlow;  //;O ?5@540G8 =87H59 F5=K Low 2 @0<:0E ?5@8>40



//---  4;O C:070=8O @568<0 8=48:0B>@0 -----------------------------

enum I_MODE

  {

   m1, //Difference

   m2  //Ratio

  };



//---  4;O 2K1>@0 287C0;878@C5<KE ?0@0<5B@>2 ----------------------

enum I_SHOW

  {

   s1, //Show

   s2  //Not show

  };



//--- E>4=K5 ?0@0<5B@K ---------------------------------------------

input I_MODE  mode     = m1; //Mode

input uint    kperiod  = 5;  //K period

input I_SHOW  sline    = s1; //Signal line

input uint    dperiod  = 3;  //D period

input I_SHOW  levels   = s2; //Levels

input double  ulevel   = 80, //Upper level

              llevel   = 20; //Lower level



//-------------------------------------------------------------------------------------------------

int OnInit()

  {

   

  //--- @>25@:0 =0 :>@@5:B=>ABL 2E>4=KE ?0@0<5B@>2 -----------------

   if(kperiod < 1) {Print("Main period can not be less than 1"); return(INIT_PARAMETERS_INCORRECT);};

   if(dperiod < 1) {Print("Signal period can not be less than 1"); return(INIT_PARAMETERS_INCORRECT);};

   if(mode == m2 && levels == s1 && (ulevel > 100 || ulevel < 0)) {Print("Upper level cannot be greater than 100 or less than 0"); return(INIT_PARAMETERS_INCORRECT);};

   if(mode == m2 && levels == s1 && (llevel > 100 || ulevel < 0)) {Print("Lower level cannot be more than 100 or less than 0"); return(INIT_PARAMETERS_INCORRECT);};

   

  //--- 5@2>5 3@0D8G5A:>5 ?>AB@>5=85 8=48:0B>@0 (4;O >A=>2=>3> 7=0G5=8O)

   SetIndexBuffer(0,KV_S,INDICATOR_DATA);

   SetIndexStyle(0,(mode == m1 ? DRAW_HISTOGRAM : DRAW_LINE),STYLE_SOLID,(mode == m1 ? 2 : 1),(mode == m1 ? clrLime : clrBlack));

   SetIndexLabel(0,"K line");

   

  //--- B>@>5 3@0D8G5A:>5 ?>AB@>5=85 8=48:0B>@0 (4;O A;01>3> >A=>2=>3> 7=0G5=8O 38AB>3@0<<K)

   SetIndexBuffer(1,KV_W,(mode == m1 ? INDICATOR_DATA : INDICATOR_CALCULATIONS)); //@82O7K205< 1CD5@ : 287C0;878@C5<>9 ?5@5<5==>9 !1 8 C:07K205< 53> ?@54=07=0G5=85

   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,2,clrRed);                          //#:07K205< B8?, AB8;L, B>;I8=C 8 F25B 287C0;878@C5<>9 ?5@5<5==>9 !1

   SetIndexLabel(1,"K line");                                                     //A?;K20NI55 >?8A0=85 287C0;878@C5<>9 ?5@5<5==>9 !1

   

  //--- "@5BL5 3@0D8G5A:>5 ?>AB@>5=85 8=48:0B>@0 --------------------

   SetIndexBuffer(2,DV,(sline == s1 ? INDICATOR_DATA : INDICATOR_CALCULATIONS));

   SetIndexStyle(2,DRAW_LINE,STYLE_DOT,1,clrBlack);

   SetIndexLabel(2,"D line");

   

  //--- '5B2Q@B>5 3@0D8G5A:>5 ?>AB@>5=85 8=48:0B>@0 -----------------

   SetIndexBuffer(3,VHigh,(mode == m1 ? INDICATOR_DATA : INDICATOR_CALCULATIONS));

   SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,1,clrBlue);

   SetIndexLabel(3,"High value");

   

  //--- OB>5 3@0D8G5A:>5 ?>AB@>5=85 8=48:0B>@0 ---------------------

   SetIndexBuffer(4,VLow,(mode == m1 ? INDICATOR_DATA : INDICATOR_CALCULATIONS));

   SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,1,clrBlue);

   SetIndexLabel(4,"Low value");

   

  //---

   string window_name = "MCC " + (mode == m1 ? "D(" : "R(") + (sline == s1 ? (string)kperiod+","+(string)dperiod : (string)kperiod) + ")";

   IndicatorShortName(window_name);

   IndicatorDigits((mode == m1 ? 0 : 2));

   

   if(levels == s1)

     {

      IndicatorSetInteger(INDICATOR_LEVELS,2);

      IndicatorSetDouble(INDICATOR_LEVELVALUE,0,ulevel);

      IndicatorSetDouble(INDICATOR_LEVELVALUE,1,llevel);

     };

   

   if(mode == m2)

     {

      IndicatorSetDouble(INDICATOR_MAXIMUM,100);

      IndicatorSetDouble(INDICATOR_MINIMUM,0);

     };

   

  //-----------------------------------------------------------------

   return(INIT_SUCCEEDED);

  }



//-------------------------------------------------------------------------------------------------

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[])

  {

   

  //---  0AG8BK205< 7=0G5=8O ----------------------------------------

   double mv0, mv1; //;O ?5@540G8 7=0G5=89 =0 @0AG8BK205<>9 8 ?@54K4CI59 B09<A5@88

   int limit = rates_total - prev_calculated - (int)kperiod - 1; if(limit < 0) {limit = 0;};

   for(int i = limit; i >= 0; i--)

     {

      KValues(i);

      mv0 = Close[i] - Open[i+(int)kperiod-1];

      mv1 = Close[i+1] - Open[i+(int)kperiod];

      KV_S[i] = (mode == m1 ? (mv0 >= 0 ? (mv0 > mv1 ? mv0 * MathPow(10,_Digits) : EMPTY_VALUE) : (mv0 < mv1 ? mv0 * MathPow(10,_Digits) : EMPTY_VALUE)) : 100 * ((Close[i] - vlow) / (vhigh - vlow)));

      KV_W[i] = (mode == m1 ? (mv0 >= 0 ? (mv0 <= mv1 ? mv0 * MathPow(10,_Digits) : EMPTY_VALUE) : (mv0 >= mv1 ? mv0 * MathPow(10,_Digits) : EMPTY_VALUE)) : EMPTY_VALUE);

      DV[i] = (sline == s1 && (prev_calculated != 0 || (prev_calculated == 0 && i < limit - (int)dperiod)) ? (mode == m1 ? DValue(i) * MathPow(10,_Digits) : DValue(i)) : EMPTY_VALUE);

      VHigh[i] = (mode == m1 ? (vhigh - Open[i+kperiod-1]) * MathPow(10,_Digits) : EMPTY_VALUE);

      VLow[i] = (mode == m1 ? (vlow - Open[i+kperiod-1]) * MathPow(10,_Digits) : EMPTY_VALUE);

     };

  //-----------------------------------------------------------------

   return(rates_total);

  }



//--- $C=:F8O 4;O ?>;CG5=8O <0:A8<0;L=>3> High 8 <8=8<0;L=>3> Low 2 @0<:0E ?5@8>40 ----------------

void KValues(int i)

  {

   vhigh = 0; vlow = High[i];

   int limit = i + (int)kperiod;

   for(int a = i; a < limit; a++)

     {

      vhigh = (High[a] > vhigh ? High[a] : vhigh);

      vlow  = (Low[a] < vlow ? Low[a] : vlow);

     };

  }



//--- $C=:F8O 4;O @0AGQB0 A83=0;L=>9 ;8=88 --------------------------------------------------------

double DValue(int i)

  {

   double value = 0, vm;

   int limit = i + (int)dperiod;

   for(int a = i; a < limit; a++)

     {

      vm = (KV_S[a] != EMPTY_VALUE ? KV_S[a] : KV_W[a]);

      value += (mode == m1 ? Close[a] - Open[a+(int)kperiod-1] : vm);

     };

  //-----------------------------------------------------------------

   return(value/(int)dperiod);

  }

Comments