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

Hello,
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.

regards.

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). https://www.amibroker.com/guide/afl/plottext.html
  3. Use DateTime() function and conversion to string to output date/time as text.
/// @link http://forum.amibroker.com/t/plottext-to-print-buy-sell-price-and-date-at-the-signals/4433/2
/// signal text output: Signal type, price, date/time
Version(6.20);

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 );
    }
}

03145

17 Likes

@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:

Version(6.15);

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

http://www.amibroker.com/devlog/2016/07/06/amibroker-6-15-0-beta-released/

Regards

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).

22108

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.

regards.

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("BarCount="+BarCount);
_TRACE("BarIndex="+Bi);
_TRACE("First Visible Bar"+fvv);
_TRACE("Last Visible Bar="+lvv);
_TRACE("High="+High[BarCount-1]);

Output in Log Window on running above formula

BarCount

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.

Thanks.

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 http://www.amibroker.com/kb/2008/07/03/quickafl/

4 Likes

Thanks Tomasz. It was very helpful.

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

http://bbb-amibroker.blogspot.com/2015/02/absolute-bar-numbering.html

Thanks.

1 Like

@mark,

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

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