Monday, 28 May 2012

Update on Gold Delta Solution

Back in February I published a post about the Delta solution for Gold, and this post is a follow up to that earlier post.

Below is an updated chart of Gold showing the next few turning points following on from where the previous chart ended.

My read of this chart is that MTD 1 (solid green line) is a high which came in early March, followed by a low MTD 2 which came in a week late (the second solid red line). The market then struggled to move up to a high MTD 3, indicating a very weak market, and now the market is moving down to a low MTD 4 (the second solid yellow line). Since the most recent MTD 3 high is lower than the previous MTD 1 high and it looks like the MTD 4 low will come in lower that the MTD 2 low, I'd say that on the MTD time frame Gold is now a bear market.

On an unrelated note, it has been more than a month since my last post. During this time I have been busy working through the free, online version of Andrew Ng's Machine Learning course. More on this in a future post.

Tuesday, 24 April 2012

First Use of Rcpp Package

In preparation for the tests I want to conduct, I have finally started to use the Rcpp package. The reason for this decision is straightforward; I cannot begin to imagine how the code for the tests I want to conduct could be vectorised, and since loops in R are slow and not recommended there is no realistic alternative to biting the Rcpp bullet. Another reason is that some R packages, such as the ttrTests package, require an R function as part of the input and using Rcpp will enable me to transfer my .oct C++ function coding directly into R without having to rewrite in R code itself. A simple proof of concept programming exercise I set myself is shown below.
rm(list=ls()) # clear the entire workspace
library(xts)  # load the required library
library(zoo)  # load the required library
library(Rcpp) # load the required library
library(inline) # load the required library
library(compiler) # load the required library

# load the "indicator" file 
data <- read.csv(file="sind",head=FALSE,sep=,)
tick_size <- 0.025
tick_value <- 12.50

# extract other vectors of interest
#open <- data[,2]
market_mode <- data[,228]
kalman <- data[,283]

# source the Rcpp inline file for the C++ code for the test in question
# this file effectively creates a function that takes input and gives test 
# output in the desired form e.g. equity curve, position vector etc. for
# further statistical tests in the R environment, e.g the ttrTests package.

source("basic_market_mode_equity.r")
results <- basic_market_mode_equity(data[,2],market_mode,kalman,tick_size,tick_value)
This is "normal" R code which
  • loads the required libraries
  • loads the required file(s) from disk and takes other inputs for the test in question
  • extracts the required information from the loaded file(s)
  • sources and then calls the test function, named "basic_market_mode_equity"
This second code block contains the actual C++ test function code, which is contained in a file named "basic_market_mode_equity.r"
# This function takes as inputs vectors of opening prices, the "market mode," 
# the kalman filter of vwap, and single values for tick size and tick value.
# The values of "market_mode" are as follows:-
# 0 = cyclic
# 1 = up with retracement
# 2 = up with no retracement
# 3 = down with retracement
# 4 = down with no retracement
# 
# This first test is very simple - be long when market mode is 1 or 2, 
# short when 3 or 4, and when 0 be long or short depending on the direction
# of the kalman filter. This test is just practice in coding using Rcpp:- 
# don't expect any meaningful results. Equity curves for each market mode 
# are the function outputs

src <- '
Rcpp::NumericVector open(a) ;
Rcpp::NumericVector market_mode(b) ;
Rcpp::NumericVector kalman(c) ;
Rcpp::NumericVector tick_size(d) ;
Rcpp::NumericVector tick_value(e) ;
int n = open.size() ;
Rcpp::NumericVector cyc(n) ; // create output vector
Rcpp::NumericVector uwr(n) ; // create output vector
Rcpp::NumericVector unr(n) ; // create output vector
Rcpp::NumericVector dwr(n) ; // create output vector
Rcpp::NumericVector dnr(n) ; // create output vector

// fill the equity_curve with zeros for "burn in" period
for ( int ii = 0 ; ii < 100 ; ii++ ) {
    cyc[ii] = 0.0 ;
    uwr[ii] = 0.0 ;
    unr[ii] = 0.0 ;
    dwr[ii] = 0.0 ;
    dnr[ii] = 0.0 ; }

for ( int ii = 100 ; ii < n ; ii++ ) {
    
    // uwr market type
    if ( market_mode[ii-2] == 1 ) { 
    cyc[ii] = cyc[ii-1] ;
    uwr[ii] = uwr[ii-1] + tick_value[0] * ( (open[ii]-open[ii-1])/tick_size[0] ) ;
    unr[ii] = unr[ii-1] ;
    dwr[ii] = dwr[ii-1] ;
    dnr[ii] = dnr[ii-1] ; }

    // unr market type
    if ( market_mode[ii-2] == 2 ) { 
    cyc[ii] = cyc[ii-1] ;
    uwr[ii] = uwr[ii-1] ; 
    unr[ii] = unr[ii-1] + tick_value[0] * ( (open[ii]-open[ii-1])/tick_size[0] ) ;
    dwr[ii] = dwr[ii-1] ;
    dnr[ii] = dnr[ii-1] ; }

    // dwr
    if ( market_mode[ii-2] == 3 ) { 
    cyc[ii] = cyc[ii-1] ;
    uwr[ii] = uwr[ii-1] ;
    unr[ii] = unr[ii-1] ;
    dwr[ii] = dwr[ii-1] + tick_value[0] * ( (open[ii-1]-open[ii])/tick_size[0] ) ; 
    dnr[ii] = dnr[ii-1] ; }

    // dnr
    if ( market_mode[ii-2] == 4 ) { 
    cyc[ii] = cyc[ii-1] ;
    uwr[ii] = uwr[ii-1] ;
    unr[ii] = unr[ii-1] ;
    dwr[ii] = dwr[ii-1] ; 
    dnr[ii] = dnr[ii-1] + tick_value[0] * ( (open[ii-1]-open[ii])/tick_size[0] ) ; }

    // cyc long
    if ( market_mode[ii-2] == 0 && kalman[ii-2] > kalman[ii-3] ) { 
    cyc[ii] = cyc[ii-1] + tick_value[0] * ( (open[ii]-open[ii-1])/tick_size[0] ) ;
    uwr[ii] = uwr[ii-1] ;
    unr[ii] = unr[ii-1] ;
    dwr[ii] = dwr[ii-1] ; 
    dnr[ii] = dnr[ii-1] ; }

    // cyc short
    if ( market_mode[ii-2] == 0 && kalman[ii-2] < kalman[ii-3] ) { 
    cyc[ii] = cyc[ii-1] + tick_value[0] * ( (open[ii-1]-open[ii])/tick_size[0] ) ;
    uwr[ii] = uwr[ii-1] ;
    unr[ii] = unr[ii-1] ;
    dwr[ii] = dwr[ii-1] ; 
    dnr[ii] = dnr[ii-1] ; }

} // end of main for loop

