Welcome to PamExx.com. This is a website where I post my thoughts on stuff, (mostly quantitative programs and models, with some tennis thrown in). Please click the links on the left to visit my blog or to contact me with any question or ideas of your own.

 

Cointegration Program

I have been working on a cointegration trading program for FX. I am currently using MQL4 to pass time series data to R for analysis. Finally got it working tonight, here is a picture of the output that is produced. Trading functionality (and more) to come.

Temp_Pic.jpeg

R checks for cointegration via an ADF test for all possible pair combinations, and produces a plot of the standardized spread between the two FX pairs that have the most significant ADF p-value and whose current price |Z-Score| >2. Here is the plot of the standardized spread between EurUsd and EurCad, along with the Z-Score for the current price and the hedge ratio at the top.

I have been working on some MQL4 programs with an R interface. First step, pass the time series data from MQL4 to R and run some R code. Here is a simple plot, I am just using it to test that the time series data is properly being passed to R and that I can pass and run R code or call an R script file from the MQL4 source code. This will be used in a cointegrated trading program, I will keep posting updates.

AudCad.jpeg

Quantitative Trading - Book by EP Chan

I started reading the book Quantitative Trading How to Build Your Own Algorithmic Trading Business by EP Chan (I am also a reader of his blog: epchan.blogspot.com) and I have started to write code in R for the trading strategies that he examines in his book (he uses in MATLAB). If you are reading the book and cannot afford MATLAB I will be posing my R code as I read through his book (mine will be slightly different, any deviations will be explained in the comments, you can always e-mail me if you have any questions). Here is the code as well as the output plots. I probably have a million mistakes so feel free to let me know if you find any (or want to chat about the book/code/etc). Clearly the "trading strategy" that I coded is a P.O.S. , I just used it make sure I could get all of the code mechanics working properly. I will code up actual strategies in the future.

Note: If you want to run my code on your own machine you will have to change the file paths that are referenced in my code for the data import. If you want a copy of the dataset just send me an e-mail.

thomas@pamexx.com.

 

 

 

## Created By: Thomas Brittain <thomas@pamexx.com>
## Note: In my comments I use the terms Fit Sample and Holdout Sample instead of
##Training Set and Test Set

## Set the working directory from which to import the data files
setwd("C:/Users/Tom/Desktop/ProgrammingStuff/R");

## To call the script: source("C:/Users/Tom/Desktop/ProgrammingStuff/R/EpChanBook/pg56.R")

## Read in the dataset GLD_GDX.csv and assign it to GLD_GDX
## Note: I created this dataset in excel out of the dataset available from books website
GLD_GDX <- read.csv("EpChanBook/GLD_GDX.csv", header = TRUE )

## GLD_GDX column 7: adjusted close prices for GLD, referenced by GLD_GDX[1:252,7]
## GLD_GDX column 13: adjusted close prices for GDX, referenced by GLD_GDX[1:252,13]

## Use OLS regression to determine the hedge ratio on the Fit Sample
## The Beta values will be stored in the vector named Betas

Betas <- as.vector(coef(lm(GLD_GDX[1:252,7] ~ GLD_GDX[1:252,13])))

## Note: Betas[2] is used as the hedge ratio. Betas[1] is the regression intercept.

## Create the Fit Sample Spread and save the spread series in a vector named Spread

Spread <- vector(length=252)

Spread[1:252] <- GLD_GDX[1:252,7] - Betas[2]*GLD_GDX[1:252,13]

## Plot the Spread time series

plot(Spread, type="o", col="blue")

## Calculate the mean and Standard Deviation of the spread on the Fit Sample,
## assign the values to SpreadMean and SpreadStd
SpreadMean <- mean(Spread)
SpreadStd <- sd(Spread)

## Calculate the Z-Score of the spread at each point as assign the series to ZScore
ZScore <- (Spread - SpreadMean) / SpreadStd

