Author: Alexandr Sokolov

This script is a technical trading tool designed for the MetaTrader platform to visualize and analyze market behavior through two distinct mathematical models. It allows traders to switch between observing price differences and percentage-based ratios to identify trends and potential cycles.

Core Functionality

The script operates in two primary modes, which fundamentally change how it interprets price data:

  • Difference Mode: This focuses on the raw numerical change in price. It calculates the distance between the current closing price and a previous opening price relative to a user-defined lookback period.
  • Ratio Mode: This mode shifts the perspective to percentage-based movement. It evaluates where the current price sits within a specific high/low range, allowing for a more normalized view of volatility.

Key Technical Components

The logic follows a structured process to convert raw market data into visual signals:

1. Range Analysis (High/Low Detection) The script continuously scans a specific window of time (determined by the "K Period" setting). It identifies the highest and lowest prices reached during that timeframe. This creates a dynamic "boundary" that helps the script understand the immediate boundaries of market movement.

2. Value Calculation Depending on the active mode, the script calculates a primary value:

  • In Difference Mode, it determines the net price change.
  • In Ratio Mode, it determines the relative position of the current price compared to the boundaries established in the range analysis.

3. Signal Generation The script produces two main types of visual feedback:

  • Main Indicator Line: A continuous line (or histogram) that shows the calculated value. This allows you to see the "momentum" or "cycle" of the price over time.
  • Signal Line: An auxiliary calculation that uses a secondary period ("D Period") to smooth out or average the main values. This helps in identifying whether the current movement is an outlier or part of a sustained trend.

4. Visual Overlays (Optional) When configured for Ratio Mode, the script can display "Upper" and "Lower" levels (e.g., 80 and 20). These act as visual boundaries, helping traders identify overbought or oversold conditions within a specific cycle.

Summary of User Controls

The tool is highly customizable via input parameters:

  • Periods: You can define how far back the script should look to calculate "Highs," "Lows," and price "Differences."
  • Visual Styles: Users can choose between a histogram or solid lines for the main data.
  • Level Visibility: Options are available to toggle the visibility of the upper and lower threshold boundaries.
2 Views
1 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 indicator_separate_window

#property indicator_buffers  9

#property indicator_plots    4



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

#property indicator_style1   STYLE_SOLID

#property indicator_label1   "K line"



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

#property indicator_type2    DRAW_LINE      //"8? 3@0D8G5A:>3> ?>AB@>5=8O !2

#property indicator_style2   STYLE_DOT      //!B8;L ;8=88 3@0D8G5A:>3> ?>AB@>5=8O !2

#property indicator_width2   1              //">;I8=0 ;8=88 3@0D8G5A:>3> ?>AB@>5=8O !2

#property indicator_color2   clrBlack       //&25B ;8=88 3@0D8G5A:>3> ?>AB@>5=8O !2

#property indicator_label2   "D line"  //A?;K20NI55 >?8A0=85 ;8=88 3@0D8G5A:>3> ?>AB@>5=8O !2



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

#property indicator_type3    DRAW_LINE

#property indicator_style3   STYLE_SOLID

#property indicator_width3   1

#property indicator_color3   clrBlue

#property indicator_label3   "High value"



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

#property indicator_type4    DRAW_LINE

#property indicator_style4   STYLE_SOLID

#property indicator_width4   1

#property indicator_color4   clrBlue

#property indicator_label4   "Low value"



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

double KV[], CI[], //;O >A=>2=>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

       Open[], High[], Low[], Close[]; //;O ?>;CG5=8O F5=



