Author: Copyright 2019, MetaQuotes Software Corp.
Indicators Used
Indicator of the average true range
0 Views
0 Downloads
0 Favorites
Poise_2
ÿþ//+------------------------------------------------------------------+

//|                                                        Poise.mq5 |

//|                        Copyright 2019, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

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

#property copyright "Copyright 2019, MetaQuotes Software Corp."

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

#property version   "1.00"

#property indicator_chart_window

#property indicator_buffers 6

#property indicator_plots   1

#property indicator_type1   DRAW_COLOR_LINE//DRAW_COLOR_SECTION

#property indicator_style1  STYLE_DOT

#property indicator_color1  clrDarkGray,clrTurquoise,clrLightPink,clrDodgerBlue,clrViolet//C'0,0,0'

#property indicator_label1  "Poise"

#property indicator_applied_price PRICE_TYPICAL

//--- input parameters

input double InpFactorAtr = 3.3; // ATR factor

input int InpPeriodAtr = 100;     // ATR period

input int InpPeriodSmooth = 9;   // Smooth period

//--- indicator buffers

double    ExtPoiseBuffer[];

double    ExtColorsBuffer[];

double    ExtATRBuffer[];

double    ExtRangeBuffer[];//EmaOfATR

//--- global variable

double    ExtFactorAtr;

int       ExtPeriodAtr;

int       ExtPeriodSmooth;

int       ExtAtrHandle = INVALID_HANDLE;

double    Alpha = 1.0;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- check for input value

   if(InpFactorAtr < 1.0)

     {

      ExtFactorAtr = MathSqrt(10.0);

      printf("Incorrect input parameter InpATRFactor = %d. Indicator will use value %d for calculations.",InpFactorAtr,ExtFactorAtr);

     }

   else

      ExtFactorAtr = InpFactorAtr;

   if(InpPeriodAtr <= 0)

     {

      ExtPeriodAtr = 14;

      printf("Incorrect input parameter InpAtrPeriod = %d. Indicator will use value %d for calculations.",InpPeriodAtr,ExtPeriodAtr);

     }

   else

      ExtPeriodAtr = InpPeriodAtr;

   if(InpPeriodSmooth <= 0)

     {

      ExtPeriodSmooth = 10;

      printf("Incorrect input parameter InpSmoothPeriod = %d. Indicator will use value %d for calculations.",InpPeriodSmooth,ExtPeriodSmooth);

     }

   else

      ExtPeriodSmooth = InpPeriodSmooth;

   Alpha = 2.0 / (ExtPeriodSmooth + 1.0);

//--- create ATR indicator and add it to collection

   ResetLastError();

   if(ExtAtrHandle != INVALID_HANDLE)

     {

      if(IndicatorRelease(ExtAtrHandle))

         ExtAtrHandle = INVALID_HANDLE;

      else

         Print("IndicatorRelease ATR failed. Error ",GetLastError());

     }

   if((ExtAtrHandle = iATR(_Symbol,_Period,ExtPeriodAtr)) == INVALID_HANDLE)

     {

      Print("Error creating ATR indicator:",GetLastError());

      return(INIT_FAILED);

     }

//--- indicator name

   IndicatorSetString(INDICATOR_SHORTNAME,"Poise(" + string(ExtFactorAtr) + "," + string(ExtPeriodAtr) + ")");

//--- indicator buffers mapping

   SetIndexBuffer(0,ExtPoiseBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtColorsBuffer,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(2,ExtATRBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,ExtRangeBuffer,INDICATOR_CALCULATIONS);

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

//--- sets first bar from what index will be drawn

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtPeriodAtr);

//--- sets drawing line empty value

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

//--- set index label

//PlotIndexSetString(0,PLOT_LABEL,"Poise("+string(InpATRFactor)+")");

//--- set number of colors in color buffer

   /*PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,3);

      PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,Red);

      PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,Blue);

      PlotIndexSetInteger(0,PLOT_LINE_COLOR,2,Green);*/

