Horizontal lines on the last bar and updated with chart

 

hallo everyone,

i want to create indicator which draw horizontal line on the last bar and updated with the chart.

as image(1), not as image(2)

Files:
imagex11.PNG  34 kb
imager2l.PNG  25 kb
 
Ahmed Elsayed: i want to create indicator which draw horizontal line on the last bar and updated with the chart. as image(1), not as image(2)

Then please show us your attempt at coding it and explain in detail the specific issues you wish to get help on.

 
Ahmed Elsayed: i want to create indicator which draw horizontal line on the last bar and updated with the chart.
  1. You don't want an Hline, you want a trend line with one coordinate equal bar zero time. The other max time (D'3000.12.31 23:59:59').
  2. Move it on the next new bar.
  3. You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting).
              No free help (2017)

 
#property indicator_chart_window
#property indicator_buffers 3

#property indicator_color0Gold
#property indicator_label0 "MA(Close) MN"
#property indicator_color1 Magenta
#property indicator_label1 "MA(High) MN"
#property indicator_color2 Orange
#property indicator_label2 "MA(Low) MN"

input string MAPlottingParameters = "--------------------------------------------";

input bool MAOnCloseLine = False;
input int MAClosePeriod = 10;
input ENUM_MA_METHOD MACloseMethod = 0;

input bool MAOnHighLine = False;
input int MAHighPeriod = 10;
input ENUM_MA_METHOD MAHighMethod = 0;

input bool MAOnLowLine = False;


double MACloseLine_MN[];
double MAHighLine_MN[];
double MALowLine_MN[];
input int MALowPeriod = 10;
input ENUM_MA_METHOD MALowMethod = 0;



//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   int counted_bars = IndicatorCounted();
   int limit;
   limit = Bars - counted_bars;
   IndicatorBuffers(197);
   SetIndexBuffer(0, MACloseLine_MN);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexShift(0, Bars-1);
   SetIndexBuffer(1, MAHighLine_MN);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexShift(1, Bars-1);
   SetIndexBuffer(2, MALowLine_MN);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexShift(2, Bars-1);

  return (0);
  }





//+------------------------------------------------------------------+
//| script program start 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[])
  {
   int i;
   int k;
   int limit;
   int j;

   int period_array[39];
   period_array[0] = MAClosePeriod;
   period_array[1] = MAHighPeriod;
   period_array[2] = MALowPeriod;




   int maxValueIdx=ArrayMaximum(period_array,0,WHOLE_ARRAY);
   int lookback = period_array[maxValueIdx];

   if(rates_total < lookback)
      return(0);

   int countedBars = IndicatorCounted();
   if(countedBars < 0)
      countedBars = 0;

   if(countedBars > 0)
      countedBars--;
   limit = rates_total - countedBars;


   for(k =limit; k >= 0; k--)
     {

      MAOnCBuffer[k] = iMA(NULL, 0, MAClosePeriod, 0, MACloseMethod, PRICE_CLOSE, k);
      MAOnHBuffer[k] = iMA(NULL, 0, MAHighPeriod, 0, MAHighMethod, PRICE_HIGH, k);
      MAOnLBuffer[k] = iMA(NULL, 0, MALowPeriod, 0, MALowMethod, PRICE_LOW, k);


      double MAClose_MN;


      if(TimeFrame_MN && MAOnCloseLine)
        {
         MAClose_MN = iMA(NULL, 43200, MAClosePeriod, 0, MACloseMethod, PRICE_CLOSE, 0);
         MACloseLine_MN[k] = MAClose_MN;
        }


      //MA on High in month time frames
      double MAHigh_MN;


      if(TimeFrame_MN && MAOnHighLine)
        {
         MAHigh_MN = iMA(NULL, 43200, MAHighPeriod, 0, MAHighMethod, PRICE_HIGH, 0);
         MAHighLine_MN[k] = MAHigh_MN;
        }



      //MA on High in month  time frames
      double MALow_MN;


      if(TimeFrame_MN && MAOnLowLine)
        {
         MALow_MN = iMA(NULL, 43200, MALowPeriod, 0, MALowMethod, PRICE_LOW, 0);
         MALowLine_MN[k] = MALow_MN;
        }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
   return (rates_total);
}

}
Fernando Carreiro #:

Then please show us your attempt at coding it and explain in detail the specific issues you wish to get help on.

 
Your code does not create any objects. Show us your attempt (using the CODE button) and state the nature of your difficulty.
 

I assume you are trying to see a higher timeframe OHLC on the native timeframe . 

So first of all - if this is the case - this is redundant , you definately don't need the timeframe to be Monthly if you want to see the monthly latest bar ohlc (because its the monthly chart and you can already see the latest candle)

if(TimeFrame_MN && MAOnHighLine)

In the event you are using the above to run a check (or the code you copied used this to check :D ) whether or not the timeframe the user has chosen to see is the Monthly one then it is redundant as well . Close , but can be done better . As mr William said , you need to be very careful with loading other chart data because you might get data from the past (never from the future unfortunately) or not get data or get -1 or 0 so .

The approach is

  • you are going to have a user input for the timeframe desired  
  • you will check if user selected period_current because they do not understand what your program does 
  • if they have selected period_current you will run checks to assign a timeframe to an active other timeframe variable which is going to be the timeframe or a period int
  • then you will initialize objects , remember these objects and update them accordingly
  • you will also delete the objects when the user changes timeframe or removes the indicator
  • you will also maintain a datetime timestamp which is going to represent the other timeframe open time of the latest bar or 0 (if you have not gotten it yet)

Then create a check other tf function which :  

  • resets errors 
  • checks if the other timeframe has posted a new bar or you have not gotten any data yet (timestamp 0)
  • if there is an error on getting the other timeframe time , you do nothing -mt4 will have some vodka and try to load it at this point-
  • if there is no error and your timestamp is zero , OR , the time is greater than your timestamp you will repeat the same error prone process for that timeframes open ,high ,low ,close .
  • if you still get no errors you will update your horizontal lines

that is it . you call the check function once on init and on calculate

i posted no code in case this is not what you want