Sell price = Buying Price * x%

post your last code with RangeType parameter

Knowing that Equity() may change Sell and Cover, should you place it before or after ApplyStop ?

I do not know how to use RangeType parameter.

Yes Equity() should be placed after ApplyStop. I corrected that but still no results.

Didi you read the help of Equity() function ?

Dear @Tomasz

Please help how to code a Sell that meet my sell criteria but also profit? the sell array below is not working

Buy = Cross(MACD(),Signal());
Sell = Cross(Signal(),MACD()) AND Sellprice>Buyprice;

Thank you

If you search the forum you will get plenty of examples and explanations of a better function, ApplyStop()

Since you have tagged TJ, see one of his posts,

Thank you @travick for your solution, but i think applystop can't answer my problem because applystop will exit trade when the stop price are met which not considering my other sell criteria. I need is to exit when Cross(Signal(),MACD()) and also when the price is higher than my buyprice (my code: sellprice>buyprice). Otherwise the sell shouldn't happen.

Kindly need help how to code this right.

Thank you.

Did you search the forum like @travick suggested?

What price? The Close price???

Post up your full code so we are not playing guessing games.

Dear @TrendSurfer, yes i did look the forum as @travick suggested, the forum did explains regarding exit trade with applystop, but what i need is how to exit when macd is crossing down and also when the sell price is higher than my buy price .

Below is my code:

//trading instructions
stop= ApplyStop( stopTypeProfit, stopModePercent, 5 );

Buy = Cross( MACD(), Signal() );

Sell = Cross( Signal(), MACD() ); // AND Sellprice>Buyprice; (i take this out because this if i put it then there will be no sell at all # i dont know why);

BuyPrice = SellPrice = Close;

//plot shapes
PlotShapes( IIf( Buy, shapeUpArrow , shapeNone ), colorBlue, 0, L * 0.95, 0, 0 );
PlotShapes( IIf( Sell, shapedownArrow, shapeNone ), colorRed, 0, H * 1.05, 0, 0 );
for( i = 0; i < BarCount; i++ )
    if( Buy[i] ) PlotText( "Buy:\n" + C[ i ], i, L[ i ] * 0.9, colorBlue, colorWhite );
    if( Sell[i] ) PlotText( "Sell:\n" + C[ i ], i, H[ i ] * 1.1, colorRed, colorYellow );



Thank you

Don't use BuyPrice and SellPrice like that.

Try this:

Sell = Cross( Signal(), MACD() ) AND Close > ValueWhen(Buy, Close);

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

Dear @TrendSurfer,

I've adjusted my code as you suggested:

Buy = Cross( MACD(), Signal() );
BuyPrice = Close;
Sell = Cross( Signal(), MACD() ) AND Close > ValueWhen(Buy, Close);
SellPrice = Close;
Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

and it still resulting some loss trades:

Using Valuewhen is wrong because in between Buy and actual Sell there may appear several Buy signals. You have to use looping. It has been shown in pictures below already.

Here is another picture using code of this thread

Just plot ValueWhen and you will see

Plot( ValueWhen(Buy, Close), "ValueWhen(Buy,Close)", colorGreen );

Wrong. There are codes using ApplyStop as well as codes using Loop (e.g. here and several more). Also AmiBroker knowledgebase has examples showing using ApplyStop and looping to apply stops (and using loop to store price at Buy signal).

function BuyExitInProfitOnly( LongEntry, LongExit, percent ) {
	/// code source:
	/// @link
	/// derived from:
	/// @link
	/// (commercial use prohibited!)
	global Buy, Sell, BuyPrice, SellPrice;
	local i, buyentryprice, BuyPrice_array, SellSignal;
	buyentryprice = 0; 
	BuyPrice_array = Null;
	pcnt_level = 1 + percent/100;
	for ( i = 0; i < BarCount; i++ ) {	
		if ( LongEntry[ i ] && buyentryprice == 0 ) {
			Buy[ i ] = 1;
			BuyPrice[i] = Close[i];
			buyentryprice = BuyPrice[ i ] * pcnt_level;
		// exit if close is higher than buyprice and if sell signal
		SellSignal = Close[ i ] > buyentryprice AND LongExit[i];	
		if ( SellSignal && buyentryprice > 0 ) {
			Sell[ i ] = 1;   
			SellPrice[i] = Close[i]; 
			buyentryprice = 0;
		if ( buyentryprice > 0 )
			BuyPrice_array[i] = buyentryprice;	
	return BuyPrice_array;

Buy = Sell = Short = Cover = 0;

LongEntry = Cross( MACD(), Signal() );
LongExit = Cross( Signal(), MACD() );

pab = BuyExitInProfitOnly( LongEntry, LongExit, percent = 0 );

Plot( C, "Price", colorDefault, styleCandle );
Plot( pab, "pab", colorgreen );
PlotShapes( Buy * shapeUpArrow + Sell * shapeDownArrow, IIf( Buy, colorGreen, colorRed ), layer = 0, y = IIf( Buy, L, H ));

_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} - {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%), Vol %g {{VALUES}}",
                           O, H, L, C, SelectedValue( ROC( C, 1 ) ), V ) );                      

So instead of using ValueWhen we get


Yes of course - 'ExRem' is kicking in after the fact.

Thanks for the correction and the great explanation @fxshrat. :+1:

This is awesome, really appreciated the effort to help. Problem is solved... Thank you @fxshrat @TrendSurfer also @travick