English

## Help with fractals, get the highest and the lowest values from the last 6 fractals

 6 aramis123 2015.11.02 19:02 #   ```//+------------------------------------------------------------------+ //|                                                    Testeiond.mq4 | //|                        Copyright 2015, MetaQuotes Software Corp. | //|                                             https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2015, MetaQuotes Software Corp." #property link      "https://www.mql5.com" #property version   "1.00" #property indicator_chart_window #property indicator_buffers 6 #property indicator_color1 Green #property indicator_width1 2 #property indicator_color4 Blue #property indicator_width4 2 #property indicator_color5 Red #property indicator_width5 2 //+------------------------------------------------------------------+ //|                                                                  | //+------------------------------------------------------------------+ double PriceD[10000]; double DateD[10000]; double PriceU[10000]; double DateU[10000]; double x[10000]; double n[10000]; int down=0; int up=0; double L[]; //+------------------------------------------------------------------+ //|                                                                  | //+------------------------------------------------------------------+ //---- input parameters extern int       lines=5;  //The amount of visible fractal lines extern int       MaxFractals=10000; // :) //--- my variables double bufUpPrice[10000];  //price array of Up fractals double bufUpDate[10000];   //date array of Up fractals double bufDownPrice[10000];   //price array of Down fractals double bufDownDate[10000]; //date array of Down fractals int Up1=0; //counter of Up fractals int Down1=0;  //counter of Down fractals //--- //--- double upArrow[10000];//Signal breakup double downArrow[10000];//Signal breakdown //--- //The function calculates the price value of penetration of the fractal line by the simplest //equations of analytic geometry double LevelCalculate(double Price1,double Time1,double Price2,                       double Time2,double NewTime)   {    double level;    if(Time2!=Time1)// Just in case, to avoid zero divide.      {       level=(NewTime-Time1)*(Price2-Price1)/(Time2-Time1)+Price1;      }    else      {       return(Price2);      }    return(level);   } //+------------------------------------------------------------------+ //| Custom indicator initialization function                         | //+------------------------------------------------------------------+ int OnInit()   { //--- indicator buffers mapping SetIndexBuffer(0,L); SetIndexStyle(0,DRAW_ARROW,clrGreen); SetIndexArrow(0,233);    SetIndexBuffer(3,upArrow);    SetIndexStyle(3,DRAW_ARROW,clrBlue);    SetIndexArrow(3,233);    SetIndexBuffer(4,downArrow);    SetIndexStyle(4,DRAW_ARROW,clrRed);    SetIndexArrow(4,234); //---    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[])   { //---    int counted_bars=IndicatorCounted();    int limit=(Bars-counted_bars); //---    double buf=0; //--- // We will rather place arrows at the moment of penetration of fractal lines, // estimate efficiency // The idea was borrowed from Rosh, hopefully he will not be offended by this :)     //The number of the penetrated fractal //Penetration of the fractal line    int FractalUp=0;    int FractalDown=0; //---    double BuyFractalLevel = 0;  //penetration level of the Up fractal line    double SellFractalLevel = 0; //penetration level of the Down fractal line    double buf2=0; // buffer value of fractal being available; if it is 0, there is no fractal at all    for(int i=limit;i>0;i--)      {       //+------------------------------------------------------------------+       //|                                                                  |       //+------------------------------------------------------------------+       //Define the current fractal levels       BuyFractalLevel=LevelCalculate(bufUpPrice[Up1],bufUpDate[Up1],                                      bufUpPrice[Up1-1],bufUpDate[Up1-1],Time[i]);       //Move the second coordinate of the Up fractal line                                     ObjectSet("LineUp"+Up1,OBJPROP_TIME1,Time[i]);       ObjectSet("LineUp"+Up1,OBJPROP_PRICE1,BuyFractalLevel);       SellFractalLevel=LevelCalculate(bufDownPrice[Down1],                                       bufDownDate[Down1],bufDownPrice[Down1-1],                                       bufDownDate[Down1-1],Time[i]);       //Move the second coordinate of the Down fractal line                                     ObjectSet("LineDown"+Down1,OBJPROP_TIME1,Time[i]);       ObjectSet("LineDown"+Down1,OBJPROP_PRICE1,SellFractalLevel);       //Search for a complex penetration       if((Close[i]>BuyFractalLevel) && (Up1>FractalUp))         {          FractalUp=Up1;          upArrow[i]=Close[i];         }       if((Close[i]FractalDown))         {          FractalDown=Down1;          downArrow[i]=Close[i];         }       //If it is available, place it in the array of fractals       buf2=iFractals(NULL,0,MODE_UPPER,i);       if(buf2!=0)         {          Up1++;          bufUpPrice[Up1]=iFractals(NULL,0,MODE_UPPER,i);          bufUpDate[Up1]=Time[i];          //The current fractal penetration level - fractal itself          BuyFractalLevel=bufUpPrice[Up1];          if(Up1>1)            {             //Simple fractal             //Draw fractal lines on 2 coordinates             ObjectCreate("LineUp"+Up1,OBJ_TREND,0,bufUpDate[Up1],                          bufUpPrice[Up1],bufUpDate[Up1-1],bufUpPrice[Up1-1]);             ObjectSet("LineUp"+Up1,OBJPROP_COLOR,Blue);             ObjectSet("LineUp"+Up1,OBJPROP_RAY,False);             //Remove the outdated lines             if(Up1>lines+1)               {                ObjectDelete("LineUp"+(Up1-lines));               }            }         }       //A similar block, but for Down fractals       buf2=iFractals(NULL,0,MODE_LOWER,i);       if(buf2!=0)         {          Down1++;          bufDownPrice[Down1]=iFractals(NULL,0,MODE_LOWER,i);          bufDownDate[Down1]=Time[i];          SellFractalLevel=bufDownPrice[Down1];          if(Down1>1)            {             ObjectCreate("LineDown"+Down1,OBJ_TREND,0,                          bufDownDate[Down1],bufDownPrice[Down1],                          bufDownDate[Down1-1],bufDownPrice[Down1-1]);             ObjectSet("LineDown"+Down1,OBJPROP_COLOR,Red);             ObjectSet("LineDown"+Down1,OBJPROP_RAY,False);             if(Down1>lines+1)               {                ObjectDelete("LineDown"+(Down1-lines));               }            }         }       //+------------------------------------------------------------------+       //|              Max and min last fractal       //+------------------------------------------------------------------+       buf=iFractals(NULL,0,MODE_LOWER,i);       if(buf!=0)         {          down++;          PriceD[down]=iFractals(NULL,0,MODE_LOWER,i);          DateD[down]=Time[i];          x[i]=MathMin(MathMin(MathMin(MathMin(MathMin(PriceD[down],PriceD[down-1]),PriceD[down-2]),PriceD[down-3]),PriceD[down-4]),PriceD[down-5]);          Print(x[i]);          if(upArrow[i]!=0)          {                Print(GetLastError());          }         }       buf=iFractals(NULL,0,MODE_UPPER,i);       if(buf!=0)         {          up++;          PriceU[up]=iFractals(NULL,0,MODE_UPPER,i);          DateU[up]=Time[i];          n[i]=MathMax(MathMax(MathMax(MathMax(MathMax(PriceU[up],PriceU[up-1]),PriceU[up-2]),PriceU[up-3]),PriceU[up-4]),PriceU[up-5]);                  }       //+------------------------------------------------------------------+       //|                                                                  |       //+------------------------------------------------------------------+        } //--- return value of prev_calculated for next call    return(rates_total);   } //+------------------------------------------------------------------+ ```