## Initialize the four price variables ShortEntry, LongEntry, ShortClose and LongClose,
## the two position indicator variables ShortPosition and LongPosition and the profit and
## loss vector PnL
ShortEntry <- 0
LongEntry <- 0
ShortClose <- 0
LongClose <- 0
PnL <- c(0)

## Create a loop that will cycle through the spreads over time and calculate profit and loss
## Only allow one position at a time, open a long/short if the Z-Score is
## less than / greater than -2 / +2 standard deviations from the mean, close a long/short if the
## Z-Score is greater than / less than -1 / +1 standard deviations from the mean
## NOTE: There are NO STOP LOSSES under this particular scenario!

i <- 1
while(i < length(Spread)+1){
  ## Buy(long) position entry loop
  if(ZScore[i] < -2 & LongEntry == 0){
    LongEntry <- Spread[i]
  }
 
  ## Buy(long) position close loop
  if(ZScore[i] > -1 & LongEntry != 0){
    LongClose <- Spread[i]
    PnL <- union(PnL, c((LongClose - LongEntry)))
    LongEntry <- 0
  }
 
  ## Short(sell) position entry loop
  if(ZScore[i] > 2 & ShortEntry == 0){
    ShortEntry <- Spread[i]
  }
 
  ## Short(sell) position close loop
  if(ZScore[i] < 1 & ShortEntry != 0){
    ShortClose <- Spread[i]
    PnL <- union(PnL, c((ShortEntry - ShortClose)))
    ShortEntry <- 0
  }
 
  i <- i+1
 
} # End while loop

## Create a vector that is the sum of all profits and losses (in this case we can only have
## profits due to the way that the trading simulation was designed, i.e. No stop losses!)

## Initialize a SumPnL vector and set its length to the same length as the PnL vector
SumPnL <- vector(length=length(PnL))

## Assign all of the elements in the SumPnL vector
## Begin by assigning the first element to zero
SumPnL[1] <- 0

i <- 2
while(i < length(SumPnL)+1){
  SumPnL[i] <- SumPnL[i-1] + PnL[i]
  i <- i+1
}

## plot the PnL
plot(SumPnL, type="o", col="green")


## Calculate the Sharpeatio
SharpeRatio <- sqrt(252)*mean(PnL)/sd(PnL)

## Note: The Sharpe Ratio here is different (unrealistically high) from the Sharpe Ratio
## in the book because different trading strategies are used

############################################################
## Now examine the same strategy over the Hold Out Sample ##
############################################################

## The Hold Out Sample dataset is referenced by GLD_GDX[253:385,7] and GLD_GDX[253:385,13]

## Create the Hold Out Sample Spread and save the spread series in a vector named HoldOutSpread
HoldOutSpread <- vector(length=133)

## Keep the same HedgeRatio that was calculated in the Fit Sample
HoldOutSpread[1:133] <- GLD_GDX[253:385,7] - Betas[2]*GLD_GDX[253:385,13]

## Plot the HoldOutSpread time series

plot(HoldOutSpread, type="o", col="blue")

## Calculate the mean and Standard Deviation of the spread on the Hold Out Sample,
## assign the values to HoldOutSpreadMean and HoldOutSpreadStd
HoldOutSpreadMean <- mean(HoldOutSpread)
HoldOutSpreadStd <- sd(HoldOutSpread)

## Calculate the Z-Score of the Hold Out spread at each point as assign the series
## to HoldOutZScore
HoldOutZScore <- (HoldOutSpread - HoldOutSpreadMean) / HoldOutSpreadStd

## Initialize the four price variables HoldOutShortEntry, HoldOutLongEntry,
## HoldOutShortClose and HoldOutLongClose, the two position indicator variables
## HoldOutShortPosition and HoldOutLongPosition and the profit and loss vector HoldOutPnL
HoldOutShortEntry <- 0
HoldOutLongEntry <- 0
HoldOutShortClose <- 0
HoldOutLongClose <- 0
HoldOutPnL <- c(0)

