How do I make a height array?

 
//+------------------------------------------------------------------+
//|                                                 newIndicator.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2
#property indicator_type1   DRAW_ARROW
#property indicator_type2   DRAW_ARROW
#property indicator_color1  Red
#property indicator_color2  Blue
#property indicator_label1  "Fractal Up"
#property indicator_label2  "Fractal Down"
//---- indicator buffers
double ExtUpperBuffer[];
double ExtLowerBuffer[];
double height[];
int start;
//--- 10 pixels upper from high price
int    ExtArrowShift=-10;

void OnInit()
  {
//---- indicator buffers mapping, buffer means array
   SetIndexBuffer(0,ExtUpperBuffer,INDICATOR_DATA); //holds array called ExtUpperBuffer of fractal ups
   SetIndexBuffer(1,ExtLowerBuffer,INDICATOR_DATA); //first numbers are the array index, this is the array at index 1
   SetIndexBuffer(2, height, INDICATOR_CALCULATIONS);
 
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);  //sets id of indicator, also saves value of indicator
//---- sets first bar from what index will be drawn  
   PlotIndexSetInteger(0,PLOT_ARROW,217);          //sets id of plot arrow, type of data (plot_arrow) and value
   PlotIndexSetInteger(1,PLOT_ARROW,218);
//---- arrow shifts when drawing
   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,ExtArrowShift); //id of plot arrow shift, type of data, array called ExtArrowShift
   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,-ExtArrowShift);
//---- sets drawing line empty value--
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE); //
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//---- initialization done
  }
//+------------------------------------------------------------------+
//|  Accelerator/Decelerator Oscillator                              |
//+------------------------------------------------------------------+
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[])
  {

         ArrayInitialize(ExtUpperBuffer,EMPTY_VALUE);
         ArrayInitialize(ExtLowerBuffer, EMPTY_VALUE);
         //COPY BUFFER: nameOfArray, number in setIndexBuffer, startPosition of copy, amountToCopy, arrayToCopyIn)
         CopyBuffer(ExtUpperBuffer, 0, 0, rates_total, height);

         for(int i=0; i<rates_total; i++) {
            //height[i] = high[i]-low[i];
            
            Print("Value of i is", i , "and value in height[i] is", height[i]);
         }


   return(prev_calculated);
  }

Hopefully people may find my comments useful if starting out.

So I have an array buffer(?) called height of type double. All I want to do is to get the current candle height and compare it to the previous 2.

I originally had it very similar to the Fractals mq5 file but realised very quickly that I did not understand it enough to make even a simple height array.


So the copyBuffer function does not work as it says "no one of the overloads can be applied to the function call". I am assuming this is because the ExtUpperBuffer is of type double?


I know the indicator buffer is useful because it saves previous calculations using prev_calculated and rates_total, to only update new values in the array. That's why I would like to use this.

All I am trying to do is to copy the upperBuffer into this buffer. So then at least it has something!! So if I can do that, surely I can minus each index in the array with a lowerBuffer!!


I don't know how to fix or improve this. I mean, surely there is an easier way?


I am struggling, so any help would be greatly appreciated. Thank you.

 
little.trader :

Hopefully people may find my comments useful if starting out.

So I have an array buffer(?) called height of type double. All I want to do is to get the current candle height and compare it to the previous 2.

I originally had it very similar to the Fractals mq5 file but realised very quickly that I did not understand it enough to make even a simple height array.


So the copyBuffer function does not work as it says "no one of the overloads can be applied to the function call". I am assuming this is because the ExtUpperBuffer is of type double?


I know the indicator buffer is useful because it saves previous calculations using prev_calculated and rates_total, to only update new values in the array. That's why I would like to use this.

All I am trying to do is to copy the upperBuffer into this buffer. So then at least it has something!! So if I can do that, surely I can minus each index in the array with a lowerBuffer!!


I don't know how to fix or improve this. I mean, surely there is an easier way?


I am struggling, so any help would be greatly appreciated. Thank you.

You are working with an INDICATOR. The indicator carries with it ALL ARRAYS:

 //+------------------------------------------------------------------+ 
 //|  Accelerator/Decelerator Oscillator                              | 
 //+------------------------------------------------------------------+ 
 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[])

- You DO NOT NEED to use CopyBuffer.


Now clarify your question: what should the indicator draw? How many lines should the indicator draw?

 
Vladimir Karputov:

You are working with an INDICATOR. The indicator carries with it ALL ARRAYS:

- You DO NOT NEED to use CopyBuffer.


Now clarify your question: what should the indicator draw? How many lines should the indicator draw?

Hi Vladimir, thank you for replying.


I'm sorry about my lack of knowledge. I need a height indicator but there isn't one available.

There is only high and low, so I wanted to make 2 new array with these values.

But how do I save the height values to it?

I want them to draw an up arrow when it satisfies this if statement:


if(high[i-2]<low[i-1] && low[i]<low[i-1 && height[i-2]<=height[i]){

        height[i] = high[i] -  low[i];

}


Thank you very much.


All the best,

little.trader

 
little.trader :

Hi Vladimir, thank you for replying.


I'm sorry about my lack of knowledge. I need a height indicator but there isn't one available.

There is only high and low, so I wanted to make 2 new array with these values.

But how do I save the height values to it?

I want them to draw an up arrow when it satisfies this if statement:



Thank you very much.


All the best,

little.trader

Formulate your condition more precisely. So far I have settled on this option:

      if(low[i-2]<low[i-1] && low[i-1]>low[i]/* && high[i-2]<=high[i]*/)
         MyHighBuffer[i]=high[i];


Full code

//+------------------------------------------------------------------+
//|                                            Custom High Arrow.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.000"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot MyHigh
#property indicator_label1  "MyHigh"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrSpringGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input group             "Arrow"
input uchar                InpCode              = 175;         // Arrow code font Wingdings)
input int                  InpShift             = 10;          // Vertical shift of arrows in pixel
//--- indicator buffers
double   MyHighBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,MyHighBuffer,INDICATOR_DATA);
//--- define the symbol code for drawing in PLOT_ARROW
   PlotIndexSetInteger(0,PLOT_ARROW,InpCode);
//--- set the vertical shift of arrows in pixels
   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-InpShift);
//--- Set as an empty value 0
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
//---
   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[])
  {
   if(rates_total<3)
      return(0);
//---
   int limit=prev_calculated-1;
   if(prev_calculated==0)
     {
      MyHighBuffer[0]=0.0;
      MyHighBuffer[1]=0.0;
      limit=2;
     }
   for(int i=limit; i<rates_total; i++)
     {
      MyHighBuffer[i]=0.0;
      if(low[i-2]<low[i-1] && low[i-1]>low[i]/* && high[i-2]<=high[i]*/)
         MyHighBuffer[i]=high[i];
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


Result:


Files:
 
Vladimir Karputov:

Formulate your condition more precisely. So far I have settled on this option:


Full code


Result:


HI Vladimir,

Thank you so much for sending this to me! I'm going to try learn more about this and see how everything works. Thank you for all the comments also, they are very helpful.

All the best,

little.trader

Reason: