PlotText to print buy/sell price and date at the signals

I've got below code to print buy/sell price at the buy/sell signal. However, I couldn't find the way to get the date for each buy signal and print it on the chart.

Any guides in much appreciated.
I'm using AB 6.16.


for( i = 0; i < BarCount; i++ )
    if( Buy[i] )  PlotText( "Buy\n@" + C[i], i, L[i] - dist[i], colorGreen, colorYellow );
    //if( Buy[i] )  PlotText( "Buy\n@" + ValueWhen(Buy[i],Date()) + "\n" + C[i], i, L[i] - dist[i], colorGreen, colorYellow ); // not working
    if( Sell[i] ) PlotText( "Sell\n@" + C[i], i, H[i] + dist[i], colorRed, colorYellow );
  1. First of all you should not iterate through entire array but just iterate through visible chart area to output some text on chart.
  2. There is not any dist array variable required to set an y offset. PlotText function does have an y-offset argument (last argument of the function).
  3. Use DateTime() function and conversion to string to output date/time as text.
/// @link
/// signal text output: Signal type, price, date/time

fntsize = 8;// font size
dist = 65;// y-offset

bi = Barindex();
fvb = FirstVisiblevalue( bi );
lvb = LastVisiblevalue( bi );
dt = DateTime();
dtformat = "\n%Y-%m-%d\n%H:%M:%S";
bkcolor = -1;// text background color, -1 means default color (transparent)

PlotTextSetFont( "", "ARIAL", fntsize, BarCount-1, 0, -1 );

for ( i = fvb; i <= lvb; i++ ) {
    if( Buy[i] || Sell[i] )	{
		var = "\n@" + C[i] + DateTimeFormat(dtformat, dt[i]);
		if( Buy[i] )  PlotText( "Buy" + var, i, L[i], colorGreen, bkcolor, -dist+3*fntsize );
		if( Sell[i] ) PlotText( "Sell" + var, i, H[i], colorRed, bkcolor, dist );



@fxshrat thanks for the code. Just one small remark. @bursaware wrote that he uses AB 6.16, so to be able to run the code, he can either update to the newer version (which is always recommended), or change one line into:


... because (if there isn't any other reason for using AB 6.20) DateTimeFormat() was implemented in AB 6.15


1 Like

No, betas should not be used but most recent final version after beta
implementation. That's why updating to 6.20 is one and only proper way to
run the code. That's why I used Version(6.20) to force updating AB version.
The question I'm asking why is he using beta if there is final version?
Makes no sense to me. Betas are test versions of development cycles. Final
(release) versions are save versions (AB 6.20.1 is final save release).


1 Like

Thanks @fxshrat and @Milosz for your kind answer.

For the time being 6.16 is the latest beta I can use and the solution works superbly.


Hello -

I am new to AmiBroker and asking this question after going through the available documentation.

I am just curious that if using BarCount in 'for' loop doesn't iterate through entire array, how I am getting the below results with following formula:

//SetBarsRequired( sbrAll, sbrAll );
Plot (C,"Price", colorBlack, styleCandle);

bi = BarIndex();
fvv = FirstVisibleValue(bi);
lvv = LastVisibleValue(bi);

_TRACE("First Visible Bar"+fvv);
_TRACE("Last Visible Bar="+lvv);

Output in Log Window on running above formula


The output in log window suggests that BarCount also uses in-range bars (visible bars + 30 or more bars) similar to number of bars in array returned by BarIndex().

Please help me in understanding and clearing this confusion.


1 Like

I am grateful to who ever did the code for plotting date and time, have been struggling for many new moons.

Once again thanks a ton.

The answer to your dillema is here


Thanks Tomasz. It was very helpful.

Also, if someone is facing similar doubts, I would also advise to read the following:


1 Like


For posting questions on the forum, you are required to get "Verified".

Please search for "Verified" or "Verified Badge" and follow the steps.