//---

   return(INIT_SUCCEEDED);

  }

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

//| Custom indicator iteration function                              |

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

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const int begin,

                const double &price[])

  {

//---

//--- start calculation

   int start = begin + ExtPeriodAtr;

//--- check for bars count

//--- Check for the minimum number of bars for calculation

   if(rates_total <= start)// not enough bars for calculation

      return(0);

//--- Checking and calculating the number of calculated bars

   if(prev_calculated == 0)//int limit = rates_total - prev_calculated;

     {

      ArrayInitialize(ExtPoiseBuffer,EMPTY_VALUE);

      ArrayInitialize(ExtATRBuffer,0);

      ArrayInitialize(ExtRangeBuffer,0);

     }

//--- not all data may be calculated

   int calculated = BarsCalculated(ExtAtrHandle);

   if(calculated < rates_total)

     {

      Print("Not all data of ExtAtrHandle is calculated (",calculated," bars ). Error ",GetLastError());

      return(0);

     }

//--- Preparing data

//--- we can copy not all data

   int to_copy = (prev_calculated > 0 && prev_calculated <= rates_total) ? (rates_total - prev_calculated + 1) : rates_total; //(prev_calculated>rates_total || prev_calculated<=0) ?rates_total:rates_total-pos

//--- filling out the array of True Range values for each period

   int count;

   if((count = CopyBuffer(ExtAtrHandle,0,0,to_copy,ExtATRBuffer)) <= 0)

     {

      Print("Getting Atr is failed! Error ",GetLastError());

      return(0);

     }

//--- calculate position

//--- set first bar from what calculation will start

   int pos = prev_calculated - 1; //int start_pos=(prev_calculated<rates_total)?prev_calculated:prev_calculated-1;//prev_calculated>0?prev_calculated-1:0

//--- preliminary calculations

   if(pos < start)//prev_calculated<ExtPeriodATR

     {

      ExtRangeBuffer[start - 1] = ExtATRBuffer[start - 1] * ExtFactorAtr;

      pos = start;

     }

//--- the main loop of calculations

   for(int i = pos; i < rates_total && !IsStopped(); i++)//Checking for stop flag

     {

      //--- calculate EMA on ATR array

      ExtRangeBuffer[i] = ExtATRBuffer[i] * Alpha * ExtFactorAtr + ExtRangeBuffer[i - 1] * (1 - Alpha); // ExponentialMAOnBuffer(rates_total,prev_calculated,InpPeriodEMA-1,InpPeriodEMA,Ema,EmaOfEma);

      if(ExtPoiseBuffer[i - 1] == price[i])

         ExtPoiseBuffer[i] = ExtPoiseBuffer[i - 1];

      else

        {

         if(price[i - 1] < ExtPoiseBuffer[i - 1] && price[i] < ExtPoiseBuffer[i - 1])

            ExtPoiseBuffer[i] = MathMin(ExtPoiseBuffer[i - 1],price[i] + ExtRangeBuffer[i]);

         else

           {

            if(price[i - 1] > ExtPoiseBuffer[i - 1] && price[i] > ExtPoiseBuffer[i - 1])

               ExtPoiseBuffer[i] = MathMax(ExtPoiseBuffer[i - 1],price[i] - ExtRangeBuffer[i]);

            else

               ExtPoiseBuffer[i] = price[i] > ExtPoiseBuffer[i - 1] ? price[i] - ExtRangeBuffer[i] : price[i] + ExtRangeBuffer[i];

           }

        }

      //--- now we set line color for every bar

      if(price[i] < ExtPoiseBuffer[i] && ExtPoiseBuffer[i] <= ExtPoiseBuffer[i - 1])

         ExtColorsBuffer[i] = 1.0;

      else

         if(price[i] > ExtPoiseBuffer[i] && ExtPoiseBuffer[i] >= ExtPoiseBuffer[i - 1])

            ExtColorsBuffer[i] = 2.0;

         else

            ExtColorsBuffer[i] = 0.0;

     }

//--- return value of prev_calculated for next call

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