How to take profit on Heiken Ashi bars

I would like to implement take profit when using Heiken Ashi-series. I have tryed with applyStop but not able to implement on a Heiken Ashi series. I've also looked into selling depending last buy signal, with no luck. Any suggestions?

First step is to find a code which plots H-A as a line indicator overlying the normal price chart. There is one such code in the AB library I think. Then use the bar replay to see how it works in real time.

I have implemented the HA-bars:

/* Heikin Ashi */
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 ) ); 
barcolor = IIf(HaClose >= HaOpen,colorGreen,colorRed);
isHaBarBullish = HaClose > HaOpen;
isHaBarBearish = HaClose < HaOpen;

I would like to take profit automaticly when profit is above 9% and HA bar is bearish.

I find it hard to explain, but the AB backtester uses real prices rather than the H-A candles. Hopefully someone will post the code I'm referring to (I tried looking but couldn't find it). You need your H-A code converted to an indicator which plots bar-by-bar on a real price chart. Then you will find backtesting possible.

I have figured out how to plot the HA bars:

PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "", barcolor, styleCandle );

But how do I calculate the profit to above 9% ?

@arcone I don’t have a specific answer for you but suggest you review the resources concerning Heiken Ashi bars in this previous thread.

As I mentioned earlyer, the HA bars are under control. What I need help with is how to calculate the current profit after a buy signal?

Here some code to state my issue:

isHaBarBearish = HaClose < HaOpen;
stopProfit = ? //(calculate profit from last buy)
Sell = isHaBarBearish AND stopProfit

For proper (profit/loss) stops you either have to use ApplyStop function or have to create looping code.

Lots of examples in AmiBroker KB and this forum already.
https://www.amibroker.com/kb/index.php?s=Applystop
https://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/

No one can see what you did and tried and what you try to achieve specifically.
You should apply entry/exit at real prices of symbol.

Look where real price (white bars) is located compared to artificial price (HA).
8

1 Like

Tryed with apply stop (as shown below) but after the stop is triggerend I'm not able to reenter with a new buy signals:

shouldTakeProfit = InTrade AND isHaBarBearish;
IIf(shouldTakeProfit, stopModePercent, stopModeDisable); 
ApplyStop( stopTypeProfit, stopModePercent, stopProfitPercent,  ExitAtStop = 0 );

If this would have worked I would be fine, but it seems that we are not able to flip the stopMode:

shouldTakeProfit = InTrade AND isHaBarBearish;
stopMode = IIf(shouldTakeProfit, stopModePercent, stopModeDisable); 
ApplyStop( stopTypeProfit, stopMode, stopProfitPercent,  ExitAtStop = 0 );

If you look for help then you need to post complete reproducible code but not just code fractions.


That being said...

... You are applying IIf function incorrectly.
Must read here

Then please re-read my previous post and read all KB articles (and code examples there) about ApplyStop as well as read function reference of ApplyStop in AmiBroker help carefully and what each function argument is supposed to be. First two arguments expect type number but not array otherwise you will get Error 4 (incorrect argument type).

If you want to enable percent profit in bearish condition only then modify 3rd argument instead ->

percent_amount = 2;
stopProfitPercent = Iif(shouldTakeProfit, percent_amount, Null);

Third argument of ApplyStop allows array or number as input.


Last but not least...

2 Likes

I re-read your previous post and now I understand :slight_smile: Thanks for pointing out a solution:

/* STOP PROFIT */
stopProfitPercent = 8;

stopProfitPrice = 0;
stopProfitToPlot = Null;
for( i = 1; i < BarCount; i++ )
{	
	if(Buy[i] AND stopProfitPrice == 0) {
		stopProfitPrice = Close[i] * (1+(stopProfitPercent/100));
	}
	if(stopProfitPrice > 0) {
		stopProfitToPlot[i] = stopProfitPrice;
		if(Close[i] > stopProfitPrice AND isHaBarBearish[i]) {
			Sell[i] = 1;
		}
	}	
	if(Sell[i]) {
		stopProfitPrice = 0;
	}
}
Plot( stopProfitToPlot, "Stop profit line", colorBrightGreen );