Help Need to Code Trailing SL

pkgmtnl

Well-Known Member
#1
Dear Friends,
I need a help of experts to develop a trailing codes for AFL (AMIBROKER). Can some1 please help to code them for AMIBROKER :-

1. The Trailing SL in case of BUY would be as follows:-

- After first BUY is implanted (let it be candle 1);
- Then, AFL should wait for next 3 candles to complete before commencing to draw SL line/ level;
- When 4th Candle is complete, it should start BUY-SL at low of preceding "first Candle" and;
- It should thereafter take forward SL in steps of 3 candles (it means the SL would wait for further 3 candles to complete before raising the SL line upwards in manner explained above);
- Incase SELL comes as per Strategy logic, it would Square and move ahead;

2. The Trailing SL in case of SHORT would be as follows:-

- After first SHORT is implanted (let it be candle 1);
- Then, AFL should wait for next 3 candles more Candles to complete before commencing to draw SL line/ level;
- When 4th Candle is complete, it should start SHORT-SL at high of preceding "first Candle" and;
- It should thereafter take forward SL in steps of 3 candles (it means the SL would wait for further 3 candles to complete before deceding the SL line downwards in manner explained above;

The AFL should be just Drag & Drop on Trading Strategy.
 

RDutt

Active Member
#2
Good thought Sir.
Even I was searching for something of this kind, for long time. Many trading platforms are providing this facility inbuilt - called TRAILING STOPLOSS.
If developed by some kind hearted senior this code may be quite useful for many members here.

Regards,
 

pkgmtnl

Well-Known Member
#3
Dear Experts,
Some1 gave me this :-

Plz help to redesign for my conditions:-

TradeDelay = 1; // set it to 0 for no delays

SetTradeDelays( TradeDelay, TradeDelay, TradeDelay, TradeDelay );
TickSize = 0.01;

// sample entry rules
Buy = Cross( MACD(), Signal() );
Short = Cross( Signal(), MACD() );
Sell = Cover = 0; // no other exit conditions, just stops

BuyPrice = SellPrice = ShortPrice = CoverPrice = Close;

// define stop level
stopLevelLong = Ref( L, -1 ); // use previous bar low
stopLevelShort = Ref( H, -1 ); // use previous bar high

// calculate stop amount
stopAmountLong = BuyPrice - stopLevelLong;
stopAmountShort = stopLevelShort - ShortPrice;

// make sure stop-amount is at least one tick
stopAmountLong = Max( TickSize, BuyPrice - stopLevelLong );
stopAmountShort = Max( TickSize, stopLevelShort - ShortPrice );

// assign stop amount conditionally by checking if there is a Buy signal on given bar
IsLong = Ref( Buy, -TradeDelay );
stopAmount = IIf( IsLong, stopAmountLong, stopAmountShort );
ApplyStop( stopTypeLoss, stopModePoint, stopAmount, True );
 

Similar threads