Easiest way to lock Buyprice

This is a very common problem that I have when writting the code for a system.
I'm looking for the easiest way to lock BuyPrice so it doesn't change when a new Buy signal appears before selling:

//ENTRY
Buystop = Ref(H,-1);
Buy = H > BuyStop;
BuyPrice = BuyStop;

//EXIT
Sell = Open > ValueWhen(Buy,BuyStop);
SellPrice = Open;

This code sells in the first profitable opening but it fails if a new buy signal comes when still in the trade, because it updates BuyPrice, which should be locked to the initial entry price. Do I need a loop in all bars to lock BuyPrice or is there an easiest way to do it? Thanks

1 Like

Yes, loop or ApplyStop() function for stop loss/target exits.
Search through forum and knowledgebase articles. This has been covered multiple times already.

1 Like

Thank you for your answer. I have searched in the forum and found some ideas but I canot make it work in all circumstances. For example:

//ENTRY//
Buy = setupa =  Sum( H < Ref(H,-1) AND L < Ref(L,-1),3) == 3;

//FIRST PROFITABLE OPENING EXIT
priceatbuy = 0; 

for( i = 0; i < BarCount; i++ ) 
{ 
    if( priceatbuy == 0 && Buy[ i ] ) //PRIMERA COMPRA
		priceatbuy = BuyPrice[ i ]; //PRECIO DE COMPRA
	
	if( priceatbuy > 0 && Open[ i ] > priceatbuy) //SI COMPRADOS Y FPO
    { 
       Sell[ i ] = 1; 
       SellPrice[ i ] = Open[i]; 
       priceatbuy = 0; 
    } 
    else 
		Sell[ i ] = 0; 
} 

//STOP LOSS// 
stop = Nz( 4* ATR(10) );   
ApplyStop(stopTypeLoss,stopModePoint,stop,1,0); 
Equity(1,0);

This code Buys after 3 bars with lower lows and lower highs. And tries to sell in the first profitable opening or stop loss. It is not working. I see that ApplyStop is modifying the sells but it doesnt work without it either. Can you help?c31

You should always quote the source, assuming you are using this KB
http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/comment-page-1/

Why are you using both Loop and ApplyStop() at the same time?

The code is a modified version of: https://www.amibroker.com/guide/h_backtest.html

I am using ApplyStop because I though it was an easy way to place the stop loss logic, but I wasnt expecting it to interfere with the sell logic.

1 Like

I have fixed the code and included a stop loss in the loop, as suggested. Now works fine. Nstop is the number of standard deviations.

//STANDARD EXPONENCIAL  DEVIATION  
desv_carver = sqrt( EMA( ( C-Ref(C,-1) )^2, 36 ) ) ;    

//ENTRY//
Buy = setupa =  Sum( H < Ref(H,-1) AND L < Ref(L,-1),3) == 3;

//FIRST PROFITABLE OPENING EXIT
priceatbuy = 0; bi = BarIndex();

for( i = 0; i < BarCount; i++ ) 
{ 
    if( priceatbuy == 0 && Buy[ i ] ) //FIRST BUY PER TRADE
	{
		priceatbuy = BuyPrice[ i ]; 
		stopprice = priceatbuy - nstop * desv_carver[ i ];	//STOP LOSS PRICE
		EntryBar = bi[ i ];	
	}
	if( priceatbuy > 0 && bi[ i ] > EntryBar && Open[ i ] > priceatbuy) //ALREADY BOUGHT AND FPO
    { 
       Sell[ i ] = 1; 
       SellPrice[ i ] = Open[i]; 
       priceatbuy = 0; 
    } 
    else if( priceatbuy > 0 && Low[ i ] < stopprice ) //IF STOP LOSS
    { 
       Sell[ i ] = 1; 
       Buy [ i ] = 0;  
       SellPrice[ i ] = stopprice; 
       priceatbuy = 0; 
    }     
    else 
		Sell[ i ] = 0; 
}

c34

1 Like

Hi Amrin,

Since you have find the solution would request if you can help to to set the first exit profit & trail Stop loss in % against the buy price.

I tired lots of combination without any succes

[Need help in Trailing Stop Loss]

I have already requested the help on same under my thread.

Thanks & Regards

Deepak

Sorry to say @deepuanant,

You are not thorougly using the search option of this forum. Even if you are searching, you are not making any effort to solve your own problem. Even if you try, you are still expecting others to cook the soup for you.

Before you gave up, did you try Example # 1 and 2 from this post?


And Example # 4 from this post:

Please try those, then share your efforts (in form of codes) showing precisely what did not work! You will surely get solution.

Thank you

@Cougar Thanks for sharing Example # 1 and 2 and your valuable suggestion will surely try to understand.

Hi Armin - did you manage to calculate the EWMA as described in his book. If yes, can i request you to share the code for this.

Again and again, don't reinvent the wheel. Trailing stop is SINGLE LINER and does not really need anything but just

// 10% trailing stop
ApplyStop( stopTypeTrailing, stopModePercent, 10, True );

You don't need to worry about BuyPrice as it is all handled automatically for you.

1 Like