automatictrendlinesRSI with buy/sell

#1
Just something I got on my disk.


_SECTION_BEGIN("_automatictrendlinesRSI");
// RSI - TrendlineBreakout
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();
 
#2
it's not a system, it's not supposed to be. It's meant as an extra tool for confirmation of our own strategy. But be creative and play with the code to see if you can achieve better entry/exit points!
 

Similar threads