Force Index (two variants of calculation)

Author: 2005-2020, MetaQuotes Software Corp.
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
Force Index (two variants of calculation)
ÿþ//+-----------------------------------------------------------+

//|             Force Index (two variants of calculation).mq4 |

//|            Copyright 2005-2020, MetaQuotes Software Corp. |

//|                                       http://www.mql5.com |

//+-----------------------------------------------------------+

#property copyright   "2005-2020, MetaQuotes Software Corp."

#property link        "http://www.mql5.com"

#property version     "2.0"

#property description "======================================="

#property description "Two variants of indicator calculation:"

#property description "1: \"terminal\" Force Index"

#property description "2: Force Index, as it described by A. Elder, M.D."

#property description "Simplified and optimized code"

#property strict



#include <MovingAveragesTS.mqh>



#property indicator_separate_window

#property indicator_buffers    1

#property indicator_color1     clrDarkTurquoise

//----

#property indicator_level1     0.

#property indicator_levelcolor clrDarkOliveGreen

#property indicator_levelstyle STYLE_DOT

//----

enum  frc_type          {

                          typ_TRM  = 0,  // Terminal's Type

                          typ_ELD  = 1   // Elder's Type

                        };

//----

enum  smoo_set          {

                          set_SMA  = 0,  // SMA

                          set_EMA  = 1,  // EMA

                          set_SMMA = 2,  // SMMA

                          set_LWMA = 3   // LWMA

                        };

//----

enum  app_price         {

                          app_CLS  = 0,  // CLOSE

                          app_OPN  = 1,  // OPEN

                          app_HGH  = 2,  // HIGH

                          app_LOW  = 3,  // LOW

                          app_MED  = 4,  // MEDIAN

                          app_TYP  = 5,  // TYPICAL

                          app_WGT  = 6   // WEIGHTED

                        };

//---- input parameters

input  bool               ShowForceIndex        = true;                       // Display Force Index

input  uint               Accuracy              = 0;                          // Measuring Accuracy

input  string             service_line0         = "==== Force Index Select";  // ==================

input  frc_type           Inp_Force_Type        = typ_TRM;                    // Force Index Type 

input  string             service_line1         = "==== MA Select";           // ==================

input  smoo_set           Inp_Smooth_Set        = set_SMA;                    // Smooth Set

input  uint               Inp_MA_Period         = 13;                         // MA Period

input  app_price          Inp_Applied_Price     = app_CLS;                    // Applied Price

input  string             service_line1a        = "--------";                 // ------------------

input  uint               Inp_Force_Shift       = 1;                          // Force Index Shift

//---- buffers

double  Ext_Force_Buffer[];

double  Int_App_PriceBuffer[];

double  Int_Calc_TypeBuffer[];

double  Int_Smooth_Buffer[];

//----

int     nMaPeriod;

int     nForceShift;

string  sFType,sSmooth,sAPrice,sFShift;

string  sLabel;

//----

int     nDraw_begin1=0;

//+------------------------------------------------------------------+   

//| Custom indicator initialization function                         | 

//+------------------------------------------------------------------+

int OnInit()

{

   string sShort_name;

   //----

   if(Accuracy==0) IndicatorDigits(4); else IndicatorDigits(Accuracy);

   //----

   nMaPeriod  =int(Inp_MA_Period  <1?1:Inp_MA_Period);

   nForceShift=int(Inp_Force_Shift<1?1:Inp_Force_Shift);

   //----

   sFType =StringSubstr(EnumToString(Inp_Force_Type),4)+"-";

   sSmooth=StringSubstr(EnumToString(Inp_Smooth_Set),4)+" (";

   sAPrice=StringSubstr(EnumToString(Inp_Applied_Price),4)+")"; StringToLower(sAPrice);

   //----

   if(nForceShift==1) sFShift=""; else sFShift=" +"+(string)nForceShift;

   //----

   sLabel="FI:"+sFType+sSmooth+(string)nMaPeriod+","+sAPrice+sFShift;

   //---- 3 additional buffers are used for counting

   IndicatorBuffers(4);

   //----

   SetIndexBuffer(1, Int_App_PriceBuffer);

   SetIndexBuffer(2, Int_Calc_TypeBuffer);

   SetIndexBuffer(3, Int_Smooth_Buffer);

   //---- indicator lines

   SetIndexBuffer(0, Ext_Force_Buffer);     

   if(ShowForceIndex) {SetIndexStyle(0,DRAW_LINE); 

                       SetIndexLabel(0,sLabel);}

   else               {SetIndexStyle(0,DRAW_NONE); SetIndexLabel(0,NULL);}

   //---- name for indicator subwindow label

   sShort_name="Force Index (2v)("+sFType+sSmooth+(string)nMaPeriod+","+sAPrice+sFShift+")";

   IndicatorShortName(sShort_name);

   //----

   nDraw_begin1=nMaPeriod;

   SetIndexDrawBegin(0,nDraw_begin1);

//----

   return(INIT_SUCCEEDED);

}

