Sell price = Buying Price * x%

I want to put a condition in back test such that, when stock price decreases by x% from buy price, then sell the stock.

Buy = Cross( MACD(), Signal() );
Sell = IFF(close< BuyPrice * 0.95, sell_stock, hold); /* how do I code this properly */

Can you pls provide some directions or help.
Thank you.

1 Like

Check out the ApplyStop function.

Matt

I did look at the formula:
ApplyStop( type, mode, amount, exitatstop, volatile = False, ReEntryDelay = 0, ValidFrom = 0, ValidTo = -1 )

In above case it will be:
ApplyStop(0,1,0.15,0,FALSE,0,0,-1)

I tried this but it did not work. Can you refer to more concrete example where Applystop is implemented?

Thank you.

If you want to exit 5% below your entry price, then the third parameter should be set to 5, not 0.15.

Below is my updated code:

Buy = Cross( MACD(), Signal() );
EntryPrice = ValueWhen(Buy,BuyPrice);
Price_Ratio = (Close/ BuyPrice);
Sell= (Price_Ratio <0.95) OR (Price_Ratio >1.5);
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

When I look at the actual trades, Amibroker still takes the entryprice as the price when last Buy condition is satisfied.

In below example , when sale on 22nd June is executed, it does not take into account the buyprice on 28th Feb. How do I amend my code so that still reference to buy price is to the price when the buy was executed?

Ticker Date/Time Buy Sell BuyPrice Close EntryPrice PriceRatio SellingPrice
HBO 2/28/2007 1.00 0.00 16.06 16.06 16.06 1.00 16.06
HBO 6/22/2007 0.00 1.00 16.87 16.87 17.76 0.95 16.87
HBO 7/9/2007 1.00 0.00 17.47 17.47 17.47 1.00 17.47
HBO 1/23/2008 0.00 1.00 18.99 18.99 20.17 0.94 18.99
HBO 5/30/2008 1.00 0.00 19.98 19.98 19.98 1.00 19.98

Shouldn’t this:
Price_Ratio = (Close/ BuyPrice);

Be this:
Price_Ratio = (Close/ EntryPrice);

You could eliminate the Sell rule entirely if you’d just use ApplyStop. Is there a reason that you’re avoiding the obvious solution?

2 Likes

That was a typo, while pasting here.

If I apply ApplyStop without Sell condition, Amibroker’s back test wont run. It requires both buy and sell conditions.

From my internet research shows that having constant BuyPrice until sell is triggered has no proper solution other than for loops across bars. I appreciate if you can share any solution that you have.

Listen to @mradtke . He is 100% right. You should be using ApplyStop. With eliminating Sell he meant using:

Sell = 0; // that what @mradtke meant  - regular exit being 0 or False means 
// that you exit only by ApplyStop
ApplyStop( stopTypeLoss, stopModePercent, 5 ); // 5% max loss from buy price
ApplyStop( stopTypeProfit, stopModePercent, 50 ); // 50% profit target from buy price

You don’t need to use loops and you don’t need to do anything except just turning on stops.

Thank you for the response. It is very helpful.

I am trying to understand this ApplyStop codings for last 2 months---but yet unsuccessfull only.

From your post it seems that you are using it successfully.

Could you please help me and let me have the detailed code.

I m interested only in ApplyStop part. So , you can put any dummy strategy for buy and short.

Please help

What do you fail to understand?

Thanks for replying.

I have buy and short strategy and I want that sell and cover should be handled by Applystop as per below :

Once in trade (either long or short) the trade should be closed based on certain points / percentages either loss or profit.

I tried to code it by observing some codes from online---but still not successful.

Please help me to get the solution.

Have you tried the code lines of this thread?

Buy = Cross(C, Ma(c,50 ) );
Short = Cross( Ma(C, 50), C );

Sell = Cover = 0; // 0 or False means exit only by ApplyStop
ApplyStop( stopTypeLoss, stopModePercent, 5 ); // 5% max loss from short/buy price
ApplyStop( stopTypeProfit, stopModePercent, 5 ); // 5% profit target from short/buy price
1 Like
ema1 = EMA( C, 5 );
ema2 = EMA( C, 10 );
Plot(ema1,"",colorBlue,styleThick);
Plot(ema2,"",colorRed,styleThick);