return List::create(
  _["cyc"] = cyc ,
  _["uwr"] = uwr ,
  _["unr"] = unr ,
  _["dwr"] = dwr ,
  _["dnr"] = dnr ) ; '

basic_market_mode_equity <- cxxfunction(signature(a = "numeric", b = "numeric", c = "numeric",
                                d = "numeric", e = "numeric"), body=src, 
                                plugin = "Rcpp")
which basically just outputs five equity curves corresponding to the identified market mode (see the comments in the code for more details).

This final code block is again "normal" R code
# coerce the above results list object to a data frame object
results_df <- data.frame( results )
df_max <- max(results_df) # for scaling of results plot
df_min <- min(results_df) # for scaling of results plot

# and now create an xts object for plotting
results_xts <- xts(results_df,as.Date(data[,'V1']))

# a nice plot of the results_xts object
plot(results_xts[,'cyc'],ylim=c(df_min,df_max),type="l")
par(new=TRUE,col="cyan")
plot(results_xts[,'uwr'],ylim=c(df_min,df_max),type="l")
par(new=TRUE,col="blue")
plot(results_xts[,'unr'],ylim=c(df_min,df_max),type="l")
par(new=TRUE,col="green")
plot(results_xts[,'dwr'],ylim=c(df_min,df_max),type="l")
par(new=TRUE,col="red")
plot(results_xts[,'dnr'],ylim=c(df_min,df_max),type="l")
which produces this typical plot output.
This test is just a toy test and the results are not really important. The important thing is that I can now easily import the output of my Octave .oct C++ functions into R and conduct a whole range of tests utilising R packages from CRAN or simply write my own test routines in C++ (my intention) to run in R and not have to struggle with vectorising code for speed optimisation purposes.

Saturday, 14 April 2012

Bayesian Classifier Sanity Check, Part 2

Here are the results of the sliced_charts script when applied to the "down with no retracement" market mode, and again it is pleasing to see such high percentages.
octave:1> sliced_charts
Enter matrix e.g. gcmatrix: clmatrix
points_of_interest =  132
dwr_to_dnr =  118
dwr_to_dnr_percent =  0.89394
cyc_to_dnr =  13
cyc_to_dnr_percent =  0.098485
octave:2> sliced_charts
Enter matrix e.g. gcmatrix: homatrix
points_of_interest =  124
dwr_to_dnr =  100
dwr_to_dnr_percent =  0.80645
cyc_to_dnr =  20
cyc_to_dnr_percent =  0.16129
octave:3> sliced_charts
Enter matrix e.g. gcmatrix: ngmatrix
points_of_interest =  150
dwr_to_dnr =  127
dwr_to_dnr_percent =  0.84667
cyc_to_dnr =  21
cyc_to_dnr_percent =  0.14000
octave:4> sliced_charts
Enter matrix e.g. gcmatrix: rbmatrix
points_of_interest =  121
dwr_to_dnr =  104
dwr_to_dnr_percent =  0.85950
cyc_to_dnr =  13
cyc_to_dnr_percent =  0.10744
octave:5> sliced_charts
Enter matrix e.g. gcmatrix: ccmatrix
points_of_interest =  153
dwr_to_dnr =  132
dwr_to_dnr_percent =  0.86275
cyc_to_dnr =  19
cyc_to_dnr_percent =  0.12418
octave:6> sliced_charts
Enter matrix e.g. gcmatrix: kcmatrix
points_of_interest =  148
dwr_to_dnr =  123
dwr_to_dnr_percent =  0.83108
cyc_to_dnr =  23
cyc_to_dnr_percent =  0.15541
octave:7> sliced_charts
Enter matrix e.g. gcmatrix: ojmatrix
points_of_interest =  147
dwr_to_dnr =  120
dwr_to_dnr_percent =  0.81633
cyc_to_dnr =  21
cyc_to_dnr_percent =  0.14286
octave:8> sliced_charts
Enter matrix e.g. gcmatrix: sbmatrix
points_of_interest =  109
dwr_to_dnr =  94
dwr_to_dnr_percent =  0.86239
cyc_to_dnr =  12
cyc_to_dnr_percent =  0.11009
octave:9> sliced_charts
Enter matrix e.g. gcmatrix: ctmatrix
points_of_interest =  143
dwr_to_dnr =  122
dwr_to_dnr_percent =  0.85315
cyc_to_dnr =  14
cyc_to_dnr_percent =  0.097902
octave:10> sliced_charts
Enter matrix e.g. gcmatrix: lbmatrix
points_of_interest =  142
dwr_to_dnr =  120
dwr_to_dnr_percent =  0.84507
cyc_to_dnr =  20
cyc_to_dnr_percent =  0.14085
octave:11> sliced_charts
Enter matrix e.g. gcmatrix: hgmatrix
points_of_interest =  119
dwr_to_dnr =  101
dwr_to_dnr_percent =  0.84874
cyc_to_dnr =  10
cyc_to_dnr_percent =  0.084034
octave:12> sliced_charts
Enter matrix e.g. gcmatrix: smatrix
points_of_interest =  117
dwr_to_dnr =  107
dwr_to_dnr_percent =  0.91453
cyc_to_dnr =  8
cyc_to_dnr_percent =  0.068376
octave:13> sliced_charts
Enter matrix e.g. gcmatrix: smmatrix
points_of_interest =  123
dwr_to_dnr =  105
dwr_to_dnr_percent =  0.85366
cyc_to_dnr =  17
cyc_to_dnr_percent =  0.13821
octave:14> sliced_charts
Enter matrix e.g. gcmatrix: bomatrix
points_of_interest =  136
dwr_to_dnr =  121
dwr_to_dnr_percent =  0.88971
cyc_to_dnr =  11
cyc_to_dnr_percent =  0.080882
octave:15> sliced_charts
Enter matrix e.g. gcmatrix: cmatrix
points_of_interest =  137
dwr_to_dnr =  111
dwr_to_dnr_percent =  0.81022
cyc_to_dnr =  24
cyc_to_dnr_percent =  0.17518
octave:16> sliced_charts
Enter matrix e.g. gcmatrix: omatrix
points_of_interest =  138
dwr_to_dnr =  108
dwr_to_dnr_percent =  0.78261
cyc_to_dnr =  24
cyc_to_dnr_percent =  0.17391
octave:17> sliced_charts
Enter matrix e.g. gcmatrix: wmatrix
points_of_interest =  145
dwr_to_dnr =  122
dwr_to_dnr_percent =  0.84138
cyc_to_dnr =  21
cyc_to_dnr_percent =  0.14483
octave:18> sliced_charts
Enter matrix e.g. gcmatrix: lcmatrix
points_of_interest =  136
dwr_to_dnr =  113
dwr_to_dnr_percent =  0.83088
cyc_to_dnr =  22
cyc_to_dnr_percent =  0.16176
octave:19> sliced_charts
Enter matrix e.g. gcmatrix: fcmatrix
points_of_interest =  123
dwr_to_dnr =  105
dwr_to_dnr_percent =  0.85366
cyc_to_dnr =  15
cyc_to_dnr_percent =  0.12195
octave:20> sliced_charts
Enter matrix e.g. gcmatrix: lhmatrix
points_of_interest =  147
dwr_to_dnr =  122
dwr_to_dnr_percent =  0.82993
cyc_to_dnr =  21
cyc_to_dnr_percent =  0.14286
octave:21> sliced_charts
Enter matrix e.g. gcmatrix: gcmatrix
points_of_interest =  132
dwr_to_dnr =  115
dwr_to_dnr_percent =  0.87121
cyc_to_dnr =  16
cyc_to_dnr_percent =  0.12121
octave:22> sliced_charts
Enter matrix e.g. gcmatrix: simatrix
points_of_interest =  127
dwr_to_dnr =  105
dwr_to_dnr_percent =  0.82677
cyc_to_dnr =  16
cyc_to_dnr_percent =  0.12598
octave:23> sliced_charts
Enter matrix e.g. gcmatrix: plmatrix
points_of_interest =  128
dwr_to_dnr =  112
dwr_to_dnr_percent =  0.87500
cyc_to_dnr =  14
cyc_to_dnr_percent =  0.10938
octave:24> sliced_charts
Enter matrix e.g. gcmatrix: pamatrix
points_of_interest =  120
dwr_to_dnr =  96
dwr_to_dnr_percent =  0.80000
cyc_to_dnr =  18
cyc_to_dnr_percent =  0.15000
octave:25> sliced_charts
Enter matrix e.g. gcmatrix: usmatrix
points_of_interest =  94
dwr_to_dnr =  78
dwr_to_dnr_percent =  0.82979
cyc_to_dnr =  14
cyc_to_dnr_percent =  0.14894
octave:26> sliced_charts
Enter matrix e.g. gcmatrix: tymatrix
points_of_interest =  104
dwr_to_dnr =  99
dwr_to_dnr_percent =  0.95192
cyc_to_dnr =  2
cyc_to_dnr_percent =  0.019231
octave:27> sliced_charts
Enter matrix e.g. gcmatrix: edmatrix
points_of_interest =  108
dwr_to_dnr =  96
dwr_to_dnr_percent =  0.88889
cyc_to_dnr =  10
cyc_to_dnr_percent =  0.092593
octave:28> sliced_charts
Enter matrix e.g. gcmatrix: dxmatrix
points_of_interest =  116
dwr_to_dnr =  94
dwr_to_dnr_percent =  0.81034
cyc_to_dnr =  20
cyc_to_dnr_percent =  0.17241
octave:29> sliced_charts
Enter matrix e.g. gcmatrix: spmatrix
points_of_interest =  106
dwr_to_dnr =  85
dwr_to_dnr_percent =  0.80189
cyc_to_dnr =  19
cyc_to_dnr_percent =  0.17925
octave:30> sliced_charts
Enter matrix e.g. gcmatrix: esmatrix
points_of_interest =  109
dwr_to_dnr =  86
dwr_to_dnr_percent =  0.78899
cyc_to_dnr =  20
cyc_to_dnr_percent =  0.18349
octave:31> sliced_charts
Enter matrix e.g. gcmatrix: ndmatrix
points_of_interest =  107
dwr_to_dnr =  83
dwr_to_dnr_percent =  0.77570
cyc_to_dnr =  18
cyc_to_dnr_percent =  0.16822
octave:32> sliced_charts
Enter matrix e.g. gcmatrix: eurusdmatrix
points_of_interest =  72
dwr_to_dnr =  60
dwr_to_dnr_percent =  0.83333
cyc_to_dnr =  11
cyc_to_dnr_percent =  0.15278
octave:33> sliced_charts
Enter matrix e.g. gcmatrix: gbpusdmatrix
points_of_interest =  85
dwr_to_dnr =  72
dwr_to_dnr_percent =  0.84706
cyc_to_dnr =  11
cyc_to_dnr_percent =  0.12941
octave:34> sliced_charts
Enter matrix e.g. gcmatrix: usdchfmatrix
points_of_interest =  86
dwr_to_dnr =  69
dwr_to_dnr_percent =  0.80233
cyc_to_dnr =  14
cyc_to_dnr_percent =  0.16279
octave:35> sliced_charts
Enter matrix e.g. gcmatrix: usdyenmatrix
points_of_interest =  100
dwr_to_dnr =  80
dwr_to_dnr_percent =  0.80000
cyc_to_dnr =  16
cyc_to_dnr_percent =  0.16000
octave:36> sliced_charts
Enter matrix e.g. gcmatrix: eurchfmatrix
points_of_interest =  100
dwr_to_dnr =  83
dwr_to_dnr_percent =  0.83000
cyc_to_dnr =  15
cyc_to_dnr_percent =  0.15000
octave:37> sliced_charts
Enter matrix e.g. gcmatrix: eurgbpmatrix
points_of_interest =  91
dwr_to_dnr =  79
dwr_to_dnr_percent =  0.86813
cyc_to_dnr =  12
cyc_to_dnr_percent =  0.13187
octave:38> sliced_charts
Enter matrix e.g. gcmatrix: euryenmatrix
points_of_interest =  94
dwr_to_dnr =  75
dwr_to_dnr_percent =  0.79787
cyc_to_dnr =  15
cyc_to_dnr_percent =  0.15957
octave:39> sliced_charts
Enter matrix e.g. gcmatrix: eurausmatrix
points_of_interest =  99
dwr_to_dnr =  81
dwr_to_dnr_percent =  0.81818
cyc_to_dnr =  18
cyc_to_dnr_percent =  0.18182
octave:40> sliced_charts
Enter matrix e.g. gcmatrix: eurcadmatrix
points_of_interest =  92
dwr_to_dnr =  81
dwr_to_dnr_percent =  0.88043
cyc_to_dnr =  9
cyc_to_dnr_percent =  0.097826
octave:41> sliced_charts
Enter matrix e.g. gcmatrix: usdcadmatrix
points_of_interest =  97
dwr_to_dnr =  86
dwr_to_dnr_percent =  0.88660
cyc_to_dnr =  7
cyc_to_dnr_percent =  0.072165
octave:42> sliced_charts
Enter matrix e.g. gcmatrix: gbpchfmatrix
points_of_interest =  87
dwr_to_dnr =  70
dwr_to_dnr_percent =  0.80460
cyc_to_dnr =  14
cyc_to_dnr_percent =  0.16092
octave:43> sliced_charts
Enter matrix e.g. gcmatrix: gbpyenmatrix
points_of_interest =  85
dwr_to_dnr =  74
dwr_to_dnr_percent =  0.87059
cyc_to_dnr =  9
cyc_to_dnr_percent =  0.10588
octave:44> sliced_charts
Enter matrix e.g. gcmatrix: auscadmatrix
points_of_interest =  72
dwr_to_dnr =  59
dwr_to_dnr_percent =  0.81944
cyc_to_dnr =  13
cyc_to_dnr_percent =  0.18056
octave:45> sliced_charts
Enter matrix e.g. gcmatrix: aususdmatrix
points_of_interest =  74
dwr_to_dnr =  59
dwr_to_dnr_percent =  0.79730
cyc_to_dnr =  9
cyc_to_dnr_percent =  0.12162
octave:46> sliced_charts
Enter matrix e.g. gcmatrix: ausyenmatrix
points_of_interest =  65
dwr_to_dnr =  49
dwr_to_dnr_percent =  0.75385
cyc_to_dnr =  13
cyc_to_dnr_percent =  0.20000
But what does this all mean? For the purposes of simplicity I shall discuss "up with no retracement" only, but everything will apply equally to the "down with retracement," but of course in reverse.

