How to Calculate the Number of Bars from Buy / Short signal

I need to calculate the no. of bars from the buy or short signal buy0 and short0 are my systems signals and the counter should turn zero when the sell0 and cover0 signals are generated. I have written the below code but it has 2 issues -

  1. the counter starts with 2 instead of 1,

  2. as the cursor moves forward the signals change.

Can anyone pls help to solve the above issues.

Thanks & Rgds,
Sovit Manjani

Buycounter = 0; 
 
for(i=20; i<BarCount; i++) 
{ 
 
if(Buy0[i] AND Buycounter ==0 ) Buycounter = 1; 
if(Buycounter >=1) Buycounter++; 
if(Sell0[i] AND BuyCounter !=0) Buycounter = 0; 
 
} 
 
Shortcounter = 0; 
 
for(i=0; i<BarCount; i++) 
{ 
if(Short0[i] AND Shortcounter ==0 ) Shortcounter = 1; 
if(Shortcounter >=1) Shortcounter++; 
if(Cover0[i] AND ShortCounter !=1) Shortcounter = 0; 
 
}
1 Like

There’s an inbuilt function BarsSince that counts the number of bars from the last time any array was True.

To avoid the count restarting if you have intervening Buy0 signals between the initial Buy0 and Sell0 (which may be what you’re trying to overcome in the loop), you can simply eliminate the extraneous signals with ExRem.

Buy0 = ExRem(Buy0, Sell0);
BarsSinceBuy0 = BarsSince(Buy0);

https://www.amibroker.com/guide/afl/barssince.html
https://www.amibroker.com/guide/afl/exrem.html

As an aside, the reason your BuyCounter is starting at 2 is you immediately add 1 to it on the very next line after it’s initially set to 1 (perhaps it should initially be 0). You’re also saving BuyCounter as a scalar rather than an array which may be why you are seeing strange results, although it’s hard to tell without seeing the rest of the code.

2 Likes

@sovit, rather use ApplyStop for available proper n-bar stop instead of as suggested BarsSince().
https://www.amibroker.com/guide/afl/applystop.html
Using BarsSince is rather problematic if looking for regular n-bar stop. (It’s problematic because newbies often wonder why it isn’t doing what was expected if being used as nbar stop).

numbars = 50; // or you may use: IIf( Buy, 50, /*if Short then*/ 40 ); // number of bars till exit

ApplyStop( stopTypeNBar, stopModeBars, numbars, 1 );

I have used Barsince but the issue with it is that it calculates the no. of bars from each bar that fulfils the buy0 condition

I suppose u did not get me, I want to know the no. of bars from the buy / short signals so that I can put that no.as range to get the hhv / llv from the buy / short signal. Pls help if possible

Hi,

and how about LowestLowSinceBuy = LowestSince(Buy0, L, 1) for your MAE/MFE calculation?

Best regards

Buy = MACD()>0;
Sell = MACD()<0;

sellcontinue = Flip(sell,buy);

Plot(IIf(sellcontinue,0,BarsSince(sell)),"Count",colorGreen);

This should work which counts the barssince buy and resets to 0 on every sell signal.

5 Likes

I have a profitable strategy but the issue with it is it leaves a lot of earned profits, I need support to write a code that if a threshold percent profit (say 10%) is hit then exit if a certain percent (say 40%) of the maximum potential profit (buy price - the highest high after the buy signal) is lost.
I have tried Applystop, barsince, highestsince, looping but was unable to get the desired result. Can anyone guide in this matter.

Dear Rajendran,
Can u help me with the below mentioned issue:
I have a profitable strategy but the issue with it is it leaves a lot of earned profits, I need support to write a code that if a threshold percent profit (say 10%) is hit then exit if a certain percent (say 40%) of the maximum potential profit (buy price - the highest high after the buy signal) is lost.
I have tried Applystop, barsince, highestsince, looping but was unable to get the desired result.

BarsSince(Buy) is the right answer as long as your buy signals are not repetitive .

So you can use it if your Buy is in SIGNAL form (such as returned by Cross() function)

Buy = Cross(Close, MA( C, 10 )); // SIGNAL form can be used with BarsSince

but you can not use it if your Buy signal is in STATE form like this:

Buy  = Close > MA( C, 10 ); // STATE form can not be used with BarsSInce

