Australian (ASX) Stock Market Forum

Trend Line Plots

GreatPig

Pigs In Space
Joined
9 July 2004
Posts
2,368
Reactions
14
Just been playing around with this in AmiBroker and thought others might be interested in it. Was hoping to use it for some backtesting ideas, but it's far too slow as it. Would need to be rewritten into a plug-in DLL.

I realise AmiBroker has built-in functions similar to some of the ones I've written here myself, but like I say, I've just been experimenting.

I've attached the AFL as a file rather than list it all in a message. Just rename the .TXT extension to .AFL or copy and paste into a new indicator. A couple of plot results also attached to show what it looks like.

Cheers,
GP
 

Attachments

  • GUD_GP7.gif
    GUD_GP7.gif
    10.7 KB · Views: 319
  • AUO_GP1.gif
    AUO_GP1.gif
    11.3 KB · Views: 317
  • Trend Lines_GP.txt
    13.6 KB · Views: 207
&^%$ing good effort GP.

An extraordinarily difficult thing to program and you've done it rather well.

Cheers
 
Now that IS sensational!!

Now how can I get that into Metastock??

I'll have a go at coding it for M/S---might even pass it on the Roy Larson who is the best Ive seen in M/S.

Tremendous effort---thats saleable GP--you know that?
 
Thanks guys.

Sorry Tech, don't know anything about programming Metastock.

As for difficulty, well I'm a professional programmer and have been programming embedded real-time systems for about 25 years. While by no means trivial, I thought this was relatively easy compared to some of the things I've had to do :)

My original aim was to get something I could use in backtests to look for trend line breaks. As is this is way too slow, as it would need to recalculate the most recent trend line for every day of the price array. Then I'd have to figure out how to stop the most recent prices during a new rise being included in the trend line calculation, thus making the trend line always sit on top of the rising prices.

Anyway, I'll probably keep plugging away at it as I have time.

Hope you find it useful.

Cheers,
GP
 
Good work. Pity it takes a while to calculate as it would be good to do an animation of it over a period of time. If it doesn't have the same problems that the zigzag indicator has you would really be onto something.

MIT
 
I think mit is referring to that zigzag must change by a certain price percent before a turning point can be defined.

mit, I think this lie all other trend indications, or most indicators that is, the price alsoi has to move in opposite direction, or at least a certain distance in a direction before the trend line can be determined.
the opening statement mentions this in GP's afl

The turning points for the trend lines are determined by the "change" parameter, which is the percentage move in the opposite direction behind a low or high to trigger a direction change.


This is just normal as you can never define something until it has appeared.
 
GP, one way that might work to reduce the plotting time would be to reduce the number of lines calculated to either around the selected bar, or to the bars that are currently on the screen.
 
Kaveman,

Yeah, plotting just one line would be quicker. I initially started plotting just the most recent line. To be useful as a visual tool, that may be all that's necessary - just doesn't look as impressive :D. To do that, it's just a matter of altering the main routine to only do the first plot, not loop backwards through the stock.

Doing only the bars on the screen is a little trickier. I'm not sure how you can find out in code what bars are currently visible. I know how to get the start and end bars when a region is selected, but is there a function to find out what bars are currently displayed?

And for a selected bar, plotting backwards to the previous high or low would be easy, but I'd have to change things a bit to be able to move forward to the next one to do a full trend line. Although it may also be useful to only plot from the selected point back to the previous high or low, so that you can see what a trend line would look like only up to the selected point. That may also fit in better with a back tester.

And I guess being able to plot a line (or lines) within a selected region could also be useful.

Some good ideas to think about, thanks.

Cheers,
GP
 
status("barvisible") produces 0 or 1 depending if you can see the bar or not
this should be enough tog et started on that

CanSeeBar = status("b+arvisible");
FirstBar = valuewhen(CanSeeBar and ref(CanSeeBar,-1)==0, barindex() );
LastBar = valuewhen( CanSeeBar and ref(CanSeeBar,1)==0, barindex() );

if using this inside code then need to make sure that barcount-1 is used for the last chart bar as you can have the vacant bars extend to the right beyond the price data bars, eg

UseLastbar = min( LastBar, Barcount-1);
 
I've spent a bit more time on this and moved the line fitting code into a plug-in DLL, where obviously it runs a lot faster. For display and exploration purposes, it's now fast enough. Still pretty slow for backtesting, but at least usable where the fully-AFL version wasn't.

I would like to upload this latest version here. However, the plug-in kit has a comment in it that resulting software is only licenced for "personal, non-commercial use". I don't think non-commercial is a problem, as I'm not selling it, but does "personal use" effectively prevent me from uploading the plug-in here (just the DLL, not the source code)?

Have other plug-ins been uploaded to Yahoo, etc, and if so, have they needed permission from AmiBroker to do that?

Cheers,
GP
 
Before you place a plugin to AB yahoo or AB website you ahve to submit to TJ. It must ensure that there is no conflict between your variable names and those built-in to AB or into other 3rd party plugins that are laready used.
Basically any variable called up in AFL must be unique to your plugin.
Normally this is done by always having your initials as the first 2 letters of the variable/function name.

eg anything I would write into plugin and called upa s function in AFL would have gk in front, eg gkDate()

cannot recall where the info for that came from, just something in the dim memory cells of past discussions.
 
Kaveman,

Thanks for that.

There's only one function in the DLL and it does start with my initials (GP that is), but I also have a few global variables that are shared which currently don't. I might rename them first and then take a look on the AB Website.

Cheers,
GP
 
I'm impressed with the coding for these trendlines. If I may suggest 2 things which might make life a little easier for others. Both are under "Main Procedure" in the Code.

#1. The following code adjustment will automatically adjust the "tchg" to each individual stock - instead of a user defined input.

Replace "tchg = ................." with

mp =(H+L)/2;
r0 = IIf(mp>=Ref(mp,-1),ATR(1),0);
r1 = log(IIf(r0>0,ATR(1),mp)/mp);
r2 = Cum(r1)/Max(1,Cum(IIf(r0>0,1,0)));
tchg = LastValue(100*exp(LastValue(r2)));

#2. Also, I find the trendlines easier on the eye if coded with Highs & Lows:

Bottom = Low;
Top = High;

Regards Victor.
 
Thanks Victor.

Victor H said:
mp =(H+L)/2;
r0 = IIf(mp>=Ref(mp,-1),ATR(1),0);
r1 = log(IIf(r0>0,ATR(1),mp)/mp);
r2 = Cum(r1)/Max(1,Cum(IIf(r0>0,1,0)));
tchg = LastValue(100*exp(LastValue(r2)));
I'll take a look through that when I get a chance and try and see what it's doing :)

#2. Also, I find the trendlines easier on the eye if coded with Highs & Lows:
I think that's a matter of taste. With my manual trend line placement, I sometimes use highs and lows and sometimes opens and closes, depending on what seems best for the particular stock in that region. As you noted though, it's easily changed.

Cheers,
GP
 
HI GP,

I have worked mostly with Metastock and now I am slowly getting the hang of AB code. Forget my previous post as this is probably simpler.
It calculates the "average" ATR(1) [based on a Log-Normal Distribution] as a percentage of the Median Price. A turning point usually occurs when this %value is exceeded.

mp =(H+L)/2;
r1 = log(IIf(ATR(1)>0,ATR(1),mp)/mp);
r2 = Cum(r1)/Max(1,Cum(ATR(1)>0));
tchg = LastValue(100*exp(LastValue(r2)));

Regards Victor.
 
kaveman said:
Before you place a plugin to AB yahoo or AB website you ahve to submit to TJ
I sent him an email and he said I can distribute it provided it has installation and usage instructions (which I'm just finalising) and with the function and variable prefix I suggested to avoid conflicts.

So as soon as I get the documentation done up a bit, I'll upload the package (basically the DLL, AFL code for the indicator, exploration, and backtesting, and the instruction document).

Cheers,
GP
 
Top