//--- !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 || llevel < 0)) {Print("Lower level cannot be more than 100 or less than 0"); return(INIT_PARAMETERS_INCORRECT);};

   

  //--- #AB0=02;8205< A2>9AB20 ?5@2>3> 3@0D8G5A:>3> ?>AB@>5=8O 2 7028A8<>AB8 >B 2K1@0==>3> @568<0 8=48:0B>@0

   PlotIndexSetInteger(0,PLOT_DRAW_TYPE,(mode == m1 ? DRAW_COLOR_HISTOGRAM : DRAW_LINE)); //"8? ?5@2>3> 3@0D8G5A:>3> ?>AB@>5=8O

   PlotIndexSetInteger(0,PLOT_LINE_WIDTH,(mode == m1 ? 2 : 1));                           //">;I8=0 ;8=88 ?5@2>3> 3@0D8G5A:>3> ?>AB@>5=8O

   PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,(mode == m1 ? 2 : 1));                        //>;8G5AB2> F25B>2 ;8=88 ?5@2>3> 3@0D8G5A:>3> ?>AB@>5=8O

   PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,(mode == m1 ? clrLime : clrBlack));            //5@2K9 F25B ;8=88 ?5@2>3> 3@0D8G5A:>3> ?>AB@>5=8O

   if(mode == m1) {PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,clrRed);};                     //B>@>9 F25B ;8=88 ?5@2>3> 3@0D8G5A:>3> ?>AB@>5=8O

   

  //--- N0=AK 287C0;870F88 -----------------------------------------

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

   IndicatorSetString(INDICATOR_SHORTNAME,window_name);

   IndicatorSetInteger(INDICATOR_DIGITS,(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);

     };

   

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,kperiod);

   if(sline == s1) {PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,kperiod+dperiod);};

   if(mode == m1) {PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,kperiod-1);};

   if(mode == m1) {PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,kperiod-1);};

   

  //--- @82O7K205< =5:>B>@K5 1CD5@K : 3@0D8G5A:8< ?>AB@>5=8O< 8 C:07K205< 8E ?@54=07=0G5=85

   SetIndexBuffer(0,KV,INDICATOR_DATA);

   SetIndexBuffer((mode == m1 ? 1 : 2),CI,(mode == m1 ? INDICATOR_COLOR_INDEX : INDICATOR_CALCULATIONS));

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

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

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

   SetIndexBuffer(5,Open,INDICATOR_CALCULATIONS); SetIndexBuffer(6,High,INDICATOR_CALCULATIONS);

   SetIndexBuffer(7,Low,INDICATOR_CALCULATIONS);  SetIndexBuffer(8,Close,INDICATOR_CALCULATIONS);

   

  //--- #:07K205< =0?@02;5=85 8=45:A0F88 1CD5@>2 --------------------

   ArraySetAsSeries(KV,true);    ArraySetAsSeries(CI,true);

   ArraySetAsSeries(DV,true);    ArraySetAsSeries(VHigh,true);

   ArraySetAsSeries(VLow,true);  ArraySetAsSeries(Open,true);

   ArraySetAsSeries(High,true);  ArraySetAsSeries(Low,true);

   ArraySetAsSeries(Close,true);

   

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

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

  {

   

  //--- 1) >?8@C5< F5=K --------------------------------------------

   int count = rates_total - prev_calculated, copied = 0; if(count < (int)kperiod) {count = (int)kperiod;};

   if(mode == m1) {copied = CopyOpen(_Symbol,PERIOD_CURRENT,0,count,Open); if(copied != count) {return(0);};};

   copied = CopyHigh(_Symbol,PERIOD_CURRENT,0,count,High);   if(copied != count) return(0);

   copied = CopyLow(_Symbol,PERIOD_CURRENT,0,count,Low);     if(copied != count) return(0);

   copied = CopyClose(_Symbol,PERIOD_CURRENT,0,count,Close); if(copied != count) return(0);

  

  //--- 2) =8F80;878@C5< 1CD5@K ------------------------------------

   if(prev_calculated == 0)

     {

      ArrayInitialize(KV,EMPTY_VALUE);

      ArrayInitialize(CI,EMPTY_VALUE);

      ArrayInitialize(DV,EMPTY_VALUE);

      ArrayInitialize(VHigh,EMPTY_VALUE);

      ArrayInitialize(VLow,EMPTY_VALUE);

     };

   

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

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

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

     {

      KValues(i);

      KV[i] = (mode == m1 ? (Close[i] - Open[i+(int)kperiod-1]) * MathPow(10,_Digits) : 100 * ((Close[i]-vlow) / (vhigh-vlow)));

      CI[i] = (mode == m1 ? (KV[i] >= 0 ? (KV[i] > KV[i+1] ? 0 : 1) : (KV[i] < KV[i+1] ? 0 : 1)) : EMPTY_VALUE);

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

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

      VLow[i] = (mode == m1 ? (vlow - Open[i+(int)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;

   int limit = i + (int)dperiod;

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

     {

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

     };

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

   return(value/(int)dperiod);

  }

Comments