Okay. Here is working code. I have used Short /Cover for cross of trailing Stop.
Buy/ Sell can be your preferred logic. Four type of stops can be used and set in Parameters. Buy/Sell Price and Stop Price also can be plotted.
Though I have not checked the prices being displayed.
//Stopeloss Code
StopMode = ParamList("Stop Mode", "FixPoints|FixedPercent|Chandelier|Modified ATR" );
StopLevel = Param("Fixed perc %", 1, 0.1, 5, 0.1)/100;
FixedSL = Param("FixSL", 40,1,1,100);
StopATRFactor = k;
StopATRPeriod = Per;
// calculate support and resistance levels
if( StopMode == "FixedPercent" ) // fixed percent trailing stop
{
sup = C * (1- StopLevel);
res = C * (1+ StopLevel);
}
else // Chandelier ATR-based stop
if( StopMode == "Chandelier" )
{
sup = C - StopATRFactor * ATR( StopATRPeriod );
res = C + StopATRFactor * ATR( StopATRPeriod );
}
else
if ( StopMode == "Modified ATR" )
{
HL = H - L;
MAHL = 1.5 * MA( HL, StopATRPeriod );
HiLo = IIf( HL < MAHL, HL, MAHL );
H1 = Ref( H, -1 );
L1 = Ref( L, -1 );
C1 = Ref( C, -1 );
Href = IIf( L <= H1, H - C1, ( H - C1 ) - ( L - H1 ) / 2 );
Lref = IIf( H >= L1, C1 - L, ( C1 - L ) - ( L1 - H ) / 2 );
diff1 = Max( HiLo, HRef );
diff2 = Max( diff1, LRef );
ATRmod = Wilders( diff2, StopATRPeriod );
sup = C - StopATRFactor * ATRmod ;
res = C + StopATRFactor * ATRmod ;
}
else
if ( StopMode == "FixPoints" )
{
ApplyStop( stopTypeTrailing, mode = 1, amount = FixedSL, exitatstop = 1, volatile = True, ReEntryDelay = 0 ) ;
sup = H - FixedSL;
res = L + FixedSL ;
}
Equity( 1, 0 ); // evaluate stops, all quotes
// calculate trailing stop line
trailARRAY = Null;
trailstop = 0;
for( i = 1; i < BarCount; i++ )
{
//if( Started[ i ] == 0 ) continue;
if( C[ i ] > trailstop AND C[ i - 1 ] > trailstop )
trailstop = Max( trailstop, sup[ i ] );
else
if( C[ i ] < trailstop AND C[ i - 1 ] < trailstop )
trailstop = Min( trailstop, res[ i ] );
else
trailstop = IIf( C[ i ] > trailstop, sup[ i ], res[ i ] );
trailARRAY[ i ] = trailstop;
}
// generate buy/sell signals based on crossover with trail stop line
Cover = Cross( C, trailArray );
Short = Cross( trailArray, C );
PlotShapes(Cover*shapeUpArrow,colorYellow,0,trailarray);
PlotShapes(Short*shapeDownArrow,colorYellow,0,trailarray);
//Plot( Close,"Price",colorBlack,styleBar);
//SetBarFillColor( colorYellow );
TRAILCOLOR =IIf(C>trailARRAY,colorAqua,colorYellow);
Plot( trailARRAY,"trailing stop level", TRAILCOLOR, styleStaircase );
//New Code End
Buy = Your Buy Logic ;
Sell = Your Sell Logic;
Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);
BuyPrice = C; // Substitute your own prices
SellPrice = C; // Substitute your own prices
//dist = 1.5*ATR(15);
for( i = 0; i < BarCount; i++ )
{
if( Cover ) PlotText( "Cover@" + C[ i ], i, Halow-31, colorSkyblue );
AlertIf(Buy, "SOUND H:\\AMIBROKER\\Sound\\ding.WAV", "Audio Alert", 1);
if( Short ) PlotText( "Short@" +C[ i ], i, Hahigh+31, colorRose );
AlertIf(Sell, "SOUND H:\\AMIBROKER\\Sound\\tada.WAV", "Sell", 3);
//if( Sell ) PlotText( "Sell@" +H[ i ], i-4, H[ i ]+Trend+50, colorRed, colorRose );
if( Buy ) PlotText("\n Buy\n "+NumToStr(BuyPrice,1.2),i,BuyPrice-10,colorAqua);
if( Sell ) PlotText("\n Sell\n "+NumToStr(SellPrice,1.2),i,SellPrice+30,colorYellow);
}
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,Halow,-30);
PlotShapes(IIf(Sell, shapeHollowDownTriangle, shapeNone),colorWhite, 0,Hahigh,-15);
PlotShapes(IIf(Cover, shapeHollowUpTriangle, shapeNone),colorWhite, 0,Halow,-15);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,Hahigh,-30);
PlotShapes( IIf(Buy, shapeSmallCircle, shapeNone),colorRed, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone),colorBrightGreen, 0 ,SellPrice, 0 );
PlotShapes(Buy*shapeUpArrow,colorYellow,0,trailarray);
PlotShapes(Sell*shapeDownArrow,colorYellow,0,trailarray);
// Plot the Trade Lines
Sig = Buy OR Sell;
y0 = 0;
y1 = C[0];
FirstVisibleBar = Status( "FirstVisibleBar" );
Lastvisiblebar = Status( "LastVisibleBar" );
CombinedColor = colorWhite;
CombinedLine = Null;
for ( b = Firstvisiblebar; b <= Lastvisiblebar AND b < BarCount; b++ )
{
if ( Buy )
{
Co = colorRed;
TPrice = BuyPrice;
}
else if ( Sell )
{
Co = colorBrightGreen;
TPrice = SellPrice;
}
if ( Sig )
{
x0 = y0;
x1 = y1;
y0 = b;
y1 = TPrice;
La = LineArray( x0, x1, y0, y1 );
CombinedLine = IIf( IsNull( la ), CombinedLine, la );
CombinedColor = IIf( IsNull( La ), CombinedColor, Co );
}
}
Plot( CombinedLine, "", CombinedColor );