As a trader it is very important for you to enter the trade in the right direction. For example, you should be pretty sure the market is going to go up before you enter into a buy trade. If you enter a buy trade and market goes down, trade will end up hitting stop loss. If you have a method that can predict market direction with an accuracy above 70%, you can cut your overall risk of losing by 70%. In the last post I told you about R language and software and **how you can use R in improving your trading system**. R is a powerful statistical analysis software and is open source. There are more than 2000 R packages that are available to you that you can use to make your predictive models.

Our aim is to improve our trading system. The strategy is to combine your predictive statistical model with your naked trading model that is solely based on the technical analysis of price action. For example you spot a Gartley pattern on your chart and you have a buy signal. Your statistical predictive model tells you there is a 75% chance market will go down today. What should you do? You should not enter into a buy trade. You will see by the end of the day you have saved yourself from a bad trade.In this post we will discuss how to forecast market direction using a Support Vector Machine (SVM). You can use a support vector machine on any time frame. Suppose you are planning to enter into a buy trade on H1 timeframe. Your SVM model predicts that next candle to be bearish. You can use this prediction to enter at a much better price in the next hour.

### How To Use Support Vector Machine (SVM) In Predicting Market Direction On Any Timeframe?

You should download R software and RStudio software and install it before you continue with this post. Support Vector Machines are non-linear classifiers that use a kernel function to transform a non-linear classification problem into a linear classification problem. Watch the video below that explains what an SVM is:

Support Vector Machine is an important Machine Learning Algorithm. Now you don’t need to master how to do the modelling with an SVM. You should just have a broad understanding what is this algorithm and how you can use to cut your losses in your daily trading.

> # Import the csv file > quotes <- read.csv("E:/MarketData/NZDUSD1440.csv", header=FALSE) > > > > # load quantmod package > library(quantmod) > > #convert the data frame into an xts object > quotes <- as.ts(quotes) > > > #convert time series into a zoo object > quotes1 <- as.zoo(quotes) > > #calculate simple returns > sr <- diff(quotes) / lag(quotes, k = -1, na.pad=TRUE) > > #calculate log returns > > lr <- diff(log(quotes)) > > #number of rows in the dataframe > x <-nrow(sr) > > # lag the data > x1 <- lag(sr, k=-1, na.pad=TRUE) > x2 <- lag(sr, k=-2, na.pad=TRUE) > x3 <- lag(sr, k=-3, na.pad=TRUE) > x4 <- lag(sr, k=-4, na.pad=TRUE) > x5 <- lag(sr, k=-5, na.pad=TRUE) > > # combine all the above matrices into one matrix having close prices > CQuotes <- cbind (x1[ ,6], x2[ ,6], x3[ ,6], x4[ ,6], x5[ ,6], + + sr[,6]) > > > Direction <- ifelse(CQuotes[, 6] >=0, 1, 0) > > CQuotes <- cbind(CQuotes, Direction) > > #name the columsn Open, High, Low, Close, Volume > colnames(CQuotes) <-c('Lag1','Lag2','Lag3','Lag4','Lag5', 'Today', 'Direction') > > #load the kernlab library > > library(kernlab) > > > # train a support vector machine > filter <- ksvm(Direction~.,data=CQuotes[10:x,1:7],kernel="rbfdot", + kpar=list(sigma=0.05),C=5,cross=3) > print(filter) Support Vector Machine object of class "ksvm"

SV type: eps-svr (regression)

parameter : epsilon = 0.1 cost C = 5

Gaussian Radial Basis kernel function.

Hyperparameter : sigma = 0.05

Number of Support Vectors : 1884

Objective Function Value : -3870.229

Training error : 0.285461

Cross validation error : 0.08664

>

> #calculate the time taken to make an SVM

>

> system.time(filter <- ksvm(Direction~.,data=CQuotes[10:x,1:7],kernel=”rbfdot”,

+ kpar=list(sigma=0.05),C=5,cross=3))

> user system elapsed

5.19 0.30 6.65

As you can see from the above calculations it took around 6.65 seconds for R to perform all the above calculations. The training error is 28% meaning the predictive accuracy of this SVM Model is 72%. We have used a radial basis function as the kernel. We need to do some tweaking and see if we can improve the predictive accuracy. The above calculations have been made using 5 lags of simple returns. With these 5 simple return lags we are getting a training error of 28%. Let’s repeat the calculations with 10 lags and see if we succeed in reducing the training.

