Conditional horizontal lines. Custom values displayed on Y axis area

Want to Plot horizontal line when condition fulfills… say 5 bars.
Correct%20Plot

Problem is when signal frequency within 5 bar … getting continuous line...
Wrong%20Plot

Cond1 = ........;

LRpoint = valuewhen( Cond1 ,High, 1);
Drawpoint = IIF(barssince(BBuy)<=5,LRpoint,null);

Don’t want to use loop ….

Drawpoint = IIF(barssince(Cond1)<=5,LRpoint,null);     //mistake corrected
Cond1 = ........;
LRpoint = valuewhen( Cond1 ,High, 1);
Drawpoint = LRpoint != Ref(LRpoint,-1);
Plot( IIf(Drawpoint,Null,LRpoint),..............);

can do it this way but if consecutive signal then horizontal line not visible.
Instead PlotShapes how to plot line for 5 bars!!!!
wrong1

@beppe / @Milosz / @Tomasz or Anyone

Cond1 = C < ref(C,-1); // could be anything

If condition TRUE ....instead PlotShapes ... Plot Line (fixed length) on that candle….. (like below image)
cond1

@Fossil you need to iterate through all visible bars (but only visible - so it's fast) to draw n different lines. For example like this:

Condition = C < Ref( C, -1 ) * 0.98; // Any condition

fvbi = Status( "firstvisiblebarindex" );
lvbi = Status( "lastvisiblebarindex" );

GfxSetCoordsMode( 1 ); GfxSetOverlayMode( 1 );

for( i = fvbi; i <= Min( lvbi, BarCount - 1 ) ; i++ )
{
    if( Condition[i] )
    {
        RandomColor = ColorHSB( mtRandom() * 255, 255, 255 );
        GfxSelectPen( RandomColor, 2 );
        GfxSelectSolidBrush( RandomColor );
        GfxCircle( i, H[i], -6 );
        GfxMoveTo( i, H[i] );
        GfxLineTo( i + 5, H[i] );
    }
}

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

Lines

I draw a line and a circle in all places meeting the condition (in random colors to visually differentiate the signals and give them a cool, blinking look :wink: ) but you can modify the code to suit your needs.

Regards

10 Likes

@Milosz
Thanks man …. :heart_eyes:
One small question :sweat_smile: … if put the whole code under inHourly TimeFrameSet() then how to do TimeFrameExpand() … because without TimeFrameExpand() if try to plot on 5min chart then distorted view.

TimeFrameSet( inHourly);
Condition = C < Ref( C, -1 ) * 0.98; // Any condition
//remaining code ...........
TimeFrameRestore();
//now TimeFrameExpand() how ....
//Plot( C, "C", colorDefault, styleCandle );

@Milosz
Thanks … TimeFrameExpand problem solved.
Instead GFX if use LineArray how to plot each and every Line when condition true …. as when use LineArray it plot Last True condition.

If you need to use LineArray() and want to draw the lines, just replace these two lines:

GfxMoveTo( i, H[i] );
GfxLineTo( i + 5, H[i] );       

with these:

Line = LineArray( i, H[i], i+5, H[i], 0);
Plot( Line, "", RandomColor, styleLine|styleNoLabel, Null, Null, 0, 0, 2 );

The result is the same.


https://www.amibroker.com/guide/afl/linearray.html

You can find further examples of using LineArray() in this article:

http://www.amibroker.com/kb/2015/01/14/drawing-line-extensions-on-future-bars-using-afl/

1 Like

One additional note to my first code with Gfx lines ( link ) When scrolling the chart, the lines may appear on the Y axis area. To avoid that, just replace the line:

GfxLineTo( i + 5, H[i] );

with this one:

GfxLineTo( Min(i + 5, lvbi), H[i] );
3 Likes

@Milosz
If use plot function within loop then warning 502 … & if use LineArray and Plot function outside loop then plot Last value only.
Warning%20502

Plot should not be called inside loop but outside, see this:
http://www.amibroker.org/userkb/2007/04/20/plotting-trade-zigzag-lines/

3 Likes

@Fossil initially you only wanted to know how you can a plot/draw short horizontal lines in places meeting some condition. I offered you a ready to use solution. Then you asked if it is possible to use LineArray() for that. Because it is possible, I've shown how you can do that. In this case, there would be probably no other way of using LineArray() than inside a loop because - a quote from the documentation:

Linearray() accepts only numbers therefore generates single line. To produce multiple lines you have to call it many times with different co-ordinates.

But because it is used inside a loop (and in your case it is called hundreds of times) it becomes an inefficient solution and that's why you get this warning. So it is not a good solution. On a side note, you can read what is the definition of a warning --> Warning, Error, Notice.

What is most important, up until now - you haven't written what is your goal and what are you actually trying to accomplish? I have no idea and I don't want to play guessing games...

I can only guess that using LineArray() in your case makes no sense, because:

  • If you just want to plot those lines you absolutely don't need LineArray() . The proof is in my first reply.

  • If you want to check if those lines or levels have been approached, reached or crossed you also don't need LineArray(). There are much simpler and much more effective ways to do that.

3 Likes

@Milosz

Sorry for the trouble... GFX solution better than LineArray in this example... & it really fulfill my need.
Instead using GFX, I depend too much on LineArray to Plot line from Higher Timeframe.
When someone good as you helping me then why should I lose the opportunity to learn something new that I don’t know.
& Thanks :smiley: again for answering my query.

LineArray() should be used only when absolutely necessary. Definitely not just to plot lines. If it is used just for that, it is a less efficient and slower solution comparing to other ways of plotting/drawing lines. I quote Tomasz reply from this thread :

LineArray serves different purpose. It is provided NOT to draw lines, but to detect crosses of lines against other indicators using combination of LineArray and Cross

3 Likes

@Milosz
we Can plot text using …….

GfxSelectFont("Times New Roman", 16, 700, True ); 
GfxTextOut(StrFormat("%g", H[ i ] ), i + 7 , H[i] ); 
//GfxDrawText(StrFormat("%g", H[ i ] ), i + 5, H[i]+5, i + 10, H[i]-5, 32|1|4|16);

GfxTextOut
but how to plot the value on Y-axis when using GFX……
Y%20axis%20Plot

--1 more question--
I am using Tick database
Chart base interval 3min… so from Higher Timeframe (divisible by 3) … plot with ease.
but can we plot 5min HT on 3min chart …. (above example condition logic modified)... because instead doing the calculation using 5min TimeFrame it using 6min TF and plot on 3min chart.

@Fossil I couldn't reply earlier because I've been busy today (and unfortuantelly the day is not over yet :crazy_face: ... ) but modyfing the code was really simple. Take into account, that It was made on the go and for this reason it's not the most sophisticated solution, but rather one of the simplest :wink:

I would rather plot those values directly on the chart, but if you want them to be visible on the Y-axis area it requires adding only 3-4 additional lines of code:

Condition = C < Ref( C, -1 ) * 0.98; // Any condition

fvbi = Status( "firstvisiblebarindex" );
lvbi = Status( "lastvisiblebarindex" );
pxchr = Status( "pxchartright" );
LVBI = LastVisibleValue( BarIndex() );

GfxSetZOrder(-1);
GfxSelectFont( "Arial narrow", 9, 700, False );

for( i = fvbi; i <= Min( lvbi, BarCount - 1 ) ; i++ )
{
    if( Condition[i] )
    {
        RandomColor = ColorHSB( mtRandom() * 255, 255, 255 );
        Spot = H[i];
        GfxSetCoordsMode( 1 );
        GfxSelectPen( RandomColor, 2 );
        GfxSelectSolidBrush( RandomColor );
        GfxCircle( i, Spot, -6 );
        GfxMoveTo( i, Spot );
        GfxLineTo( Min( i + 5, lvbi ), Spot );

        GfxSetCoordsMode( 2 );
        GfxSetTextColor( RandomColor );      
        GfxTextOut( "" + Spot, pxchr + 4, Spot );
    }
}

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

Scale%202

Displaying those values precisely in the Y-axis area requires x coordinates expressed in pixels (not bars) so convertion from bars to pixels would be natural (and would give more visual posssibilities of presenting the outcome) but as I wanted to keep the code simple I used Status("pxchartright") and GfxSetCoordsMode(2) (X coordinate is pixel, Y coordinate is price) instead. So there are two different GfxSetCoordsModes in the code.

Regards

9 Likes

@Milosz
:crazy_face:"Crazy_face" means trying something out of the box coding... I guess.
but truly you dont need to give reason... its very kind of you to take time & help someone whom you don't even know.

@beppe help me learning jscript. You people are doing more than enough to help inexperienced coder like us.

Thank you :smiley: .

1 Like

I am in need to use this code to draw horizontal conditional line.
I copied the code and I see price candles are drawn in the chart, but not horizontal line.

Can you help me please.

Changed the condition, it is working fine.