Different exits

I would like to have different exits depending on the entry in the same Code. So for a Position that was entered on HHV(c,50) I want to Exit at LLV(c,50). But a Position that was entered on HHV(c,30) I want to Exit at LLV(c,30). How can I do that using the standard Applystop function!

2 Likes

@gewalek I don’t know if that is possible without using the Custom Backtest Interface. Have you thought your system through? Will you allow different signals to trigger multiple trades on the same symbol? If so then you will need to be Scale trading, in and out. Do you have that coded into your strategy?

If you are running this on a portfolio then you will have multiple positions with a variety of entry triggers and exit conditions. Keeping track of all of that is beyond my coding knowledge and since no one else has answered your post in 3 days, it does not seem likely. You can look over an example of identifying the different entries and how to explore for them at this knowledge base article,
http://www.amibroker.com/kb/2014/09/24/how-to-identify-which-signal-triggers/

You could do that without ApplyStop and without CBT. See example below:

TriggerPrice1 = Ref( HHV( High, 50 ), -1 );
TriggerPrice2 = Ref( HHV( High, 30 ), -1 );

Buy1 = High > TriggerPrice1;
Buy2 = High > TriggerPrice2;

ExitPrice1 = Ref( LLV( Low, 50 ), -1 );
ExitPrice2 = Ref( LLV( Low, 30 ), -1 );

Sell1 = Low < ExitPrice1;
Sell2 = Low < ExitPrice2;

InTrade = 0;

Buy = 0;
Sell = 0;

for( i = 0; i < BarCount; i++ )
{
	switch( InTrade )
	{
	case 0: // not in trade
		if( Buy1[ i ] ) 
			InTrade = Buy[ i ] = 1;
		else
		if( Buy2[ i ] )
			InTrade = Buy[ i ] = 2;;
		
		break;
		
	case 1: // in trade mode 1 - HHV 50 entry
		if( Sell1[ i ] ) 
		{
			Sell[ i ] = InTrade;
			InTrade = 0; 
		}
		break;

	case 2: // in trade mode 2 - HHV 30 entry
		if( Sell2[ i ] ) 
		{
			Sell[ i ] = InTrade;
			InTrade = 0; 
		}
		break;
	}
}
23 Likes

Hi Tomaz
i want to sell1 when i buy by buy1 method
and sell2 when i buy by buy2 method

when i do the above , the exit comes whichever comes first
please guide

Mmmm... @aknagdev, I'm going to tell you two important secrets :wink::

  1. Tomasz hates SMS style questions (sorry, by your post even seems like a tongue-twister for me).

  2. Tomasz loves people showing the code they say that is not working. This helps to see if there are bugs in the code, for example...

Happy day :smile:

1 Like

For that you need to:

  1. track your which "system" generated trade in the code
  2. specify which signal takes precedence if both occur same bar

And actually the code I have given ABOVE is already the solution.

1 Like

ok thanks will give the code

Hi
in the below system there are 3 methods by which i will buy and sell
what i need is when i buy with Method 1 then my sell should be of Method 1 only
I dont want method 2 sell to trigger when i have bought by method1
the code is

_SECTION_BEGIN("Price1"); 
SetChartOptions(0,chartShowArrows|chartShowDates); 
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) )); 
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );  
_SECTION_END(); 
 
 
_SECTION_BEGIN("MA"); 
//buy1 
ma1=MA(C,50); 
Buy1 = Cross(C,ma1); // buy1 condition
buylimitprice1=ValueWhen(Ref(Buy1,-1),O);  // buyprice 
sllong1=buylimitprice1-(buylimitprice1*0.0050); // stop loss 
sell1=Cross(ma1,c) OR Cross(sllong1,c); // sell on crossover or on stoploss
 
//buy2 
ma2=MA(C,100); 
Buy2 = Cross(C,ma2);// buy2 condition 
buylimitprice2=ValueWhen(Ref(Buy2,-1),O); // buyprice 
sllong2=buylimitprice2-(buylimitprice2*0.0050);  //stoploss
sell2=Cross(ma2,c) OR Cross(sllong2,C);//sell on crossover or on stoploss


//buy3
ma3=MA(C,200);
Buy3 =Cross(C,ma3); // buy3 condition
buylimitprice3=ValueWhen(Ref(Buy3,-1),O); //buyprice
sllong3=buylimitprice3-(buylimitprice3*0.0050); //stoploss
sell3=Cross(ma3,C) OR Cross(sllong3,C); //sell on crossover or on stoploss
 
//short1 
 
short1=cross(ma3,C); //short
shortlimitprice1=ValueWhen(Ref(Short1,-1),O); // short price
slshort1=shortlimitprice1+(shortlimitprice1*0.005); //stoploss
Cover1=Cross(C,ma3) OR Cross(C,slshort1); //cover

// 3 buy conditions if i buy by method 1 then i need to exit by method 1 sell only 
// if i buy by method2 then sell by method 2 only 
 
InTrade = 0; 
Buy = 0; 
Sell = 0; 
Short=0; 
Cover=0; 
 
for( i = 0; i < BarCount; i++) 
{ 
	switch( InTrade ) 
	{ 
	case 0: // not in trade 
		if( Buy1[ i ] )  
			InTrade = Buy[ i ] = 1; 
		else 
		 
		if( Buy2[ i ] ) 
			InTrade = Buy[ i ] = 2; 
		else	 
		 
		if( Buy3[ i ] ) 
			InTrade = Buy[ i ] = 3; 
	 
		else 
			if( short1[ i ] ) 
			InTrade = short[ i ] = 4;; 
				 
		break; 
		 
	case 1: // original 
		if( Sell1[ i ] )  
		{ 
			Sell[ i ] = InTrade; 
			InTrade = 0;  
		} 
		break; 
 
	case 2: // long sl 
		if( Sell2[ i ] )  
		{ 
			Sell[ i ] = InTrade; 
			InTrade = 0;  
		} 
		break; 
		 
	case 3: // long short sl 
		if( Sell3[ i ] )  
		{ 
			Sell[ i ] = InTrade; 
			InTrade = 0;  
		} 
		break; 
		 
	case 4: // short 
		if( cover1[ i ] )  
		{ 
			Cover[ i ] = InTrade; 
			InTrade = 0;  
		} 
		break; 
	} 
} 
 
 _SECTION_END(); 
 
 // 
 PositionSize = MarginDeposit = 1; 
	 
 

Thanks

1 Like

The solution has already been given ABOVE. The solutions being given are general purpose. If you copy-pasted and then adjusted/changed the original code given in this thread and it does not work after modifications to your expectations you should check your changes yourself: How do I debug my formula? as it is absolutely crucial for YOU to understand how your code works if you want to trade actual money with it. Nothing can replace eye-opening experience of debugging your code by yourself.

2 Likes

got it
thanks Tomasz

Hi all,
I have a doubt that , Is it possible to use applystop for a particular strategy in multiple different exit strategy as i am implementing in one strategy it is applying in all strategy. ?

I was looking for something like this and it works great. Brilliant. Thanks.

The only thing I cannot explain is on the trade list, when some of the trades are closed they are labeled as (max loss). I tried the code in the example. I have no stops marked in the backtester.