Author: Alexandr Sokolov
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
RIF
ÿþ#property copyright    "Alexandr Sokolov"

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

#property version      "1.00"

#property description  "Relative Impulse Force"

#property indicator_separate_window

#property indicator_buffers  7

#property indicator_plots    1

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

#property indicator_type1    DRAW_COLOR_HISTOGRAM //"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   "RIF"                //?8A0=85 ;8=88 3@0D8G5A:>3> ?>AB@>5=8O

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

double RIF[],   //;O E@0=5=8O 7=0G89 8=48:0B>@0

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

       MA[],    //;O ?>;CG5=8O 7=0G5=89 8=48:0B>@0 MA

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

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

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

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

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

static int    ma_handle; //;O EM=4;0 8=48:0B>@0 MA

static double atr_up,    //;O ?5@540G8 7=0G5=8O A@54=53> @>AB0

              atr_down;  //;O ?5@540G8 7=0G5=8O A@54=53> ?045=8O

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

input int                 period           = 7,           //Period

                          ma_shift         = 0;           //Shift (MA)

input ENUM_MA_METHOD      ma_method        = MODE_EMA;    //Method (MA)

input ENUM_APPLIED_PRICE  ma_applied_price = PRICE_CLOSE; //Applied price (MA)

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

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 2"); return(INIT_FAILED);};

   if(ma_shift < 0) {Print("Indicator shift must be equal to or higher than 0"); return(INIT_FAILED);};

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

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

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

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

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

   SetIndexBuffer(2,MA,INDICATOR_CALCULATIONS);    SetIndexBuffer(3,Open,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,High,INDICATOR_CALCULATIONS);  SetIndexBuffer(5,Low,INDICATOR_CALCULATIONS);

   SetIndexBuffer(6,Close,INDICATOR_CALCULATIONS);

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

   ArraySetAsSeries(RIF,true);   ArraySetAsSeries(CI,true);

   ArraySetAsSeries(MA,true);    ArraySetAsSeries(Open,true);

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

   ArraySetAsSeries(Close,true);

  //--- >;CG05< EM=4; 8=48:0B>@0 MA --------------------------------

   ma_handle = iMA(_Symbol,PERIOD_CURRENT,period,ma_shift,ma_method,ma_applied_price);

  //--- =8F80;870F8 ?@>945=0 CA?5H=> -------------------------------

   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=>2K5 40==K5 ----------------------------------

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

   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) #AB0=02;8205< ;8<8B 8 :>?8@C5< 7=0G5=8O MA ---------------

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

   copied = CopyBuffer(ma_handle,0,0,limit,MA); if(copied != limit) return(0);

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

     {

     //--- 3) KAG8BK205< 7=0G5=8O ATR_UD

      ATR_UD(i);

     //--- 5) KAG8BK205< 7=0G5=8O 8=48:0B>@0

      if(Close[i] >= MA[i])

        {

         RIF[i] = (Close[i] - MA[i]) / atr_up;

        }

      else

        {

         RIF[i] = ((MA[i] - Close[i]) / atr_down) * (-1);

        };

     //--- 6) #AB0=02;8205< F25B 4;O >B45;L=>9 B09<A5@88

      if(RIF[i] >= 0)

        {

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

        }

      else

        {

         CI[i] = (RIF[i] <= RIF[i+1] ? 0 : 1);

        };

     };

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

   return(rates_total);

  }

//--- ATR Up and Down -----------------------------------------------------------------------------

void ATR_UD(int i)

  {

  //--- 3) KAG8BK205< 7=0G5=8O ATR Up 8 ATR Down

   atr_up = 0; atr_down = 0;

   int limit = i + period;

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

     {

      atr_up += (High[a] - Open[a] != 0 ? High[a] - Open[a] : 1 / MathPow(10,SymbolInfoInteger(_Symbol,SYMBOL_DIGITS)));

      atr_down += (Open[a] - Low[a] != 0 ? Open[a] - Low[a] : 1 / MathPow(10,SymbolInfoInteger(_Symbol,SYMBOL_DIGITS)));

     };

  //--- 4) 5@540Q< 7=0G5=8O ATR Up 8 ATR Down

   atr_up = atr_up / period;

   atr_down = atr_down / period;

  }

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