Buy = Cross(ema1,ema2 );
Short = Cross( ema2, ema1 );

Sell = Cover = 0; // 0 or False means exit only by ApplyStop
ApplyStop( stopTypeLoss, stopModePercent, 5 ); // 5% max loss from short/buy price
ApplyStop( stopTypeProfit, stopModePercent, 5 ); // 5% profit target from short/buy price

HaClose =EMA((O+H+L+C)/4,3); 
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );   
PlotShapes( IIf( Buy , shapeUpArrow,0) , colorGreen ,layer = 0, yposition = HaLow, offset = -8);
PlotShapes( IIf( SHORT , shapeDownArrow,0) , colorRed ,layer = 0, yposition = HaHigh, offset = -8);
//PlotShapes( IIf( SELL , shapeHollowDownArrow,0) , colorRed ,layer = 0, yposition = HaHigh, offset = -8);
//PlotShapes( IIf( COVER, shapeHollowUpArrow,0) , colorGreen ,layer = 0, yposition = HaLow, offset = -8);

The above might be working---but can we plot signals for the exits so that it will be visible on the charts like buy and short?

Study this specially Flag = 1. https://www.amibroker.com/guide/afl/equity.html

ema1 = EMA( C, 5 );
ema2 = EMA( C, 10 );
Plot(ema1,"",colorBlue,styleThick);
Plot(ema2,"",colorRed,styleThick);

Buy = Cross(ema1,ema2 );
Short = Cross( ema2, ema1 );

Equity(1); 

Sell = Cover = 0; // 0 or False means exit only by ApplyStop
ApplyStop( stopTypeLoss, stopModePercent, 5 ); // 5% max loss from short/buy price
ApplyStop( stopTypeProfit, stopModePercent, 5 ); // 5% profit target from short/buy price

HaClose =EMA((O+H+L+C)/4,3); 
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );   
PlotShapes( IIf( Buy , shapeUpArrow,0) , colorGreen ,layer = 0, yposition = HaLow, offset = -8);
PlotShapes( IIf( SHORT , shapeDownArrow,0) , colorRed ,layer = 0, yposition = HaHigh, offset = -8);
//PlotShapes( IIf( SELL , shapeHollowDownArrow,0) , colorRed ,layer = 0, yposition = HaHigh, offset = -8);
//PlotShapes( IIf( COVER, shapeHollowUpArrow,0) , colorGreen ,layer = 0, yposition = HaLow, offset = -8);

Ok, After reading your advise I added "Equity(1); " in the above code.

But , Please advise how to plot the exits by applystops on the chart.

Uncomment Sell and Cover PlotShapes

image

no signal plotting at all for the below code:

ema1 = EMA( C, 5 );
ema2 = EMA( C, 10 );
Plot(ema1,"",colorBlue,styleThick);
Plot(ema2,"",colorRed,styleThick);

Buy = Cross(ema1,ema2 );
Short = Cross( ema2, ema1 );

Equity(1); 

Sell = Cover = 0; // 0 or False means exit only by ApplyStop
ApplyStop( stopTypeLoss, stopModePercent, 5 ); // 5% max loss from short/buy price
ApplyStop( stopTypeProfit, stopModePercent, 5 ); // 5% profit target from short/buy price

HaClose =EMA((O+H+L+C)/4,3); 
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );   
PlotShapes( IIf( Buy , shapeUpArrow,0) , colorGreen ,layer = 0, yposition = HaLow, offset = -8);
PlotShapes( IIf( SHORT , shapeDownArrow,0) , colorRed ,layer = 0, yposition = HaHigh, offset = -8);
PlotShapes( IIf( SELL , shapeHollowDownArrow,0) , colorRed ,layer = 0, yposition = HaHigh, offset = -8);
PlotShapes( IIf( COVER, shapeHollowUpArrow,0) , colorGreen ,layer = 0, yposition = HaLow, offset = -8);

Now read Equity again, specially RangeType parameter

Tried to the best of my ability. But no success yet.

Could you please help and make the code work to get the desired output I.e plotting the exit signals by applystop.