Would you please help me to write a code that makes the first candle visible in a chart (switch to first candle when chart is launched)

 

Hi everyone,

I need an urgent help with writing a few lines of code that makes the first candle visible in a chart, in other words, when the chart is opened, it automatically scrolls the chart to the first candle available in that chart.

I would appreciate your kind help in advance

 
MT5.2014 :

Hi everyone,

I need an urgent help with writing a few lines of code that makes the first candle visible in a chart, in other words, when the chart is opened, it automatically scrolls the chart to the first candle available in that chart.

I would appreciate your kind help in advance

You should use ChartNavigate.
 
MT5.2014:

Hi everyone,

I need an urgent help with writing a few lines of code that makes the first candle visible in a chart, in other words, when the chart is opened, it automatically scrolls the chart to the first candle available in that chart.

I would appreciate your kind help in advance

No need for code.

  1. Disable AutoScroll. (this can be saved in the Default template).
  2. Press 'Home' key.
 
Alain Verleyen:

No need for code.

  1. Disable AutoScroll. (this can be saved in the Default template).
  2. Press 'Home' key.
Thanks for your help, but I want this to happen once automatically with an indicator before that indicator starts its calculations.
 
Karputov Vladimir:
You should use ChartNavigate.

Thanks for your help, I am a beginner so, sorry for stupid questions.

I tried

{ChartNavigate(0,CHART_BEGIN,0);}


But it did not work, I placed it in "int OnInit()", should I place it somewhere else in the indicator? Please help me. I appreciate your kind guidance.


 

 
MT5.2014:

Thanks for your help, I am a beginner so, sorry for stupid questions.

I tried

{ChartNavigate(0,CHART_BEGIN,0);}

But it did not work, I placed it in "int OnInit()", should I place it somewhere else in the indicator? Please help me. I appreciate your kind guidance.

    You want to use:
    • indicator
    • script
    • Advisor?
     
    Karputov Vladimir:
      You want to use:
      • indicator
      • script
      • Advisor?

      Sorry that I did not explain more,

      Actually, it is an indicator. I want to make first candle visible (to load all the history for that symbol before the indicator starts its calculations). I want it to happen automatically every time the indicator starts and it is enough to happen once before all other calculations start, so it does not need to be in a loop.

      Thank you 

       
      MT5.2014 :

      Sorry that I did not explain more,

      Actually, it is an indicator. I want to make first candle visible (to load all the history for that symbol before the indicator starts its calculations). I want it to happen automatically every time the indicator starts and it is enough to happen once before all other calculations start, so it does not need to be in a loop.

      Thank you 

      What is "the first candle"? Picture, please.
       
      Karputov Vladimir:
      What is "the first candle"? Picture, please.
      By first candle, I mean the first available bar in a chart (you cannot go further to the left), please see the picture:
      Files:
       

      This indicator scrolls graph:

      //+------------------------------------------------------------------+
      //|                                                  CHART_BEGIN.mq5 |
      //|                              Copyright © 2015, Vladimir Karputov |
      //|                                           http://wmua.ru/slesar/ |
      //+------------------------------------------------------------------+
      #property copyright "Copyright © 2015, Vladimir Karputov"
      #property link      "http://wmua.ru/slesar/"
      #property version   "1.00"
      #property indicator_chart_window
      #property indicator_plots     0
      
      bool start=false;
      //--- input parameters
      input int      begin_bar=0;
      //+------------------------------------------------------------------+
      //| Custom indicator initialization function                         |
      //+------------------------------------------------------------------+
      int OnInit()
        {
      //--- indicator buffers mapping
      
      //---
         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(!start)
            first_bar();
      //--- return value of prev_calculated for next call
         return(rates_total);
        }
      //+------------------------------------------------------------------+
      //|                                                                  |
      //+------------------------------------------------------------------+
      bool first_bar(void)
        {
      //--- get handle of the current chart
         long handle=ChartID();
         if(handle>0) // if successful, additionally set up the chart
           {
            //--- disable auto scroll
            ChartSetInteger(handle,CHART_AUTOSCROLL,false);
            //--- set a shift from the right chart border
            ChartSetInteger(handle,CHART_SHIFT,true);
            //--- draw candlesticks
            ChartSetInteger(handle,CHART_MODE,CHART_CANDLES);
            //--- set the display mode for tick volumes
            ChartSetInteger(handle,CHART_SHOW_VOLUMES,CHART_VOLUME_TICK);
            //--- scroll 0 bars to the right of the history start
            ChartNavigate(handle,CHART_BEGIN,begin_bar);
           }
         return(true);
        }
      //+------------------------------------------------------------------+
      
       
      Karputov Vladimir:

      This indicator scrolls graph:

      Thank you so much for your help, it worked.