My original concern was that the market classifications might have been erratically switching directly from "dnr" to "unr" with resultant lurches from short to long market positions and along the way incurring whipsaw losses from false signals. However, the high percentages from these simple tests show that this is not actually the case. Based on this, one can assume that
  • when the market classification changes to "unr" a long position will already be held because either
  1. a cyclic long position is held, having been initiated at the most recent low cyclic turn, or
  2. a "uwr" long position is held, having been initiated at the most recent rebound from a resistance/retracement level
  • which means that this market classification change is not necessarily a new entry signal, but rather a signal to change to a trend following exit criteria
Therefore, my next test(s) will be to assume that such a long position is held when the market classification changes to "unr" and will continued to be held until a trend following exit occurs, coupled with a "re-entry" signal if appropriate. My intent at the moment is simply to plot equity curves of these long positions, in the supposition that these equity curves will be "additions" or "extensions" to the equity curves generated by signals when in other market modes. More details in a coming post.

Friday, 13 April 2012

My Naive Bayesian Classifier Sanity Check

Following on from my previous post, as part of my initial attempts to "come up with a robust rule set that combines all these disparate indicators into a coherent whole," I have written a simple Octave script to conduct a basic sanity check of my Naive Bayesian Classifier. The purpose of this is to ensure that the various classifications are not wildly fluctuating between bull and bear market modes with no discernible order. This particular test identifies which market modes are indicated immediately prior to an "up with no retracement" mode being indicated. This code box shows the Octave terminal output of the test script, which is named "sliced_charts".
octave:1> sliced_charts
Enter matrix e.g. gcmatrix: clmatrix
points_of_interest =  147
uwr_to_unr =  126
uwr_to_unr_percent =  0.85714
cyc_to_unr =  16
cyc_to_unr_percent =  0.10884
octave:2> sliced_charts
Enter matrix e.g. gcmatrix: homatrix
points_of_interest =  130
uwr_to_unr =  106
uwr_to_unr_percent =  0.81538
cyc_to_unr =  20
cyc_to_unr_percent =  0.15385
octave:3> sliced_charts
Enter matrix e.g. gcmatrix: ngmatrix
points_of_interest =  123
uwr_to_unr =  104
uwr_to_unr_percent =  0.84553
cyc_to_unr =  15
cyc_to_unr_percent =  0.12195
octave:4> sliced_charts
Enter matrix e.g. gcmatrix: rbmatrix
points_of_interest =  162
uwr_to_unr =  134
uwr_to_unr_percent =  0.82716
cyc_to_unr =  23
cyc_to_unr_percent =  0.14198
octave:5> sliced_charts
Enter matrix e.g. gcmatrix: ccmatrix
points_of_interest =  123
uwr_to_unr =  95
uwr_to_unr_percent =  0.77236
cyc_to_unr =  24
cyc_to_unr_percent =  0.19512
octave:6> sliced_charts
Enter matrix e.g. gcmatrix: kcmatrix
points_of_interest =  114
uwr_to_unr =  98
uwr_to_unr_percent =  0.85965
cyc_to_unr =  14
cyc_to_unr_percent =  0.12281
octave:7> sliced_charts
Enter matrix e.g. gcmatrix: ojmatrix
points_of_interest =  129
uwr_to_unr =  109
uwr_to_unr_percent =  0.84496
cyc_to_unr =  17
cyc_to_unr_percent =  0.13178
octave:8> sliced_charts
Enter matrix e.g. gcmatrix: sbmatrix
points_of_interest =  141
uwr_to_unr =  113
uwr_to_unr_percent =  0.80142
cyc_to_unr =  23
cyc_to_unr_percent =  0.16312
octave:9> sliced_charts
Enter matrix e.g. gcmatrix: ctmatrix
points_of_interest =  114
uwr_to_unr =  90
uwr_to_unr_percent =  0.78947
cyc_to_unr =  16
cyc_to_unr_percent =  0.14035
octave:10> sliced_charts
Enter matrix e.g. gcmatrix: lbmatrix
points_of_interest =  113
uwr_to_unr =  89
uwr_to_unr_percent =  0.78761
cyc_to_unr =  19
cyc_to_unr_percent =  0.16814
octave:11> sliced_charts
Enter matrix e.g. gcmatrix: hgmatrix
points_of_interest =  132
uwr_to_unr =  118
uwr_to_unr_percent =  0.89394
cyc_to_unr =  14
cyc_to_unr_percent =  0.10606
octave:12> sliced_charts
Enter matrix e.g. gcmatrix: smatrix
points_of_interest =  141
uwr_to_unr =  118
uwr_to_unr_percent =  0.83688
cyc_to_unr =  20
cyc_to_unr_percent =  0.14184
octave:13> sliced_charts
Enter matrix e.g. gcmatrix: smmatrix
points_of_interest =  140
uwr_to_unr =  110
uwr_to_unr_percent =  0.78571
cyc_to_unr =  29
cyc_to_unr_percent =  0.20714
octave:14> sliced_charts
Enter matrix e.g. gcmatrix: bomatrix
points_of_interest =  121
uwr_to_unr =  97
uwr_to_unr_percent =  0.80165
cyc_to_unr =  16
cyc_to_unr_percent =  0.13223
octave:15> sliced_charts
Enter matrix e.g. gcmatrix: cmatrix
points_of_interest =  128
uwr_to_unr =  103
uwr_to_unr_percent =  0.80469
cyc_to_unr =  23
cyc_to_unr_percent =  0.17969
octave:16> sliced_charts
Enter matrix e.g. gcmatrix: omatrix
points_of_interest =  128
uwr_to_unr =  104
uwr_to_unr_percent =  0.81250
cyc_to_unr =  20
cyc_to_unr_percent =  0.15625
octave:17> sliced_charts
Enter matrix e.g. gcmatrix: wmatrix
points_of_interest =  105
uwr_to_unr =  91
uwr_to_unr_percent =  0.86667
cyc_to_unr =  11
cyc_to_unr_percent =  0.10476
octave:18> sliced_charts
Enter matrix e.g. gcmatrix: lcmatrix
points_of_interest =  144
uwr_to_unr =  119
uwr_to_unr_percent =  0.82639
cyc_to_unr =  23
cyc_to_unr_percent =  0.15972
octave:19> sliced_charts
Enter matrix e.g. gcmatrix: fcmatrix
points_of_interest =  132
uwr_to_unr =  110
uwr_to_unr_percent =  0.83333
cyc_to_unr =  19
cyc_to_unr_percent =  0.14394
octave:20> sliced_charts
Enter matrix e.g. gcmatrix: lhmatrix
points_of_interest =  136
uwr_to_unr =  113
uwr_to_unr_percent =  0.83088
cyc_to_unr =  21
cyc_to_unr_percent =  0.15441
octave:21> sliced_charts
Enter matrix e.g. gcmatrix: gcmatrix
points_of_interest =  137
uwr_to_unr =  118
uwr_to_unr_percent =  0.86131
cyc_to_unr =  16
cyc_to_unr_percent =  0.11679
octave:22> sliced_charts
Enter matrix e.g. gcmatrix: simatrix
points_of_interest =  121
uwr_to_unr =  102
uwr_to_unr_percent =  0.84298
cyc_to_unr =  15
cyc_to_unr_percent =  0.12397
octave:23> sliced_charts
Enter matrix e.g. gcmatrix: plmatrix
points_of_interest =  149
uwr_to_unr =  126
uwr_to_unr_percent =  0.84564
cyc_to_unr =  18
cyc_to_unr_percent =  0.12081
octave:24> sliced_charts
Enter matrix e.g. gcmatrix: pamatrix
points_of_interest =  129
uwr_to_unr =  103
uwr_to_unr_percent =  0.79845
cyc_to_unr =  22
cyc_to_unr_percent =  0.17054
octave:25> sliced_charts
Enter matrix e.g. gcmatrix: usmatrix
points_of_interest =  143
uwr_to_unr =  122
uwr_to_unr_percent =  0.85315
cyc_to_unr =  16
cyc_to_unr_percent =  0.11189
octave:26> sliced_charts
Enter matrix e.g. gcmatrix: tymatrix
points_of_interest =  148
uwr_to_unr =  128
uwr_to_unr_percent =  0.86486
cyc_to_unr =  17
cyc_to_unr_percent =  0.11486
octave:27> sliced_charts
Enter matrix e.g. gcmatrix: edmatrix
points_of_interest =  150
uwr_to_unr =  118
uwr_to_unr_percent =  0.78667
cyc_to_unr =  26
cyc_to_unr_percent =  0.17333
octave:28> sliced_charts
Enter matrix e.g. gcmatrix: dxmatrix
points_of_interest =  141
uwr_to_unr =  122
uwr_to_unr_percent =  0.86525
cyc_to_unr =  14
cyc_to_unr_percent =  0.099291
octave:29> sliced_charts
Enter matrix e.g. gcmatrix: spmatrix
points_of_interest =  158
uwr_to_unr =  123
uwr_to_unr_percent =  0.77848
cyc_to_unr =  31
cyc_to_unr_percent =  0.19620
octave:30> sliced_charts
Enter matrix e.g. gcmatrix: esmatrix
points_of_interest =  160
uwr_to_unr =  125
uwr_to_unr_percent =  0.78125
cyc_to_unr =  30
cyc_to_unr_percent =  0.18750
octave:31> sliced_charts
Enter matrix e.g. gcmatrix: ndmatrix
points_of_interest =  129
uwr_to_unr =  116
uwr_to_unr_percent =  0.89922
cyc_to_unr =  12
cyc_to_unr_percent =  0.093023
octave:32> sliced_charts
Enter matrix e.g. gcmatrix: eurusdmatrix
points_of_interest =  79
uwr_to_unr =  69
uwr_to_unr_percent =  0.87342
cyc_to_unr =  9
cyc_to_unr_percent =  0.11392
octave:33> sliced_charts
Enter matrix e.g. gcmatrix: gbpusdmatrix
points_of_interest =  84
uwr_to_unr =  72
uwr_to_unr_percent =  0.85714
cyc_to_unr =  11
cyc_to_unr_percent =  0.13095
octave:34> sliced_charts
Enter matrix e.g. gcmatrix: usdchfmatrix
points_of_interest =  80
uwr_to_unr =  73
uwr_to_unr_percent =  0.91250
cyc_to_unr =  6
cyc_to_unr_percent =  0.075000
octave:35> sliced_charts
Enter matrix e.g. gcmatrix: usdyenmatrix
points_of_interest =  74
uwr_to_unr =  62
uwr_to_unr_percent =  0.83784
cyc_to_unr =  8
cyc_to_unr_percent =  0.10811
octave:36> sliced_charts
Enter matrix e.g. gcmatrix: eurchfmatrix
points_of_interest =  93
uwr_to_unr =  80
uwr_to_unr_percent =  0.86022
cyc_to_unr =  10
cyc_to_unr_percent =  0.10753
octave:37> sliced_charts
Enter matrix e.g. gcmatrix: eurgbpmatrix
points_of_interest =  91
uwr_to_unr =  78
uwr_to_unr_percent =  0.85714
cyc_to_unr =  11
cyc_to_unr_percent =  0.12088
octave:38> sliced_charts
Enter matrix e.g. gcmatrix: euryenmatrix
points_of_interest =  103
uwr_to_unr =  88
uwr_to_unr_percent =  0.85437
cyc_to_unr =  14
cyc_to_unr_percent =  0.13592
octave:39> sliced_charts
Enter matrix e.g. gcmatrix: eurausmatrix
points_of_interest =  73
uwr_to_unr =  64
uwr_to_unr_percent =  0.87671
cyc_to_unr =  9
cyc_to_unr_percent =  0.12329
octave:40> sliced_charts
Enter matrix e.g. gcmatrix: eurcadmatrix
points_of_interest =  76
uwr_to_unr =  56
uwr_to_unr_percent =  0.73684
cyc_to_unr =  17
cyc_to_unr_percent =  0.22368
octave:41> sliced_charts
Enter matrix e.g. gcmatrix: usdcadmatrix
points_of_interest =  73
uwr_to_unr =  60
uwr_to_unr_percent =  0.82192
cyc_to_unr =  13
cyc_to_unr_percent =  0.17808
octave:42> sliced_charts
Enter matrix e.g. gcmatrix: gbpchfmatrix
points_of_interest =  98
uwr_to_unr =  84
uwr_to_unr_percent =  0.85714
cyc_to_unr =  12
cyc_to_unr_percent =  0.12245
octave:43> sliced_charts
Enter matrix e.g. gcmatrix: gbpyenmatrix
points_of_interest =  97
uwr_to_unr =  84
uwr_to_unr_percent =  0.86598
cyc_to_unr =  11
cyc_to_unr_percent =  0.11340
octave:44> sliced_charts
Enter matrix e.g. gcmatrix: auscadmatrix
points_of_interest =  107
uwr_to_unr =  92
uwr_to_unr_percent =  0.85981
cyc_to_unr =  14
cyc_to_unr_percent =  0.13084
octave:45> sliced_charts
Enter matrix e.g. gcmatrix: aususdmatrix
points_of_interest =  97
uwr_to_unr =  82
uwr_to_unr_percent =  0.84536
cyc_to_unr =  14
cyc_to_unr_percent =  0.14433
octave:46> sliced_charts
Enter matrix e.g. gcmatrix: ausyenmatrix
points_of_interest =  103
uwr_to_unr =  89
uwr_to_unr_percent =  0.86408
cyc_to_unr =  11
cyc_to_unr_percent =  0.10680
  • The clmatrix indicates which market is being tested e.g. crude oil (cl)
  • points of interest is the number of times the market mode changes to "up with no retracement" from a previously different classification
  • uwr_to_unr is the number of times such a change is from "up with retracement" to "up with no retracement"
  • cyc_to_unr is similarly from "cyclic" to "up with no retracement"
  • the *_percent is the previous two expressed as a percentage of the points of interest
