Hello All,
I tried to implement trailingstop loss wih 1% as the activation floor. The trailing amount is varying and set as the previous HA( Heinken Ashi) open+- some amount. This will be active after 1 % profit.
I want to apply the concept for both longs and shorts.
In order to feed the correct trailing amount, I need to know whether I am in long position or short position.
when dealing with dynamic stopModePoints trailing stops, the math is a bit different: the stop value should be subtracted/added from the highest high/lowest low. Well, at least that's what I thought how it should be done.
I tried to invoke Equity function before ApplyStop but Changes to buy/sell/short/coverrules made after the call have no effect. I dont get the status of the position correctly.
So the question is: what math one should use to match the values used by ApplyStop(stopTypeTrailing, stopModePoints, ARRAY, 1, TRUE) in AB? Or maybe that's not how dynamic ApplyStop() works?
How can I find whether I am on Long or Short before ApplyStop??
Any help?
_SECTION_BEGIN("TS");
Version(6.30);
HaClose = ( O+H+L+C ) / 4;
HaOpen = AMA( Ref(HaClose, -1), 0.5);
HaHigh = Max(H, Max(HaClose, HaOpen));
HaLow = Min(L, Min(HaClose, HaOpen));
TradeDelay = 1; // set it to 0 for no delays
SetTradeDelays( TradeDelay, 0, TradeDelay, 0 );
SetOption("EveryBarNullCheck", True );
SetOption("ActivateStopsImmediately",True);
SetOption("AllowSameBarExit",True);
SetOption( "FuturesMode", False );
SetOption( "MaxOpenPositions", 100 );
SetPositionSize( 1, spsShares );
SetBacktestMode(backtestRegular);
TickSize = 0.05;
BuyPrice = SellPrice = ShortPrice = CoverPrice = Open;
BarsSinceNewDay = 1 + BarsSince( Day() != Ref(Day(), -1));
Buy = Cross( MACD(), Signal() );
Short = Cross( Signal(), MACD() );
Sell=iif(BarsSinceNewDay==25,1,0);
Cover=iif(BarsSinceNewDay==25,1,0);
BuyPrice = ValueWhen( Ref(Buy,-TradeDelay), BuyPrice );
ShortPrice = ValueWhen( Ref(Short,-TradeDelay), ShortPrice );
stopTrailingBuff=Ref(ATR(14),-1)*0.45;
AddColumn(stopTrailingBuff,"stopTrailingBuff");
stopTrailHigh=HighestSince( Ref( Buy, -TradeDelay ), High );
stopTrailLowLine=Ref(HaOpen,-1) - stopTrailingBuff;
trailAmountLong=((stopTrailHigh-stopTrailLowLine)/stopTrailHigh)*100;
AddColumn(trailAmountLong,"trailAmountLong");
stopTrailLow=LowestSince( Ref( Short, -TradeDelay ), Low );
stopTrailHighLine=Ref(HaOpen,-1) + stopTrailingBuff;
trailAmountShort=((stopTrailHighLine-stopTrailLow)/stopTrailLow)*100;
AddColumn(trailAmountShort,"trailAmountShort");
//using Volatile true to change the trail amount and activation floor parameters
ApplyStop( stopTypeTrailing, stopModePercent, trailAmountLong,1, True, 0, 0, -1, 1 ); // I dont know how to select the trailAmount for shorts & longs before applying ApplyStop??
// ApplyStop( stopTypeTrailing, stopModePercent, trailAmountShort,1, True, 0, 0, -1, 1 );
Equity(1,0); // // update equity to get the status of the open positions
//InLong=Flip(1,0);
//InShort=Flip(1,0);
Filter=Buy;
AddColumn(Buy,"Buy");
AddColumn(Sell,"Sell");
_SECTION_END();
I have already experimented with https://www.amibroker.com/kb/2014/10/17/using-price-levels-with-applystop-function/ - where the stoploss is fixed from entry.