VolatilityAverageSingleHighLimit

Author: Scriptong
Indicators Used
Indicator of the average true range
Miscellaneous
It issuies visual alerts to the screenImplements a curve of type %1
0 Views
0 Downloads
0 Favorites
VolatilityAverageSingleHighLimit
ÿþ#property copyright "Scriptong"

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

#property description "Displays the average, increased average and single volatility in one subwindow."



#property strict



#property indicator_separate_window

#property indicator_buffers 3

#property indicator_color1 clrDodgerBlue

#property indicator_color2 clrRed

#property indicator_color3 clrGoldenrod



#property indicator_width1 1

#property indicator_width2 1



// 0AB@>5G=K5 ?0@0<5B@K 8=48:0B>@0

input uint    i_volatilityAveragePeriod  = 24;   // Volatility average period

input double  i_highLimit                = 2.5;  // Average volatility ratio

input int     i_indBarsCount             = 5000; // Number of bars to display



double g_volatilityAverage[];

double g_volatilitySingle[];

double g_highVolatility[];



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

//| Custom indicator initialization function                                                               |

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

int OnInit()

{

   if (!IsTuningParametersCorrect())                                                               

      return (INIT_FAILED);                                 

      

   if (!BuffersBind())

      return (INIT_FAILED);                                                   

      

   return(INIT_SUCCEEDED);

}

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

//| @>25@:0 :>@@5:B=>AB8 =0AB@>5G=KE ?0@0<5B@>2                                                           |

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

bool IsTuningParametersCorrect()

{

   string name = WindowExpertName();



   if (i_volatilityAveragePeriod == 0)

   {

      Alert(name, ":  volatility average period must be greater than zero. Indicator is turned off.");

      return (false);

   }

   

   return (true);

}

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

//| Custom indicator deinitialization function                                                             |

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

void OnDeinit(const int reason)

{

}

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

//| !2O7K20=85 1CD5@>2 8=48:0B>@0 A <0AA820<8                                                              |

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

bool BuffersBind()

{

   string name = WindowExpertName();



   // !2O7K20=85 1CD5@>2 8=48:0B>@0 A <0AA820<8

   if (!SetIndexBuffer(0, g_volatilityAverage)              ||

       !SetIndexBuffer(1, g_volatilitySingle

      )                 ||

       !SetIndexBuffer(2, g_highVolatility))

   {

      Alert(name, ": buffers binding error N", GetLastError());

      return (false);

   }



   for (int i = 0; i < 3; i++)   

      SetIndexStyle(i, DRAW_LINE);

      

   SetIndexLabel(0, "Volatility Average");

   SetIndexLabel(1, "Volatility Single");

   SetIndexLabel(2, "High Limit");

   

   return (true);

}

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

//| =8F80;870F8O 1CD5@>2 8=48:0B>@0                                                                       |

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

void BuffersInitialize()

{

   ArrayInitialize(g_volatilityAverage, EMPTY_VALUE);

   ArrayInitialize(g_volatilitySingle

  , EMPTY_VALUE);

   ArrayInitialize(g_highVolatility, EMPTY_VALUE);

}

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

//| ?@545;5=85 8=45:A0 10@0, A :>B>@>3> =5>1E>48<> ?@>872>48BL ?5@5@0AG5B                                 |

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

int GetRecalcIndex(int& total, const int ratesTotal, const int prevCalculated)

{

   total = ratesTotal - 4;                                                                         

                                                   

   if (i_indBarsCount > 0 && i_indBarsCount < total)

      total = MathMin(i_indBarsCount, total);                      

                                                   

   if (prevCalculated < ratesTotal - 1)                     

   {                                               

      BuffersInitialize();

      return (total);

   }

   

   return (MathMin(ratesTotal - prevCalculated, total));                            

}

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

//| 1@01>B:0 40==KE >4=>3> 10@0                                                                           |

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

void ProcessOneBar(int barIndex)

{

   g_volatilitySingle

  [barIndex] = High[barIndex] - Low[barIndex];

   g_volatilityAverage[barIndex] = iATR(NULL, 0, i_volatilityAveragePeriod, barIndex);

   g_highVolatility[barIndex] = g_volatilityAverage[barIndex] * i_highLimit;

}

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

//| B>1@065=85 40==KE 8=48:0B>@0                                                                          |

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

void ShowIndicatorData(int limit, int total)

{

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

      ProcessOneBar(i);

}

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

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

{

   int total;   

   int limit = GetRecalcIndex(total, rates_total, prev_calculated);                                



   ShowIndicatorData(limit, total);                                                                

   

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