Tuesday, 21 June 2011

Completion of Naive Bayesian Classifier coding

I am pleased to say that my .oct coding of the Naive Bayesian classifier is now complete. The purpose of the function is to take as inputs various measurements of the current state of the time series, and then for the classifier to classify the time series as being in one of the five following states:-
  • trending sideways with a cyclic action
  • trending upwards with 50% retracements of previous up-legs
  • trending upwards with no retracements
  • trending downwards with 50% retracemenst of previous down-legs
  • trending downwards with no retracements
These classifications will determine the most appropriate trading approach to take given the current state of the "tradable," and two screen shots of the classifier are given below, rendered as a "paint bar" study in the upper candlestick chart.

This first image just shows three classifications; trending sideways with a cycle in cyan, trending either up or down with 50% retracement in green, and trending up or down without retracement when the bars are blue or red (blue when close > open, red when close < open). Additionally, the coloured triangles show when the Cybercycle leading functions in the first subgraph cross, giving a count down to the predicted cyclic highs and lows, the blue "0" triangle being the predicted actual high or low. The green "fp high" and red "fp low" lines are the respective full period channel highs and lows of the price series, with the dotted yellow line being the midpoint of the two.

The second subgraph predicts turning points based on zero line crosses of the Cybercycle in the first subgraph. Read more about this indicator here.

The third subgraph is a plot of the sine of the phase of the Cybercycle, with leading signals, superimposed over a stochastic of the price bars. I may discuss this particular indicator in more depth in a future post.

This second screen shot is similar to the first, except that the classifications for retracement and no retracement are separated out into upward and downwards (chart key uwr = up with retracement, unr = up no retracement etc.). In this graph the coloured triangles represent the leading function crosses of the sine in the third subgraph.


Personally I am pleased that the effort to produce this indicator (almost six weeks of Monte Carlo simulation and statistical analysis in R, plus C++ coding) has resulted in a useful addition to my stable of indicators. I think there could still be some tweaks/improvements/additions that could be added, but for the moment I will leave the indicator as it is. The next thing I have set myself to do is implement my collection of C++ .oct functions as DLLs for Metatrader 4 (maybe an EA?) on a demo account so that I can utilise/test them intraday on forex data. Hopefully this will turn out to be a relatively simple case of "dragging and dropping" the already written C++ code.

Saturday, 14 May 2011

Bayesian Analysis Revisited

After having abandoned my work on Bayesian analysis late last year I am now working on this again, and have been for a few weeks now. I resumed work on this because a response to a question I asked on a forum here led me to the mixdist R package which has now enabled me to model kernel density estimates for a naive Bayes classifier. For more on using a kernel density estimate in a naive Bayes classifier see the link in the answer here.

I expect that it will be a few weeks yet before all the coding and testing of this is complete.

Bug in DX data confirmed?

In my post of 3 April 2011 I suggested that there might be an error in the data I have for the Dollar Index contact. This turns out to have been a well-founded suspicion. More details here.

Sunday, 10 April 2011

Coding and testing of AFIRMA completed

Following on from the previous post, the coding of the AFIRMA trend line using the leading function values as a proxy for the "peek into the future" values of price is complete, and I have to say that the results are quite disappointing. Using the rules outlined in the previous post it soon became obvious from cursory scanning of the equity curves that my version AFIRMA trend line did not live up to its early, potential promise. In fact the equity curves were so disappointing that I did not even bother to do the Monte Carlo permutation and bootstrap tests. The equity curves were no better than those produced by my simple benchmark suite of "systems" and the draw downs were such that I would never trade the AFIRMA as a stand alone "system," at least with the rules outlined in the previous post.

Below is a screen shot of the AFIRMA with a window length of 21 (peeks 10 days into the future) shown on the last 150 daily bars of the S&P E-mini. The red line is my version of it, with a Blackman-Harris window, and the yellow and green lines are two original versions with a Blackman and Blackman-Harris window. As can be seen, the original versions are smooth and accurately pick out major turning points whilst my version is not as smooth and gives many false signals that result in losses, even during a trending period.

Simple analysis of this chart, knowing the reasoning and coding behind it, shows why my version fails as a directional system. Each time the price moves contrary to any immediately prevailing trend, even those of short duration, the leading functions project this small movement as if it were the turning point of a major cycle and hence one ends up with trades in the opposite direction of the major trend, the result being that one is whipsawed in and out of this major trend. My version of the AFIRMA is simply too sensitive to minor price direction changes and I do not really have an idea as to how I can dampen this sensitivity. Smoothing it would probably be pointless as one might as well just smooth the prices directly.

