KB TrailStop for Short Position

Hello Ami-users

I use the code of the KB http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/

I want to apply the amibroker code for the short case but the result I have on the chart is that
(1) it spoils the scale and
(2) does NOT SHOW the trail stop line.

I did verify the mathematical act (LowestSince( Short, Low ) * ( 1 + 0.01 * StopLevel )) and indeed the stop comes out (in the case of short) to be above the selling price.
I don't know what i miss?
Any comments welcome


/// @link http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/

// I am try to apply the Short version of trailing stop, but below Code is wrong. 

StopLevel = Param("trailing stop %", 3, 0.1, 10, 0.1 );
SetTradeDelays(0,0,0,0);
Short = Cross( Signal(), MACD()  );
Cover= 0;
ApplyStop( stopTypeTrailing, stopModePercent, StopLevel, True );

Equity( 1,0); // evaluate stops, all quotes

InTradeShort = Flip( Short,cover );
SetOption("EveryBarNullCheck", True );
stoplineShort = IIf( InTradeShort, LowestSince( Short, Low ) * ( 1 + 0.01 * StopLevel ), Null );

Plot( stoplineShort, "trailing stop lineShort ", colorRed,styleLine );

Plot( Close,"Price",colorDefault,64);

Look at the last AA window or if any Analysis window is running (or ran last) with "General Settings": Long

If it is, change it to Long and Short, and try it.
Equity() draws its settings from AA window settings and SetOption()

You might be blocking shorts from evaluating.

Also, put this piece of code in your chart

printf( "Total shorts =%g", LastValue( Cum( Short )));

and see the count. For Long only system, this will obviously be 0.

6 Likes

Thank you very much for the answer, dear Trabik. You helped me to understand the importance of Equity(). By changing the settings in the AA window, the scale seems to be ok and the two stops lines are also shown.