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

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

#property version     "1.00"

#property description "Market Phase Index"

#property strict

#property indicator_separate_window

#property indicator_minimum 0

#property indicator_buffers 3

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

double MPI_weak[], MPI_strong_1[], MPI_strong_2[];

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

input int period = 5; //Period

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

int OnInit()

  {

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

   if(period < 1) {Print("The period of the indicator can not be less than 1"); return(INIT_FAILED);};

  //--- N0=AK ------------------------------------------------------

   IndicatorDigits(2);                             //>;8G5AB2> 45AOB8G=KE 7=0:>2 ?>A;5 70?OB>9

   IndicatorShortName("MPI ("+(string)period+")"); //8F52>5 8<O 2 >:=5 8=48:0B>@0

  //--- !2>9AB20 ?5@2>3> (A;01>3>) 3@0D8G5A:>3> ?>AB@>5=8O ----------

   SetIndexBuffer(0,MPI_weak);                      //@82O7K205< 1CD5@ : ;8=88 8=48:0B>@0 !0

   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2,clrRed); //#:07K205< B8?, AB8;L, B>;I8=C 8 F25B ;8=88 !0

   SetIndexLabel(0,"MPI");                          //@>?8AK205< 2A?;K20NI55 >?8A0=85 4;O ;8=88 !0

  //--- !2>9AB20 2B>@>3> (A8;L=>3> - ?5@2>3> ?@><56CB>G=>3>) 3@0D8G5A:>3> ?>AB@>5=8O

   SetIndexBuffer(1,MPI_strong_1);

   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2,clrLime);

   SetIndexLabel(1,"MPI");

  //--- !2>9AB2> B@5BL53> (A8;L=>3> - 2B>@>3> ?@><56CB>G=>3>) 3@0D8G5A:>3> ?>AB@>5=8O

   SetIndexBuffer(2,MPI_strong_2);

   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2,clrLime);

   SetIndexLabel(2,"MPI");

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

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

  {

   double mpi0, mpi1; //;O ?5@540G8 7=0G5=89

  //--- 1) #AB0=02;8205< ?0@0<5B@K 4;O F8:;0 ------------------------

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

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

     {

     //--- 2) KAG8BK205< 8 ?>;CG05< 7=0G5=8O 8=48:0B>@0 :064>9 >B45;L=>9 B09<A5@88

      mpi0 = Value(i);

      mpi1 = (MPI_weak[i+1] != EMPTY_VALUE ? MPI_weak[i+1] : (MPI_strong_1[i+1] != EMPTY_VALUE ? MPI_strong_1[i+1] : MPI_strong_2[i+1]));

     //--- 3) @8A208205< 7=0G5=85 ?>AB@>5=8N A A>>B25BA2CNI8< F25B>< A A>>B25BAB2CNI59 ?>A;54>20B5;L=>ABLN/:><18=0F859

      if(mpi0 > mpi1)

        {

         MPI_weak[i] = EMPTY_VALUE;

         MPI_strong_1[i] = ((MPI_strong_1[i+1] == EMPTY_VALUE && MPI_strong_1[i+2] != EMPTY_VALUE) || MPI_strong_2[i+1] != EMPTY_VALUE ? EMPTY_VALUE : mpi0);

         if(MPI_strong_1[i] != EMPTY_VALUE && MPI_strong_1[i+1] == EMPTY_VALUE) {MPI_strong_1[i+1] = mpi1;};

         MPI_strong_2[i] = ((MPI_strong_2[i+1] == EMPTY_VALUE && MPI_strong_2[i+2] != EMPTY_VALUE) || MPI_strong_1[i] != EMPTY_VALUE ? EMPTY_VALUE : mpi0);

         if(MPI_strong_2[i] != EMPTY_VALUE && MPI_strong_2[i+1] == EMPTY_VALUE) {MPI_strong_2[i+1] = mpi1;};

        }

      else

        {

         MPI_weak[i] = mpi0;

         MPI_strong_1[i] = EMPTY_VALUE; if(MPI_strong_1[i+1] != EMPTY_VALUE && MPI_strong_1[i+2] == EMPTY_VALUE) {MPI_strong_1[i+1] = EMPTY_VALUE;};

         MPI_strong_2[i] = EMPTY_VALUE; if(MPI_strong_2[i+1] != EMPTY_VALUE && MPI_strong_2[i+2] == EMPTY_VALUE) {MPI_strong_2[i+2] = EMPTY_VALUE;};

        };

      if(MPI_weak[i+1] == EMPTY_VALUE) {MPI_weak[i+1] = mpi1;};

     };

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

   return(rates_total);

  }

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

double Value(int i)

  {

  //--- 2) KG8A;O5< 7=0G5=8O 8=48:0B>@0 :>=:@5B=>9 B09<A5@88 -------

   double up = 0, down = 0, sum = 0;

   int limit = i + period;

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

     {

      up += (High[a] > High[a+1] ? High[a] - High[a+1] : 1 / MathPow(10,_Digits));

      down += (Low[a] < Low[a+1] ? Low[a+1] - Low[a] : 1 / MathPow(10,_Digits));

      sum += (High[a] - Low[a] > 0 ? High[a] - Low[a] : 2 / MathPow(10,_Digits));

     };

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

   return((up + down) / sum);

  }

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---