ChartNavigate() is driving me crazy...

 

Hey guys,

I am really desperate about the function ChartNavigate().

The following script should scroll to the the start of yesterday's daily candle on the m1 timeframe.

The variable "pos" is absolutely correct. Why doesn't ChartNavigate() work? The chart jumps to random dates instead to the beginning of yesterday.

void OnStart() {
   ChartSetInteger(0,CHART_AUTOSCROLL,false);
   ChartSetInteger(0,CHART_SHIFT,false);
   int pos=iBarShift(_Symbol,PERIOD_M1,iTime(_Symbol,PERIOD_D1,1));
   ChartSetSymbolPeriod(0,_Symbol,PERIOD_M1);
   ChartNavigate(0,CHART_END,-pos);
}
 
Marbo:Hey guys, I am really desperate about the function ChartNavigate(). The following script should scroll to the the start of yesterday's daily candle on the m1 timeframe. The variable "pos" is absolutely correct. Why doesn't ChartNavigate() work? The chart jumps to the month June instead of to the beginning of yesterday.

The function carries out relative shift, not a absolute shift. It shifts "n" bars from the current visual position.

If you need absolute shift, then you will first need to reset the view to the beginning of the chart with a "zero shift", and then shift forward to the desired position.

EDIT: Remember to take into account CHART_VISIBLE_BARS and CHART_WIDTH_IN_BARS.

 
Fernando Carreiro #:

The function carries out relative shift, not a absolute shift. It shifts "n" bars from the current visual position.

If you need absolute shift, then you will first need to reset the view to the beginning of the chart with a "zero shift", and then shift forward to the desired position.

EDIT: Remember to take into account CHART_VISIBLE_BARS and CHART_WIDTH_IN_BARS.

Awesome! Thank you very much for your help!

 
Marbo #:

Awesome! Thank you very much for your help!

Obviously I was a bit too fast... Same problem again. The relative shift is correct but the chart doesn't navigate to the correct candle.

void OnStart() {
   long chartID=ChartOpen(_Symbol,PERIOD_M1);
   ChartSetInteger(chartID,CHART_AUTOSCROLL,false);
   ChartSetInteger(chartID,CHART_SHIFT,false);
   long firstBar=ChartGetInteger(chartID,CHART_FIRST_VISIBLE_BAR);
   int pos=iBarShift(_Symbol,PERIOD_M1,iTime(_Symbol,PERIOD_D1,1));
   pos=pos-firstBar;
   Print(pos);
   ChartNavigate(chartID,CHART_END,-pos);
}

And even though the shift might not be 100% correct, I don't understand why the chart jumps to absolutely random dates. It doesn't make any sense to me.

 
Marbo #: Obviously I was a bit too fast... Same problem again. The relative shift is correct but the chart doesn't navigate to the correct candle. And even though the shift might not be 100% correct, I don't understand why the chart jumps to absolutely random dates. It doesn't make any sense to me.
Fernando Carreiro #:

The function carries out relative shift, not a absolute shift. It shifts "n" bars from the current visual position.

If you need absolute shift, then you will first need to reset the view to the beginning of the chart with a "zero shift", and then shift forward to the desired position.

EDIT: Remember to take into account CHART_VISIBLE_BARS and CHART_WIDTH_IN_BARS.

 
Fernando Carreiro #

I didn't find a solution with CHART_VISIBLE_BARS and CHART_WIDTH_IN_BARS. Therefore I looked at the example from Metaquotes and they worked with CHART_FIRST_VISIBLE_BAR. But for whatever reason it doesn't work with my code.

Can you just tell me what you mean with "zero shift"? That sounds pretty easy if I can use the absolute shift then. Is it ChartNavigate(chartID,CHART_END,0) ?

 
Marbo #:I didn't find a solution with CHART_VISIBLE_BARS and CHART_WIDTH_IN_BARS. Therefore I looked at the example from Metaquotes and they worked with CHART_FIRST_VISIBLE_BAR. But for whatever reason it doesn't work with my code. Can you just tell me what you mean with "zero shift"? That sounds pretty easy if I can use the absolute shift then. Is it ChartNavigate(chartID,CHART_END,0) ?

Exactly! As per the documentation (see highlighted text below) ...

ChartNavigate

Performs shift of the specified chart by the specified number of bars relative to the specified position in the chart.

bool  ChartNavigate(
   long                  chart_id,     // Chart ID
   ENUM_CHART_POSITION   position,     // Position
   int                   shift=0       // Shift value
   );

Parameters

chart_id

[in]  Chart ID. 0 means the current chart.

position

[in]  Chart position to perform a shift. Can be one of the ENUM_CHART_POSITION values.

shift=0

[in]  Number of bars to shift the chart. Positive value means the right shift (to the end of chart), negative value means the left shift (to the beginning of chart). The zero shift can be used to navigate to the beginning or end of chart.

Return Value

Returns true if successful, otherwise returns false.

 
Fernando Carreiro #:

Exactly! As per the documentation (see highlighted text below) ...

Thank you once more. I will try my best... :)

 

Relevant code sample

                                                                          //< 1>
                                                                          //< 2>
double CHART_MAX_PRICE = NULL                                           ; //< 3>
double CHART_MIN_PRICE = NULL                                           ; //< 4>
long   CHART_RANGE     = NULL                                           ; //< 5>
                                                                          //< 6>
long   BARS_VISIBLE    = NULL                                           ; //< 7>
long   BARS_TOTAL      = NULL                                           ; //< 8>
long   BARS_SHIFT      = NULL                                           ; //< 9>
long   BAR_FIRST_LEFT  = NULL                                           ; //<10>
                                                                          //<11>
                                                                          //<12>
                                                                          //<13>
long   CHART_HEIGHT    = NULL                                           ; //<14>
long   CHART_WIDTH     = NULL                                           ; //<15>
                                                                          //<16>
                                                                          //<17>
void   CHART_METRICS_MEASURE ()                                         { //< 1>
                                                                          //< 2>
       CHART_MAX_PRICE = ChartGetDouble ( 0 , CHART_PRICE_MAX         ) ; //< 3>
       CHART_MIN_PRICE = ChartGetDouble ( 0 , CHART_PRICE_MIN         ) ; //< 4>
       CHART_RANGE     = ROUND_POINTS(CHART_MAX_PRICE- CHART_MIN_PRICE) ; //< 5>
                                                                          //< 6>
       BARS_VISIBLE    = ChartGetInteger( 0 , CHART_VISIBLE_BARS      ) ; //< 7>
       BARS_TOTAL      = ChartGetInteger( 0 , CHART_WIDTH_IN_BARS     ) ; //< 8>
       BARS_SHIFT      =                  BARS_TOTAL - BARS_VISIBLE     ; //< 9>
       BAR_FIRST_LEFT  = ChartGetInteger( 0 , CHART_FIRST_VISIBLE_BAR ) ; //<10>
                                                                          //<11>
                                                                          //<12>
                                                                          //<13>
       CHART_HEIGHT    = ChartGetInteger( 0 , CHART_HEIGHT_IN_PIXELS  ) ; //<14>
       CHART_WIDTH     = ChartGetInteger( 0 , CHART_WIDTH_IN_PIXELS   ) ; //<15>
                                                                          //<16>
                                                                        } //<17>
 
long   ROUND_POINTS (        double  D                  )               { //< 1>
                                                                          //< 2>
return              ( (long) round ( D / _Point )       )               ; //< 3>
                                                                          //< 4> ..
                                                                        } //<17>
Reason: