Created
November 6, 2014 02:05
-
-
Save Inpirical-Coder/f737061b3b03a89c82eb to your computer and use it in GitHub Desktop.
Trains and tests SVM on two features (relative strength index and trend over x observations)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Some code to asses an SVM with a two-dimensional feature space: | |
# trend (price - simple moving average) and relative strength index. | |
# The code is an adaptation of the code found in the following linkedin post: | |
# "Trading the RSI using a Support Vector Machine" | |
# "https://www.linkedin.com/pulse/article/20141103165037-172934333-trading-the-rsi-using-a-support-vector-machine" | |
# Settings | |
sma.window = 50 # Number of observations in simple moving average. | |
rsi.window = 3 # Number of observation in relative strength index (RSI) | |
# Specify and load required R packages. | |
packs = c( | |
"quantmod", # quant trading | |
"e1071", # machine learning library containing SVM | |
"ggplot2", # plotting | |
"xts" # working with time-series | |
) | |
sapply(packs, require, character.only=TRUE) # Load the packages. | |
# Load the data (from the working directory). | |
file.name = "AUDUSD.csv" | |
dat = read.csv(file.name, stringsAsFactors=FALSE) | |
# Convert the data to an xts time series object. | |
dat = as.xts( | |
dat[ , -1], | |
order.by= as.POSIXct(dat[ , 1], tz="GMT", format="%m/%d/%y %H:%M") | |
) | |
# Create the data set for training, testing and validation. | |
dat = data.frame( | |
"rsi" = unname(RSI(dat$Open, n=rsi.window)), # relative strength indicator | |
"trend" = unname((dat$Open - SMA(dat$Open, n=sma.window))), | |
"direction" = unname(ifelse(dat$Close > dat$Open, "UP", "DOWN")) | |
) | |
dat = dat[complete.cases(dat), ] # Remove rows containing NAs. | |
# Separate the data into 60% training set to build our model, 20% test set to test the patterns we found, and 20% validation set to run our strategy over new data | |
Training = dat[1:4528,] | |
Test = dat[4529:6038,] | |
Val = dat[6039:7548,] | |
# Train support vector machine. | |
machine = svm( | |
direction ~ rsi + trend, | |
data=Training, | |
kernel="polynomial", | |
cost=1, | |
gamma=1/2 | |
) | |
# Generate predictions for the training, test, and validation sets. | |
TrainingPredictions = predict(machine, Training, type="class") | |
TestPredictions = predict(machine, Test, type="class") | |
ValPredictions = predict(machine, Val, type="class") | |
ShowContours = function() { | |
# Plot the predictions distribution across the feature space using the test set. | |
ggplot( | |
data.frame(Training, TrainingPredictions), | |
aes(x=trend,y=rsi) | |
) + stat_density2d( | |
geom="contour", | |
aes(color=TrainingPredictions) | |
) + labs( | |
title= "Polynomial kernel", | |
x="Trend", | |
y="RSI", | |
color="Training Predictions" | |
) | |
} | |
HitRatios = function() { | |
# Calculates the fraction of test cases where the classification matches | |
# the outcome. | |
print(mean(TrainingPredictions == Training$direction) * 100) | |
print(mean(TestPredictions == Test$direction) * 100) | |
print(mean(ValPredictions == Val$direction) * 100) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment