Locking profits

Have been working on a trailing stop code to lock the profits but hv not been able get the desired resuts, 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]); 
	 
	 
	 
	} 
	 
} 

What I believe you are looking for has been discussed on another thread recently on this forum. The latest upgrade to AmiBroker has a new "ActivationFloor" parameter that serves to activate your stop once a certain profit level is attained.

An read here,

And refresh your memory on what the ApplyStop does,

http://www.amibroker.com/guide/afl/applystop.html

1 Like

Hi,

  1. How to set sl to Break-Even + $x?
    Lets say that I trade CL and have long for $56 and when it reach $56.50 I want to move my SL to $56.10.
    It doesn't work with stopTypeLoss and when I change it to stopTypeProfit it close trades on Open price of next candle which met +$0.50.
  2. Also how to set couple SL rules, lets say I have same position and want to set 1st sl on (buyprice - $1) and then want to have this BE +$x triggered after $0.50.
    I have tried different options, but nothing is working.
    Do anyone know how to solve this?

@likhazar, if you still have not read this past post from @Tomasz, I suggest you do it now.

It provides useful additional information about stops and links to some code examples that you can use as a starting point to develop your formula.

3 Likes

Thank you very much. I didn't find it earlier. Hope it help to solve my problem :slight_smile: