I am looking for printing the bar number right above or below the chart in increasing order from left to right.Currently I have a code that only works for intraday and not only daily chart .I am looking for an universal bar counter..So I am not posting that intraday code here.Also if it can be made in such a way that that only the multiple of 5 in displayed ie 1,5,10 etc..this will help keep chart less clutter plus an option to Toggle On/Off will be super helpful.
@krisnara you can try something like this snippet:
GraphXSpace = 10;
Plot( C, "Price", colorDefault, styleCandle );
SetBarsRequired( sbrAll, sbrAll ); // get ALL the database bars
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
bir = bi >= fvb AND bi <= lvb; // plot text only on visible bars
for( i = 0; i < BarCount; i++ )
{
if( bir[i] AND( i % 5 == 0 ) )
PlotText( "" + i, i, H[i] * 1.01, colorOrange, colorDefault );
}
Indeed it makes no sense to loop over all the bars to process only the visible ones... I wasn't very awake yet when I wrote that snippet!
Anyway:
showBarNumbers = ParamToggle( "Show bars numbers", "No|Yes", 1 );
GraphXSpace = 10;
SetBarsRequired( sbrAll, sbrAll ); // get ALL the database bars
Plot( C, "Price", colorDefault, styleCandle );
if( showBarNumbers )
{
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
for( i = fvb; i <= lvb; i++ )
{
if( i % 5 == 0 )
PlotText( "" + i, i, H[i] * 1.005, colorOrange, colorDefault ); // Increase the H by a small fraction to plot the text a little above it
}
}
I also added the option to Toggle On/Off the numbers.
Thank you for your help on this.May I ask you how to modify the code if I want to put a configurable "Bars Back" parameter.To keep the bar numbers smaller,if I want the code to display only the last 100 bars for example ? I confirm this solution you have provided is working on Daily charts too .I dont have intraday data at present.Much Thanks !
@krisnara I probably don't understand what your goal is (particularly what you expect to see if you use an RT data source).
However, here's one more example, which I hope you can modify on your own to get what you want to achieve:
showBarNumbers = ParamToggle( "Show bars numbers", "No|Yes", 1 );
backBarsCounter = Param( "Plot only x back bars", 0, 0, 1000, 10 ); // leave it to zero to show absolute bar index numbers
GraphXSpace = 10;
SetBarsRequired( sbrAll, sbrAll ); // get ALL the database bars
Plot( C, "Price", colorDefault, styleCandle );
if( showBarNumbers )
{
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
if( backBarsCounter > 0 )
{
// change the first bar to plotText to
// the last bar number will be == backBarsCounter
// NOTE: displayed bar numbers will shift when adding new bars
fvb = BarCount - backBarsCounter;
// in this case we plot text numbers from (1 to backBarsCounter) % 5
for( i = fvb; i <= lvb; i++ )
{
index = i - fvb + 1;
if( index % 5 == 0 )
PlotText( "" + index, i, H[i] * 1.005, colorOrange, colorDefault ); // Increase the H by a small fraction to plot the text a little above it
}
}
else
{
for( i = fvb; i <= lvb; i++ )
{
if( i % 5 == 0 )
PlotText( "" + i, i, H[i] * 1.005, colorOrange, colorDefault ); // Increase the H by a small fraction to plot the text a little above it
}
}
}
Your code shows that you still don't quite understand AFL at all.
Why would use SetbarsRequired for simple text plot??? It's beyond me. It's simply not efficient AFL coding. Period. An then even using sbrAll on future bars in addition???
Instead of using ChatGPT non sense I suggest using human brain.
Here is better efficient code without SetbarsRequired non sense.
showBarNumbers = ParamToggle( "Show bars numbers", "No|Yes", 1 );
backBarsCounter = Param( "Plot only x back bars", 0, 0, 1000, 10 ); // leave it to zero to show absolute bar index numbers
GraphXSpace = 10;
Plot( C, "Price", colorDefault, styleCandle );
bi = BarIndex();
fdb = Status("quickaflfirstdatabar");
visible = Status("barvisible");
index = Max(0, bi - BarCount + backBarsCounter + 1);
index = IIf(backBarsCounter > 0, index, fdb+bi);
cond = index % 5 == 0 AND index > 0 AND visible;
if( showBarNumbers ) {
for( i = 0; i < BarCount; i++ ) {
if( cond[i] ) {
PlotText( "" + index[i], i, H[i], colorOrange, colorDefault, 20 );
}
}
}
Thanks to both of you for your great support on this. The text size is coming out as too small. I tried to change it with GFxtextout but I am getting error and also plotText has some limitation with regards to increasing the text size,I am told.How can I increase the bar number output text to double the size ?
since in the help is noted to be "This feature is for internal use only."
"quickaflfirstdatabar", "quickafllastdatabar" - This feature is for internal use only. These are bar indexes of actual underlying compressed quotation array that make up AFL's array[ 0 ] and array[ BarCount - 1]
Using a precalculated condition (like avoiding the % operation for each bar) is definitely the best approach.
Anyway, when backBarsCounter > 0, your code isn't doing exactly what my simple example did. My snippet only counts the last bars (counting backward from BarCount) while your code does it from the last visible bar.
Actually, as I wrote, it wasn't clear to me what the OP wanted.
Now he have both modes to choose from.