Plotting multiple lines with 2 buffers

 

Hey all,

I have developed a function to plot prices of interest - however it uses ObjectCreate() which does not visualise nicely when backtesting, so I am in the process of creating it as an indicator. The problem is that the number of prices/levels it returns is variable depending on its parameters, making it problematic to transform into an indicator.

I would want these to plot across the whole screen as a horizontial trendline - such that there are no "breaks" between the prices. 

Suppose my prices of interest are 

[0.65380, 0.66380, etc]

My questions are:

1. Is it possible to create a variable amount of buffers ( and therefore, arrays ) which may hold the price levels I am interested in. 

* E.g. Am I able to declare the number of indicator buffers ( in this case , 3 ) after the someFunction runs?
I assume not, given it needs to be instantiated with #property from what I've seen online.

2. Otherwise, am I able to plot more than one horizontal line ( say 0.65380 and 0.66380 - though it may be extended up to 50-100) using only one or two indicator buffers?

I've attached some dummy code as to what I have attempted - it looks like we need to set up a new indicator buffer for each line I am interested in?

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2020, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+



#property indicator_chart_window
#property indicator_buffers  2
#property indicator_plots  2

#property indicator_type1   DRAW_LINE   // Type of plotting is line
#property indicator_color1  clrRed      // Line color
#property indicator_style1  STYLE_SOLID // Line style

#property indicator_type2   DRAW_LINE   // Type of plotting is line
#property indicator_color2  clrRed      // Line color
#property indicator_style2  STYLE_SOLID // Line style

double buffer1[];
double buffer2[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0, buffer1, INDICATOR_DATA);
   SetIndexBuffer(1, buffer2, INDICATOR_DATA);

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
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[])
  {
  
// someFunction to calculate an array of prices ()
// Suppose I have an ( variable length ) array of prices [0.65380, 0.66380, etc], 
// which may change depending on the bar etc
   
   for(int i = 0 ; i < rates_total ; i++)
     {
      buffer1[i] = 0.65380;
      buffer2[i] = 0.66380;
     }
     
   return(rates_total);
  }
//+------------------------------------------------------------------+

Which results in :

However, how could I extend this to a variable number of prices I calculate?

Let me know of any tips, much appreciated!

Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Drawing Styles
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Drawing Styles
  • www.mql5.com
Drawing Styles - Indicator Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
screneshot.png  15 kb
 
You may find this thread helpful: https://www.mql5.com/en/forum/441048
Continuous R/S zones using buffers
Continuous R/S zones using buffers
  • 2023.02.04
  • www.mql5.com
Hello everyone. When painting horizontal R/S indicator buffers I have to leave an index blank...
 
Yashar Seyyedin #:
You may find this thread helpful: https://www.mql5.com/en/forum/441048

Hi Yashar,


I think what I am struggling with - is how to even paint those R/S zones using buffers:

< Looks like I cannot embed the picture itself, but I have attached it in the above post >

For example - if I want to plot two lines, I can specify two buffers ahead of time. But what if I want to plot N amount of lines which i cannot specify ahead of time?

Can I set a dynamic amount of buffers or am I able to capture the say two price lines using one buffer?

 
sushiix:

Hey all,

I have developed a function to plot prices of interest - however it uses ObjectCreate() which does not visualise nicely when backtesting, so I am in the process of creating it as an indicator. The problem is that the number of prices/levels it returns is variable depending on its parameters, making it problematic to transform into an indicator.

I would want these to plot across the whole screen as a horizontial trendline - such that there are no "breaks" between the prices. 

Suppose my prices of interest are 

My questions are:

1. Is it possible to create a variable amount of buffers ( and therefore, arrays ) which may hold the price levels I am interested in. 

* E.g. Am I able to declare the number of indicator buffers ( in this case , 3 ) after the someFunction runs?
I assume not, given it needs to be instantiated with #property from what I've seen online.

2. Otherwise, am I able to plot more than one horizontal line ( say 0.65380 and 0.66380 - though it may be extended up to 50-100) using only one or two indicator buffers?

I've attached some dummy code as to what I have attempted - it looks like we need to set up a new indicator buffer for each line I am interested in?

Which results in :

However, how could I extend this to a variable number of prices I calculate?

Let me know of any tips, much appreciated!

Yes, you need to assign buffers beforehand, if you intend to use buffers. You can assign as much as 511 buffers, if I remember right.

If you initialize them with "EMPTY_VALUE", they won't show on chart, except for when they have a different value.


 
Dominik Egert #:
Yes, you need to assign buffers beforehand, if you intend to use buffers. You can assign as much as 511 buffers, if I remember right.

If you initialize them with "EMPTY_VALUE", they won't show on chart, except for when they have a different value.


Thanks Dominik - given this then, I would have to intialize 511 ( empty ) arrays, check how many prices/zones I'd like to plot and then fill them in?

Is that the best way around this?

 
sushiix #:

Thanks Dominik - given this then, I would have to intialize 511 ( empty ) arrays, check how many prices/zones I'd like to plot and then fill them in?

Is that the best way around this?

The only way I can think of...