_automatictrendlinesRSI

#1
3 of 4 automatic trendlines; enjoy!

_SECTION_BEGIN("_automatictrendlinesRSI");
// RSI - TrendlineBreakout
SetChartOptions(0,0,chartGrid20 |chartGrid50 |chartGrid80);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} < open %g > < high %g > < low %g > < close %g > (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
SetChartBkColor(ParamColor("Outer panel color ",colorBlack)); // color of outer border
SetChartBkGradientFill( ParamColor("Inner panel color upper half",colorBlack),
ParamColor("Inner panel color lower half",colorDarkTeal)//color of inner panel
,ParamColor("behind Text Color", colorBlack));
SetOption("MaxOpenPositions", 2 );
PositionSize = 1000;
GraphXSpace=5;
SetBarsRequired( sbrAll, sbrAll );
SetOption("MaxOpenPositions", 2 );
PositionSize = 1000;
indicatorMode = Status( "action" ) == actionIndicator;
Buy = Short = Sell = Cover = 0;
BuyZone = 0;
ShortZone = 0;

r= Optimize("Reverse ",Param("Reverse ",1,1,28,1),1,28,1);

reverse = r / 100;

tangent = 0;
firstVisible = Status( "FirstVisibleBarIndex" );
LastVisible = Status( "LastVisibleBarIndex" );
isVisible = 0;
breakOut = 0;
graphColor = colorWhite;

pds = Optimize("pds ",Param("pds ",7,1,28,1),1,28,1);

myArray = RSI( pds );
j = 0;
direction[0] = 0;
dd = 0 ;
y[0] = myArray[pds];
x[0] = 0;

for ( i = pds + 1; i < BarCount; i++ )
{
if ( direction[j] == 0 )
{
if ( myArray <= y[j] )
{
y[j] = myArray;
x[j] = i;
}
else
if ( myArray >= y[j]*( 1 + reverse ) )
{
j++;
y[j] = myArray;
direction[j] = 1;
x[j] = i;
}
}
else
{
if ( myArray >= y[j] )
{
y[j] = myArray;
x[j] = i;
}

else
if ( myArray <= y[j]* ( 1 - reverse ) )
{
j++;
y[j] = myArray;
direction[j] = 0;
x[j] = i;

}
}

dd = direction[j];

if ( x[j] >= firstVisible && x[j] <= LastVisible )
isVisible[j] = 1;

if ( j > 2 )
{
dy = ( y[j] - y[j-2] ) ;
dx = x[j] - x[j-2];
tangent[j] = dy / dx;

if ( direction[j] )
{
breakOut[j] = tangent[j] > tangent[j-2] ;

if ( breakOut [j] )
{
BuyZone = 1 ;
graphColor = colorGreen;


}
}

else
{
breakOut[j] = tangent[j] < tangent[j-2];

if ( breakOut[j] )
{
ShortZone = 1;
graphColor = colorDarkRed;


}

}

}

}


Buy =Cover = ExRem( BuyZone, !dd );
Short =Sell = ExRem( ShortZone, dd ) ;
//SetChartOptions(0,0,ChartGrid30 | ChartGrid70 );

if ( IndicatorMode )
{
SetChartOptions( 0,0, ChartGrid50, chartShowDates + chartShowArrows );
Plot ( myArray, "" , Graphcolor );
//PlotOHLC( myArray,myArray,50,myArray, "", IIf( myArray> 50, colorYellow, colorWhite ), styleCloud | styleClipMinMax, 30, 70 );

//PlotShapes( Buy * shapeSmallCircle, colorBrightGreen, 0, BuyPrice, 0 );
PlotShapes( Buy * shapeSmallUpTriangle, colorBrightGreen, 0, myArray );
// PlotShapes( Sell * shapeStar, colorBrightGreen, 0, SellPrice, 0 );
// PlotShapes( Short * shapeSmallCircle, colorRed, 0, BuyPrice, 0 );
PlotShapes( Short * shapeSmallDownTriangle, colorRed, 0, myArray);
// PlotShapes( Cover * shapeStar, colorRed, 0, CoverPrice, 0 );


//-------display trendlines------------//

tlcolor = colorGreen;
myTrendLine = Null;

for ( n = 1; n <= j; n++ )
{
if ( isVisible[n] && n > 3 )
{
if ( direction[n] )
tlcolor = colorGreen;
else
tlcolor = colorRed;

if ( breakOut[n] )
myTrendline = LineArray( x[n-4], y[n-4], x[n-2], y[n-2], 1 );

myTrendline = IIf( BarIndex() > x[n], Null, myTrendline );
Plot( myTrendline, "", tlcolor, styleLine + styleNoRescale | styleNoLabel | styleDashed );
}

//Plot( LineArray( x[n-1], y[n-1], x[n], y[n] ), "", colorViolet, styleLine + styleNoRescale , styleNoLabel ) ; // plots ZigZag
}

}
_SECTION_END();