It is pleasing to see such high percentages overall. I will perform a similar analysis for "down with retracement" and discuss the significance of this in my next post.

Friday, 30 March 2012

Kalman Filter Octave Coding Completed

I am pleased to say that the first phase of my Kalman filter coding, namely writing Octave code, is now complete. In doing so I have used/adapted code from the MATLAB toolbox available here. The second phase of coding, at some future date, will be to convert this code into a C++ .oct function. My code is a stripped down version of the 2D CWPA demo, which models price as a moving object with position and velocity, and which is described in detail with my model assumptions below.

The first thing I had to decide was what to actually model, and I decided on VWAP. The framework of the Kalman filter is that it tracks an underlying process that is not necessarily directly observable but for which measurements are available. VWAP calculated from OHLC bars fits this framework nicely. If one had access to high frequency daily tick data the VWAP could be calculated exactly, but since the only information available for my purposes is the daily OHLC, the daily OHLC approximation of VWAP is the observable measurement of the "unobservable" exact VWAP.

The next thing I considered was the measurement noise of the filter. Some algebraic manipulation of the VWAP approximation formula (see here) led me to choose two thirds (or 0.666) of the Hi-Lo range of the bar as the measurement noise associated with any single VWAP approximation, this being the maximum possible range of values that the VWAP can take given a bar's OHLC values.

Finally, for the process noise I employed a simple heuristic of the noise being half the bar to bar variation in successive VWAPs, the other half in this assumption being attributable to the process itself.

Having decided on the above the next step was to initialise the filter covariances, and to do this I decided to use the Median Absolute Deviation (MAD) of the noise processes as a consistent estimator of the standard deviation and use the scale factor of 1.4826 for normally distributed data (the Kalman filter assumes Gaussian noise) to calculate the noise variances (see this wiki for more details.) However, I had a concern with "look ahead bias" with this approach but a simple test dispelled these fears. This code box

   1279.9   1279.9   1279.9   1279.9   1279.9   1279.9   1279.9   1279.9
   1284.4   1284.4   1284.4   1284.4   1284.4   1284.4   1284.4   1284.4
   1284.0   1284.0   1284.0   1284.0   1284.0   1284.0   1284.0   1284.0
   1283.3   1283.3   1283.3   1283.3   1283.3   1283.3   1283.3   1283.3
   1288.2   1288.2   1288.2   1288.2   1288.2   1288.2   1288.2   1288.2
   1298.8   1298.7   1298.7   1298.8   1298.7   1298.7   1298.7   1298.7
   1305.0   1305.0   1305.0   1305.0   1305.0   1305.0   1305.0   1305.0
   1306.1   1306.2   1306.2   1306.1   1306.2   1306.2   1306.2   1306.2
   1304.9   1305.0   1305.0   1304.9   1305.0   1305.0   1305.0   1305.0
   1308.3   1308.3   1308.3   1308.3   1308.3   1308.3   1308.3   1308.3
   1312.0   1312.0   1312.0   1312.0   1312.0   1312.0   1312.0   1312.0
   1309.1   1309.1   1309.1   1309.1   1309.1   1309.1   1309.1   1309.1
   1304.3   1304.3   1304.3   1304.3   1304.3   1304.3   1304.3   1304.3
   1302.3   1302.3   1302.3   1302.3   1302.3   1302.3   1302.3   1302.3
   1306.5   1306.5   1306.5   1306.5   1306.4   1306.4   1306.4   1306.4
   1314.6   1314.5   1314.5   1314.6   1314.5   1314.5   1314.5   1314.5
   1325.1   1325.0   1325.0   1325.1   1325.0   1325.0   1325.0   1325.0
   1332.7   1332.7   1332.7   1332.7   1332.7   1332.7   1332.7   1332.7
   1336.7   1336.8   1336.8   1336.7   1336.8   1336.8   1336.8   1336.8
   1339.7   1339.8   1339.8   1339.7   1339.8   1339.8   1339.8   1339.8
   1341.6   1341.7   1341.7   1341.6   1341.7   1341.7   1341.7   1341.7
   1338.3   1338.4   1338.4   1338.3   1338.4   1338.4   1338.4   1338.4
   1340.6   1340.6   1340.6   1340.6   1340.6   1340.6   1340.6   1340.6
   1341.1   1341.1   1341.1   1341.1   1341.1   1341.1   1341.1   1341.1
   1340.4   1340.4   1340.4   1340.4   1340.3   1340.3   1340.3   1340.3
   1341.3   1341.3   1341.3   1341.3   1341.3   1341.3   1341.3   1341.3
   1349.7   1349.7   1349.7   1349.7   1349.6   1349.6   1349.6   1349.6
   1357.6   1357.6   1357.6   1357.6   1357.5   1357.5   1357.5   1357.5
   1355.2   1355.3   1355.3   1355.2   1355.3   1355.3   1355.3   1355.3
   1353.6   1353.6   1353.6   1353.6   1353.6   1353.6   1353.6   1353.6
   1356.6   1356.6   1356.6   1356.6   1356.6   1356.6   1356.6   1356.6
   1358.2   1358.2   1358.2   1358.2   1358.2   1358.2   1358.2   1358.2
   1362.8   1362.7   1362.7   1362.8   1362.7   1362.7   1362.7   1362.7
   1362.7   1362.7   1362.7   1362.7   1362.7   1362.7   1362.7   1362.7
   1362.6   1362.6   1362.6   1362.6   1362.6   1362.6   1362.6   1362.6
   1365.1   1365.1   1365.1   1365.1   1365.1   1365.1   1365.1   1365.1
   1360.8   1360.9   1360.9   1360.8   1360.9   1360.9   1360.9   1360.9
   1348.8   1348.9   1348.9   1348.8   1348.9   1348.9   1348.9   1348.9
   1340.8   1340.8   1340.8   1340.8   1340.8   1340.8   1340.8   1340.8
   1349.0   1348.9   1348.9   1349.0   1348.9   1348.9   1348.9   1348.9
   1361.7   1361.6   1361.6   1361.7   1361.5   1361.5   1361.5   1361.5
   1368.0   1368.0   1368.0   1368.0   1367.9   1367.9   1367.9   1367.9
   1379.2   1379.2   1379.2   1379.2   1379.2   1379.2   1379.2   1379.2
   1390.3   1390.4   1390.4   1390.3   1390.4   1390.4   1390.4   1390.4
   1394.1   1394.2   1394.2   1394.1   1394.2   1394.2   1394.2   1394.2
   1397.7   1397.8   1397.8   1397.7   1397.8   1397.8   1397.8   1397.8
   1400.6   1400.6   1400.6   1400.6   1400.6   1400.6   1400.6   1400.6
   1400.8   1400.8   1400.8   1400.8   1400.8   1400.8   1400.8   1400.8
   1399.2   1399.2   1399.2   1399.2   1399.2   1399.2   1399.2   1399.2
   1393.2   1393.2   1393.2   1393.2   1393.2   1393.2   1393.2   1393.2
   1389.3   1389.3   1389.3   1389.3   1389.3   1389.3   1389.3   1389.3
shows the last 50 values of the Kalman filter with different amounts of data used for the calculations for the initialisation of the filter. The leftmost column shows filter values using all available data for initialisation, the next all data except the most recent 50 values, then all data except the most recent 100 values etc. with the rightmost column being calculated using all data except for the most recent 350 values. This last column is akin to using the data through to the end of 2010, and nothing after this date. Comparison between the left and rightmost columns shows virtually insignificant differences. If one were to begin trading the right hand edge of the chart today, initialisation would be done using all available data. If one then traded for the next one and a half years and then re-initialised the filter using all this "new" data, there would be no practical difference in the filter values over this one and a half year period. So, although there may be "look ahead bias," frankly it doesn't matter. Such is the power of robust statistics and the recursive calculations of the Kalman filter combined!

This next code box shows my Octave code for the Kalman filter
data = load("-ascii","esmatrix") ;
tick = 0.25 ;

n = length(data(:,4))
finish = input('enter finish, no greater than n  ')

if ( finish > length(data(:,4)) )
   finish = 0 % i.e. all available data is used
end

open = data(:,4) ;
high = data(:,5) ;
low = data(:,6) ;
close = data(:,7) ;
market_type = data(:,230) ;

clear data

vwap = round( ( ( open .+ close .+ ( (high .+ low) ./ 2 ) ) ./ 3 ) ./ tick) .* tick ;
vwap_process_noise = ( vwap .- shift(vwap,1) ) ./ 2.0 ;
median_vwap_process_noise = median(vwap_process_noise(2:end-finish,1)) ;
vwap_process_noise_deviations = vwap_process_noise(2:end-finish,1) .- median_vwap_process_noise ;
MAD_process_noise = median( abs( vwap_process_noise_deviations ) ) ;

% convert this to variance under the assumption of a normal distribution
std_vwap_noise = 1.4826 * MAD_process_noise ;
process_noise_variance = std_vwap_noise * std_vwap_noise 

measurement_noise = 0.666 .* ( high .- low ) ;
median_measurement_noise = median( measurement_noise(1:end-finish,1) ) ;
measurement_noise_deviations = measurement_noise(1:end-finish,1) .- median_measurement_noise ;
MAD_measurement_noise = median( abs( measurement_noise_deviations ) ) ;

% convert this to variance under the assumption of a normal distribution
std_measurement_noise = 1.4826 * MAD_measurement_noise ;
measurement_noise_variance = std_measurement_noise * std_measurement_noise

% Transition matrix for the continous-time system.
F = [0 0 1 0 0 0;
     0 0 0 1 0 0;
     0 0 0 0 1 0;
     0 0 0 0 0 1;
     0 0 0 0 0 0;
     0 0 0 0 0 0];

% Noise effect matrix for the continous-time system.
L = [0 0;
     0 0;
     0 0;
     0 0;
     1 0;
     0 1];

% Process noise variance
q = process_noise_variance ;
Qc = diag([q q]);

% Discretisation of the continuous-time system.
[A,Q] = lti_disc(F,L,Qc,1); % last item is dt stepsize set to 1

% Measurement model.
H = [1 0 0 0 0 0;
     0 1 0 0 0 0];

% Variance in the measurements.
r1 = measurement_noise_variance ;
R = diag([r1 r1]);

% Initial guesses for the state mean and covariance.
m = [0 vwap(1,1) 0 0 0 0]';
P = diag([0.1 0.1 0.1 0.1 0.5 0.5]) ;

% Space for the estimates.
MM = zeros(size(m,1), length(vwap));

% create vectors for eventual plotting
predict_plot = zeros(length(vwap),1) ;
MM_plot = zeros(length(vwap),1) ;
sigmaP_plus = zeros(length(vwap),1) ;
sigmaP_minus = zeros(length(vwap),1) ;

% Filtering steps.
for ii = 1:length(vwap)
   [m,P] = kf_predict(m,P,A,Q);

   predict_plot(ii,1) = m(2,1) ;

   [m,P] = kf_update(m,P,vwap(ii,:),H,R);
   MM(:,ii) = m;

   MM_plot(ii,1) = m(2,1) ;

   % sigmaP is for storing the current error covariance for plotting purposes
   sigmaP = sqrt(diag(P)) ; 
   sigmaP_plus(ii,1) = MM_plot(ii,1) + 2 * sigmaP(1) ;
   sigmaP_minus(ii,1) = MM_plot(ii,1) - 2 * sigmaP(1) ;
end

% output in terminal for checking purposes
kalman_last_50 = [kalman_last_50,MM_plot(end-50:end,1)] 

% output for plotting in Gnuplot
x_axis = ( 1:length(vwap) )' ;
A = [x_axis,open,high,low,close,vwap,MM_plot,sigmaP_plus,sigmaP_minus,predict_plot,market_type] ;
dlmwrite('my_cosy_kalman_plot',A)
Note that this code calls three functions; lti_disc, kf_predict and kf_update; which are part of the above mentioned MATLAB toolbox. If readers wish to replicate my results, they will have to download said toolbox and put these functions where they may be called by this script.

Below is a screen shot of my Kalman filter in action.
This shows the S & P E-mini contact (daily bars) up to a week or so ago. The white line is the Kalman filter, the dotted white lines are the plus and minus 2 sigma levels taken from the covariance matrix and the red and light blue triangles show the output of the kf_predict function, prior to being updated by the kf_update function, but only shown if above (red) or below (blue) the 2 sigma level. As can be seen, while price is obviously trending most points are with these levels. The colour coding of the bars is based upon the market type as determined by my Naive Bayesian Classifier, Mark 2.

This next screen shot
shows price bars immediately prior to the first screen shot where price is certainly not trending, and it is interesting to note that the kf_predict triangles are now appearing at the turns in price. This fact may mean that the kf_predict function might be a complementary indicator to my Perfect Oscillator function
and Delta
along with my stable of other turn indicators. The next thing I will have to do is come up with a robust rule set that combines all these disparate indicators into a coherent whole. Also, I am now going to use the Kalman filter output as the input to all my other indicators. Up till now I have been using the typical price; (High+Low+Close)/3; as my input but I think the Kalman filtered VWAP for "today's" price action is a much more meaningful price input than "tomorrow's" pivot point!

Wednesday, 14 March 2012

Kalman Filter - Youtube Video Tutorial

In my travels around the internet as part of research on the Kalman filter I have found this youtube tutorial which, although quite chatty, is a good introduction and as an added bonus the MATLAB/Octave code is also supplied. A typical plot of this code is:
where
  •  cyan is the noisy measurement
  • red is the underlying trajectory (hardly discernible as it lies under the plot of the filter)
  • green is the Kalman filter output
I'd say that's pretty impressive filtering!

P.S. more introductory sites here, here and here; some MATLAB code here, here and here. Closely related to Kalman filters are particle filters (R package info. here and here.)

Sunday, 11 March 2012

Kalman Filter

Over the years, on and off, I have tried to find code or otherwise code for myself a Kalman filter but unfortunately I have never really found what I want; the best I have at the moment is an implementation that is available from the technical papers and seminars section at the MESA Software web page. However, I recently read this R-Bloggers post which inspired me to look again for code on the web, and this time I found this, which is exactly what I want; accessible Octave like code that will enable me to fully understand (I hope!) the theory behind the Kalman filter and to be able to code my own Kalman filter function. After a little tinkering with the code (mostly plotting and inputs) a typical script run produces this plot:
which is a plot of a sine wave where
  • red is the underlying price (sine wave plus noise); e.g. typical price, vwap, close etc.
  • the yellow dots are "measurement noise;" e.g. high-low range
  • cyan is the Kalman filter itself
  • green are the 2 Sigma confidence levels for the filter
  • magenta is my current "MESA" implementation
I particularly like this example script as it mirrors the approach I have taken in the past with regard to creating my "idealised" sine wave time series for development purposes. I think the screen shot speaks for itself; the Kalman filter seems uncannily accurate in filtering out the noise to get the "true" underlying signal, with almost no lag at all! I shall definitely be doing some work with this in the very near future.