Need help to plot vertical line on Buy / Sell

Dear Seniors,

Need help to plot vertical line at Buy/Sell.

period = 20;
m = MA( Close, period ); 
Buy = Cross( Close, m ); 
Sell = Cross( m, Close );

Plot( C, "Price", colorDefault, styleCandle );
Plot( Buy, "", colorGreen, style = styleHistogram | styleOwnScale, 0, 1 );
Plot( Sell, "", colorRed, style, 0, 1 );
2 Likes

Don't take the easy way out @RUPEEXN You can easily find here (on this forum) many example codes showing how to plot a vertical line:

https://forum.amibroker.com/search?q=vertical%20line

1 Like

Thank you, its work for me.
Thanks a lot.

Hi fx,

In the code you gave, how would one limit the upper and lower y-values of the vertical line using an array rather than number please?

Upper and lower? Why lower?

Here is for upper (note: for true/false histograms)

period = 20;
m = MA( Close, period ); 
Buy = Cross( Close, m ); 
Sell = Cross( m, Close );

Plot( C, "Price", colorDefault, styleCandle );

threshold = Param( "Threshold", 0.1, 0, 1, 0.01);
Plot( 1, "", -1, styleLeftAxisScale | styleNoDraw );
Plot( Buy * threshold, "", colorGreen, style = styleHistogram | styleLeftAxisScale | styleNoLabel, 0, 1 );
Plot( Sell * threshold, "", colorRed, style, 0, 1 );

test

Here is for up and down. (note: for true/false histograms)

period = 20;
m = MA( Close, period ); 
Buy = Cross( Close, m ); 
Sell = Cross( m, Close );

Plot( C, "Price", colorDefault, styleCandle );

threshold_up = Param( "Threshold upper", 1, 0, 1, 0.01);
threshold_dn = Param( "Threshold lower", -1, -1, 0, 0.01);
Plot( 1, "", -1, style_none = styleLeftAxisScale | styleNoDraw );
Plot( -1, "", -1, style_none );
Plot( Buy * threshold_up, "", colorGreen, style = styleHistogram | styleLeftAxisScale | styleNoLabel, 0, 1 );
Plot( Buy * threshold_dn, "", colorGreen, style, 0, 1 );
Plot( Sell * threshold_up, "", colorRed, style, 0, 1 );
Plot( Sell * threshold_dn, "", colorRed, style, 0, 1 );
5 Likes

That's very neat, thank you.

Can the upper threshold be an array such as 'close' so that's it's different at each bar? Or should I try to use low level gfx for such an effect?

The upper simple example is for True/False histogram as stated in my post.
You said for upper code. Upper code uses Buy/Sell as histogram.
You did not say price or indicators.

If you want to do something like in below picture then it is little bit more code (actually a custom function).
And no, you do not need Gfx for array Plot. You would need Gfx as addition to Plot() only if you want to separate titles and prices drawn on axes being vertically inline with partial pane's plots (as seen in picture below).

843

But it is not worth the effort. Simply use two native AB chart panes if you want to draw histogram separately. I don't understand the over-complication via AFL code. There is zero benefit.

1 Like

Thanks. My aim is to have it on the price chart. When the trendline is broken (yellow circle), I want to plot the blue line from the trendline upwards to a target. Obviously the trendline's value and the target will vary day by day. This requires custom function?

x

You see, this is another case of multiple other cases of providing limited information. Why is it so much difficult to provide full info (example) in the first place?
For that (plotting short vertical line instead of a shape) you may use Gfx. GfxSelectPen, GfxMoveTo, GfxLineto (also take a look at GfxSetCoordsMode).

Yes, but it is hard to know how much info is enough, and how much is too much. I always try to make it as brief as possible but include what's needed.

I have been experimenting with gfx whilst we are talking but again finding that the x and y values cannot be arrays.

You have to iterate visible chart and check for signal (your line cross).

crss = /*your line cross array*/;

bi = Barindex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );

GfxSetCoordsMode(1);
GfxSelectPen(colorBlue, 1, 0 );
for ( i = fvb; i <= lvb; i++ ) {
    if ( crss[i] ) {
		// then vertical line Gfx drawing
    }
}
2 Likes

Thanks. Something is wrong with the bit I added. Not sure what though.

crss = Buy;

bi = Barindex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );

GfxSetCoordsMode(1);
GfxSelectPen(colorBlue, 1, 0 );
trendline = 0; bi = 0;
for ( i = fvb; i <= lvb; i++ ) {
    if ( crss[i] ) {
		 GfxSetPixel( bi, trendline,colorblue );
		   GfxLineTo( bi, trendline+1 ); 
    }
}
1 Like

Why do yo use SetPixel? I did not mention SetPixel anywhere. You wanted to draw vertical line.

"bi" variable is an array so in loop you have to access elements of any array via subscripts. But for x argument you do not need "bi" since "i" is accessing bar element already.

Also I do not understand why you initialize trendline with zero.
Didn't you say it is existing array? So it already exists, right? And if or since it is array it requires subscripts too.

crss = Buy;

bi = Barindex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );

GfxSetCoordsMode(1);
GfxSelectPen(colorBlue, 1, 0 );
for ( i = fvb; i <= lvb; i++ ) {
    if ( crss[i] ) {
		GfxMoveTo(i, trendline[i]);
		GfxLineTo(i, trendline[i]+1); 
    }
}
2 Likes

I'm learning a lot! Thanks, it's doing what I needed.

I get an unfortunate situation now. When each new trendline appears, the blue target line repaints and starts plotting from there instead of the line it should use. I suspect this might be much harder to fix.

x

1 Like

I don't know your code. So in order to make it simple... simply use H array or C array for "y" argument instead of trendline.

//....
GfxMoveTo(i, H[i]);
GfxLineTo(i, H[i]+1); 
//...
2 Likes

That's a good quick fix thanks.

I feel like I need a way to say "when trendine cross, return the value of the trendline as a number, not array". Is such a thing possible?

1 Like