Plot a line backwards

I want to plot a line between Cond1 = true and Cond2 = true. But only when I know cond2 is true, I mean, I need to plot the line backwards.

The following code works fine for the last ocurrence (my best attempt):

//CHART
Plot(C,"C",colorBlack, styleBar);
Plot(MA(C,20),"MA",colorRed,styleLine);

//CONDITIONS
cond1 = Cross( C, MA(C,20) );
cond2 = Cross( MA(C,20), C);

//I WANT TO PLOT A LINE THAT GOES FROM COND1 = TRUE TO COND2 = TRUE
//BUT ONLY IF AND WHEN I KNOW COND2 IS TRUE
bi = BarIndex();
Cond2Bar = SelectedValue( ValueWhen( Cond2, bi) ); 
Cond1Bar = SelectedValue( ValueWhen( Cond2, ValueWhen( Cond1, bi) ) );
Line = SelectedValue(ValueWhen( Cond1, H) );
Plot( LineArray( Cond1Bar, line, Cond2Bar, line ), "Start",colorBlue,styleThick);

But when I try to plot all ocurrences I cannot make it work. If I use a normal plot then I need to shift it and have a problem with the number of bars to shift, which is an array and the function requieres a scalar. If I use LineArray() then it gives me only 1 ocurrence, then I place it in a loop and a error message of being called more than 500 times appears.

I have tried different options from the Knowledge Base with no success: http://www.amibroker.com/kb/2015/01/14/drawing-line-extensions-on-future-bars-using-afl/
z651

And of course I have searched the forum, but didn't find something that I can use or know how to apply properly.

Thank you very much!

@Armin_Tamzarian, if you want to go with the looping code using LineArray(), to avoid the warning, you should use the "improved" method explained in this article (2nd example).
You can also achieve the same result using the low-level graphics functions avoiding the plot function (search this forum for "LineArray" to see some examples of alternative solutions).

By the way, in general, there is no need to "draw" all the lines, but many times is sufficient to display only the ones that have at least one point in the chart visible range.
Again a search for "FirstVisibleValue" will find many examples where the extra elements (lines, text, etc.) are limited to the range of visible bars.

1 Like

Thank you very much Beppe. As usual your answer is 100% accurate. I share the fixed AFL in case it can help others

//CHART
Plot(C,"C",colorBlack, styleBar);
Plot(MA(C,20),"MA",colorRed,styleLine);

//CONDITIONS
cond1 = Cross( C, MA(C,20) );
cond2 = Cross( MA(C,20), C);

//DEFINITIONS
bi = BarIndex();
Line = ValueWhen( Cond1, H);
Cond2Bar = ValueWhen( Cond2, bi); 
Cond1Bar = ValueWhen( Cond2, ValueWhen( Cond1, bi) );
CombinedLine = Null;

//TRICK FROM http://www.amibroker.org/userkb/2007/04/20/plotting-trade-zigzag-lines/
//https://forum.amibroker.com/t/plot-a-line-backwards/31569/2?u=armin_tamzarian
FirstVisibleBar = Status( "FirstVisibleBar" );
Lastvisiblebar = Status( "LastVisibleBar" );
for ( b = Firstvisiblebar; b <= Lastvisiblebar AND b < BarCount; b++ )
{
    if ( Cond2[b] )
    {
		La = LineArray( Cond1bar[b], line[b], Cond2bar[b], line[b] );
		CombinedLine = IIf( IsNull( la ), CombinedLine, la );        
    }
}
Plot( CombinedLine, "", colorBlue, styleThick );

z652

3 Likes

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