Download MetaTrader 5

Why is this simple indicator not drawing any line ?

To add comments, please log in or register
Suleman Khan
729
Suleman Khan  


Hi I was creating a simple indicator that will show High-Low for each bar but it doesn't do anything when I attach it to the chart. When I start debugging, debugging stops just after I press "Ok" for the indicator settings box.


//+------------------------------------------------------------------+
//|                                                           AD.mq4 |
//|                                                          Suleman |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Suleman"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_minimum 1
#property indicator_maximum 100
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "Label1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#property indicator_level1 10 //Set the first indicator buffer level
#property indicator_level2 65.5 //Second buffer level
//--- indicator buffers
double         Label1Buffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,Label1Buffer);
   IndicatorShortName("Your first indicator is running!");
//---
   return(INIT_SUCCEEDED);
  }
  int OnStart()
  {
  int counted_bars = IndicatorCounted();
  //Check for possible errors
  if (counted_bars<0) return(-1);
  // last counted bar will be recounted
  if (counted_bars>0) counted_bars--;
  int pos= Bars - counted_bars;
  Comment("Value of Bars is" + Bars + " Value of counted bars is: " + counted_bars + " value of Pos is: " + pos);
  double dHigh, dLow, dResult;
  //Comment ("Hi I'm here on the main chart windows");
   //main calculation loop
  while(pos>=0)  
  {
  dHigh = High[pos];
  dLow = Low[pos];
  dResult = dHigh - dLow;
  Label1Buffer[pos] = High[pos];    
  pos--;
  
  }
   
   
  return (0);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
Choose a suitable trading strategy and subscribe to it with a few clicks. All Signals are provided with detailed statistics and informative charts. Become a trading signal provider and sell subscriptions to thousands of traders around the world. With the Signals service, your successful strategy can generate income with a small start-up budget...
whroeder1
16104
whroeder1  
  1. When you post code please use the SRC button! Please edit your post.
               General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. int OnStart(){
    ..
    int OnCalculate(const int rates_total, ... 
    Indicators use OnCalculate, which does nothing. They do not have an OnStart, it is never called.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference

  3.   int counted_bars = IndicatorCounted();
      int pos= Bars - counted_bars;
      while(pos>=0){
      dHigh = High[pos];
    
    The first time counted_bars is zero so pos equals Bars and High[pos] is array exceeded.

  4. Use OnCalculate and stop using IndicatorCounted.
               See How to do your lookbacks correctly.
Suleman Khan
729
Suleman Khan  

Thanks

I edited it and I'll try as you said.

To add comments, please log in or register