help me correct it - page 2

 
mt4 money #:
in the chart window have a line ,how you coding it

I'll make a simple example to show you . 

 

Here you go , consult the photo and the code together . 

#property copyright "Read The Discussion"
#property link      "https://www.mql5.com/en/forum/440950"
#property version   "1.00"
#property strict
#property indicator_chart_window
/*
You need to store data somewhere , but , because you don't know how much data the 
    chart has you can let the indicator handle that.
1 data "container"       = 1 buffer 
1 data "display/line"    = 1 plot 
For this we want one buffer and one plot .
We will store one type of data and we will draw it so:    
*/
#property indicator_buffers 1
#property indicator_plots   1
/*
awesome , now what are we going to do with the plot ? 
          We will draw it , that needs colors and styles 
          So we declare to the indicator what do we want plot 1 
          to be like , like this :
*/
//WE WANT PLOT 1 TO BE A LINE 
#property indicator_type1 DRAW_LINE
//WE WANT IT TO BE PINK
#property indicator_color1 clrPink
//WE WANT IT TO BE 2 PIXELS WIDE (FAT) 
#property indicator_width1 2

/*
now , we need the buffers . 
Buffers are double type 
and are arrays ?
What is arrays ? : https://www.mql5.com/en/docs/array
(In simple words array=list)
But , we don't know how big the list will be (the array) so we declare it like this :
*/
double buffer[];//if we wanted 5 we would buffer[5] , but we don't want 5 because we dont know how much data the chart has
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
  /*
  THE PROGRAM STARTS HERE , THIS (OnInit()) IS THE FIRST THING THAT HAPPENS 
  --------------------------------------------------------------
  So we must tell the indicator to handle our array size (the buffer[]) automatically
  So we do this : 
  */ 
  SetIndexBuffer(0,buffer);
  /*
  What on earth happened above ?
  How many buffers (on the indicator) have we "linked" to arrays so far ? 0 
  Like in Trading view , the first element is [0] not [1]
  So the first buffer we link will be 0 , the 2nd 1 , the 3rd 2 etc
  That is it , now the indicator knows that it will draw buffer[] with the 
               draw settings we set for plot1 on line 23 , 25 , 27
  That is it , now when the program runs for the first time it will call OnCalculate()
               so what we want to happen in the calculation of our "line" we must 
               put in there
               
  */
//---
   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[])
  {
//---
  /*
  The indicator has 2 values for the calculation : 
      A. rates_total
      B. prev_calculated (previously_calculated)
  
  Like in the drawing , if the chart has 5 candles then rates_total will be 5
      and like in the drawing if we want to read the first (oldest) candle 
      we will use the index [4] or [rates_total-1]
      also index [0] will always be the newest bar
  Great , so what is the prev_calculated ?
      It tells us how many candles the indicator has processed.
      But the indicator doesn't process anything , one might say , EXACTLY.
      It essentially counts how many candles YOUR CALCULATIONS processed 
      and it helps you avoid calculating the same thing all the time.
  
  An example 
  Again , like in the drawing the chart has 5 candles only .
     The first time the indicator runs this OnCalculate 
         : rates_total=5 prev_calculated=0
           the rates_total will be 5 and since we have not run this before 
           we have processed 0 of those bars.
           What is that telling us ? that if we want things to be drawn for our line
           in the past we have to go in the past , from the past till the present 
           and do our calculation . We have to do that .
     
     Then when this function runs again it will have 
         : rates_total=5 prev_calculated=5
           Which will indicate to us that we don't need to do
           the entire history calculation again and can just 
           calculate the newest bar [0]
     
     And , if a new candle starts then rates_total will become 5+1 = 6
           and prev_calculated will be 5 which will show us 
           that we have not calculated one candle 
     So for the simplest version of an indicator , without limitations 
     we can estimate where the loop will start based on the numbers 
     given to us rates_total and prev_calculated
     
     The amount of bars we have to scan is easily found 
     by this "formula"
     candles_to_read=rates_total-prev_calculated;
     
     Makes total sense right ? 
     "how many candles are there" "minus" "how many candles we have read already"
     
     Let's do that now 
  */ 
  int candles_to_read=rates_total-prev_calculated;
  int first_candle=candles_to_read;
  /*
  piece of cake but like in Trading view , we remember that 
  the first item available is [0] and the last item available is [total_items-1]
  so , if the number of candles to read is equal to the rates_total we must subtract 1
  so that we can start from the last item and not crash the universe.
  */
  if(candles_to_read==rates_total){first_candle=candles_to_read-1;}
  /*
  Note that what we though of above , for the -1 , you must think and do it alone 
  all the time depending on your calculation . Trading view does this automatically 
  but MQL does not do it automatically .
  What does it mean ? 
  it means that if you wanted to add the close price of 2 candles (every previous and every current)
  then you could not start from candles-1 because it has not previous candle to it.
  You would have to start from candles_to_read - 2 so that your first calculation CAN have 
  the data you need.
  So let's do that , this is what the indicator will be 
  So the above line of code becomes :
  */
  if(candles_to_read==rates_total){first_candle=candles_to_read-2;}
  /*
  Perfect , but , how do i go and calculate in all the candles ? 
  This is how , you tell the indicator , start from 
       candles_to_read and go down until 0 and for EACH 
       one candle that is represented by the index i (there is your i)
       do the calculation. These are the [4][3][2][1][0] on the drawing 
  In the drawing if you wanted to calculate the previous and current candle 
       prices sum then you could start from [3] because [4] has no previous candle.
       kay?sco
            
  */
     //   from first_candle , as long as it is bigger than 0 , and the step is -1
  for(int i=first_candle;i>=0;i--)
     {
     /*
     here we do our calculations 
     Like in Trading View if you want to say "Get me the previous in time" you 
          type +1
     What do we want ? the sum of the prices of the previous candle and this one (each one at i)
     Where do we store it ? buffer[]
     So:
     *///      Previous    Current
     buffer[i]=(Close[i+1]+Close[i])/2.00;
     //done 
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }