# Applystop question

``````SetOption("InitialEquity",100000);
SetOption("AllowSameBarExit",false);
SetOption("ActivateStopsImmediately",False);

percentrisk = 0.1 * BuyPrice / (ATR(14)*2);
SetPositionSize(percentrisk,spsPercentOfEquity);

entrysignal = Cross(30,RSI(2));

BuyPrice = SellPrice = ShortPrice = CoverPrice = Open;
Sell = 0;

ATRdistance = 2 * Ref(ATR(14),-1);

ApplyStop(stopTypeLoss,stopModePoint,ATRdistance,2,False,0,0,-1,0);
ApplyStop(stopTypeProfit,stopModePoint,ATRdistance,2,False,0,0,-1,0);

eq = Equity(1,0);
SetOption("EveryBarNullCheck",True);
PlotShapes((Sell>0)*shapeDigit5,colorblue,0,O,0,0);

Plot(stopline,"stop_line",colorWhite,styleLine,Null,Null,0,0,2);
Plot(profitline,"profitline_line",colorWhite,styleLine,Null,Null,0,0,2);
``````

Hi I'm new with AFL, I'm trying to understand the Applystop function, I know there are alot of topics about it but I can't find any that can help me with this, so I'm just trying to plot the stop loss and take profit prices on the chart to try to understand how it works, it supose to close the trade at the open of the next bar as long a the High or Low breached the stop loss or take profit levels, for some reason I can't make it work, please I need some advice, another question, is the stop loss or take profit calculated using the buyprice? ( for example in order to replicate the stoploss price ( so I can plot it ) woudln't it be Stoplosslvel = Buyprice - ATR(14) * 2?

adding SetTradeDelays( 0 ,0, 0, 0 ); fixed the issue, I'm really sorry for creating a new post of something that has been posted so many times, my bad...

@Francisco, FWIW, setting SetTradeDelays( 0 ,0, 0, 0 ), in general, produce unrealistic results!
I suggest reviewing some past threads where the topic was discussed like, for example this one or this one

I'm sure my code has alot of mistakes, I'm still learning, another related question, I'm testing the stoploss and takeprofit using ATR, but when I try to place the stoploss at the low of the previous candle it doesn't work. I'm calculating the stoploss like this stopdistance = Buyprice - Ref(L,-1);

``````SetTradeDelays( 0 ,0, 0, 0 );
SetOption("InitialEquity",100000);
SetOption("AllowSameBarExit",True);
SetOption("ActivateStopsImmediately",True);
SetOption("CommissionMode",2);
SetOption("CommissionAmount",1.5);
SetOption("EveryBarNullCheck",True);

percentrisk = 0.1 * BuyPrice / (ATR(14)*2);
SetPositionSize(percentrisk,spsPercentOfEquity);

entrysignal1 = Ref(C,-5) - Close > ATR(14)*2;
entrysignal2 = Close > Open ;

ATRdistance = 2 * Ref(ATR(14),-1);
stopdistance =  Buyprice - Ref(L,-1);

Buy = Ref(entrysignal1,-1) && Ref(entrysignal2,-1);
BuyPrice = SellPrice = BuyPrice = SellPrice = Open;
Sell = 0;

ApplyStop(stopTypeLoss,stopModePoint,stopdistance,1,False,0,0,-1,0);
ApplyStop(stopTypeProfit,stopModePoint,ATRdistance,1,False,0,0,-1,0);

eq = Equity(1,0);