//+------------------------------------------------------------------+ 

//| Custom indicator iteration function                              | 

//+------------------------------------------------------------------+ 

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

{

//----

   if(rates_total<=nDraw_begin1) return(0);

   //----

   int limit=rates_total-prev_calculated;

   //----

   if(limit>1)

     {

      limit=rates_total-nDraw_begin1-1;

      //----

      ArrayInitialize(Ext_Force_Buffer,     0.);

      ArrayInitialize(Int_App_PriceBuffer,  0.);

      ArrayInitialize(Int_Calc_TypeBuffer,  0.);

      ArrayInitialize(Int_Smooth_Buffer,    0.);

     }

   //---- applied price box

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

     {

           if(Inp_Applied_Price==0)  Int_App_PriceBuffer[i] = close[i];

      else if(Inp_Applied_Price==1)  Int_App_PriceBuffer[i] = open[i];

      else if(Inp_Applied_Price==2)  Int_App_PriceBuffer[i] = high[i];

      else if(Inp_Applied_Price==3)  Int_App_PriceBuffer[i] = low[i];

      else if(Inp_Applied_Price==4)  Int_App_PriceBuffer[i] = (high[i]+low[i])/2;

      else if(Inp_Applied_Price==5)  Int_App_PriceBuffer[i] = (high[i]+low[i]+close[i])/3;

      else                           Int_App_PriceBuffer[i] = (high[i]+low[i]+close[i]+close[i])/4; // 6

     }

   //---- force index calculation type box

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

     {

      if(Inp_Force_Type==0) Int_Calc_TypeBuffer[i] = Int_App_PriceBuffer[i];

      else                  Int_Calc_TypeBuffer[i] = (double)tick_volume[i]*(Int_App_PriceBuffer[i]-Int_App_PriceBuffer[i+nForceShift]);  // 1 // default: [i+1]

     }

   //---- smooth type box

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

     { 

           if(Inp_Smooth_Set==0) Int_Smooth_Buffer[i] = SimpleMA        (Int_Calc_TypeBuffer,nMaPeriod,i);

      else if(Inp_Smooth_Set==1) Int_Smooth_Buffer[i] = ExponentialMA   (Int_Calc_TypeBuffer,Int_Smooth_Buffer[i+1],nMaPeriod,i);

      else if(Inp_Smooth_Set==2) Int_Smooth_Buffer[i] = SmoothedMA      (Int_Calc_TypeBuffer,Int_Smooth_Buffer[i+1],nMaPeriod,i);

      else                       Int_Smooth_Buffer[i] = LinearWeightedMA(Int_Calc_TypeBuffer,nMaPeriod,i); // 3

     }

   //---- force index output selection

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

     { 

      if(Inp_Force_Type==0)  Ext_Force_Buffer[i] = (double)tick_volume[i]*(Int_Smooth_Buffer[i]-Int_Smooth_Buffer[i+nForceShift]); // terminal's force index

      else                   Ext_Force_Buffer[i] = Int_Smooth_Buffer[i];    // 1                                                    // elser's force index

     }

//---- 

   return(rates_total);

}

//+------------------------------------------------------------------+

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