However, all is not completely lost. The fact that my AFIRMA is so sensitive could be useful in identifying pullbacks in trends, acting as a set up to add to positions or for continuation trades. This is something I may investigate in the future, and this idea has been added to my "to do" list. For the moment I do not think that working more on the AFIRMA would be productive.

For interest, this second chart shows AFIRMA trend lines with a window length of nine (peeks four days into the future). It can be seen that my version of AFIRMA is quite robust in that the two trend lines (this chart and the one above) have different length windows but are almost identical.

Sunday, 3 April 2011

Exploratory AFIRMA trend line tests completed

As per my previous post, the exploratory tests of the AFIRMA trend line are now complete and the results are amazing. The tests in question were:
  • a Monte Carlo permutation test to accept or reject the null hypothesis that the results of the AFIRMA "system" are no better than could be expected from a random re-ordering of the system's position vector
  • a Monte Carlo bootstrap test to accept or reject the null hypothesis that the returns of the AFIRMA "system" are randomly centred around a zero return
Both the above tests are those that are described in Aronson's Evidence Based Technical Analysis and the AFIRMA passed both tests on all historical data series it was tested on, with the exception of the Dollar Index contract, with a p-value of zero. I suspect that it failed on the Dollar Index because of errors in the data I have for this contract. The AFIRMA that was tested had a window length of 9, which means that it "peeks into the future" for 4 bars, and a Blackman windowing function was used. The rules were quite simple: if the current bar's AFIRMA value is greater than that for the previous bar, go long at the open of the next bar or remain long if already so; and the reverse logic for shorts. No money management or stops were employed - it is a pure, one contract, always in the market test. The tests were conducted on daily bars covering the period from March 2001 to last week. The number crunching was done in Octave.

The next test was a simple visual check of the tick return equity, a simple plot of the cumulative number of ticks that the "system" would have returned. For this no allowance was made for commissions and slippage and a typical plot is shown below. This happens to be the S&P E-mini contract.


The AFIRMA is the blue line and the other lines are a simple benchmark suite I knocked up for comparative purposes, the benchmarks being
  • the equivalent of a buy and hold strategy
  • price closing above/below the 20 period simple moving average
  • price closing above/below the 50 period simple moving average
  • crossovers of the 20 and 50 period moving averages
  • a Donchian breakout system with a parameter of 20 periods to enter and 10 to exit
This second chart was created in R using the PerformanceAnalytics package and shows the log return equity, the daily log returns and the draw downs of the above, the AFIRMA being the dark blue lines, labelled V2 in the legend.


This final shot is a screen capture of the R session, using RStudio, used to create the performance summary chart. This was the first time I had used RStudio, and I am quite impressed with it.


In summary I can say that the AFIRMA has passed the above tests sufficiently well that I am going to code the AFIRMA using the leading functions as described in my previous post.

Thursday, 31 March 2011

AFIRMA trend line

I recently came across a blog post here that talks about an Autoregressive Finite Impulse Response MA, with a link to Metatrader 4 code here. Whilst the blogger in question dismisses the AFIRMA as being "completely useless" I think that in fact this could be quite useful for me. I can guess that s/he dismisses it due to the fact that the windowing function "peeks into the future" (10 days in the case of the default setting of 21).

However, using the leading functions I have talked about in my recent posts, I believe it might be possible to use the values of these leading functions as a proxy for this "peek into the future." Essentially the idea is to take the current bar's 1 bar leading function value of the Cybercycle as a proxy for the next bar's actual Cybercycle value, and then add this to the current bar's Instantaneous trend line value plus its 1 bar momentum to arrive at a "best guess" for the next bar's estimated value. Then all calculations for the indicators are re-run using this estimated value, a new 1 bar leading function calculated, added etc. and wash, rinse and repeat as required.

I can envisage that the coding of this will be difficult and time consuming, so before attempting to do so I will conduct some basic tests of the AFIRMA as is, with the luxury of this perfect "peek into the future" on historical data. I will have to be satisfied that the AFIRMA is possibly worth using before I put time and effort in to this coding task.

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!