In the last post we talked about **8 Machine Learning Algorithms that can help us in trading**. You should read that post. In that post we pointed out 8 machine learning algorithms that can help us in predicting market direction. These 8 machine learning algorithms were Neural Networks, Support Vector Machines, Fuzzy Logic, Wavelets, Kalman Filter, Particle Filter, Decision Trees and Genetic Algorithm. Support Vector Machine is a difficult concept to grasp for a newbie. Watch this video below in which an MIT Professor explains this algorithm.

After watching the above video if you still need help, you can read this post in which we have explained the **support vector machine algorithm** in detail. There are a number of good videos posted in that post that will make the concept of support vector machine clear to you.

A support vector machine can be used for both classification as well as regression. This is what a Support Vector Machine does. It uses a non linear kernel to map the input data onto a higher plain. This kernel is mostly Gaussian. This mapping helps in making a non linear function into a linear function most of the time. Once we have mapped the input data which is highly jumbled up onto a higher plain, it becomes somewhat clearly separated. Once the input data has been mapped onto a higher plain, we use a support vector to classify the data and separate the data into 2 classes.

We will be implementing this algorithm using R software. R is a powerful machine learning and data analysis software that can be downloaded FREE. You can read this post in which we explain **how to design algorithmic trading strategies using R**. You can implement almost all the above machine learning algorithms using R. You need to know R scripting language. Python is another very powerful scripting language. R is a pure data analysis language whereas Python can do many more things. Python is 2 times faster than R. You should learn both R and Python. Either one of these languages can be used to implement the above machine learning algorithms.

Binary options trading is quite popular now a days. You just need to predict whether price will close above to below the present price when you click the put or call button. If your prediction is correct and price ends up as you had predicted, you win 80% return. Trading 5 minute binary options can be highly profitable if you can win each time. Each time you win a 5 minute expiry binary options you make 80% return in just 5 minutes. Just make 5 winning trades in 60 minutes and you can end your day with a 40% return.

This was the easy part. The difficult part is how to predict the next 5 minute candle. This is what we will be doing. We will first read M1 data into R. Then we will convert that data into a 5 minute candle. Then we will try to predict the next 5 minute candle using the support vector machine algorithm. We will calculate the return for each candle. If the return is positive, we will classify it as 1. If the return is negative we will classify it as 0. Direction is just 1 and 0. We will input the last 4 returns together with the present direction to a support vector machine. Support vector machine will then learn from the past data and predict the direction of the next 5 minute candle. If the predicted direction is 1, it means we will buy a 5 minute call option. If the predicted direction is 0, we will buy a 5 minute put option. This algorithm has a winrate of around 70%.

> #import the data > > data <- read.csv("E:/MarketData/GBPUSD1.csv", header = FALSE) > > > > colnames(data) <- c("Date", "Time", "Open", "High", + "Low", "Close", "Volume") > > > x1 <- nrow(data) > > > #convert this data to n timeframe > > n=5 > > #define lookback > > lb=300 > > #define a new data frame > > data1 <-data.frame(matrix(0, ncol=7, nrow=300)) > > colnames(data1) <- c("Date", "Time", "Open", "High", + "Low", "Close", "Return") > > # run the sequence to convert to a new timeframe > > for ( k in (1:lb)) + { + data1[k,1] <- as.character(data[x1-lb*n+n*(k-1),1]) + data1[k,2] <- as.character(data[x1-lb*n+n*(k-1),2]) + data1[k,3] <- data[x1-lb*n+n*(k-1),3] + data1[k,6] <- data[x1-lb*n+n*k-1,6] + data1[k,4] <- max(data[(x1-lb*n+n*(k-1)):(x1-lb*n+k*n-1), 4:5]) + data1[k,5] <- min(data[(x1-lb*n+n*(k-1)):(x1-lb*n+k*n-1), 4:5]) + } > > for ( k in (2:lb)) + { + data1[k,7] <- (data1[k,6]-data1[k-1,6])/data1[k-1,6] + + + } > > > Return <- as.data.frame(embed(data1[ ,7], dimension=5)) > > colnames(Return) <- c("R", "R1","R2", "R3", "R4") > > > Return$Direction <- as.factor(ifelse(Return[, 1] >0, 1, 0)) > > x3 <- nrow(Return) > > Return <- Return[,-1] > > Return[x3+1, 1] <- data1[lb,7] > Return[x3+1, 2] <- data1[lb-1,7] > Return[x3+1, 3] <- data1[lb-2,7] > Return[x3+1, 4] <- data1[lb-3,7] > > > > #load the kernlab library > > > library(kernlab) > > > > # train a support vector machine > filter <- ksvm(Direction~.,data=Return[1:x3 ,1:5],kernel="rbfdot", + kpar=list(sigma=0.05),C=5,cross=3) > > > ## predict the next candle > pred <- predict(filter, Return[(x3+1), (1:4)]) > > pred [1] 1 Levels: 0 1

Above was the R code that implemented the support vector machine algorithm. The above code makes the prediction in just 1-2 seconds. It is pretty fast. In the above example, you can see the predicted direction is 1 which means we will buy a call option. This algorithm will predict market direction for the next 5 minutes. The next step is to develop an MT4 indicator based on this algorithm. It would be difficult to download 1 minute OHLC csv from MT4, read it in R and run the algorithm on R and then trade that prediction. In a future post we will show you how to connect R with MT4 and call R software from within MT4 using a script that makes the calculations and then returns the predicted value to MT4 in 1-2 seconds. Once we have that prediction, we can trade it. There are a number of brokers now who allow you to connect your MT4 platform with binary options broker account. So we can use the above algorithm and develop an expert advisor based on it.

The key to success in trading is increasing your odds of winning. Using the above algorithm you can increase your odds of winning. But always make sure you follow proper risk management all the time. Which means you should never risk more than 1% of your account equity on a single trade. If you want you can increase risk level to 2% but never go above this level. Trading is just like the game of cricket. You keep the wickets in your hand and go for singles or doubles. Sometime when you get a good chance, you hit a boundary which can be a four or a six. Over time these singles and doubles will help you win the match.

## Leave A Response

You must be logged in to post a comment.