Hi,
Found this apply stop function from https://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/.
Working fine for Buy only. When trying to do it with short, could not put it at work.
Thanks.
//Bars, Entry & Exit Candle, Quantity:
SetBarsRequired(sbrAll,sbrAll); // AFL will backtest all available data.
SetPositionSize(1,spsShares); /*Money Management - How much to buy*/ SetTradeDelays(1,0,1,0); /*buy/short will take place in next candle after signal generation & Sell/Cover will take place very moment of signal generation .*/
PositionSize = -100; // invest 100% of portfolio equity in single trade
BuyCondition1 = L == Ref(L,-1); BuyCondition2 = Ref(O,-1) != Ref(C,-1); BuyCondition3 = C > (H+L)/2;
BuyCondition4 = C>50; BuyCondition5 = C<1500;
ShortCondition1 = H == Ref(H,-1); ShortCondition2 = Ref(O,-1) != Ref(C,-1); ShortCondition3 = C < (H+L)/2;
ShortCondition4 = C>50; ShortCondition5 = C<1500;
Buy = BuyCondition1 AND BuyCondition2 AND BuyCondition3 AND BuyCondition4 AND BuyCondition5;
BuyPrice = ValueWhen(Buy,Open);
Short = ShortCondition1 AND ShortCondition2 AND ShortCondition3 AND ShortCondition4 AND ShortCondition5;
ShortPrice = ValueWhen(Short,Open);
Sell= Cover= 0;
StopLevel = Param("trailing stop %",1.5,0,10,1);
ApplyStop( stopTypeTrailing, stopModePercent, StopLevel, True );
Equity( 1, 0 ); // evaluate stops, all quotes
InTrade = Flip( Buy, Sell );
SetOption("EveryBarNullCheck", True );
stopline = IIf( InTrade, HighestSince( Buy, High ) * ( 1 - 0.01 * StopLevel ), Null );
Plot( stopline, "trailing stop line", colorAqua );
StopLevel2 = Param("trailing stop %", 1.5, 0, 10, 0.1);
ApplyStop( stopTypeTrailing, stopModePercent, StopLevel, True );
Equity( 1, 0 ); // evaluate stops, all quotes
InTrade2 = Flip( Short, Cover );
SetOption("EveryBarNullCheck", True );
stopline = IIf( InTrade2, HighestSince( Short, Low ) * ( 1 - 0.01 * StopLevel2 ), Null );
Plot( stopline, "trailing stop line", colorGold );
//Shape 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); PlotShapes(IIf(Buy, shapedigit1, shapeNone),colorWhite, 0,L, Offset=-80);
//Shape 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);PlotShapes(IIf(Short, shapedigit1, shapeNone),colorGold, 0,H, Offset=80);
//Shaping Buy & Short T.SL:
//Shaping Sell & Cover:
PlotShapes(IIf(Sell, shapeStar, shapeNone),colorGold, 0, L, Offset=-15);
PlotShapes(IIf(Cover, shapeStar, shapeNone),colorWhite, 0,L, Offset=-15);
SetFormulaName("Two High/Low Equal");
SetChartOptions(0,chartShowArrows|chartShowDates);
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
//Coder's Description on Technical Chart:
GfxSetBkMode(1); X=750; Y=1; Font=7;
GfxSelectFont("Arial",Font*2, 550);GfxSetTextColor(colorYellow);GfxTextOut("Price Action Strategy 1 :",x,y);
GfxSelectFont("Arial",Font*2,550);GfxSetTextColor(colorWhite);GfxTextOut("Two High/Low Equal",x+200,y);
GfxSelectFont("Arial",Font*2, 550);GfxSetTextColor(colorYellow);GfxTextOut("Chart Window Screen Resolution :",x,y+30);
pxwidth = Status("pxwidth"); pxheight = Status("pxheight");
GfxSelectFont("Arial",Font*2, 550); GfxSetTextColor(colorWhite); GfxTextOut("("+pxwidth+","+pxheight+")",x+300,y+30);
GfxSelectFont("Arial",Font*2,True,True,550);GfxSetTextColor(colorBrightGreen);GfxTextOut("Developed by- Aritra K.Sinha",x,y+60);
GfxSelectFont("Arial",Font*2, 550);GfxSetTextColor(colorAqua);GfxTextOut("Email: aritranicknamearka@gmail.com",x+260,y+60);