Differential Average by Sultonov

Author: Scriptong by Sultonov's idea, starring Sevan62
Price Data Components
Series array that contains close prices for each bar
Miscellaneous
It issuies visual alerts to the screenImplements a curve of type %1
0 Views
0 Downloads
0 Favorites
Differential Average by Sultonov
ÿþ#property copyright "Scriptong by Sultonov's idea, starring Sevan62"

#property link      "http://advancetools.net"

#property description "English: Sultonovs Differential Indicator."

#property description "Russian: 8DD5@5=F80;L=K9 8=48:0B>@ !C;B>=>20"

#define VERSION "220.917"

#property version VERSION

#property strict



#property indicator_separate_window

#property indicator_buffers 2

#property indicator_color1 clrBlue

#property indicator_color2 clrRed



#property indicator_width1 2

#property indicator_width2 2



// Indicators tunning paramaters

input int i_nPeriod       = 1000;  // Calculate period / 5@8>4 @0AG5B0

input int i_nIndBarsCount = 10000; // The number of bars to display / >;8G5AB2> 10@>2 >B>1@065=8O



bool  g_bIsActivate;



double g_fPoint;                                                                                                   

                                                                                                   

// Indicator buffers

double g_farrBullsPower[];

double g_farrBearsPower[];                                                                                                

    

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

//| Custom indicator initialization function                                             |

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

int OnInit()

{

   Print("Version: ", VERSION);

   g_bIsActivate = false;

 

   if (!TuningParameters())                                                                        

      return INIT_FAILED; 

      

   if (!BuffersBind())

      return INIT_FAILED;      

      

   g_bIsActivate = true;

   return INIT_SUCCEEDED;

}

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

//| Custom indicator deinitialization function                                           |

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

void OnDeinit(const int reason)

{

}

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

//| 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 (!g_bIsActivate)

      return rates_total;

      

   int nTotal;   

   int nLimit = GetRecalcIndex(nTotal, rates_total, prev_calculated);                                



   if (!ShowIndicatorData(nLimit, nTotal))

      g_bIsActivate = false;

      

   return rates_total;

}

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

//| Checking the correctness of tuning parameters                                        |

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

bool TuningParameters()

{

   string name = MQLInfoString(MQL_PROGRAM_NAME);

      

   if (i_nPeriod <= 0)

   {

      Alert(name, ", ", Symbol(), 

            (TerminalInfoString(TERMINAL_LANGUAGE) == "Russian")? 

            ": 25;8G8=0 ?5@8>40 @0AG5B0 4>;6=0 1KBL 1>;LH5 =C;O. =48:0B>@ >B:;NG5=." : 

            ": value of period must be greate than zero. Indicator has turned off.");

      return false;

   }

   

   g_fPoint = Point() / 10;

   if (g_fPoint <= 0.0)

   {

      Alert(name, ", ", Symbol(), 

            (TerminalInfoString(TERMINAL_LANGUAGE) == "Russian")? 

            ": D0B0;L=0O >H81:0 - 25;8G8=0 ?C=:B0 @02=0 =C;N. =48:0B>@ >B:;NG5=." : 

            ": fatal error - the point value equals to zero. Indicator has turned off.");

      return false;

   }

   

   return true;

}

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

//| Indicator buffers binding                                                            |

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

bool BuffersBind()

{

   if (!SetIndexBuffer(0, g_farrBullsPower)          ||

       !SetIndexBuffer(1, g_farrBearsPower))       

   {

      Alert(MQLInfoString(MQL_PROGRAM_NAME), ", ", Symbol(), 

            (TerminalInfoString(TERMINAL_LANGUAGE) == "Russian")? 

            ": >H81:0 A2O7K20=8O <0AA82>2 A 1CD5@0<8 8=48:0B>@0. H81:0 !":

            ": indicator buffers binding error N", GetLastError());

      return false;

   }



   SetIndexStyle(0, DRAW_LINE);

   SetIndexStyle(1, DRAW_LINE);



   SetIndexLabel(0, "Bulls Power");

   SetIndexLabel(1, "Bears Power");

   

   IndicatorSetString(INDICATOR_SHORTNAME, "Differential Average by Sultonov (" + 

                      IntegerToString(i_nPeriod) + ")");

   

   return true;

}

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

//| Determination of bar index which needed to recalculate                               |

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

int GetRecalcIndex(int &nTotal, const int nRatesTotal, const int nPrevCalculated)

{

   nTotal = nRatesTotal - 1 - i_nPeriod;                                                                         

                                                   

   if (i_nIndBarsCount > 0 && i_nIndBarsCount < nTotal)

      nTotal = MathMin(i_nIndBarsCount, nTotal);                      

                                                   

   if (nPrevCalculated < nRatesTotal - 1)                     

   {       

      InitializeBuffers();

      return nTotal;

   }

   

   return MathMin(nRatesTotal - nPrevCalculated, nTotal);                            

}

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

//| Indicators buffers inizialization                                                    |

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

void InitializeBuffers()

{

   ArrayInitialize(g_farrBullsPower, 0.0);

   ArrayInitialize(g_farrBearsPower, 0.0);

}

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

//| Displaying of indicators values                                                      |

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

bool ShowIndicatorData(int nLimit, int nTotal)

{

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

      if (!ProcessBar(i))

         return false;



   return true;

}

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

//| Process the specified bar                                                           |

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

bool ProcessBar(int nBarIndex)

{

   g_farrBearsPower[nBarIndex] = 0.0;

   g_farrBullsPower[nBarIndex] = 0.0;

   

   double fBullsSumm = 0.0, fBearsSumm = 0.0;

   int nBullsCnt = 0, nBearsCnt = 0;

   

   for (int i = nBarIndex + i_nPeriod - 1; i >= nBarIndex; --i)

   {

      double fCloseCur = iClose(NULL, 0, i);

      double fClosePrev = iClose(NULL, 0, i + 1);

      double fPower = fCloseCur - fClosePrev;

      if (fPower > 2 * DBL_EPSILON)

      {

         fBullsSumm += fPower;

         nBullsCnt++;

      }

      if (fPower < -2 * DBL_EPSILON)

      {

         fBearsSumm -= fPower;

         nBearsCnt++;

      }

   }

   

   g_farrBullsPower[nBarIndex] = (nBullsCnt == 0)? 0.0 : 

                                 fBullsSumm / nBullsCnt / g_fPoint;

   g_farrBearsPower[nBarIndex] = (nBearsCnt == 0)? 0.0 : 

                                 fBearsSumm / nBearsCnt / g_fPoint;



   return true;

}

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