Exit at HHV 3 from entry

Hi,

can anyone help mi please? I need exit from position when Close > HHV(High, 3) from entry
I tried Sell = Close > HHV(H, 3) but it doesnt work when I use backtest. It counts HHV from today not from BuyPrice.

Thanks H.

First of all you have to add Ref() if using just larger sign. Close can not be higher than H of same bar since HHV includes current bar!

Sell = Close > Ref(HHV(H, 3),-1); 

Or add >= (as Close can be equal to High of same bar but not higher).

Sell = Close >= HHV(H, 3); 

You may use for loop to store HHV at entry and then exit when level is violated.
Example
http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/

Or use ApplyStop.
Example
http://www.amibroker.com/kb/2014/10/17/using-price-levels-with-applystop-function/

/// Derived from 
/// @link http://www.amibroker.com/kb/2014/10/17/using-price-levels-with-applystop-function/
/// Discussed here
/// @link https://forum.amibroker.com/t/exit-at-hhv-3-from-entry/9422
SetOption( "ActivateStopsImmediately", 1 );

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

Buy = Ref( Cross( MACD(), Signal() ), -TradeDelay);
Sell = 0; 
Short = Cover = 0;

BuyPrice = Open;// if trade delay > 0
SellPrice = Close;

ProfitLevelLong = Ref(HHV(H, 3), -0);
ProfitAmountLong = ProfitLevelLong - BuyPrice;
ProfitAmountLong = Max( TickSize, ProfitAmountLong );

/// "I need exit from position when Close > HHV(High, 3) from entry"
ApplyStop( stopTypeProfit, stopModePoint, ProfitAmountLong, /*exit at trade price*/ exitatstop = 0 );

3 Likes

Thank you very much! It works!

H.