Copy-pasting spaghetti code is not the way to go, was: ApplyStop

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);

This happens because of blindly copy and pasting code 1:1 from other sites and putting "developed by" and email at the end. You should rather modify that info to "copy and pasted by" ...

If you don't know yourself and need help then it is not developed but then it is just something of category "fishing in muddy waters" been put together and hoping for it to work + praying to God.

That being said... you simply copy and pasted incorrectly.

Who told you to apply Equity() function two times (besides only required for stop output in chart of your posted code)? And who told you to call Equity() before ApplyStop? In addition you pasted identical ApplyStop code two times.

Those are the main causes. Your code structure + understanding simply is incorrect.
As for code structure... it is input, processing, output.

Also writing

ShortPrice = ValueWhen(Short, Open);

is unnecessary code.

It is just BuyPrice = Open;
as well as ShortPrice = Open; !

Other things... you set position size two times. Simply use SetPositionSize single time per AFL (same applies for other SetOption lines). That's enough.

Also you don't need to set all bars.

BTW, measures and tools to avoid "fishing in muddy waters" are mentioned in first post here.


PS: Thread subject should be changed to "Incorrectly written code was: Apply Stop Function Not Working"

2 Likes

As @fxshrat wrote you - don't copy paste. Start with something simple.
Start with specifying in plain English what you actually want. Written specification BEFORE starting to write first line of code would help you a lot. And You will be able to search for help if you actually wrote clear English spec. Copy-pasting spaghetti code from random corners of Internet would lead you to nowhere.

2 Likes