> # Import the csv file > quotes <- read.csv("E:/MarketData/NZDUSD1440.csv", header=FALSE) > > > > # load quantmod package > library(quantmod) > > #convert the data frame into an xts object > quotes <- as.ts(quotes) > > > #convert time series into a zoo object > quotes1 <- as.zoo(quotes) > > #calculate simple returns > sr <- diff(quotes) / lag(quotes, k = -1, na.pad=TRUE) > > #calculate log returns > > lr <- diff(log(quotes)) > > #number of rows in the dataframe > x <-nrow(sr) > > # lag the data > x1 <- lag(sr, k=-1, na.pad=TRUE) > x2 <- lag(sr, k=-2, na.pad=TRUE) > x3 <- lag(sr, k=-3, na.pad=TRUE) > x4 <- lag(sr, k=-4, na.pad=TRUE) > x5 <- lag(sr, k=-5, na.pad=TRUE) > x6 <- lag(sr, k=-6, na.pad=TRUE) > x7 <- lag(sr, k=-7, na.pad=TRUE) > x8 <- lag(sr, k=-8, na.pad=TRUE) > x9 <- lag(sr, k=-9, na.pad=TRUE) > x10 <- lag(sr, k=-10, na.pad=TRUE) > > # combine all the above matrices into one matrix having close prices > CQuotes <- cbind (x1[ ,6], x2[ ,6], x3[ ,6], x4[ ,6], x5[ ,6], + x6[,6], x7[,6], x8[, 6], x9[,6], x10[,6], + sr[,6]) > > > Direction <- ifelse(CQuotes[, 11] >=0, 1, 0) > > CQuotes <- cbind(CQuotes, Direction) > > #name the columsn Open, High, Low, Close, Volume > colnames(CQuotes) <-c('Lag1','Lag2','Lag3','Lag4','Lag5', + 'Lag6','Lag7','Lag8','Lag9','Lag10', + 'Today', 'Direction') > > #load the kernlab library > > library(kernlab) > > > # train a support vector machine > filter <- ksvm(Direction~.,data=CQuotes[20:x,1:12],kernel="rbfdot", + kpar=list(sigma=0.05),C=5,cross=3) > print(filter) Support Vector Machine object of class "ksvm" SV type: eps-svr (regression) parameter : epsilon = 0.1 cost C = 5 Gaussian Radial Basis kernel function. Hyperparameter : sigma = 0.05 Number of Support Vectors : 1908 Objective Function Value : -3281.592 Training error : 0.220525 Cross validation error : 0.102132 > > #calculate the time taken to make an SVM > > system.time(filter <- ksvm(Direction~.,data=CQuotes[10:x,1:12],kernel="rbfdot", + kpar=list(sigma=0.05),C=5,cross=3)) user system elapsed 5.80 0.39 5.13

As you can see above we did the calculations with 10 lags and were able to reduce the training error to 22% meaning we achieved a predictive accuracy of 78%. The time taken by R to do all the calculations is just 5.13 seconds which means we can also use our model on intraday timeframes like M15 and M30. We can trade binary options with this model on intraday timeframes of M15 and M30. Training error is always an overestimate. When we do out of sample testing, we will get an error that will be something between 22% to 28%. As long as we have a predictive accuracy above 70%, we can use this SVM Model in our daily trading. Now let’s do the calculations for the intraday timeframe of M15. This time we make the prediction for EURUSD.

> # Import the csv file > quotes <- read.csv("E:/MarketData/EURUSD15.csv", header=FALSE) > > > > # load quantmod package > library(quantmod) > > #convert the data frame into an xts object > quotes <- as.ts(quotes) > > > #convert time series into a zoo object > quotes1 <- as.zoo(quotes) > > #calculate simple returns > sr <- diff(quotes) / lag(quotes, k = -1, na.pad=TRUE) > > #calculate log returns > > lr <- diff(log(quotes)) > > #number of rows in the dataframe > x <-nrow(sr) > > # lag the data > x1 <- lag(sr, k=-1, na.pad=TRUE) > x2 <- lag(sr, k=-2, na.pad=TRUE) > x3 <- lag(sr, k=-3, na.pad=TRUE) > x4 <- lag(sr, k=-4, na.pad=TRUE) > x5 <- lag(sr, k=-5, na.pad=TRUE) > x6 <- lag(sr, k=-6, na.pad=TRUE) > x7 <- lag(sr, k=-7, na.pad=TRUE) > x8 <- lag(sr, k=-8, na.pad=TRUE) > x9 <- lag(sr, k=-9, na.pad=TRUE) > x10 <- lag(sr, k=-10, na.pad=TRUE) > > # combine all the above matrices into one matrix having close prices > CQuotes <- cbind (x1[ ,6], x2[ ,6], x3[ ,6], x4[ ,6], x5[ ,6], + x6[,6], x7[,6], x8[, 6], x9[,6], x10[,6], + sr[,6]) > > > Direction <- ifelse(CQuotes[, 11] >=0, 1, 0) > > CQuotes <- cbind(CQuotes, Direction) > > #name the columsn Open, High, Low, Close, Volume > colnames(CQuotes) <-c('Lag1','Lag2','Lag3','Lag4','Lag5', + 'Lag6','Lag7','Lag8','Lag9','Lag10', + 'Today', 'Direction') > > #load the kernlab library > > library(kernlab) > > > # train a support vector machine > filter <- ksvm(Direction~.,data=CQuotes[20:x,1:12],kernel="rbfdot", + kpar=list(sigma=0.05),C=5,cross=3) > print(filter) Support Vector Machine object of class "ksvm" SV type: eps-svr (regression) parameter : epsilon = 0.1 cost C = 5 Gaussian Radial Basis kernel function. Hyperparameter : sigma = 0.05 Number of Support Vectors : 1977 Objective Function Value : -3604.957 Training error : 0.246456 Cross validation error : 0.108163 > > #calculate the time taken to make an SVM > > system.time(filter <- ksvm(Direction~.,data=CQuotes[10:x,1:12],kernel="rbfdot", + kpar=list(sigma=0.05),C=5,cross=3)) user system elapsed 5.75 0.38 5.04

