Australian (ASX) Stock Market Forum

AmiBroker Plugin for Trailing Stop Plots

GreatPig

Pigs In Space
Joined
9 July 2004
Posts
2,368
Reactions
14
I've developed a plugin DLL for AmiBroker to generate trailing stop plots from any type of data. It calculates a proper trailing stop array from a specified buy date, where the buy date can either come from a portfolio-like file or be passed from the AFL code. With the sample AFL code provided, the latter is used when a date range has been set, making the beginning of the range the buy date.

The DLL also includes a Guppy Count Back Line function to allow that to be used for trailing stops (there's no Count Back entry function though).

The attached ZIP file includes the following files:

GP_Stops.dll - the plugin code
GP_ATRStop.afl - AFL code for an ATR trailing stop indicator
GP_CBStop.afl - AFL code for a Guppy Count Back trailing stop indicator
GP_EMAStop.afl - AFL code for a simple EMA trailing stop indicator
GP Stops DLL.pdf - an instruction sheet
Pfolio1.csv - a sample portfolio file

See the PDF file for instructions.

These files can be freely distributed and modified. The only thing I ask is that if you modify the AFL code and pass it on to someone else, make it clear that it's not my original code by changing the filename or adding a comment to that effect.

Let me know if you have any problems with it or any comments.

Cheers,
GP
 

Attachments

  • GP_Stops.zip
    135 KB · Views: 1,133
A sample chart showing the three included trailing stop plots on SYB.

Cheers,
GP
 

Attachments

  • SYB_GP1.gif
    SYB_GP1.gif
    24 KB · Views: 1,124
Damn! I wish I could do stuff like that! I'd make short versions of each!
 
wayneL said:
I'd make short versions of each!
Okay, since you asked so nicely :D

And here's what they look like on a short (hope I've got this right, as I've never done a short).

Cheers,
GP

[If someone could copy this second version into the first message would be good, so that latest version is there]
 

Attachments

  • GP_Stops_v2.zip
    140.6 KB · Views: 593
  • GUD_GP8.gif
    GUD_GP8.gif
    27.3 KB · Views: 840
Oops... I just realised I haven't done the Guppy count back correctly. No wonder it gave different results to the version I was using before :eek:

Although I actually think mine's a bit better - especially for stocks like SYB at the moment. I was starting the count back from the current bar, whereas it should start from the most recent high in the current trend (basically since purchase).

Anyway, will get that fixed shortly.

GP
 
On second thoughts, I'm going to leave it like it is. Finding the most recent high in the current trend is a little tricky and I think mine is better in those few situations where it's different.

Attached is a diagram of how the proper Guppy Count Back works, where the count back should always start from the most recent high. The method doesn't mention anything though about preventing the stop from falling (in a long position) if the new high also has a low lower than the low of the previous high, as shown in the attachment. This means that over the period of the long position, there could be times where the stop actually drops in situations like this. I think he's done it this way so that he doesn't need to track an ongoing stop position, and thus it can be quickly done at a glance on an unmarked chart.

The way I've done it is by always starting the count back from the current day and tracking the highest value it's ever been since purchase. If the current day's value is less than the highest value, it stays at the highest value. In situations like in the attachment, this allows the stop to keep going up if there are inside days after the long bar with lows much higher than the long bar. With the proper Guppy method, the stop won't move any higher until there's a new high day in that case.

So unless someone can see a problem with the way I'm doing it, which I think will only be noticeably different in rare situations like in the attachment (and currently in SYB), I'm going to leave it like it is.

Cheers,
GP
 

Attachments

  • CountBackIssue.gif
    CountBackIssue.gif
    4.8 KB · Views: 1,571
GP.

Is this your own work or did you paste it up?

Have you tried it on stocks as a stop and reverse swing trade method?
 
Tech/a,

tech/a said:
Is this your own work or did you paste it up?
I wrote it all myself. I've had count back and ATR trailing stop indicators for a while now, but just separated them out into a separate plugin and modified them to be more generic.

Have you tried it on stocks as a stop and reverse swing trade method?
I haven't tried any sort of back testing with them yet. I mainly use these as visual comparisons with my own system.

Cheers,
GP
 
I thought the Countback used the low of the highest high bar, then went back to next low lower than this low etc.
Same as lowest low gives the high start point and goes back to next higher high
 
Kaveman,

kaveman said:
I thought the Countback used the low of the highest high bar, then went back to next low lower than this low etc.
Yes, that's how it's supposed to work.

However, I don't really see how the low of the highest high is more relevant than the low of the current day. I think Guppy does it that way for simplicity, because the highest high is typically going to be close to the highest low as well, and thus you don't need to track the high value of a trailing stop when working it out.

The only time there's going to be a great difference is in cases like my attachment, which just happens to be the case with SYB at the moment (at least the way I handled the demerger). Because the highest high day had a very long bar, the low is very low compared to more recent days, which keeps the stop pretty low. For example, SYB closed at 3.47 yesterday but the proper count back stop was only at 2.815, compared to a 2.5xATR stop at 3.34 and 3.50 for my variation of the count back stop.

But regardless of that, my way is a lot easier to handle in software, as I don't need to find the most recent high when calculating the count back array.

Cheers,
GP
 
hi there great pig,
wondering if you could help someone out who is semi retarded when it comes to AFL :(
I copied and pasted the c/b line stop into amibroker and it doesn't seem to be working for me properly. I get the blue line in my chart like the ones you posted earlier, but mine zig zags below the price action, never getting hit!
Any help would be appreciated.
 

Attachments

  • osh.PNG
    osh.PNG
    22 KB · Views: 722
GP
Nice work - especially the CBL's. I have never got my head around how to code the CBL's.

I was wondering if anyone has done any backtesting on the Guppy countback lines? It would be nice to compare the original CBL's with the modified lines and ATR style stops. I don't know if they work all that well - I did some testing in MetaStock years ago with CBL's.

stevo
 
Prof Frink,

That looks like just the countback plot without it being turned into a stop-loss.

I'm not running that particular function, as I have another version that's tied in with my own portfolio manager, but from looking in the documentation, one of the AFL parameters selects whether you want to view just the raw countback data or as a stop/loss plot. Make sure that is selecting stop/loss plot.

To find the parameter, right-click over that chart and select Parameters, then I think it's the last one.

Another thing is I think you might need to have an entry for the stock in the file mentioned in the documentation, so that it has a starting date. Try double-clicking somewhere on the chart to set a region start date. Does it look right then?

GP
 
thanks for that GP. Looks like I won't be able to use it unless I've set up a portfolio for it to track(is that right?).
I was just hoping to to get the indicator so I could plot it in a similar fashion to ezy chart. Oh well, another negative tick for amibroker here!
 
stevo said:
I was wondering if anyone has done any backtesting on the Guppy countback lines?
I've done some testing of my own system using CBL as part of it (where ultimately it was better without it), but not a system using that alone. Plus I haven't done any code for countback entry.

The method Guppy often talks about using CBLs is rather subjective, so difficult to code. He looks for certain characteristics with his GMMA to select a stock then often uses CBL to manage it. To mechanise that I think would require making up some rules combining various aspects of GMMA and other qualities for stock selection, perhaps CBL entry for timing (although he doesn't often mention that), then CBL stop/loss for exit.

Cheers,
GP
 
professor_frink said:
Looks like I won't be able to use it unless I've set up a portfolio for it to track(is that right?).
Well, to plot a stop/loss you need to have a start date, as the stop price can only ever go up from that date. With no start date specified, there's no base to start climbing from. A stop/loss starting from the beginning of the chart would be meaningless today.

That's why I also allowed a region to be marked, so that you can manually set any arbitrary start date. If you double-click on the desired start date, the plot should look correct from that date onwards.

I don't know what EzyChart does or looks like, but I can't see how they can plot a stop/loss without a starting date either.

GP

[Edit: damn, these are ATR plots, but it's the same with countback]


OSH with no date set:

oshgp13sg.gif



OSH with a region start marked:

oshgp25ay.gif



And with another region start marked:

oshgp35cx.gif
 
gotcha. thanks for your help GP. Basically in Ezychart you just clicked on countbackline under the line section and dragged it to your start date. It looks just like guppy plots it as in his books. Now that I've got it working it's pretty well doing the same thing. Sorry to all the fine folk at amibroker for trashing their product. I'm just a very moody person :D .

Now how did you plot in on the short side?
P.s sorry to be such a pain in the ass :)
 
professor_frink said:
how did you plot in on the short side?
If you're not using the portfolio file, there's another parameter to select either long or short.

GP
 
Top