Draw Vertical line on specific barindex()

Seniors please help ! I am stuck with something might be silly but can't figure out anything

I want to create an array of number like X = [31,37,42,49,55........]
and want to draw vertical line on bar where barindex()== any one of the number in X array.
please show me a way to do this. Thanks.

You should not assume BarCount and BarIndex(). Especially when QuickAFL is enabled.

Make sure you have enough bars and you are within a valid Bar Range.
Code a more robust if() check than my loosely written one.

To plot vertical lines at specific bars, you can use this approach.

b = BarCount - 1;
if (b > 100) {

x[b - 4] = 1;
x[b - 10] = 1;
x[b - 12] = 1;
x[b - 20] = 1;

Plot( x, "", colorAqua, styleHistogram | styleOwnScale );


This idea is to use a single array x and set the desired index to 1 and let the Plot() function do the rest.

1 Like

thanks for the reply sir.
i think its working from lastbar to fastbar. means i can subtract desired bar and plot line. but i want to plot line from start or any fixed bar say 10th bar of dataset and want to add those 4,10,12,20 and plot that resultant bar

I haven't asked you to copy-paste the code. Just showed you a very quick and simple approach.

How you calculate the Index is up to you, relative from last-bar, first-bar or a specific bar condition.
What you have to understand is that the BarIndex is not visually tied to a specific bar, it can change, for example due to QucikAFL.

Instead, you should identify a condition, say newday, and count relatively from there. Absolute vs relative counting are opposite ends.

In your first post, you just mentioned some random numbers in your Array X, so one can't figure it out for you.

If bar number array is of small size else proper loop………..

bi = BarIndex();
BarNumber = "31,37,42,49,55";
CountNumber = StrCount(BarNumber,",")+1;

for(i=0; i < CountNumber; i++ )
	Bar_I = StrToNum(StrExtract( BarNumber, i));
	BarCheck = bi == Bar_I;
	Plot( BarCheck , "", colorBlue, styleHistogram | styleOwnScale | styleNoLabel, 0, 1, 0, 0, -20 );

@Fossil its not very efficient to call plot function once for each line. That also in a For loop, hence the idea of populating an array first.
Using the loop to populate a single array would be better as in your case.

Gfx in coords mode 2,3 was also in mind but i thought array plot was far simpler.
One wouldjust need to get max and min y once for GfxLineTo() alongwith GfxMoveTo().

Plot() is for plotting arrays. Just for plotting few lines use Gfx.


SetChartOptions( 0, chartShowDates | chartShowArrows );
Plot( C, "Price", colorDefault, styleCandle );

/// @link https://forum.amibroker.com/t/draw-vertical-line-on-specific-barindex/11051/7
startbar = FirstVisibleValue(BarIndex());// your start bar
mat = startbar + MxFromString("[4;10;12;20]");
rownum = MxGetSize(mat, 0);
pxcb = Status( "pxchartbottom" );



GfxSelectPen(colorOrange, 1, 0);	
GfxMoveTo(startbar, 0);
GfxLineTo(startbar, pxcb);

GfxSelectPen( colorRed, 1, 0 );	
for (i = 0; i < rownum; i++ ) {
	GfxMoveTo( mat[i][0], 0 );
	GfxLineTo( mat[i][0], pxcb );
	GfxTextOut( "" + (mat[i][0]-startbar), mat[i][0], pxcb);

Esoteric hocus pocus? Or what is idea behind it?



@travick Thanks
But this much I know :sunglasses: . Look like you haven’t read my post properly :stuck_out_tongue: .
Just remove the plot and use Gfx…….. simple :rofl: .
Anyway @fxshrat already post what @nudip want & what you are saying……….

1 Like

You might also like to have a look at this thread: Checking if value is in a set, it sound similar to what I think you're asking for.

thanks sir thanks a lot.

yes , Esoteric hocus pocus to some extent. basically planning to plot some planetary reference point in chart. and it works perfectly.

Hi Fxshrat,

startbar = FirstVisibleValue(BarIndex());

This code selects first bar on the chart to plot the vertical lines.

Request you to suggest the code change to use the selected bar as the startbar and then plot the vertical lines from that bar.

Thanks in advance.

Hi @fxshrat,

Any specfic direction I should be looking at to update startbar to use the selected bar from which to draw the vertical lines at the intervals specified.


@insaneike, You have given the answer yourself two times already. So why don't you just replace FirstVisibleValue function by SelectedValue function out of list of available functions?

@fxshrat, Thanks for your reply. I was able to draw from select bar.