Horizontal Line

I am at present using the following code to plot arrow at the Buy / Sell trigger in chart

shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes( shape, IIf( Buy, colorGreen, colorRed ), 0, IIf( Buy, Low, High ) );

I want to draw a horizontal line at the trigger candle :- Green Line for Buy Trigger at the Open Price and Red Line for Sell Trigger at the Open Price. This line should be extended up to trading close time

How to code this in afl?

@SHANTVCI try this:

// sample Buy/Sell signals - replace with your code
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
Plot( C, "Close", colorDefault, styleCandle );
PlotShapes( shape, IIf( Buy, colorBrightGreen, colorOrange ), 0, IIf( Buy, Low, High ) );

// the following drawing lines code is adapted from a post found in the AmiBroker Community forum:
// https://forum.amibroker.com/t/help-with-iif-function-in-a-loop-horizontal-lines-with-labels-how-to-check-if-the-code-is-efficient/7187/10

// plot lines only for the visible bars
fvb = Status( "firstvisiblebar" );
lvb = Status( "lastvisiblebar" );
// plot lines behind the price plot
GfxSetZOrder( -1 );
start = Max( 0, fvb );
end = Min( lvb, BarCount - 1 );
// set mode = 1 - bar / price mode where X is expressed in bar index and Y is expressed in price
GfxSetCoordsMode( 1 );

for( x = start; x <= end ; x++ )
{
    if( Buy[x] OR Sell[x] )
    {
        if( Buy[x] )
            colorLine = colorGreen;
        else
            colorLine = colorRed;

        // Draw line from the open price 
        y = Open[x]; 
        GfxSelectPen( colorLine, 1, 0 );
        GfxMoveTo( x, y );
        GfxLineTo( lvb , y );
    }
}

Before posting new requests, I recommend you to always search the Forum and spend a good amount of time looking at the previous answers and above all to test the full working code examples!

For instance, searching for "horizontal line" I found this thread where a code sample (courtesy of @Milosz and @awilson) is doing something similar to your goal.

I only adapted the drawing lines part of their code, using the Buy/Sell arrays instead of the array (of mouse clicks points) that was used in that formula.

3 Likes

Thanks for your reply. I also searched the forum and find out the following code which was also useful :-
Condition = Buy OR Sell ; // 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 = o[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 );

In your code How to restrict the Line Till the end of trading session.

@SHANTVCI If you copy and paste someone else's code, you should:

  1. Mention who is the author of the code and (especially if you found it on this forum), provide a link to the original post which usually contains additional information, screenshots etc.:
  1. Use proper code tags. It's been written hundreds of times why it is so important and how to do this. Compare how the same code looks above and in my original post.

Please don't ignore some very basic rules of this forum.

3 Likes

Sorry I apologized that I did not mentioned your name . Your code was fantastic and I found it very helpful Thanks for your advise,

Just wanted to know whether it is possible to draw horizontal line till the end of trading session ?

@SHANTVCI here is a revised version to be used with intraday data (should work with a minimum of 1-second interval but I have not executed with any RT data under 1-minute).

I added some extra lines to handle your request and also appended an exploration to examine the arrays I used to determine what is the "day" session last bar (where the drawing lines will terminate).

// sample Buy/Sell signals - replace with your code
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
Plot( C, "Close", colorDefault, styleCandle );
PlotShapes( shape, IIf( Buy, colorBrightGreen, colorOrange ), 0, IIf( Buy, Low, High ) );

// the following drawing lines code is adapted from a post found in the AmiBroker Community forum:
// https://forum.amibroker.com/t/help-with-iif-function-in-a-loop-horizontal-lines-with-labels-how-to-check-if-the-code-is-efficient/7187/10
// added some logic to draw the lines only till the end of each intraday session

// plot lines only for the visible bars
fvb = Status( "firstvisiblebar" );
lvb = Status( "lastvisiblebar" );
// plot lines behind the price plot
GfxSetZOrder( -1 );

// go back some bars in order to paint lines in the day session also when the buy/sell bars are not yet displayed (hidden at the left)
numBarsInADay = IIf( Interval() >= in1Second, 86400 / Interval(), 0 ); // 24h * 60m * 60s = 86400s
start = Max( 0, fvb - numBarsInADay );
end = Min( lvb, BarCount - 1 );

// added logic to draw lines "per session"
bi = BarIndex();
dn = DateNum();
nd = ( dn != Ref( dn, -1 ) ); // Detect a new day session
vwndbi = ValueWhen( nd, bi ); // Bar index at new day session
vwnextndbi = ValueWhen( nd, bi, 0 ); // Bar index for the next day session (looking to future bars)
lastValidSessionBar = LastValue( Highest( vwnextndbi ) );
firsValidSessionBar = LastValue( Lowest( vwndbi ) );
// Show days separation - Remove/comment out if using Axis/Grid Vertical Gridlines preferences
Plot( nd, "", colorBlue, styleNoTitle | styleNoLabel | styleHistogram | styleOwnScale, 0, 1, 0, -2 ); 

// set mode = 1 - bar / price mode where X is expressed in bar index and Y is expressed in price
GfxSetCoordsMode( 1 );

for( x = start; x <= end ; x++ )
{

    if( Buy[x] OR Sell[x] )
    {
    
		// Handle first and last (could be incomplete) sessions
		daySessionLastBar = IIf( x >= lastValidSessionBar, lvb, Max( vwnextndbi[x] - 1, 0 ) );
		daySessionLastBar = IIf( x < firsValidSessionBar, Max( firsValidSessionBar - 1, 0 ), daySessionLastBar );
    
        if( Buy[x] )
            colorLine = colorGreen;
        else
            colorLine = colorRed;

        // Draw line from the open price
        y = Open[x];
        GfxSelectPen( colorLine, 1, 0 );
        GfxMoveTo( x, y );
        GfxLineTo( daySessionLastBar, y );
    }
}

// Exploration to better understand the used arrays
function nil( a )
{
    return ( IIf( a, 1, Null ) );
}
Filter = 1;
AddColumn( Nil( Buy ), "Buy", 1, colorWhite, colorGreen );
AddColumn( Nil( Sell ), "Sell", 1, colorWhite, colorRed );
AddColumn( Nil( nd ), "New Day", 1, colorWhite, colorBlue );
AddColumn( bi, "Bar # (index)", 1 );
AddColumn( vwndbi, "New Day bar #", 1 );
AddColumn( vwnextndbi, "Next Day bar #", 1 );
AddColumn( Lowest( vwndbi ) - 1, "1st closed session bar #" );
AddColumn( Highest( vwnextndbi ) - 1, "Last closed session bar #" );

if( Status( "action" ) == actionExplore )
    SetSortColumns( -2 );

I hope it works as expected. I have not tested it extensively since intraday strategies are not my field of action; in any case, if necessary, I leave you the task to improve it and fix any mistake.

5 Likes

Thanks a lot for AFL Code