Download MetaTrader 5
To add comments, please log in or register
Find out how to install MetaTrader on Linux
aramis
20
aramis 2015.11.01 23:03 

Hello guys, please excuse my poor english. Im trying to do an indicator with fractals but i have some problems when i try to get the previous values.

Until now what i did was to connect trend lines between the most recent fractal and the previous fractal, when the trend line break it will plot an arrow. After this i want to get the highest value from the last 6 fractals if i have a trend line break to the down side and the lowest value if i have a break of the trend line to the up side, and this is my problem i try to use arraymaximum and it works with up fractals but i couldnt use arrayminimum to the down fractals, so i used mathmax and mathmin to compare the previous values, but it doesnt return just the values in the past from the arrow, some times it shows values after the arrows. So i though it could be because fractals could change between some bars. Well if someone could help me i will appreciate it alot. Im newbie, trying to code and learn at the same time. Thanks guys, the code will be bellow .

 

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

//|                                                    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]<SellFractalLevel) && (Down1>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);

  }

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


qjol
3243
qjol 2015.11.02 06:44  
where in your code is stored the values of the last 6 fractals ? sorry it's very hard to read your code without using SRC
aramis
20
aramis 2015.11.02 18: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]<SellFractalLevel) && (Down1>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);
  }
//+------------------------------------------------------------------+
aramis
20
aramis 2015.11.02 18:04  
qjol:
where in your code is stored the values of the last 6 fractals ? sorry it's very hard to read your code without using SRC

hello qjol thanks for your reply, so i though i could use the prices stored PriceU[] array for the up fractals, like PriceU[up], is that correct? 

thanks for advising me to use SRC it is my first time here so i dont know how to use the forum properly. 

aramis
20
aramis 2015.11.03 22:43  
problem solved guys, thanks
/
To add comments, please log in or register