I would like to know how to make an indicator that, when the price crosses the Parabolic SAR, draws a line at the price where it crossed and keeps it fixed until it’s crossed again. But I want the line to remain drawn only until the next crossover, so there aren’t a thousand lines on the chart.
You can use or build up from here.
bi = BarIndex();
sar1 = sar( 0.02, 0.2 );
upCross = Cross( C, sar1 );
recentUp= LastValue( ValueWhen( upCross != 0, bi , 1) );
upCross = IIf( bi < recentUp, 0, upCross ); // clear previous crosses
dnCross = Cross( sar1, C );
recentDn= LastValue( ValueWhen( dnCross != 0, bi, 1) );
dnCross = IIf( bi < recentDn, 0, dnCross ); // clear previous crosses
which1 = recentUp > recentDn;
val1 = IIf( which1, C[ recentUp ], C[ recentDn ] ); // C price
Plot( val1, "SarLine", IIf( which1, colorGreen, colorRed ), styleLine );
Plot( sar1, "SAR", colorAqua, styleLine );
PlotShapes( upCross * shapeHollowSmallUpTriangle +
dnCross * shapeHollowSmallDownTriangle, colorWhite,0 ,
IIf( upCross, L, H ) );
@PatrickGarrido, here's a slightly different approach that gives you a bit more flexibility.
Since it wasn’t fully clear whether you wanted the line to be drawn only from the most recent crossover forward, or between the last two crossovers, I added a parameter that lets you choose between both behaviors.
As per your request, to avoid plotting many historical lines, the formula always displays only one line at a time. It does this by detecting crossovers only within the visible portion of the chart (firstvisiblebar / lastvisiblebar). This means that the line automatically adjusts when you scroll the chart.
I also highlighted the crossover points so you can visually confirm that the detected events match what you expect.
Code below:
SetOption( "WarningLevel", 1 );
sarAccel = Param( "SAR Accel", 0.02, 0.005, 0.30, 0.005 );
sarMax = Param( "SAR Max", 0.20, 0.10, 0.30, 0.01 );
lineMode = ParamToggle( "Line mode", "From last cross|Last 2 crosses", 0 );
psar = SAR( SarAccel, SARMax );
bi = BarIndex();
firstVisible = Status( "firstvisiblebar" );
lastVisible = Status( "lastvisiblebar" );
// used to plot all the shapes at crosses
crossUp_ = Cross( Close, psar );
crossDown_ = Cross( psar, Close );
bir = bi >= firstVisible and bi <= lastVisible;
crossUp = Cross( Close * bir, psar * bir );
crossDown = Cross( psar * bir, Close * bir );
crossEvent = crossUp OR crossDown;
if( lineMode == 1 )
{
// show line between 2 crosses on visible bars
biLastCross = LastValue( ValueWhen( crossEvent, bi, 1 ) );
pricePrevCross = LastValue( ValueWhen( crossEvent, Close, 2 ) );
biPrevCross = LastValue( ValueWhen( crossEvent, bi, 2 ) );
}
else
{
// show line after the last visible cross
biLastCross = LastVisible;
pricePrevCross = LastValue( ValueWhen( crossEvent, Close, 1 ) );
biPrevCross = LastValue( ValueWhen( crossEvent, bi, 1 ) );
}
Plot( Close, "Price", colorDefault, styleCandle );
Plot( psar, "PSAR", colorBlue, styleDots | styleNoLine );
if( pricePrevCross != 0 )
{
y = pricePrevCross;
xStart = biPrevCross;
xEnd = biLastCross;
GfxSetCoordsMode( 1 );
// optional: change color on crossUp vs. crossDown
color = iif( C[biPrevCross - 1] < psar[biPrevCross - 1] AND
C[biPrevCross] > psar[biPrevCross], colorGreen, colorRed );
GfxSelectPen( color, 3 );
GfxMoveTo( xStart, y );
GfxLineTo( xEnd, y );
if( lineMode == 1 )
{
// extend the line - optional - comment out or delete if not desired
GfxSelectPen( color, 2, 1 );
GfxMoveTo( xEnd, y );
GfxLineTo( lastvisible, y );
}
Plot( y, "Cross", color, styleNoLine ); // Show only the axis label
}
PlotShapes( iif( crossUp_, shapeSmallCircle, null ), colorRed, 0, psar, -12 );
PlotShapes( iif( crossDown_, shapeSmallCircle, null ), colorGreen, 0, psar, 12 );
Title = _N( Title = StrFormat( "{{NAME}} - " + FullName() + " - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +
WriteVal( V, 1.0 ) + " {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Feel free to customize the visuals to better match your chart color scheme.