Author: Alexandr Sokolov
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 indicator_separate_window

#property indicator_minimum 0

#property indicator_buffers 4

#property indicator_plots   1

//--- !2>9AB20 3@0D8G5A:>3> ?>AB@>5=8O 8=48:0B>@0 -------------------

#property indicator_type1   DRAW_COLOR_LINE //"8? ;8=88 3@0D8G5A:>3> ?>AB@>5=8O

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

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

#property indicator_color1  clrLime, clrRed //&25B0 ;8=88 3@0D8G5A:>3> ?>AB@>5=8O

#property indicator_label1  "MPI"           //A?;K20NI55 >?8A0=85 ;8=88 3@0D8G5A:>3> ?>AB@>5=8O

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

double MPI[],  //;O E@0=5=8O 7=0G5=89 8=48:0B>@0

       CI[],   //;O C:070=8O F25B0 >B45;L=>9 B09<A5@88 8=48:0B>@0

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

       Low[];  //;O ?>;CG5=8O F5= Low

//--- 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 ------------------------------------------------------

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

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

  //--- #:07K205< B8?K 1CD5@>2 --------------------------------------

   SetIndexBuffer(0,MPI,INDICATOR_DATA);          SetIndexBuffer(1,CI,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(2,High,INDICATOR_CALCULATIONS); SetIndexBuffer(3,Low,INDICATOR_CALCULATIONS);

  //--- #:07K205< ?>@O4>: 8=45:A0F88 1CD5@>2 (2AQ A?@020 =0 ;52>) ---

   ArraySetAsSeries(MPI,true);  ArraySetAsSeries(CI,true);

   ArraySetAsSeries(High,true); ArraySetAsSeries(Low,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< =5>1E>48<K5 F5=K 4;O 40;L=59H59 >1@01>B:8 -------

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

   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);

  //--- 2) #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--)

     {

     //--- 3) KG8A;O5< 7=0G5=8O 8=48:0B>@0 4;O :064>9 >B45;L=>9 B09<A5@88

      MPI[i] = Value(i);

     //--- 4) #:07K205< F25B ;8=88 8=48:0B>@0 4;O :064>9 >B45;L=>9 B09<A5@88

      CI[i] = (MPI[i] > MPI[i+1] ? 0 : 1);

     };

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

   return(rates_total);

  }

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

double Value(int i)

  {

  //--- 3) KG8A;O5< 7=0G5=8O 8=48:0B>@0 4;O :>=:@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] : 1 / 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 ---