Thursday, 10 March 2011

Leading signal for oscillators #3

Following on from the previous two posts about creating a leading indicator for oscillators, I have decided not to use the high pass filter I was using. The reason for this is that in the presence of a trend the high pass filter tends to drift upwards in an uptrend or downwards in a down trend with the result that the filter ends up being centred around a non zero mean. The high pass filter has now been replaced by Ehler's "Cybercycle" which does not suffer from this problem.

Below there is a video showing the leading indicators in action on real life data, the data being continuous, back adjusted futures for the Live Cattle contract, covering approximately a year's worth of data up to and including the 8th March 2011.

The upper graph is obviously a candlestick chart of the data with colour coded bars, but also plotted is Ehler's "Instantaneous trend line" in blue and the Cybercycle Tukey control chart upper control lines and lower control lines, in dotted yellow, mapped on to the price chart. The first sub graph is the actual Cybercycle indicator with its leading functions and its Tukey control chart upper control line and lower control line. The one bar, two bar, three bar and four bar leading functions of the Cybercycle are coloured yellow, green, cyan and white respectively. The second and third sub graphs are a band pass filter applied to the Cybercycle and Ehler's "Sine wave Indicator" applied to the Cybercycle, and both follow the colour scheme as above.

It can be seen that in the sub graphs the lines are either solid or dotted lines, as is the trend line in the price chart. When the Cybercycle is "in control" according to the logic of the Tukey control chart the lines in the sub graphs are solid and the trend line is dotted. When the Cybercycle is "out of control" according to the logic of the Tukey control chart the lines in the sub graphs are dotted and the trend line is solid. This is a simple, visual way to determine whether prices are deemed to be trending or possibly cyclic in nature.

In the price chart the bars are coloured green if the Cybercycle is "in control" AND the leading functions of the Cybercycle are "in order" i.e. the four bar lead is leading the three bar lead, which in turn is leading the two bar lead etc. and indicating upwards cyclic action; similarly the bars are coloured cyan if downwards cyclic action is indicated.

Finally, in the price chart, there are little, coloured triangles above the bars pointing downwards or below the bars pointing upwards. These indicate when the Cybercycle leading functions actually cross each other; when the triangle is cyan the cyan four bar leading function has crossed the green three bar leading function; when the triangle is green the green three bar leading function has crossed the yellow two bar leading function, and so on. It should be noted here that there is an order of preference given to the plotting of these triangles. If there are simultaneous crossovers the "most recent" crossover is displayed, i.e. if the four bar crosses the three bar, and the three bar crosses the two bar, the triangle is green only for the three bar crossing the two bar. The "perfect" set up sequence is cyan, green, yellow and finally blue, signalling three days to the turn, two days to the turn, the turn is tomorrow, and this is the turn bar respectively.

In summary, when the trend line is solid and the leading functions are dotted the market is trending, when the trend line is dotted and the leading functions are solid the market is possibly cycling. When the market is possibly cycling green bars mean be long and cyan bars mean be short. When the market is possibly cycling the triangles give advance warning of market turns.

I leave it up to the viewers to make their own minds up about the efficacy of these signals, but personally I am quite pleased with them.

n.b. for geeks
1) all calculations were done in Octave
2) all plotting was done in Gnuplot, animated by using the reread command
3) the desktop was recorded using recordMyDesktop
4) the video was edited using the Linux video editor LiVES
5) the .ogv output of recordMyDesktop was converted to .avi for upload to Youtube using mencoder

How I love FOSS!


Anonymous said...

Hi there!
Fantastic video of your system! Love it!
Your mentioning of AFIRMA got me curious enough to investigate it to...
can you share how you used gnuplot to draw different colored candlestick bars?
Thanks and good luck! :-)

Dekalog said...


Thanks for your comment.

I post below the code used to draw different coloured candlestick bars in Gnuplot. This assumes your data is in a .csv file with columns 1 to 5 being date, open, high, low and close respectively. Column 6 is the index column for plotting the coloured bar; in this case a value of 1 means plot the coloured bar with title 'cyclic'.

plot "chart" using 1:($$6==1?$$2:1/0):($$6==1?$$3:1/0):($$6==1?$$4:1/0):($$6==1?$$5:1/0) title 'cyclic' with candlesticks linecolor rgb "#008B45"

The above will only plot 'cyclic' bars, so assuming non-cyclic bars are indexed 0 you will need to add more code:

plot "chart" using 1:($$6==1?$$2:1/0):($$6==1?$$3:1/0):($$6==1?$$4:1/0):($$6==1?$$5:1/0) title 'cyclic' with candlesticks linecolor rgb "#008B45", \
"chart" using 1:($$6==0?$$2:1/0):($$6==0?$$3:1/0):($$6==0?$$4:1/0):($$6==0?$$5:1/0) title 'non cyclic' with candlesticks linecolor rgb "#FFFFFF"

so that there are no missing bars in the final plot.

Additionally, assuming columns 7 and 8 contain values for an indicator (moving averages, MACD etc.), 7 being a fast indicator and 8 a slower one, the code be changed thus:

plot "chart" using 1:($$7>$$8?$$2:1/0):($$7>$$8?$$3:1/0):($$7>$$8?$$4:1/0):($$7>$$8?$$5:1/0) title 'long' with candlesticks linecolor rgb "#008B45", \
"chart" using 1:($$7<$$8?$$2:1/0):($$7<$$8?$$3:1/0):($$7<$$8?$$4:1/0):($$7<$$8?$$5:1/0) title 'short' with candlesticks linecolor rgb "#FFFFFF", \
"chart" using 1:($$7==$$8?$$2:1/0):($$7==$$8?$$3:1/0):($$7==$$8?$$4:1/0):($$7==$$8?$$5:1/0) title 'neutal' with candlesticks linecolor rgb "#00F5FF"

This will plot different coloured bars depending on whether the fast line is above or below the slow line. Note the condition $$7==$$8 will plot a third neutral colour for the rare times the lines have the same value.

Finally, the code can be nested thus:

plot "chart" using 1:($$6==1?($$7>$$8?$$2:1/0):1/0):($$6==1?($$7>$$8?$$3:1/0):1/0):($$6==1?($$7>$$8?$$4:1/0):1/0):($$6==1?($$7>$$8?$$5:1/0):1/0) title 'cyclic & fast above slow' with candlesticks linecolor rgb "#008B45"

This will plot a coloured bar only when column 6 index is 1 AND column 7 value is greater than column 8 value.

NOTE!! All the above code assumes that a Gnuplot script is being "called," hence the use of double $$. If you are not "calling" a script use a single $. For more details about this see the Gnuplot documentation, Part III, Commands, #53 Call,

Anonymous said...

Thanks for the elaborate response!

What I was missing is the "1/0" expression - without that the graph would have some artifacts. your familiary with gnuplot is very good. I'm using similar methods to draw my graphs. In my case an application (C#) is communicating with gnuplot process via stdin/out to generate images which are then being read and displayed in the application.

My previous message was written in haste so I couldn't elaborate about my opinion on the knowledge you present on your site. The ideas, indicators and algorythms you meantion are top notch, very professional! Some of the stuff here is a Math Ph.D material , and some even in EE. That's before mentioning your way around programming and operating software. To give you a clue how advanced it is - I'm not a newbie in trading or technical analysis, working as software engineer and I'm a Bs.C in Math, so I know a thing or two about it - and the terms and formulas you use here are *way* out of my league! Wikipedia cleared out some of the terms, if it didn't I would have thought we are talking about different topics.

I will be keeping tabs on your blog.
Keep up the great work! :-)

The OP.