Why are my custom indicator lines not showing on MT4 chart?

Rahul Shaji Parmeshwar
139
Rahul Shaji Parmeshwar  

Hello,


I believe I am fairly competent in coding expert advisors but coding technical indicators seem to be a whole another challenge. I have attached my code below, if anyone could point me in the right direction I would be grateful. Why isn't my custom indicator showing when I apply it to the symbol chart?


//+------------------------------------------------------------------+
//|                                                        VLCTY.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Velocity
#property indicator_label1  "Velocity"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

extern int IndicatorPeriod = 14;

//--- indicator buffers
double         VelocityBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,VelocityBuffer);
   
//---
   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[])
  {
  
  for(int i = Bars-1; i >= IndicatorPeriod;i++){

  

   double Velocity = (open[5]-close[5]/2);
   double DividedBy = (Velocity*(close[5]/(2)*sin(0.47)));
   
   VelocityBuffer[i]= DividedBy;
   }
//---

   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   
  }
//+------------------------------------------------------------------+
rrocchi
371
rrocchi  
Rahul Shaji Parmeshwar:

Hello,


I believe I am fairly competent in coding expert advisors but coding technical indicators seem to be a whole another challenge. I have attached my code below, if anyone could point me in the right direction I would be grateful. Why isn't my custom indicator showing when I apply it to the symbol chart?


This logic makes no sense:


for(int i = Bars-1; i >= IndicatorPeriod;i++)

Bars is a huge number, like 10000 or even more.. is the total number of bars which displays on the chart window.. 


lets consider bars is 10000. For i =9999, loop the for function, increasing i by 1, until i is greater than 14.


i is being initialized as 9999, which already is greater than 14


Nothing will be done at all. 

Keith Watford
Moderator
31745
Keith Watford  
Rahul Shaji Parmeshwar:


Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

William Roeder
25333
William Roeder  
  1. #property indicator_buffers 1
    #property indicator_plots   1

    MT4 does not have indicator_plots.

  2.  double Velocity = (open[5]-close[5]/2);

    You are drawing a straight line. You have not set which direction (as-series/non-series) you are accessing the arrays.

  3.  for(int i = Bars-1; i >= IndicatorPeriod;i++){

    See How to do your lookbacks correctly #9#14 & #19.

Rahul Shaji Parmeshwar
139
Rahul Shaji Parmeshwar  
William Roeder:
  1. MT4 does not have indicator_plots.

  2. You are drawing a straight line. You have not set which direction (as-series/non-series) you are accessing the arrays.

  3. See How to do your lookbacks correctly #9#14 & #19.

I see, I've looked at the documentation but I cannot understand it, can you clarify how I'd code this please?

Rahul Shaji Parmeshwar
139
Rahul Shaji Parmeshwar  
rrocchi:

This logic makes no sense:


Bars is a huge number, like 10000 or even more.. is the total number of bars which displays on the chart window.. 


lets consider bars is 10000. For i =9999, loop the for function, increasing i by 1, until i is greater than 14.


i is being initialized as 9999, which already is greater than 14


Nothing will be done at all. 

So should I do it as 

for(int i = Open[0]; i >= IndicatorPeriod;i++)
William Roeder
25333
William Roeder  
Rahul Shaji Parmeshwar: So should I do it as 

A price is not an int.