Need help on how to ApplyStop..

I'm trying to impose a stoploss and takeprofit to my system.

However, when I tried using it and done the debugging part, I noticed that every new bar the ApplyStop is calculated again and changed the original stoploss level. I'm not sure how does it function.
And if it's not correctly used, what is the appropriate way to impose a stoploss and takeprofit only once at order entry, and never re-calculate it until the order is closed?

Here is the part of the code where I used it:

BuyCondition1	= //Condition 1
BuyCondition2	= //Condition 2

BuyDecision	= BuyCondition1 && BuyCondition2;
Buy	         	= BuyDecision;
	
ApplyStop(stopTypeLoss, stopModePoint, Close[0] - stoploss, 0);
	
ApplyStop(stopTypeProfit , stopModePoint, Close[0] + TakeProfit, 0);
Sell = 0;

http://www.amibroker.com/kb/index.php?s=applystop
https://www.amibroker.com/guide/afl/applystop.html

// adapted from http://www.amibroker.com/kb/index.php?s=applystop

SetOption( "ActivateStopsImmediately", False );
SetTradeDelays( 0, 0, 0, 0 );

ExitAtStop = 0;

Ticksize = 0.01;

BuyCondition1	= //Condition 1
BuyCondition2	= //Condition 2

BuyDecision	= BuyCondition1 AND BuyCondition2;
Buy	         	= Ref(BuyDecision, -1);

BuyPrice = Open;

Sell = 0;
SellPrice = Close;

// make sure stop-amount is at least one tick
stopAmountLong = Max( TickSize, 30*TickSize );
profitAmountLong = Max( TickSize, 30*TickSize );
	
ApplyStop(stopTypeLoss, stopModePoint, stopAmountLong, ExitAtStop );	
ApplyStop(stopTypeProfit, stopModePoint, profitAmountLong, ExitAtStop );
2 Likes

Thank codejunkie.

I will try it and let you know.

could you please add signal arrows to the above code?

Also , I think it is only buy ad exit. So, can I just reverse for short and exit and add to this code for a complete system?