Need Help for Custom Indicator With Variables

 

Hello everyone

I work on a custom indicator and begin learning coding following tutorials and articles, after several months some limitations persist and I thought getting your opinion could help me.

Please excuse any possible beginner's mistakes, the basic idea of the indicator may seem simple, but it's the one I'm having the most trouble with in the code.

Any comments will certainly be very useful for my learning! 

Thank you for your help



The indicator (and the way I went about transcribing it) :


It consists of 2 histograms and a line 


The basic idea is to create 2 variables "Hist1a" and "Hist2a" equal either to 1 or 0


Hist1a=1 if low < previous low and =0 otherwise.

Hist1a values are accumulated in "Hist1b" as long as they are >0.

Hist1b" values are then displayed as a histogram under 2 conditions (so that the graph is not overloaded): 

* they are >= 5 

* Hist1b[n+1] = 0


The 2nd histogram displayed follows the same process, this time with :

Hist2a=1 if vol<previous vol and =0 otherwise.


The EMAs of Hist1b and Hist2b are calculated and their difference "EMAs" (EMA2-EMA1) is plotted.




day  Hist1a Hist1b
 1 1 1
 2 0 0
 3 1 1
 4 1 2
 5 0 0
 6 1 1
 7 1 2
 8 1 3
 9 1 4
 10 1 5     Hist1b>=5
 11 0 0 Hist1b[n+1]=0



//+------------------------------------------------------------------+
//|                                                        Idctr.mq5 |
//|                             Copyright 2000-2023, MetaQuotes Ltd. |
//+------------------------------------------------------------------+
#property copyright "Copyright 2000-2023, MetaQuotes Ltd."
#property description "Idctr"
#property strict

//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 8
#property indicator_plots 3
#property indicator_type1 DRAW_LINE
#property indicator_type2 DRAW_HISTOGRAM
#property indicator_type3 DRAW_HISTOGRAM
#property indicator_color1 clrLime
#property indicator_color2 clrMoccasin
#property indicator_color3 clrPeru
#property indicator_width1 2
#property indicator_width2 4
#property indicator_width3 4
#property indicator_label1 "-EMAs"

//+------------------------------------------------------------------+
//|                                                            Idctr |
//+------------------------------------------------------------------+
input uchar InpEMA1=20;
input uchar InpEMA2=20;
input double InpEMAs;
input int InpHist1b;
input int InpHist2b;

//--- indicator buffers
double EMAsBuffer[];
double EMA1Buffer[];
double EMA2Buffer[];
int Hist1aBuffer[];
int Hist2aBuffer[];
int Hist1bBuffer[];
int Hist2bBuffer[];

void OnInit()
{

SetIndexBuffer(0,EMAsBuffer,INDICATOR_DATA);
SetIndexBuffer(2,EMA1Buffer,INDICATOR_CALCULATIONS);
SetIndexBuffer(3,EMA2Buffer,INDICATOR_CALCULATIONS);


IndicatorSetInteger(INDICATOR_DIGITS,0);


PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpEMAs);
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpHist1b);
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpHist2b);

string short_name=StringFormat("Indctr(%d,%d)",InpEMA1,InpEMA2);
IndicatorSetString(INDICATOR_SHORTNAME,short_name);
}

int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double &low[],
const long &volume[]);

int first, bar;
int Hist1a;
int Hist1b;

{
if(rates_total < IntHist1b - 1)
return(0);
if(rates_total < IntHist2b - 1)
return(0);

if(prev_calculated == 0)
first = IntHist1b - 1 + begin;
else
first = prev_calculated - 1;

if(prev_calculated == 0)
first = IntHist2b - 1 + begin;
else
first = prev_calculated - 1;

if(low<previouslow; Hist1a=1; 0)
if(currentVolume<previousvol; Hist2a=1; 0)

for(bar = first; bar < rates_total; bar++)

as(Hist1a=1 && Hist1a[n-1]=1)
Hist1b==Hist1a+Hist1a[n-1];
else
Hist1b==0

Hist1bBuffer[bar] = Hist1b;

as(Hist2a=1 && Hist2a[n-1]=1)
Hist2b==Hist2a+Hist2a[n-1];
else
Hist2b==0

Hist2bBuffer[bar] = Hist2b;

EMA1(int rates_total,int prev_calculated,0,double InpEMA1,int Hist1bBuffer,double EMA1Buffer);

EMA2(int rates_total,int prev_calculated,0,double InpEMA2,int Hist2bBuffer,double EMA2Buffer);

EMAs==EMA2-EMA1;

return(rates_total);
}
Files:
sw_b_x6.png  48 kb
 

The first thing that catches my eye is that the semicolon is clearly unnecessary here:

User11111:
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double &low[],
const long &volume[]);

Trying to declare variables between the header and the function definition is also a bad idea:

(Check out the documentation: https://www.mql5.com/en/docs/basis/function)

User11111:

int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double &low[],
const long &volume[]);

int first, bar;
int Hist1a;
int Hist1b;

{

And your OnCalculate function header does not match any of its 2 possible types:

https://www.mql5.com/en/docs/event_handlers/oncalculate

 
Vladislav Boyko #:

The first thing that catches my eye is that the semicolon is clearly unnecessary here:

Trying to declare variables between the header and the function definition is also a bad idea:

(Check out the documentation: https://www.mql5.com/en/docs/basis/function)

And your OnCalculate function header does not match any of its 2 possible types:

https://www.mql5.com/en/docs/event_handlers/oncalculate

Thank you Vladislav Boyko for your answer
 
To be honest it looks like some ChatGPT code with a few personal "tweaks".
There, I said it.