Stoploss that triggers at EMA

Hi,

I am working in a monthly basis code for which I need that the position exits intraday on price equal to stop level on the same bar when stop is triggered. The reference for that stoplevel is an EMA based on the price close of the traded asset, so it changes every month. For example, this could be the level I need to exit intraday:

IntradayExitLevel = Close < Ref(EMA(Close, 20),-1);

The ApplyStop types refer to a maximum loss from the initial amount (stoptypeloss) or from the maximum profit (stoptypetrailing). Is there an easy way to programme a stoploss that triggers at a particular level that changes every month?

Thank you in advance for your answer.

Best regards

I do not think there is ApplyStop required.

/// @link https://forum.amibroker.com/t/stoploss-that-triggers-at-ema/17746/2
Buy = Cross( Close, MA(C, 50) );
Buy = Ref(Buy, -1);
BuyPrice = Open;
 
stop = Ref(EMA(C, 20), -1);
Sell = stop > L; 
SellPrice = Min(O, stop);

Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

Short = Cover = 0;

Plot( C, "Price", colorDefault, styleBar );
Plot( stop, "EMA", colorRed );

PlotShapes( Buy * shapeUpArrow + Sell * shapeDownArrow,
            IIf( Buy, colorGreen, colorRed ), layer = 0,
            y = IIf( Buy, L, H ) );
PlotShapes( Buy * shapeSmallCircle + Sell * shapeSmallCircle,
            IIf( Buy, colorGreen, colorRed ), layer,
            y = IIf( Buy, BuyPrice, SellPrice ), 0 );

9

2 Likes

Hi fxshrat,

You are right, the way you have exposed is much easier.

Thank you for your help!

Best regards