In state form you need to keep track of when you are “in-trade”.
In simple cases one can do this with Flip() and/or ExRem(), but a general-purpose solution is presented in the Knowledge Base
http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/

7 Likes

My signals are very repetitive, they may repeat almost at 50% of bars

1 Like

My signals are very repetitive, they may repeat almost at 50% of bars, so what is the solution for In State form

The solution is re-reading my previous reply. All required info is there.

Have been working on the above as per your recommendation but hv not been able to crack the problem, below is the code I have written, pls identify my mistake.
The problem is that TSL is giving 0 value and TSLLDIFFARR is giving {Empty} value.


TSLMLong = Param("Initial Long SL %", 3.0,4,15,0.50);//12, 12.50 
TSLMShort = Param("Initial Short SL %", 3.0, 6, 13,0.50);//9.0, 9.50 
TSLLDIFF = Param("Trailing SL Long % Diff", 10,10,80,5);//85,90,100 
TSLSDIFF = Param("Trailing SL Short % Diff", 10,10,80,5);//75,80,85,90,95,100 

BuyFlag = ShortFlag= 0; 
TSL=TSLS=0; 
TSLLDIFFARR = TSLSDIFFARR = Null; 
TSLSDIFFARR = 100000; 


for(i=0; i<BarCount; i++) 
{ 
	if(buy0[i] AND BuyFlag ==0 ) 
	{ 
	Buy[i] = 1; 
	BuyFlag = 1;
	HHVSinceBuy = Null; 
	HHVSinceBuy = HighestSince(Buy, H, 1);
	TSL = (HHVSinceBuy[i] - (((HHVSinceBuy[i] - BuyPrice[i]) * TSLLDIFF)/100)); 
	if(Buy0[i]) BuyPrice[i]= Open[i]; 
	printf(" HHVSinceBuy = "+ HHVSinceBuy ); 
	printf(" TSL = "+ TSL ); 
	printf(" BuyPrice = "+ BuyPrice[i] ); 
	
	} 
	
	 
	if(Buyflag AND (HHVSinceBuy[i] > (BuyPrice[i] + (BuyPrice [i] * TSLMLong / 100)))) 
	{ 
	TSLLDIFFARR[i] = Max(TSLLDIFFARR[i-1], (HHVSinceBuy[i] - (((HHVSinceBuy[i] - BuyPrice[i]) * TSLLDIFF)/100))); 
	printf(" TSLLDIFFARR = "+ TSLLDIFFARR[i]); 
	} 
	 
	if((Sell0[i] OR L[i]<TSLLDIFFARR[i] ) 
		AND buyFlag ==1) 
		 
	{ 
	Sell[i]=1;  
	BuyFlag=0; 
	 
	if(sell0[i]) SellPrice[i] = Open[i]; 
	else if (L[i]<TSLLDIFFARR[i]) SellPrice[i] = TSLLDIFFARR[i]; 
	 
	} 
	 
	if(short0[i] AND ShortFlag ==0 ) 
	{ 
	Short[i] = 1; 
	ShortFlag = 1; 
	LLVSinceShort = LowestSince(Short[i], L, 1);
	if(Short0[i]) ShortPrice[i]= Open[i]; 
	 
 
	 
	TSLS = (LLVSinceShort[i] + (((ShortPrice[i] - LLVSinceShort[i]) * TSLSDIFF)/100)); 
	 
	 
	} 
	 
	if(ShortFlag AND (LLVSinceShort[i] < (ShortPrice[i] - (ShortPrice[i] * TSLMShort /100)))) 
	{ 
	TSLSDIFFARR[i] = Min(TSLSDIFFARR[i-1], (LLVSinceShort[i] + (((ShortPrice[i] - LLVSinceShort[i]) * TSLSDIFF)/100))); 
	 
	} 
	if ((Cover0[i] OR H[i] >TSLSDIFFARR[i]  )  
		AND ShortFlag == 1) 
	{ 
	Cover [i] =1; 
	ShortFlag = 0; 
	if(Cover0[i]) CoverPrice[i] = Open[i]; 
	else if (H[i] > TSLSDIFFARR[i]) (CoverPrice[i] = TSLSDIFFARR[i]); 
	 
	 
	 
	} 
	 
}