How "plot" a variable on chart, like plot in pinescript ?

 
Hello,

I have a problem, I can't understand and especially to make a line that will follow the high (or low) on the graph.

I looked at several forums, articles, etc. but I can't do it, so I'm going to create this topic to get some help.

Before I coded in pinescript and as the mql5 is completely different I am lost 😂

I know how to make a horizontal line, but it is not what I would like, I would like a line that is on the graph and visible in the past.

Basically the same operation as a plot in pinescript on tradingview.

If someone could explain me how to do it, it would be very nice
 

Hello , consult this example that plots one line 

The comments will guide you to the process 

To start , go to File>>New>>Custom Indicator>>move through all the steps and once your code appears , select all , delete it and replace with this 

#property version   "1.00"
#property indicator_separate_window //<<<<<WHERE IT DRAWS 
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Plot
#property indicator_label1  "Plot"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrWhite
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

//--- indicator buffers
double         Plot[];

int OnInit()
  {
//--- indicator buffers mapping
   /*
   This instructs the program to handle the size of the array Plot[] automatically according to how many bars 
   exist on the chart (new or old)  
   */
   SetIndexBuffer(0,Plot,INDICATOR_DATA);
//---
  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[])
  {
//---
  /*
  There are several ways to access data in mt5 
  Imagine for a second T is the total number of bars on the chart 
          in arrays you can access elements from [0] till [T-1]
  So for indicators the constant arrays provided above (in the function) are mapped :
          with [0]   being the oldest bar in time (the leftmost) and
          with [T-1] being the newest bar in time (or the live bar if the market is open)
          example : high[0] is the first bar's high
  But there are also the iSeries arrays to access data and are mapped : 
          with [T-1] being the oldest bar in time 
          with [0]   being the newest bar in time 
          example : iHigh(_Symbol,_Period,0)
  For this example let's use the arrays above 
  
  Now , in indicators you have the value rates_total which is self explanatory , it is the "T" value from above
        and 
        the prev_calculated value which tells you how many bars have been processed in the previous call 
  This is the reason we use the arrays above for the first example , we can conveniently know where to start 
       our processing since the prev_calculated can also be used to point to a position in these arrays since
       they are mapped from left[0] to right[T-1] (right[rates_total-1])
  So what do we need ? we need to run calculations from the last calculated bar -1 but we cannot go below zero in 
       the arrays index . 
       So our formula is :       
  */
  int from=(int)MathMax(0,prev_calculated-1);//so that we don't start from -1 
  /*
  what are we doing above ?
  the first time the OnCalculate runs it will have 0 as prev_calculated (prev_calculated = previously_calculated btw)
  so if we start from prev_calculated-1 we hit -1 . 
  We choose the biggest number , 0 , -1 => 0 so we will go through all the bars 
  and here is our loop
  */
  for(int i=from;i<rates_total;i++)
  {
  /*
  this will go from the leftmost bar to the rightmost bar , and 
  ,if we have calculated up to rates_total (i.e. rates_total==prev_calculated) we will 
   calculate (or update) the live bar because from is prev_calculated-1 and we loop until rates_total-1 (< instead of <=)
  So , let's do an easy a** calculation the high - low 
  We fill the buffer we are ploting directly in the same position 
  */
  Plot[i]=high[i]-low[i];
  
  }
//--- return value of prev_calculated for next call
  return(rates_total);
  }
//+------------------------------------------------------------------+
 
Lorentzos Roussos #:

Hello , consult this example that plots one line 

The comments will guide you to the process 

To start , go to File>>New>>Custom Indicator>>move through all the steps and once your code appears , select all , delete it and replace with this 

Hi,

Thank you very much for taking the time to answer me.

I tried with the script you gave me, the problem is that my script (on which I want to draw the highs and lows) is an EA.
So I tried to make it work with what you show me, but without success.

Basically, I have this ( to keep it simple ) and I would like to just display a line that will follow the highs, simply.

double high;

  
void OnTick()


{
   
   high  = iHigh(Symbol(),Period(),0);
   
}
 
GREED #:
Hi,

Thank you very much for taking the time to answer me.

I tried with the script you gave me, the problem is that my script (on which I want to draw the highs and lows) is an EA.
So I tried to make it work with what you show me, but without success.

Basically, I have this ( to keep it simple ) and I would like to just display a line that will follow the highs, simply.

You cannot plot the line as it evolves on the EA or Script unfortunately , but , if you want a line that moves (a horizontal line) that can be done anywhere .

//declare your variable 
double high=0;
//you will create an object , the object has a name so you can use a tag for your "system" objects 
//it seems too much but its not 
string system_tag="GREED_";//note this is your variable not a mandate of the platform , you are 
//just doing it to handle your objects when the program starts and when the program finishes (your program)
int OnInit()
  {
//---
  //so it starts what do you do you need to create the line 
    high=iHigh(_Symbol,_Period,0);
    ObjectCreate(ChartID(),system_tag+"_HIGH",OBJ_HLINE,0,0,high);
    //now the line exists which means you won't need to be creating it all the time but 
    //to just update its price 
    //also , when you are finished with adding or editing objects on the chart you call 
    ChartRedraw();
    //next onTick
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  //now on tick you need to update the line 
    high=iHigh(_Symbol,_Period,0);
    //so update the object (same name with what you created) with the new high
    ObjectSetDouble(ChartID(),system_tag+"_HIGH",OBJPROP_PRICE,high);
    ChartRedraw();
  }  
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  //your program exits , delete all your objects 
    ObjectsDeleteAll(ChartID(),system_tag);//delete all objects that start with this ("GREED_")
  }
 
Lorentzos Roussos #:

You cannot plot the line as it evolves on the EA or Script unfortunately , but , if you want a line that moves (a horizontal line) that can be done anywhere .

Okay, I understand much better now!

Thanks for the explanations and the script with the comments, it's really good!
 
GREED #:
Okay, I understand much better now!

Thanks for the explanations and the script with the comments, it's really good!

Anytime . Welcome to mql5.com  😇