AFL for Trailing Stop Loss

Hi Everyone,
So, I was coding this below afl. And then, I was looking for a trailing stop loss code & someone gave it to me. And it's working fine but I could not use that trailing stop loss code for short position. So guys, please help me out here.
Thank you.

SignalStartNSEintra = 092000; SignalEndNSEintra = 030500; SquareOffNSEintra = 031000;
TimeFrameSet(in15Minute);
BuyCondition1 = L == Ref(L,-1);
BuyCondition2 = Ref(O,-1) != Ref(C,-1);
BuyCondition3 = C > (H+L)/2;
BuyCondition4 = V > Ref(V,-1);
TimeFrameRestore();
TimeFrameExpand(BuyCondition1,in15Minute);
TimeFrameExpand(BuyCondition2,in15Minute);
TimeFrameExpand(BuyCondition3,in15Minute);
TimeFrameExpand(BuyCondition4,in15Minute);
Buy = TimeFrameExpand(BuyCondition1,in15Minute) AND TimeFrameExpand(BuyCondition2,in15Minute) AND TimeFrameExpand(BuyCondition3,in15Minute) AND TimeFrameExpand(BuyCondition4,in15Minute)
      AND TimeNum()>=SignalStartNSEintra AND TimeNum()<=SignalEndNSEintra;
BuyPrice = ValueWhen(Buy,Open);
Sell = 0 OR TimeNum()>=SquareOffNSEintra;
SellPrice = ValueWhen(Sell,Open);
SetPositionSize(1,spsShares); // Money Management - How much to buy
SetTradeDelays(1,1,1,1); // buy/sell/short/cover will take place in next candle after signal generation.

StopLevel = 1 - Param("trailing stop %", 3, 0.1, 10, 1, 0.1)/100;
trailARRAY = Null;
trailstop = 0;
for( i = 1; i < BarCount; i++ ) 
{
 if( trailstop == 0 AND Buy[ i ] ) 
 {
  trailstop = High[ i ] * StopLevel;
   } 
else Buy[ i ] = 0; // remove excess buy signals
if( trailstop > 0 AND Low[ i ] < trailstop ) 
{
Sell[ i ] = 1; 
SellPrice[ i ] = trailstop; 
trailstop = 0; 
}
if( trailstop > 0 ) 
{
trailstop = Max(High[ i ] * StopLevel, trailstop);
trailARRAY[ i ] = trailstop;
}
}

//Shaping for Buy:
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
//Shaping Buy T.SL:
Plot(trailARRAY,"Trailing SL",colorAqua, styleLine | styleThick);
//Shaping Sell:
PlotShapes(IIf(Sell, shapeStar, shapeNone),colorGold, 0, L, Offset=-15); 

TimeFrameSet(in15Minute);
ShortCondition1 = H == Ref(H,-1);
ShortCondition2 = Ref(O,-1) != Ref(C,-1);
ShortCondition3 = C < (H+L)/2;
ShortCondition4 = V > Ref(V,-1);
TimeFrameRestore();
TimeFrameExpand(ShortCondition1,in15Minute);
TimeFrameExpand(ShortCondition2,in15Minute);
TimeFrameExpand(ShortCondition3,in15Minute);
TimeFrameExpand(ShortCondition4,in15Minute);
Short = TimeFrameExpand(ShortCondition1,in15Minute) AND TimeFrameExpand(ShortCondition2,in15Minute) AND TimeFrameExpand(ShortCondition3,in15Minute) AND TimeFrameExpand(ShortCondition4,in15Minute)
        AND TimeNum()>=SignalStartNSEintra AND TimeNum()<=SignalEndNSEintra;
ShortPrice = ValueWhen(Short,Open);       
Cover = 0 OR TimeNum()>=SquareOffNSEintra;
CoverPrice = ValueWhen(Cover,Open);
SetPositionSize(1,spsShares); //Money Management - How much to buy
SetTradeDelays(1,1,1,1); // buy/sell/short/cover will take place in next candle after signal generation.

//Shaping for Short:
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
//Shaping Cover:
PlotShapes(IIf(Cover, shapeStar, shapeNone),colorGold, 0,L, Offset=-15);

Two things:

  1. Make sure you verify your AmiBroker license, or you won't be able to post on the forum
  2. Check out the ApplyStop function and consider using that rather than coding the trailing stop yourself.
2 Likes