## Create a loop that will cycle through the spreads over time and calculate profit and loss.
## Only allow one position at a time, open a long/short if the Z-Score is
## less than / greater than -2 / +2 standard deviations from the mean, close a long/short if the
## Z-Score is greater than / less than -1 / +1 standard deviations from the mean
## NOTE: There are NO STOP LOSSES under this particular scenario!

i <- 1
while(i < length(HoldOutSpread)+1){
  ## Buy(long) position entry loop
  if(HoldOutZScore[i] < -2 & HoldOutLongEntry == 0){
    HoldOutLongEntry <- HoldOutSpread[i]
  }
 
  ## Buy(long) position close loop
  if(HoldOutZScore[i] > -1 & HoldOutLongEntry != 0){
    HoldOutLongClose <- HoldOutSpread[i]
    HoldOutPnL <- union(HoldOutPnL, c((HoldOutLongClose - HoldOutLongEntry)))
    HoldOutLongEntry <- 0
  }
 
  ## Short(sell) position entry loop
  if(HoldOutZScore[i] > 2 & HoldOutShortEntry == 0){
    HoldOutShortEntry <- HoldOutSpread[i]
  }
 
  ## Short(sell) position close loop
  if(HoldOutZScore[i] < 1 & HoldOutShortEntry != 0){
    HoldOutShortClose <- HoldOutSpread[i]
    HoldOutPnL <- union(HoldOutPnL, c((HoldOutShortEntry - HoldOutShortClose)))
    HoldOutShortEntry <- 0
  }
 
  i <- i+1
 
} # End while loop

## Create a vector that is the sum of all profits and losses (in this case we can only have
## profits due to the way that the trading simulation was designed, i.e. No stop losses!)

## Initialize a HoldOutSumPnL vector and set its length to the same length as the
## HoldOutPnL vector
HoldOutSumPnL <- vector(length=length(HoldOutPnL))

## Assign all of the elements in the HoldOutSumPnL vector
## Begin by assigning the first element to zero
HoldOutSumPnL[1] <- 0

i <- 2
while(i < length(HoldOutSumPnL)+1){
  HoldOutSumPnL[i] <- HoldOutSumPnL[i-1] + HoldOutPnL[i]
  i <- i+1
}

## plot the PnL
plot(HoldOutSumPnL, type="o", col="green")


## Calculate the HoldOutSharpeatio
HoldOutSharpeRatio <- sqrt(252)*mean(HoldOutPnL)/sd(HoldOutPnL)

I am currently working on a Monte Carlo method for evaluating trading strategies using Auto Regressive models in the programming language R. I will post snippets of code with an explanation of what they are doing and how they work as I write in case anybody else may be interested in how I go about creating these types of programs. When I finish the program I will post all of the source code and hopefully some of you may find it interesting or useful for your own experimentation or trading. I will try to post as often as I have free time.

If you are interested in following along you can download R at:

http://www.r-project.org/

Please feel free to e-mail me at thomas@pamexx.com with any questions or comments (and if you have any idea how to use concrete5 please let me know bc I have no idea how to set up the blog portion).

 

Here is the first section of code, for now please don't worry about the variable named CurrentPriceIndex as it will make sense later. Oh and I will also explain why a price series needs to be of length 101,025 instead of some ordinary round number like 100,000, and also ignore the outer while loop for now, all will be explained in the next post.

CODE:

# Set the current price index named CurrentPriceIndex

    CurrentPriceIndex = 1014;

    # (Step 1) Create a price series of length 101,025 called RawPriceSeries with
    # elements sampled from a normal distribution with mean = r_mean and std dev = r_std_dev
    # RawPriceSeires is the series that the strategy will be tested upon during each
    # iteration of the outer while loop.

    RawPriceSeries = vector(length = 101025);

    RawPriceSeries[1] = 1;

    i=2;

    while(i<101026){
        return = rnorm(1, r_mean, r_std_dev);
        RawPriceSeries[i] = RawPriceSeries[i-1]*(1+return);
        i = i+1;
    }