Here is the full code
SHT = 1;
LNG = 2;
direction = LNG ;
_SECTION_BEGIN( "RSI" );
SetBarsRequired( -2, -2 ); // require all bars.
dt = DateTime() ;
Buy = Sell = Cover = Short = False;
_rsi = RSI( 2 );
_SECTION_END();
SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorBlack ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );
Plot( ema200 = MA( C, 200 ), "200 ma" , colorBlue, ParamStyle( "Style" ) );
Plot( ema5 = eMA( C, 5 ), "5ema" , colorYellow, ParamStyle( "Style" ) );
if( direction == LNG )
{
Plot( don10l = Ref( LLV( L, 10 ), -1 ), "dn10l" , colorPink, styleLine );
donh20 = HHV( H, 20 );
r = ATR( 14 );
gotsignal = C<ema5 AND C>ema200 AND _rsi <= 6 AND L <= don10l;
//PlotShapes( gotsignal*shapeHollowSquare, IIf( L <= don10l, colorLightBlue, colorYellow ), Graph0, l );
opentrade = False;
quantity = 0;
tradebar = scaleoutbar = trailexitbar = stopexitbar = 0;
entryDate = Null;
entryprice = 0;
currentpnl = 0;
risk = 0;
tradenumber = 0;
jump = 2;
tradeScaleoutPrice = tradeSellPrice = tradeBuyPrice = 0;
myOpen = Prec( Open, 4 );
for( i = 1; i < BarCount - 1; i++ )
{
if( gotsignal[i ] AND opentrade == False )
{
opentrade = True;
Buy[i] = True;
BuyPrice[i] = Open[i];
quantity = 2;
tradebar = i;
currentpnl = 0;
entryDate = DateTimeToStr( dt[i + 1] , 3 ) ;
tradenumber += 1;
jump = IIf( jump == 2, 3, 2 );
}
if( i == tradebar + 1 )
{
scaleoutbar = trailexitbar = stopexitbar = 0;
risk = don10l[i] - r[i];
tradeBuyPrice[i] = entryprice = myOpen[i]; // remembmer that the buy price is at open of next day in amibroker.
PlotText( "entry#" + tradenumber + "[" + entryprice + "]" , i, Status( "axisminy" ) + jump * r[i], colorLightBlue, coloryellow, yoffset = 0 ) ;
}
if( i == scaleoutbar + 1 )
{
tradeScaleoutPrice[i] = myOpen[i]; //remembmer that the sell price is at open of next day in amibroker.
currentpnl = myOpen[i] - entryprice;
PlotText( "Scaleout(" + tradenumber + ")" + currentpnl + "[" + myOpen[i] + "]", i, Status( "axismaxy" ) - 2 * r[i], IIf( currentpnl > 0, colorBlue, colorred ), coloryellow, yoffset = 0 ) ;
}
if( i == stopexitbar + 1 )
{
currentpnl = myOpen[i] - entryprice; //remembmer that the sell price is at open of next day in amibroker.
tradeSellPrice[i] = myOpen[i];
PlotText( "stoploss(" + tradenumber + ")" + currentpnl + "[" + myOpen[i] + "]", i, Status( "axismaxy" ) - jump * r[i], IIf( currentpnl > 0, colorBlue, colorred ), coloryellow, yoffset = 0 ) ;
}
if( i == trailexitbar + 1 )
{
tradeSellPrice[i] = myOpen[i]; //remembmer that the sell price is at open of next day in amibroker.
currentpnl = myOpen[i] - entryprice ;
PlotText( "trail(" + tradenumber + ")" + currentpnl + "[" + myOpen[i] + "]", i, Status( "axismaxy" ) - 4 * r[i], IIf( currentpnl > 0, colorBlue, colorred ), coloryellow, yoffset = 0 ) ;
}
if( opentrade AND i > tradebar AND quantity > 0 )
{
if( quantity == 2 AND _rsi[i - 1] > 70 ) // profit targets
{
Buy[ i ] = sigScaleOut;
SellPrice[i] = Open[i];
quantity = 1;
scaleoutbar = i;
}
// if( quantity == 2 AND L[i] < don10l[i] -r[i]) // stops
if( quantity == 2 AND L[i] < risk ) // stops
{
Sell[ i ] = true;
SellPrice[i] = Open[i];
quantity = 0;
opentrade = False;
stopexitbar = i;
}
if( quantity == 1 AND( L[i] < don10l[i] ) OR( L[i] < entryprice ) ) // trailing stops.
{
Sell[ i ] = true;
sellPrice = Open[i];
quantity = 0;
opentrade = False;
trailexitbar = i;
}
}
}
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // scale out 50% of position
PlotShapes( tradeBuyPrice * shapeUpArrow, colorLightBlue, tradeBuyPrice );
PlotShapes( tradeScaleoutPrice * shapeSmallCircle, colorLightOrange, tradeScaleoutPrice );
PlotShapes( tradeSellPrice * shapedownArrow, colorLightOrange, tradeSellPrice );
}
if( direction == SHT )
{
Plot( don10s = Ref( hhv( h, 10 ), -1 ), "dn10s" , colorPink, styleLine );
donl20 = llv( l, 20 );
r = ATR( 14 );
gotsignal = C > ema5 AND C<ema200 AND _rsi >= 94 AND h >= don10s;
//PlotShapes( gotsignal*shapeHollowSquare, IIf( L <= don10l, colorLightBlue, colorYellow ), Graph0, l );
opentrade = False;
quantity = 0;
tradebar = scaleoutbar = trailexitbar = stopexitbar = 0;
entryDate = Null;
entryprice = 0;
currentpnl = 0;
risk = 0;
tradenumber = 0;
jump = 2;
tradeScaleoutPrice = tradeCoverPrice = tradeShortprice = 0;
myOpen = Prec( Open, 4 );
for( i = 1; i < BarCount - 1; i++ )
{
if( gotsignal[i ] AND opentrade == False )
{
opentrade = True;
Short[i] = True;
ShortPrice[i] = Open[i];
quantity = 2;
tradebar = i;
currentpnl = 0;
entryDate = DateTimeToStr( dt[i + 1] , 3 ) ;
tradenumber += 1;
jump = IIf( jump == 2, 3, 2 );
}
if( i == tradebar + 1 )
{
scaleoutbar = trailexitbar = stopexitbar = 0;
risk = don10s[i] + r[i];
tradeShortPrice[i] = entryprice = myOpen[i]; // remembmer that the buy price is at open of next day in amibroker.
PlotText( "entry#" + tradenumber + "[" + entryprice + "]" , i, Status( "axisminy" ) + jump * r[i], colorLightBlue, coloryellow, yoffset = 0 ) ;
}
if( i == scaleoutbar + 1 )
{
tradeScaleoutPrice[i] = myOpen[i]; //remembmer that the sell price is at open of next day in amibroker.
currentpnl = entryprice - myOpen[i];
PlotText( "Scaleout(" + tradenumber + ")" + currentpnl + "[" + myOpen[i] + "]", i, Status( "axismaxy" ) - 2 * r[i], IIf( currentpnl > 0, colorBlue, colorred ), coloryellow, yoffset = 0 ) ;
}
if( i == stopexitbar + 1 )
{
currentpnl = entryprice - myOpen[i]; //remembmer that the sell price is at open of next day in amibroker.
tradeCoverPrice[i] = myOpen[i];
PlotText( "stoploss(" + tradenumber + ")" + currentpnl + "[" + myOpen[i] + "]", i, Status( "axismaxy" ) - jump * r[i], IIf( currentpnl > 0, colorBlue, colorred ), coloryellow, yoffset = 0 ) ;
}
if( i == trailexitbar + 1 )
{
tradeCoverPrice[i] = myOpen[i]; //remembmer that the sell price is at open of next day in amibroker.
currentpnl = entryprice - myOpen[i];
PlotText( "trail(" + tradenumber + ")" + currentpnl + "[" + myOpen[i] + "]", i, Status( "axismaxy" ) - 4 * r[i], IIf( currentpnl > 0, colorBlue, colorred ), coloryellow, yoffset = 0 ) ;
}
if( opentrade AND i > tradebar AND quantity > 0 )
{
if( quantity == 2 AND _rsi[i - 1] < 30 ) // profit targets
{
Cover[ i ] = sigScaleOut;
CoverPrice[i] = Open[i];
quantity = 1;
scaleoutbar = i;
}
// if( quantity == 2 AND L[i] < don10l[i] -r[i]) // stops
if( quantity == 2 AND h[i] > risk ) // stops
{
Cover[ i ] = true;
CoverPrice[i] = Open[i];
quantity = 0;
opentrade = False;
stopexitbar = i;
}
if( quantity == 1 AND( h[i] > don10s[i] ) ) // trailing stops.
// if( quantity == 1 AND( h[i] > don10s[i] ) OR(h[i] > entryprice ) ) // trailing stops.
{
cover[ i ] = true;
coverPrice = Open[i];
quantity = 0;
opentrade = False;
trailexitbar = i;
}
}
}
PlotShapes( tradeShortPrice * shapedownArrow, colorLightBlue, tradeShortPrice );
PlotShapes( tradeScaleoutPrice * shapeSmallCircle, colorLightOrange, tradeScaleoutPrice );
PlotShapes( tradeCoverPrice * shapeupArrow, colorLightOrange, tradeCoverPrice );
SetPositionSize( 50, spsPercentOfPosition * ( short == sigScaleOut ) ); // scale out 50% of position
}
SetCustomBacktestProc( "" );
if( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject();
bo.Backtest( 1 ); // run default backtest procedure
tradeid = 1;
// iterate through closed trades first
for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
{
trade.AddCustomMetric( "Trade #", tradeid );
// bo.RawTextOutput("Trade #" + tradeid);
tradeid = tradeid + 1;
}
bo.ListTrades();
}
// rules buy when gotsignal
// sell half when i hit 70
// trail stop at 10 don
// put stop above entry after hitting 70
// put stop at don 10 at all times 2 bars after entr.
// if 9 <20 ema then exit at 20 donchian highs.