These were the calculations for EURUSDM15 candles. As you can see R made the calculations pretty fast in 5 seconds. The training error is approximately 25% meaning we achieved a predictive accuracy of 75%. Now if we tweak the model more we can make the predictive accuracy better.

This is what I do. I have this model make the prediction. As you have seen above this model hardly takes a few seconds to predict the next candle. Once I have the prediction, I use my candlestick trading system and see if it confirms with the prediction. When both the predictions confirm each other, I open the trade otherwise I skip the trade. This helps in reducing the losses.

> # Import the csv file > quotes <- read.csv("E:/MarketData/EURUSD15.csv", header=FALSE) > > > > # load quantmod package > library(quantmod) > > #convert the data frame into an xts object > quotes <- as.ts(quotes) > > > #convert time series into a zoo object > quotes1 <- as.zoo(quotes) > > #calculate simple returns > sr <- diff(quotes) / lag(quotes, k = -1, na.pad=TRUE) > > #calculate log returns > > lr <- diff(log(quotes)) > > #number of rows in the dataframe > x <-nrow(sr) > > # lag the data > x1 <- lag(sr, k=-1, na.pad=TRUE) > x2 <- lag(sr, k=-2, na.pad=TRUE) > x3 <- lag(sr, k=-3, na.pad=TRUE) > x4 <- lag(sr, k=-4, na.pad=TRUE) > x5 <- lag(sr, k=-5, na.pad=TRUE) > > > # combine all the above matrices into one matrix having close prices > CQuotes <- cbind (x1[ ,6], x2[ ,6], x3[ ,6], x4[ ,6], x5[ ,6], + + sr[,6]) > > > Direction <- ifelse(CQuotes[, 6] >=0, 1, 0) > > CQuotes <- cbind(CQuotes, Direction) > > #name the columsn Open, High, Low, Close, Volume > colnames(CQuotes) <-c('Lag1','Lag2','Lag3','Lag4','Lag5', + + 'Today', 'Direction') > > #load the kernlab library > > library(kernlab) > > > # train a support vector machine > filter <- ksvm(Direction~.,data=CQuotes[20:x,1:7],kernel="rbfdot", + kpar=list(sigma=0.05),C=5,cross=3) > print(filter) Support Vector Machine object of class "ksvm" SV type: eps-svr (regression) parameter : epsilon = 0.1 cost C = 5 Gaussian Radial Basis kernel function. Hyperparameter : sigma = 0.05 Number of Support Vectors : 1954 Objective Function Value : -4132.577 Training error : 0.301412 Cross validation error : 0.093362 > > #calculate the time taken to make an SVM > > system.time(filter <- ksvm(Direction~.,data=CQuotes[10:x,1:7],kernel="rbfdot", + kpar=list(sigma=0.05),C=5,cross=3)) user system elapsed 5.27 0.36 4.73

You can see in the above calculations we reduced the number of lags from 10 to 5. We reduced the time of calculations by 1 second by training error went up to 30%. This is an indication that we should not try to reduce the number of lags. As you can see we can perform these calculations in just a few seconds. After this statistical modelling we know with at least 70% certainty the direction of the next candle. If the prediction is an up candle, we should avoid a sell trade. **Use your knowledge of candlestick patterns and enter into a buy trade**.

## 0 Comments