Australian (ASX) Stock Market Forum

AmiBroker - Stop/Loss Plots

GreatPig

Pigs In Space
Joined
9 July 2004
Posts
2,368
Reactions
14
Hi guys,

I spent a bit of time over the weekend playing around with AmiBroker's formula language and have created some stop/loss plots. I've done one using ATR (Average True Range) and another using Guppy's count back.

These are not just basic plots as they have to start from a particular purchase date and only ever go up from there. Consequently, the actual purchase date has to be available somewhere. AmiBroker's portfolio manager doesn't link to the formula language (that I've seen), so I had to create a folder of files to enter the purchase dates into. Currently these would have to be manually entered at each purchase. The files also include the parameters for the ATR and count back plots, so can be customised separately for each stock, plus there's a dynamic parameter feature where they can then be adjusted on the fly.

Here are a few charts showing plots from selected dates (picked simply for display purposes). The default options I've used are 2 times 14 period ATR or 3 times 21 period ATR (as recommended by Leon Wilson), and a count back of two steps (the standard Guppy method).

This first plot is GUD weekly with a purchase date of 5/10/2001. The blue triangles on the date line indicate the purchase date and the blue down-arrows indicate prices crossing below the stop line, giving a sell signal. The stop line is only relevant after the purchase date, so ignore the fact it sometimes goes down before that. Given that the GUD trend continued up for the whole period, I think this would imply that the stops are too close to the price action for a long term hold.

Cheers,
GP
 

Attachments

  • GUD_GP1.gif
    GUD_GP1.gif
    17.9 KB · Views: 514
Here are the same plots but this time using a count back of 3 steps and 3 times 21 period ATR. No sell signals have been indicated here.

GP
 

Attachments

  • GUD_GP2.gif
    GUD_GP2.gif
    18.3 KB · Views: 476
And a shorter term daily plot for BSL over the last couple of months. The count back method gave two sell signals during this period, while the ATR method only gave one (although was border-line on the other one).

GP
 

Attachments

  • BSL_GP1.gif
    BSL_GP1.gif
    16.3 KB · Views: 464
hi

Just looking for some tips on setting contingent buy orders once you get a buy signal so as not to chase prices too high.
 
Hi GP,
I know this thread is old but I thought this might help some people. The enclosed url will take you to a page about Volatility Stops. He tested them on about 100 of his trades against 2 other methods and the conclusion is interesting. Here is the url
http://thepatternsite.com/stops.html#WIU

Regards,

Tim
 
Does anyone have a code for this?

I want to use an ATR trailing stop (of multiple of ATR) that only goes up, as with the first post in this thread.

I have no idea how to create this code. Any help would be appreciated
 
I tried it under the Formula heading and then tried to apply it as an indicator and this came up. Do you know what I'm doing wrong?

Amibroker ATR code.png
 
No mate. It is a plot which means you plot it on a chart. Exempli gratia 3*ATR(14)... (thanks Yoe)

OK. I am a little confused. I apologise for my ignorance but I am not sure how to code this. I have gone through the help on Amibroker and am new to it. I've managed to get some codes working but am not sure how to implement this one.

This is the code I got from the other thread:

stopArray = GP_CreateStop(Close - 2.5*ATR(15), Name(), 1, -1);
Buy = GP_GetBuyDate(Name(), 1);
Sell = Close < Ref(stopArray , -1);

Plot (stopArray, "ATR Stop Loss", colorBlue, styleLine | styleThick | styleDots);

PlotShapes(IIf(Buy = Now(3) , shapeUpTriangle, shapeNone), colorBlue, 0, Low, -30);
PlotShapes(IIf(Sell, shapeDownTriangle, shapeNone), colorOrange, 0, Low, 30);


Now I'm not sure how to implement this. Am I supposed to create the stop first? Why is the buy and sell there (is that only for backtesting)?

All I want to do it plot the 2.5*ATR on the chart but am not sure how to.

Are you (or someone else who knows) able to give me a step by step proceedure for this? It would be much appreciated.

Thanks,
Matt:)
 
It also mentions 'array not initialised' which I don't understand. I've been going through the help section most of this morning but am completely stuck!
 
This is the code I got from the other thread:
That is not the complete formula. When you click on the .zip for GP_ATRStop.afl the whole formula is there. For the ATRStop to work you need to load the .dll into plug-ins. I don't know if the trial version will allow plug-ins.

Alternatively go to Wise Stock Trader and search for ATR formulas to get another version.
 
I would also like the know the code to create an indicator that is on the same chart as the price and the price + the ATR or the price - the ATR.

