How to identify multiple exit signals and case switch query

Hi all,

I got three exit codes which is included in a for loop as below:

for (t = 1; t < BarCount-1;t++){
	if(t>1){
		If(
		Close[t-1] >EL200[t-1]  
		) {
			Buy[t]=1;
		}              
                
                If(
	        EL10[t-1] < EL20[t-1] AND 
		EL10[t-1]<EL50[T-1] AND
		Close[t] < EL10[t-1]-0.2*ATR_20 [t-1]		
		){
			Short[t]=1;
		}

		If(
		EL20[t-1] < EL10[t-1] AND 
		EL20[t-1]<EL50[T-1] AND
		Close[t] < EL20[t-1]-0.2*ATR_20 [t-1]
		){
			Short[t]=1;
		}
		
		If(		
		EL50[t-1] < EL10[t-1] AND 
		EL50[t-1]<EL20[T-1] AND
		Close[t] < EL50[t-1]-0.2*ATR_20 [t-1]		
		){
			Short[t]=1;
		}

Backtesting results are fine.

According to @tomasz by post Different exits - #3 by Tomasz, I try code --- Sell 1, Sell 2 and Sell 3 out of the for Loop. Below are the modified code:

Buy1 =	Ref(Close,-1) >Ref(EL200,-1)	 ;

Sell1=	Ref(EL10,-1) < Ref(EL20,-1) AND 
		Ref(EL10,-1)< Ref(EL50,-1)  AND
		Close < Ref(EL10,-1)-0.2*Ref(ATR_20,-1)	;
		
Sell2=	Ref(EL20,-1) <Ref(EL10,-1)  AND 
		Ref(EL20,-1) <Ref(EL50,-1)  AND
		Close < Ref(EL20,-1)-0.2*Ref(ATR_20,-1)	;
		
Sell3=	Ref(EL50,-1) < Ref(EL10,-1) AND 
		Ref(EL50,-1) < Ref(EL20,-1) AND
		Close < Ref(EL50,-1)-0.2*Ref(ATR_20,-1)	;
		
Buy = 0;
Sell = 0;

InTrade = 0;

for( i = 0; i < BarCount; i++ )
{
	switch( InTrade )
	{
	case 0: // not in trade
		if( Buy1[ i ] ) 
			InTrade = Buy[ i ] = 1;		
		break;		
	case 1: 
		if( Sell1[ i ] ) 
		{
			Sell[ i ] = InTrade;
			InTrade = 0; 
		}
		break;
	case 2: 
		if( Sell2[ i ] ) 
		{
			Sell[ i ] = InTrade;
			InTrade = 0; 
		}
		break;		
	case 3: 
		if( Sell3[ i ] ) 
		{
			Sell[ i ] = InTrade;
			InTrade = 0; 
		}
		break;
	}
}

Are there any method that can identify if the sell trades are based on Sell1 , Sell2 or Sell3? is CBT required?

By the way, the backtest results of two methods are totally different. Do I code in a wrong way?

Thanks.

Backtest Trade volumes are 30 times different.

With for loop, there are around 1800 trades but for switch cases from case 0 to case 3, only about 60s are processed.

Any idea on the big difference? Thanks.

@Jodie, in your loops you can assign different values to the Sell signals (see this previous post and its associated links).
Search the forum for further usage examples.

FYI,

This one

can be written shorter ->

// Incomplete snippet

Buy1 =	Ref(Close > EL200,-1);

Sell1=	Ref(EL10 < EL20 AND EL10 < EL50,-1) AND
		Close < Ref(EL10-0.2*ATR_20,-1);
      
Sell2=	Ref(EL20 < EL10 AND EL20 < EL50,-1)  AND
		Close < Ref(EL20-0.2*ATR_20,-1);
      
Sell3=	Ref(EL50 < EL10 AND EL50 < EL20,-1) AND
		Close < Ref(EL50-0.2*ATR_20,-1);

You just have to read the code.

The code you posted always exits at Sell1.
Intrade at Buy is 1. So case 1 exit will take place always (in your case).


The first code (looping one) does not have Sell exit but you are using Short variable.


According to this KB article you may do like this

// Incomplete snippet

Buy1 =	Ref(Close > EL200,-1);

Sell1=	Ref(EL10 < EL20 AND EL10 < EL50,-1) AND
		Close < Ref(EL10-0.2*ATR_20,-1);
      
Sell2=	Ref(EL20 < EL10 AND EL20 < EL50,-1)  AND
		Close < Ref(EL20-0.2*ATR_20,-1);
      
Sell3=	Ref(EL50 < EL10 AND EL50 < EL20,-1) AND
		Close < Ref(EL50-0.2*ATR_20,-1);

Sell = Sell1*10 + Sell2*20 + Sell3*40;

thanks @beppe and @fxshrat

read the doc and look forward to the result from below code

Buy1 =	Ref(Close > EL200,-1);

Sell1=	Ref(EL10 < EL20 AND EL10 < EL50,-1) AND
		Close < Ref(EL10-0.2*ATR_20,-1);
      
Sell2=	Ref(EL20 < EL10 AND EL20 < EL50,-1)  AND
		Close < Ref(EL20-0.2*ATR_20,-1);
      
Sell3=	Ref(EL50 < EL10 AND EL50 < EL20,-1) AND
		Close < Ref(EL50-0.2*ATR_20,-1);

Sell = Sell1*10 + Sell2*20 + Sell3*40;

but there is no any results shown ( even I haven't used switch case) while I use for loop, ten thousand trades shown ( all other AFL codes are the same)

Don't understand what is the different between the above code and for loop to perform sell 1?
I guess you mean they will look for Sell 1 first then Sell 2 and then Sell 3. But isn't for loop doing the same??

is there any method I could insert some code in a for loop to indicate which sell strategy is applied ?

thanks.

Here is my code, below one works well. Around 10,000 trades.


SetOption("MaxOpenPositions",100); 
SetBacktestMode(backtestRegular);
TradeDelay = 0;
SetTradeDelays( TradeDelay, TradeDelay, TradeDelay, TradeDelay );
TickSize = 1;


Buy=0;
Short=0;
Cover=0;
Sell=0;

EH10=EMA(High,10);
EL10=EMA(Low,10);
EH20=EMA(High,20);
EL20=EMA(Low,20);
EH50=EMA(High,50);
EL50=EMA(Low,50);
EH80=EMA(High,80);
EL80=EMA(Low,80);
EH100=EMA(High,100);
EL100=EMA(Low,100);
EH200=EMA(High,200);
EL200=EMA(Low,200);
ATR_20=ATR(20);




//Parameter Values
Capital=10000000;

RiskPerShare=4*(EH10-EL10);  //set our stop at the entry bar 
PositionRisk = 0.75; // risk 0.75% of entire equity on single trade
MaxEquity = 5;     //maximum allowed 6% of Equity (i.e. from 0.00% to 5.99%)

// position size calculation
PctSize = Min ( PositionRisk * BuyPrice / RiskPerShare, MaxEquity   );       
SetPositionSize( PctSize, spsPercentOfEquity );


for (t = 1; t < BarCount-1;t++){
	if(t>1){
		
		If(

		Close[t-1] >EL200[t-1] 

		) {
			Buy[t]=1;
		}
		
				
		If(
		
		EL10[t-1] < EL20[t-1] AND 
		EL10[t-1]<EL50[T-1] AND
		Close[t] < EL10[t-1]-0.2*ATR_20 [t-1]		
		){
			Short[t]=1;
		}
		

		If(
		
		EL20[t-1] < EL10[t-1] AND 
		EL20[t-1]<EL50[T-1] AND
		Close[t] < EL20[t-1]-0.2*ATR_20 [t-1]
		){
			Short[t]=1;
		}
		
		
		If(
		
		EL50[t-1] < EL10[t-1] AND 
		EL50[t-1]<EL20[T-1] AND
		Close[t] < EL50[t-1]-0.2*ATR_20 [t-1]		
		){
			Short[t]=1;
		}

	}
}

Sell=Short;
Cover=Buy;

And another one didn't work. Empty results.


SetOption("MaxOpenPositions",100); 
SetBacktestMode(backtestRegular);
TradeDelay = 0;
SetTradeDelays( TradeDelay, TradeDelay, TradeDelay, TradeDelay );
TickSize = 1;


Buy=0;
Short=0;
Cover=0;
Sell=0;

EH10=EMA(High,10);
EL10=EMA(Low,10);
EH20=EMA(High,20);
EL20=EMA(Low,20);
EH50=EMA(High,50);
EL50=EMA(Low,50);
EH80=EMA(High,80);
EL80=EMA(Low,80);
EH100=EMA(High,100);
EL100=EMA(Low,100);
EH200=EMA(High,200);
EL200=EMA(Low,200);
ATR_20=ATR(20);




//Parameter Values
Capital=10000000;

RiskPerShare=4*(EH10-EL10);  //set our stop at the entry bar 
PositionRisk = 0.75; // risk 0.75% of entire equity on single trade
MaxEquity = 5;     //maximum allowed 6% of Equity (i.e. from 0.00% to 5.99%)

// position size calculation
PctSize = Min ( PositionRisk * BuyPrice / RiskPerShare, MaxEquity   );       
SetPositionSize( PctSize, spsPercentOfEquity );


Buy1 =	Ref(Close,-1) >Ref(EL200,-1)	 ;

Sell1=	Ref(EL10,-1) < Ref(EL20,-1) AND 
		Ref(EL10,-1)< Ref(EL50,-1)  AND
		Close < Ref(EL10,-1)-0.2*Ref(ATR_20,-1)	;
		
Sell2=	Ref(EL20,-1) <Ref(EL10,-1)  AND 
		Ref(EL20,-1) <Ref(EL50,-1)  AND
		Close < Ref(EL20,-1)-0.2*Ref(ATR_20,-1)	;
		
Sell3=	Ref(EL50,-1) < Ref(EL10,-1) AND 
		Ref(EL50,-1) < Ref(EL20,-1) AND
		Close < Ref(EL50,-1)-0.2*Ref(ATR_20,-1)	;
Sell = Sell1*10 + Sell2*20 + Sell3*30;
Sell=Short;
Cover=Buy;


As mentioned all other factors are the same.

Because you posted INCOMPLETE CODE with missing variables in post #1!

Fact remains. Ref() with same delay can be combined to single use of Ref().


To identify all different exit possibilities it rather should be:

In your one (if using sell3*30) if sell1 and sell2 are true then they are 30 so it looks like as if 30 was true but wasn't. So that's why sell3*40. Also see here.


You get different results in second one because you did this:

But in second one Short = 0; and no defined Buy signal (other than Buy = 0;).

So simply comment/remove

//Sell=Short;
//Cover=Buy;

And add Buy = buy1;

yes got it to 40 (sorry)and results show but the problem is the securities since I first buy, they never get sell....

Buy1 =	Ref(Close,-1) >Ref(EL200,-1)	 ;

Sell1=	Ref(EL10,-1) < Ref(EL20,-1) AND 
		Ref(EL10,-1)< Ref(EL50,-1)  AND
		Close < Ref(EL10,-1)-0.2*Ref(ATR_20,-1)	;
		
Sell2=	Ref(EL20,-1) <Ref(EL10,-1)  AND 
		Ref(EL20,-1) <Ref(EL50,-1)  AND
		Close < Ref(EL20,-1)-0.2*Ref(ATR_20,-1)	;
		
Sell3=	Ref(EL50,-1) < Ref(EL10,-1) AND 
		Ref(EL50,-1) < Ref(EL20,-1) AND
		Close < Ref(EL50,-1)-0.2*Ref(ATR_20,-1)	;
Buy=Buy1;
Sell = Sell1*10 + Sell2*20 + Sell3*40;

31

Enable detailed log if you get empty result list.
31

BTW this one
RiskPerShare=4*(EH10-EL10); //set our stop at the entry bar
may become future leak (see EH10 and EL10 and possible trade delays).
And why setting ticksize if not trading futures?
Max. open pos. 100?

Thanks @fxshrat
work perfectly and same result with for loop setting!!!!

Regards to future leak, first time heard the term and my very basic idea it means my trading setup is unrealistic. let me research this topic further on this.

Will keep you updated great thanks for everyone.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.