PoiseFractal

Author: Copyright 2020, MetaQuotes Software Corp.
Indicators Used
Fractals
0 Views
0 Downloads
0 Favorites
PoiseFractal
ÿþ//+------------------------------------------------------------------+

//|                                                 PoiseFractal.mq5 |

//|                        Copyright 2019, MetaQuotes Software Corp. |

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

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

#property copyright "Copyright 2020, 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_SOLID

#property indicator_color1  clrDarkGray,clrDarkBlue,clrMaroon

#property indicator_width1  2

//C'0,0,0'

#property indicator_label1  "PoiseFractal"

#property indicator_applied_price PRICE_TYPICAL

//--- input parameters

input int InpPeriodSmooth = 14;  // Smooth period

input double InpFactor = 1.0;    // Factor

//--- indicator buffers

double    upper[],lower[];

double    ExtPoiseBuffer[],ExtColorsBuffer[];

double    ExtFractalBuffer[],ExtRangeBuffer[];

//--- global variable

double    ExtFactor;

int       ExtPeriodSmooth;

int       ExtFractalHandle = INVALID_HANDLE;

double    Alpha = 1.0;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- check for input value

   if(InpFactor < 1.0)

     {

      ExtFactor = MathSqrt(2.0);

      printf("Incorrect input parameter InpFactor = %f. Indicator will use value %f for calculations.",InpFactor,ExtFactor);

     }

   else

      ExtFactor = InpFactor;



   if(InpPeriodSmooth <= 2)

     {

      ExtPeriodSmooth = 2;

      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(ExtFractalHandle != INVALID_HANDLE)

     {

      if(IndicatorRelease(ExtFractalHandle))

         ExtFractalHandle = INVALID_HANDLE;

      else

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

     }

   if((ExtFractalHandle = iFractals(_Symbol,_Period)) == INVALID_HANDLE)

     {

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

      return(INIT_FAILED);

     }

//--- indicator name

   IndicatorSetString(INDICATOR_SHORTNAME,"PoiseFractal(" + string(ExtFactor) + "," + string(ExtPeriodSmooth) + ")");

//--- indicator buffers mapping

   SetIndexBuffer(0,ExtPoiseBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtColorsBuffer,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(2,ExtFractalBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,ExtRangeBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,upper,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,lower,INDICATOR_CALCULATIONS);

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

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

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtPeriodSmooth);

//--- 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 + 5;

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

     {

      //--- clean up arrays

      ArrayInitialize(ExtPoiseBuffer,EMPTY_VALUE);

      ArrayInitialize(ExtFractalBuffer,0.0);

      ArrayInitialize(ExtRangeBuffer,0.0);

     }

//--- not all data may be calculated

   int calculated = BarsCalculated(ExtFractalHandle);

   if(calculated < rates_total)

     {

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

      return(0);

     }

//--- we can copy not all data

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

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

   int up_size = CopyBuffer(ExtFractalHandle,0,0,to_copy,upper);

   int low_size = CopyBuffer(ExtFractalHandle,1,0,to_copy,lower);

   if(up_size <= 0 || low_size <= 0)

     {

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

      return(0);

     }

//--- calculate position

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

   int pos = prev_calculated - 1;

//--- preliminary calculations

   if(pos < start)

      pos = start;



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

     {

      //--- Preparing data

      double low_sum = 0.0,up_sum = 0.0;

      int a = 0,b = 0;

      for(int j = i; j >= 0 && !IsStopped(); j--)

        {

         if(upper[j] > 0 && upper[j] != EMPTY_VALUE)

           {

            up_sum += upper[j];

            a++;

           }

         if(lower[j] > 0 && lower[j] != EMPTY_VALUE)

           {

            low_sum += lower[j];

            b++;

           }

         if(a + b >= ExtPeriodSmooth && a*b>0)

            break;

        }



      ExtFractalBuffer[i] = (a > 0 && b > 0) ? MathAbs(up_sum / a - low_sum / b) : ExtFractalBuffer[i - 1];

      //--- calculate EMA on ATR array

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

      //--- the main loop of calculations

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