What is the code for each of these?
 
That is not the complete formula. When you click on the .zip for GP_ATRStop.afl the whole formula is there. For the ATRStop to work you need to load the .dll into plug-ins. I don't know if the trial version will allow plug-ins.

Alternatively go to Wise Stock Trader and search for ATR formulas to get another version.

This is the code from Wise Stock Trade_SECTION_BEGIN("ERO ATR BUY SELL"); ero = Param("ATR multiple", 2.8, 0.5, 10, 0.1 )*Param("ATR period", 10, 3, 50 ); ero_col=ParamColor( "Color", colorCycle ); r=HHV(H,ero); s=LLV(L,ero); ab=IIf(H>Ref(r,-1),1,IIf(L<Ref(s,-1),-1,0)); ac=ValueWhen(ab!=0,ab,1); sl=IIf(ac==1,s,r); Plot(sl, _DEFAULT_NAME(), ero_col, styleStaircase); // or styleaArea Buy=Cross(H,sl); Sell=Cross(sl,L); PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40); PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50); PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40); PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50); PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45); _SECTION_END(); _SECTION_BEGIN("Price"); SetChartOptions(0,chartShowArrows|chartShowDates); _N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) )); Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); _SECTION_END();

Where in Amibroker do I enter this code? I am completely new to it and don't understand what screens to load up to enter this code and what file type to save it as?

Any help would be greatly appreciated.

Thanks,
Matt :)
 
OK I created an indicator with that code and got the following result:

Amibroker ATR trail.png

I have a couple of questions.

1. Why doesn't the ATR trail move up every day that the price moves up? (for example above where I drew the purple line)

2. How do I change the buy and sell signals? (e.g. where I drew the green circle)

3. It is possible for me to simply enter a buy date manually that it can start the trailing stop from?

Thanks,
Matt :)
 
I have a couple of questions.

1. Why doesn't the ATR trail move up every day that the price moves up? (for example above where I drew the purple line)
This the formula how an ATR is created using a true range (TR). (thanks to chart school definition)

Current ATR = [(Prior ATR x 9) + Current TR] / 10

- Multiply the previous 10-day ATR by 9.
- Add the most recent day's TR value.
- Divide the total by 10


2. How do I change the buy and sell signals? (e.g. where I drew the green circle)
Change the ATR period or ATR multiple in "Parameters" by clicking on the slider beside them.

3. It is possible for me to simply enter a buy date manually that it can start the trailing stop from?
The ATR plot will be whatever you set it at no matter when you buy your shares.
 
Thank you for your help. I am trying to work my way through this but my understanding is still limited.

Amibroker ATR trail.jpg

I tried to change the "buy" as you can see by the code boxed in red. I wanted to enter on the highest bar of the previous 10 bars.

1. Why are there no entry signals coming up? How do I create entry signals to for the highest bar of the previous 10 bars

2. Why is trailing line the same as the one I previously posted but without the buy and sell signals?

Thanks,
Matt
 
Thank you for your help. I am trying to work my way through this but my understanding is still limited.

View attachment 41586

I tried to change the "buy" as you can see by the code boxed in red. I wanted to enter on the highest bar of the previous 10 bars.

1. Why are there no entry signals coming up? How do I create entry signals to for the highest bar of the previous 10 bars
Thanks,
Matt

Click on edit and copy/paste this code at the bottom of the formula. Cick X in top right hand corner to exit and when prompted to save changes to Amibroker formula click yes. Double click on same formula in it's holding folder and the chart will appear with the 10 day high shapes. The "Exploration" in Automatic Analysis helps to find the most recent stocks by setting the "Range" to 24-Feb-2011 to 25-Feb-2011 and then click "Explore".

Note thinly traded and penny stocks do this often so you can add another filter such as MA(C*V, 50) > 50000.

PHP:
Breakout = C >= HHV(H,10);
PlotShapes(shapeHollowUpTriangle*Breakout,colorBlue, 0, L, Offset = -20);

//Exploration 
Filter = Breakout;
AddColumn(Breakout, "10 day High", 1, colorBlue, colorSeaGreen);
 

Attachments

  • untitled.jpg
    untitled.jpg
    155.5 KB · Views: 19
1. Why doesn't the ATR trail move up every day that the price moves up? (for example above where I drew the purple line)

Thanks,
Matt :)
Okay I see what you mean now. It is hhv/llv based and not ATR.


This one adjusts every bar.
 

Attachments

  • ATR line.txt
    1.6 KB · Views: 73
Top