Need help for building an Expert Advisor

 

Hello there!

Indicator:


#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Purple
#property indicator_width1 2
#property indicator_color2 Purple
#property indicator_width2 2
//---- indicator parameters
extern int ExtDepth=100;
extern int ExtDeviation=75;
extern int ExtBackstep=15;
//---- indicator buffers
double ExtMapBuffer[];
double ExtMapBuffer2[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(2);
//---- drawing settings
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0, 233);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1, 234);
//---- indicator buffers mapping
   SetIndexBuffer(0,ExtMapBuffer);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexEmptyValue(0,0.0);
   
//---- indicator short name
   IndicatorShortName("ZigZag("+ExtDepth+","+ExtDeviation+","+ExtBackstep+")");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int    shift, back,lasthighpos,lastlowpos;
   double val,res;
   double curlow,curhigh,lasthigh,lastlow;

   for(shift=Bars-ExtDepth; shift>=0; shift--)
     {
      val=Low[Lowest(NULL,0,MODE_LOW,ExtDepth,shift)];
      if(val==lastlow) val=0.0;
      else 
        { 
         lastlow=val; 
         if((Low[shift]-val)>(ExtDeviation*Point)) val=0.0;
         else
           {
            for(back=1; back<=ExtBackstep; back++)
              {
               res=ExtMapBuffer[shift+back];
               if((res!=0)&&(res>val)) ExtMapBuffer[shift+back]=0.0; 
              }
           }
        } 
      ExtMapBuffer[shift]=val;
      //--- high
      val=High[Highest(NULL,0,MODE_HIGH,ExtDepth,shift)];
      if(val==lasthigh) val=0.0;
      else 
        {
         lasthigh=val;
         if((val-High[shift])>(ExtDeviation*Point)) val=0.0;
         else
           {
            for(back=1; back<=ExtBackstep; back++)
              {
               res=ExtMapBuffer2[shift+back];
               if((res!=0)&&(res<val)) ExtMapBuffer2[shift+back]=0.0; 
              } 
           }
        }
      ExtMapBuffer2[shift]=val;
     }

   // final cutting 
   lasthigh=-1; lasthighpos=-1;
   lastlow=-1;  lastlowpos=-1;

   for(shift=Bars-ExtDepth; shift>=0; shift--)
     {
      curlow=ExtMapBuffer[shift];
      curhigh=ExtMapBuffer2[shift];
      if((curlow==0)&&(curhigh==0)) continue;
      //---
      if(curhigh!=0)
        {
         if(lasthigh>0) 
           {
            if(lasthigh<curhigh) ExtMapBuffer2[lasthighpos]=0;
            else ExtMapBuffer2[shift]=0;
           }
         //---
         if(lasthigh<curhigh || lasthigh<0)
           {
            lasthigh=curhigh;
            lasthighpos=shift;
           }
         lastlow=-1;
        }
      //----
      if(curlow!=0)
        {
         if(lastlow>0)
           {
            if(lastlow>curlow) ExtMapBuffer[lastlowpos]=0;
            else ExtMapBuffer[shift]=0;
           }
         //---
         if((curlow<lastlow)||(lastlow<0))
           {
            lastlow=curlow;
            lastlowpos=shift;
           } 
         lasthigh=-1;
        }
     }
  
   for(shift=Bars-1; shift>=0; shift--)
     {
      if(shift>=Bars-ExtDepth) ExtMapBuffer[shift]=0.0;
      else
        {
         res=ExtMapBuffer2[shift];
         if(res!=0.0) ExtMapBuffer2[shift]=res;
        }
     }
  return(0);}
  
  //end//


In the end, this indicator draws arrows at the maximums and minimums on the chart of every candle, but displays only some of them. Not all. I tried to convert it into an expert advisor, that opens and closes trades everytime an arrow appears on the chart. But all I got until now, is that it opens and closes trades immediatly with every tick. I am glad with every comment you´ll write. Thanks.

 

Search first:
Bare in mind there is practically nothing that has not been programmed for MQ4/5 yet! So search first!
Its easier and the result has fff: far fewer faults than a do-it-yourself-EA generally speaking!
For your approach have a look into the ZigZag-Indicator or a daily-High-Low and amend them according to your ideas.

Just because of the 4 buffers for op, hi, lo, cl there wont be any candle painted in the second window. You have to use graphical objects. Even here search for "draw candles" and you would find e.g. this: https://www.mql5.com/en/code/12270.

Again search first instead of trying to invent the wheel again there are tons of wheels here ;)

Indicator Candles
Indicator Candles
  • www.mql5.com
Vertical Time Lines Possible uses: Mark session open/close, mark time of regular news release, any other relevant time. OHLC Range Draws two labels: Open-Close and High-Low range of the last closed candle left of the current candle.
 

Do you know how to find variables from the data window?

Example: 

Data Window:

Value3   |    45.

How do I find that value and how can I use it for my functions?

 
Jan -Sebastian:

Do you know how to find variables from the data window?

Example: 

Data Window:

Value3   |    45.

How do I find that value and how can I use it for my functions?

use iCustom()

Value 3 will normally be buffer 4

 
So I guess that value 2 is buffer 3?
 
Jan -Sebastian:
So I guess that value 2 is buffer 3?

Yes but very easy to check

Write a small script to get the value for a Closed bar and Print the value, then you can easily check it in the data window.

 

Alright.

As you suggested Mr.Watford I used the iCustom function to display the values (double) of the two buffers(0 and 1). But in the test the values did not change at all. They always stayed at 0,00000000.

Here is the function:

double val=iCustom(NULL,0,"AK_ZigZag ROYAL Pointer",ExtDepth,ExtDeviation,ExtBackstep,0,0);
 double Value21=iCustom(NULL,0,"AK_ZigZag ROYAL Pointer",ExtDepth,ExtDeviation,ExtBackstep,1,0);
 

 
Jan -Sebastian:

Alright.

As you suggested Mr.Watford I used the iCustom function to display the values (double) of the two buffers(0 and 1). But in the test the values did not change at all. They always stayed at 0,00000000.

Here is the function:

double val=iCustom(NULL,0,"AK_ZigZag ROYAL Pointer",ExtDepth,ExtDeviation,ExtBackstep,0,0);
 double Value21=iCustom(NULL,0,"AK_ZigZag ROYAL Pointer",ExtDepth,ExtDeviation,ExtBackstep,1,0);
 

zigzag indicators have many bars with no value. Pick a bar that you know has a value.

Check your experts tab to make sure that the indicator name is correct. As there will be an entry if it cannot locate the indicator.

 

Thanks. Working on it.

 
This time I started the test at a candle, that in the data window had its Low[] value under the name ZigZag(100,75,15). I don´t know what buffer exactly it is. I think thats why the value I´m using from the iCustom function is not the same as in the data window. What do you think?
 
Jan -Sebastian:
This time I started the test at a candle, that in the data window had its Low[] value under the name ZigZag(100,75,15). I don´t know what buffer exactly it is. I think thats why the value I´m using from the iCustom function is not the same as in the data window. What do you think?

Try different buffers, but if the buffer label is "value x", the buffer is normally x-1

Reason: