Sunday, August 31, 2014

Considering an ARIMA model

I came across a blog post and a paper describing methods for implementing an ARIMA model as a price forecaster. They both deal with the stock market, which is not  exactly the same as FOREX. But given the lack of ARIMA examples involving FOREX, I've decided to take a look at these:

http://programming-r-pro-bro.blogspot.mx/2013/04/forecasting-stock-returns-using-arima.html
http://www.hindawi.com/journals/jam/2014/614342/

From the blog post it seems that ARIMA doesn't have a very good resolution in it's predictions. The most it seems to be able to predict is a likely dynamic range for the future return rate. The paper shows how an ANN predicts fluctuations more closely. Still: this paper uses a FFNN, whereas the russian paper from one of my earlier posts uses a SRN.

ARIMA seems more limited than the SRN in it's predictive power. Therefor, I have decided to give priority to the SRN approcah using the newelm function from neurolab which I mentioned in an earlier post.

The ARIMA implementation, nevertheless, has inspired me to attempt a new rustic buy-and-hold algorithm. One which takes into account the density of price falls through time, and a likely dynamic range for it. I will be reporting on the results later on.

Kalman filter disappointment

I found two identical implementations for a univariate Kalman filter:

http://greg.czerniak.info/guides/kalman1/
http://bilgin.esme.org/BitsBytes/KalmanFilterforDummies.aspx

From the formulae for Kk and Pk, it seems that all this Kalman filter does is to damp the signal as Kk simply decreases with each iteration. Which is not very impressive.

I wasn't able to extract any "predictive" component from this implementation of the Kalman filter. I've decided to desist with this approach.

Thursday, August 28, 2014

Using a Kalman filter to predict short-term currency price fluctuation

In the previous post I showed that price fluctuations have a gaussian distribution. This has a similarity with gaussian noise. Apparently, there are several ways to predict gaussian time series. One is described here:

http://www.gaussianprocess.org/gpml/chapters/RW.pdf

But it involves concepts which are too advanced and complicated for me at this moment. Besides: gaussian processes are not the same as gaussian noise. It seems that a gaussian process is formally defined as a process involving "multivariate normal distribution". Since the price fluctuations I'm studying have only one variable, I believe using gaussian process prediction methods would be overkill. I might be wrong, though.

Another method is one used to filter out gaussian noise. It's called "Kalman filtering". Kalman filtering seems useful, because it works by predicting gaussian noise in order to eliminate it. Because currency price fluctuations follow a gaussian distribution, I think the predictive component of the Kalman filter may be useful in predicting short-term future fluctuations in currency price.

The following seems like a good, comprehensive, introduction to Kalman filtering:

http://www.cs.unc.edu/~tracker/media/pdf/SIGGRAPH2001_CoursePack_08.pdf

So the basic idea is to treat FOREX price fluctuation as if it were gaussian noise, and try to predict it short-term with a Kalman filter.

I want to be able to predict short-term future price fluctuations because I discovered that sudden and large price decrements produce important losses when using my rustic buy-and-hold algorithm. These predictions may turn out to be useful in setting up an effective predictive stop-loss alarm for my rustic buy-and-hold algorithm.

Sunday, August 24, 2014

Found normal distributions in price changes and price decrement frequencies

I've been measuring the distribution of negative fluctuations in forex market history for the EUR_USD. I discovered that most variations in price (from one instant to another, with a time base of 1 minute) are "small", when compared to larger ones. Sudden, large increments or decrements are scarce. The following plot shows the data from sorted (smallest to largest) price changes (both positive and negative) along many weeks (each week has a different color).


The plot shows a large amount of "small" values near the center (which is zero), and only a few large ones near the sides. This implies that a lapse in time with a high density of price decrements has more probability of containing large negative price fluctuations than a lapse with few price decrements.

To extract the frequency of decrements at any point in time, only the direction of the currency price is required. The following picture shows price decrements in red, and increments in green. Blank spaces represent unknown currency fluctuations in a given instant. What I try to find here is how many red squares there are within a given window. Then I shift that window to the right, and count the red squares again. I keep shifting and counting until I reach the end of the data set.


Interestingly, it turns out the set of frequencies (sampled by shifting the window from top-left to bottom-right) also have a normal distribution. This means there are more "small" frequencies than "big" ones in all consecutive windows of time.


Plotting the histogram for both data-sets showed an approximate normal (Gaussian) distribution.

This shows that the variations in price are mostly smooth, with occasional outbursts. That is: they are more likely to fluctuate within a given (relatively) small range at any given instant. There is a certain stability in currency price change.

First attempt at a rustic buy-and-hold forex bot

I've achieved detecting the beginning of tiny price jumps. Maybe if I tune the program's parameters I can make it find the beginning of larger jumps.

Here's the source code. It's a little rough, fyi:


 /*  
      Rustic buy-and-hold  
      Requires fine tuning. Perhaps make it adaptive.  
   
      Vicente Oscar Mier Vela  
      <vomv1988@gmail.com>  
   
      Example  
      $ cat samples_1407099600_1407531600_M1.dat | ./t6 1407099600 1407531600 60 200 20 20  
   
      Example of samples_START_END_TB.dat  
      $ cat samples_1407099600_1407531600_M1.dat | head  
      1407099600  
      1.34247  
      1.34359  
      1407099660  
      1.34285  
      1.34326  
      1407099720  
      1.34291  
      1.34331  
      1407099780  
   
      This one trys to link density of losses with absolute losses using  
      local average minimum / maximum algorithm.  
 */  
 #include <stdio.h>  
 #include <stdlib.h>  
 #include <math.h>  
   
 #define closeBid 0  
 #define closeAsk 1  
   
 int cmp(const void *x, const void *y);  
   
 int main(int argc, char *argv[]){  
      int startdate = atoi(argv[1]);  
      int enddate = atoi(argv[2]);  
      int timebase = atoi(argv[3]);  
   
      int nsamples = (enddate - startdate) / timebase;  
      int expdate;  
      int currdate;  
   
      double *samples[2];  
      samples[closeBid] = malloc(sizeof(double)*nsamples);  
      samples[closeAsk] = malloc(sizeof(double)*nsamples);  
   
      int i;  
      for(i = 0, expdate = startdate; i < nsamples; i ++, expdate += timebase){  
           scanf("%d", &currdate);  
 /*  
           The following assumes that the first date from the  
           dataset will always yield a value. That is: 1st date is  
           always "ticked".  
 */  
           if(currdate == expdate){  
                scanf("%lf", samples[closeBid] + i);  
                scanf("%lf", samples[closeAsk] + i);  
           } else {  
                while(currdate != expdate && i < nsamples){  
                     samples[closeBid][i] = samples[closeBid][i-1];  
                     samples[closeAsk][i] = samples[closeAsk][i-1];  
                     expdate += timebase;  
                     i++;  
                }  
                if(i < nsamples){  
                     scanf("%lf", samples[closeBid] + i);  
                     scanf("%lf", samples[closeAsk] + i);  
                }  
           }  
      }  
   
      int *diffs = (int *) malloc(sizeof(int) * nsamples);  
      diffs[0] = 0;  
      for(i = 1; i < nsamples; i ++)  
           if(samples[closeBid][i] - samples[closeBid][i - 1] < 0)  
                diffs[i] = -1;  
           else if(samples[closeBid][i] - samples[closeBid][i - 1] > 0)  
                diffs[i] = 1;  
           else  
                diffs[i] = 0;  
   
      int *diffs2 = (int *) malloc(sizeof(int) * nsamples);  
      int k;  
      int range = 10;  
      for(i = 0; i < range; i ++)  
           diffs2[i] = 0;  
      for(i = nsamples; i > nsamples - range; i --)  
           diffs2[i] = 0;  
   
      for(i = range; i < nsamples - range; i ++)  
           for(k = range * -1 ; k < 0 ; k ++){  
                if(diffs[i + k] == -1)  
                     diffs2[i] ++;  
           }  
   
      for(i = 0; i < nsamples; i ++)  
           printf("%d >> %d\n", diffs2[i], diffs[i]);  
   
      int j, t = atoi(argv[4]), mxs = atoi(argv[5]), mns = atoi(argv[6]);  
      double *s = (double *) malloc(sizeof(double) * t);  
      double *q;  
      double sum, loc_avg_min = 0, loc_avg_max = 0, bal = 0;  
      int flag = 0;  
      for(i = 0; i + t < nsamples; i ++){  
           q = samples[closeBid] + i;  
   
           for(j=0;j<t;j++)  
                s[j] = q[j];  
   
           qsort(s, t, sizeof(double), cmp);  
   
           for(j = 0, sum = 0; j < mns; sum += s[j], j ++);  
           loc_avg_min = sum / (double) mns;  
   
           for(j = t - 1, sum = 0; j >= t - mxs; sum += s[j], j --);  
           loc_avg_max = sum / (double) mxs;  
   
           if(flag == 0 && samples[closeBid][i + t - 1] <= loc_avg_min){  
                bal -= samples[closeAsk][i + t - 1];  
                flag = 1;  
           }  
   
           if(flag == 1 && samples[closeBid][i + t - 1] >= loc_avg_max){  
                bal += samples[closeBid][i + t - 1];  
                flag = 0;  
                printf("%f %d\n", bal, diffs2[i + t - 1]);  
           }  
      }  
   
      free(samples[closeBid]);  
      free(samples[closeAsk]);  
      free(s);  
      free(diffs);  
      free(diffs2);  
   
      return 0;  
 }  
   
 int cmp(const void *x, const void *y){  
      double xx = *(double*)x, yy = *(double*)y;  
      if (xx < yy) return -1;  
      if (xx > yy) return 1;  
      return 0;  
 }  

Wednesday, August 20, 2014

Idea for a very rustic buy-and-hold algorithm

From NN's to buy-and-hold. From the relatively complex, to the very simple.

In these last few days I've been imagining a new (?) algorithm for doing simple buy-and-hold trades. The key is in finding the most probable highest and lowest currency prices for a given point in time. It would involve something like taking a list of the last N prices, sorting it, and averaging the top M values to get some "local average maximum" and the last L to get a "local average minimum". Then, placing limit orders with those values. Come to think of it, it sounds a little like RSI's overbought and oversold indicators. But it's not quite it.

Pretty crude. But I'll do it while I work on the NN approach, and see what happens.

Second thoughts about tuning the MACD with a GA

Let's say that I find the most profitable MACD parameters for a certain month or year. This is no guarantee that the same parameters will be profitable next year or month. I think trying to find optimal MACD parameters for a particular dataset would be curve-fitting. I recently found a message board post about skepticism towards technical analysis. It's from a trusted source (the James Randi website [atheist/skeptic/critical thinker here BTW]). Do take a look at the references mentioned by the posters, though:

http://forums.randi.org/showthread.php?t=96372

That said, I've come across a paper which says neural networks do a good job predicting forex market trends (http://arxiv.org/pdf/cond-mat/0304469.pdf). It uses a neural network architecture which is a mix between an Elman and a Joran SRN. I believe it doesn't say what training algorithm they used to teach the network to predict market trends. In any case, I will probably be using RTRL, because it seems less resource-consuming than BPTT. I doubt my crappy computer can handle BPTT for the large amounts of data I plan to feed my SRN. Also, I would like to start with a pure Elman architecture, instead of the "Elman-Jordan" architecture suggested in the paper. I just don't have the expertise in NN's to copy the paper step by step.

I should mention I've never implemented a FFN, much less a SRN. After spending several days looking into the details of how FFN's and SRN's work, I've come up with a new TODO list:

  1. Understand how FFN's work (check)
  2. Understand how SRN's work (check)
  3. Understand the backpropagation algorithm for FFN's
  4. Understand the BPTT algorithm for SRN's
  5. Understand the RTRL algorithm for SRN's
It would seem one can't understand RTRL without first understanding BPTT and the classic BP algorithm, as they are somehow part of RTRL. Fortunately, there are several youtube videos on all of them, as well as some pretty good online resources with worked examples:

https://www.youtube.com/watch?v=yecGyZFyfbQ
https://www.youtube.com/watch?v=hYenZlvBwr4
http://neuralnetworksanddeeplearning.com/chap1.html

From what I've gathered so far, the NN has to be a SRN because those are the ones useful for predicting time series. FFN's are useless for that. But that's all I know so far. I'm still grasping the BP algorithm, in order to implement it.

Also, I've found some interesting NN resources. The only library I've found (and liked) which includes an Elman network generator and trainer is "neurolab" for python:

http://code.google.com/p/neurolab/source/browse/trunk/example/newelm.py?r=26

I like the fact that it's in python because it would allow me to integrate it easily with a shell script. Still: I'd like to implement my own SRN in C. And also: I haven't found out whether neurolab's newelm uses BPTT or RTRL. I don't have enough knowledge about python to re-program the whole learning function for Elman, so I'm probably better off writing everything from scratch in C.

A final word about technical analysis though... I'm not sure if NN's fit into the "technical analysis" category. Maybe not ALL technical analysis is fake. In the Randi boards, they mostly mention the typical indicators: MACD, RSI, CCI, etc. Also, the mathematical analysis in the SRN paper from earlier goes pretty deep into statistics. I should be looking into that too. Grasping the statistics surrounding FOREX seems worthwhile.

So who knows... I guess I'll wait and see what my own experiments tell me.