Coding trouble for swing indicator

 

Hi everyone,

 

I have attempted to code a little swing indicator, where the indicator draws an arrow on the bar where the highest high occurred over the last X bars and an arrow on the bar where the lowest low occured for X bars. But something must be wrong in my code, while the indicator loads in MT4, and the individual buffers show results - but not actual price, rather absolute numbers (See attached image) which seems to be a problem as well as I want to compare prices in between the highlighted bars, in addition no actual arrows are drawn :S I would be grateful it if someone could have a look and let me know where I have gone wrong. 

 

I am developing this indicator to function as a supportive indicator to confirm direction and strength of direction as part of an EA I want to build :)

Below the code and attached the mq4 file. Once again, thank you soo much to anyone who can offer some thought!

#property copyright "Copyright 2013, Philipp Koerge"
#property link      "Unavailable"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1  Gold
#property indicator_color2  DodgerBlue

//input parameters
extern int lookback = 5;
extern int N =5;
//Buffers
double Highbuffer[];
double Lowbuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexDrawBegin(0, N);
   SetIndexDrawBegin(1, N);
   SetIndexArrow(0,159);
   SetIndexArrow(1,159);
//---- indicator buffers mapping
   SetIndexBuffer(0,Highbuffer);
   SetIndexBuffer(1,Lowbuffer);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("Swingpoints");
   SetIndexLabel(0,"Swing_High");
   SetIndexLabel(1,"Swing_Low");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
void start() 
  {
   int counted = IndicatorCounted();
//----
   if(counted < 0) 
       return (-1);
//----  
   if(counted > 0) 
       counted--;
   int limit = Bars - counted;
//----  
   for(int i = 0; i < limit; i++) 
     {
       Highbuffer[i] = iHighest(NULL,0,MODE_HIGH,lookback,i);
       
       Lowbuffer[i] = iLowest(NULL,0,MODE_LOW,lookback,i);
     }
  }
//+------------------------------------------------------------------+
Files:
 
philipp469: draws an arrow on the bar where the highest high occurred over the last X bars
Highbuffer[i] = iHighest(NULL,0,MODE_HIGH,lookback,i);
  1. RTFM. iHighest returns the INDEX of the high, and you put it (as a price) into your buffer.
    Try

    int iHH = iHighest(NULL,0,MODE_HIGH,lookback,i);
    Highbuffer[iHH] = High[iHH]; // + offset

  2. if(counted > 0) counted--;
    Unnecessary Contradictory information on IndicatorCounted() - MQL4 forum Get in the habit of counting down.
    for(int i = Bars - 1 - counted; i >= 0; i--)
 

Hi WHRoeder,

this has been very helpful - thank you. Of course, in the future I ll pay more attention to the manual.

 I have changed Setindexstyle to arrow, however, it draws me an arrow on every bar on on the chart- i would like to change it to solely draw an arrow  on the bar where the high[ihighest] or low[lowest] occured. Anyone any suggestions? once again, thanks for any help :-)

 

Arrows 

 
did you code HighBuffer[i] =
 
WHRoeder:
did you code HighBuffer[i] =


Yes, as instructed by you. I am adding the code again if it helps! Once again thanks for the help.

 

I only havent adjusted the counted bar error - I want to read on it and get it :)

 

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1  Red
#property indicator_color2  DodgerBlue

//input parameters
extern int lookback = 5;
extern int N =5;
//Buffers
double Highbuffer[];
double Lowbuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexDrawBegin(0, N);
   SetIndexDrawBegin(1, N);
   SetIndexArrow(0,159);
   SetIndexArrow(1,159);
   SetIndexEmptyValue(0,0.0);
   SetIndexEmptyValue(1,0.0);
//---- indicator buffers mapping
   SetIndexBuffer(0,Highbuffer);
   SetIndexBuffer(1,Lowbuffer);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("Swingpoints");
   SetIndexLabel(0,"Swing_High");
   SetIndexLabel(1,"Swing_Low");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
void start() 
  {
   int counted = IndicatorCounted();
//----
   if(counted < 0) 
       return (-1);
//----  
   if(counted > 0) 
       counted--;
   int limit = Bars - counted;
//----  
   for(int i = 0; i < limit; i++) 
     {
       int IHH = iHighest(NULL,0,MODE_HIGH,lookback,i);
       Highbuffer[i] = High[IHH];
       
       int ILL = iLowest(NULL,0,MODE_LOW,lookback,i);
       Lowbuffer[i] = Low[ILL];
     }
  }
//+------------------------------------------------------------------+
 
philipp469:
WHRoeder: did you code HighBuffer[i] =
Yes, as instructed by you.
Reread my post. CLOSELY this time.
 
thank you soo much! Worked :) Have a gr8 weekend!