# Horizontal lines

Hallo everyone, Can I drawing Horizontal lines in AmiBroker Like image 2 (right to left) not like Image 1 (Left to right)?

@Tomasz  @ahm.montaser, if by "drawing" you mean using Amibroker's drawing tools, you can get it by using the "Trendline" while pressing the Y key to get a horizontal line.

1 Like

I think you not understand me, I write indicator which present horizontal lines but it appeared from left to right but I need it appeared form right to left, actually I'm not use any drawing feature on the chart ,
there are any other solution to use it in my code script,

@ahm.montaser, in this case, I think this past thread may serve you as a starting point guide.

If, as in your second example image, you want to draw your lines dynamically occupying all the space of future "blank" bars (beyond the last bar in the array) look here for a way to determine their number to be used as the "shift" parameter in case you use the Plot() approach.

Anyway, since the first image seems to be generated with a formula of yours, it would have been more useful to post your code to get suggestions on how it could be modified.

Well @beppe is right (read his links), I was about to post the following, now it's done here it is:

Basically use XShift prameter of Plot. Here's an example:

``````
// Use XShift to shift charts to the left (right too)
XShift = Param("XShift", -100, -400, 400, 10);
Title = "XShift = "+XShift;
PlotOHLC(O,H,L,C,"",colorDefault,styleCandle,Null,Null, XShift);

// Sample Set of Values to display, here floor pivot levels
YH = LastValue(TimeFrameGetPrice("H", inDaily, -1));
YL = LastValue(TimeFrameGetPrice("L", inDaily, -1));
YC = LastValue(TimeFrameGetPrice("C", inDaily, -1));

P = (YC + YH + YL)/3;
R1 = 2*P - YL;
R2 = P + (YH-YL);
S1 = 2*P - YH;
S2 = P - (YH-YL);

// Draw Lines & Labels At the right of Candle Chart
BI = BarIndex(); LBI = LastValue(BI); End = (BI - LBI) > XShift;

procedure DrawLevel(Level, text, color, style) {
global End, LBI;
lv = IIf(End, Level, Null); lvl = LastValue(lv);
Plot(lv, "", color, style);
PlotText(text, LBI+3, lvl-5, color);
}

DrawLevel(P, " P", colorBlack, styleThick);
DrawLevel(S1, "S1", colorBlue, styleLine);
DrawLevel(R1, "R1", colorRed, styleline);
DrawLevel(S2, "S2", colorBlue, styleThick);
DrawLevel(R2, "R2", colorRed, styleThick);

`````` 3 Likes

@alligator, if in the preferences you set as default a certain number of "blank" bars (that you can change dynamically using END key on the keyboard and pressing the HOME key to reset the blank area), you should use a positive value for the "shift" (moving the plotted lines to the right); this has the advantage to have the visible bars aligned to the proper timestamps (on the date axis).
For this reason, I also suggested determining at runtime the number of the "blanks" (instead of using a parameter) so the chart and the line length are updated in-sync if the user (with the above-mentioned keystrokes) enlarges or resets the "blank" area width.
In this case, you should use the "shift" value on the "lines" plot: NOT on the main chart one.

1 Like

oh, thanks you so much

thanks you so much, for your time, efforts and tips

1 Like

Don't use Plot for horizontal lines.
You should use Gfx as posted here.

Also you do not need to find out blank margin.

``````/// @link https://forum.amibroker.com/t/horizontal-lines/31075/9
pvt_h = Ref(H,-1)<H AND Ref(H,1)<H;
pvt_l = Ref(L,-1)>L AND Ref(L,1)>L;

GfxSetCoordsMode(1);
GfxSetBkMode( 1 );
GfxSetTextAlign( 0 | 24 );
font_size = 9;
GfxSelectFont( "Arial", font_size, 500, 0, 0, 0 );

x0 = BarCount-1;
x1 = Status("lastvisiblebarindex")+1;
for ( i = 1; i <= 5; i++ ) {
hx = ValueWhen(pvt_h, H, i);
lx = ValueWhen(pvt_l, L, i);
y0 = y1 = LastVisibleValue(hx);
y2 = y3 = LastVisibleValue(lx);

GfxSelectPen( colorGreen, 1, 0 );
GfxSetTextColor( colorGreen );
GfxMoveTo(x0, y0);
GfxLineTo(x1, y1);
GfxTextOut("" + y1, x1, y1);

GfxSelectPen( colorRed, 1, 0 );
GfxSetTextColor( colorRed );
GfxMoveTo(x0, y2);
GfxLineTo(x1, y3);
GfxTextOut("" + y3, x1, y3);
}

Plot( C, "Price", colorDefault, styleCandle );
`````` 4 Likes

Great remarks @beppe, @fxshrat, updated version showing both solutions:

``````/*
XShift Version 2 according to @beppe & @fxshrat
https://forum.amibroker.com/t/horizontal-lines/31075/5
Use END key to move chart to the left, HOME key to reset

Advantages: easier to use, chart aligned with date axis
*/
Title = "";
// Use XShift to shift charts to the left (right too)
XShift = Param("XShift", 50, -200, 200, 10);
if (XShift >= 0) PlotOHLC(O,H,L,C,"",colorDefault,styleCandle);
else PlotOHLC(O,H,L,C,"",colorDefault,styleCandle,Null,Null, XShift);

// Sample Set of Values to display, here floor pivot levels
YH = LastValue(TimeFrameGetPrice("H", inDaily, -1));
YL = LastValue(TimeFrameGetPrice("L", inDaily, -1));
YC = LastValue(TimeFrameGetPrice("C", inDaily, -1));

P = (YC + YH + YL)/3;
R1 = 2*P - YL;
R2 = P + (YH-YL);
S1 = 2*P - YH;
S2 = P - (YH-YL);

// Compute blanks using beppe's trick
BI = BarIndex(); lbi = LastValue(BI); lvbi = Status("lastvisiblebarindex");
biLvb = LastVisibleValue( BI );
stLvb = Status("lastvisiblebar");
blanks = stLvb - biLvb;
Title = Title + " blanks = " + blanks;
if (XShift >= 0) XShift = Min(XShift, blanks);
if (XShift >= 0) End = (LBI - BI) < XShift; else End = (BI - LBI) > XShift;
Title = Title + " XShift = " + XShift;

solution = ParamToggle("Solution", "Use Plot|Use Gfx");

procedure DrawLevel(Level, text, color, style) {
global End, LBI, XShift, solution;
local lv, lvlv;
lv = IIf(End, Level, Null);

if (solution == 0) {
if (XShift >= 0) Plot(lv, "", color, style, Null, Null, XShift); else Plot(lv, "", color, style);
PlotText(text, LBI+0.5*XShift, LastValue(lv), color);
} else { // fxshrat solution
GfxSetCoordsMode(1); GfxSelectPen(color, 2);
lvlv = LastValue(lv); GfxMoveTo(lbi, lvlv); GfxLineTo(lbi+XShift, lvlv); GfxTextOut(text, lbi+0.5*XShift, lvlv);
}
}

DrawLevel(P, " P", colorBlack, styleThick);
DrawLevel(S1, "S1", colorBlue, styleLine);
DrawLevel(R1, "R1", colorRed, styleline);
DrawLevel(S2, "S2", colorBlue, styleThick);
DrawLevel(R2, "R2", colorRed, styleThick);

``````
3 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.