How To Predict Weekly Gold Price With Kernel Ridge Regression?

In this post we are going to discuss in detail how we are going to predict the gold price for the next week with Kernel Ridge Regression. Did you read the post on how we made 600 pips in 2 days with a 10 pips stop loss? Trading is all about catching the big moves in the market with a small stop loss. If you can keep the risk low, you will always be safe. Modern technical analysis is all about using machine learning algorithms in making price predictions that you can use in your trading. Python is a powerful open source machine learning and data science scripting language that is rapidly becoming the language of choice when it comes to doing data science. Did you take a look at our Python Machine Learning For Traders course? In this Python Machine Learning For Traders course we teach you how to use python for building artificial intelligence machine learning trading systems. We start from the very beginning and then take you step by step how to develop your own AI Machine Learning Trading Systems.

What Is Kernel Ridge Regression?

Regarding Kernel Ridge Regression, you can use this machine learning method for currencies as well as stocks and other commodities. The most elementary algorithm that can be kernelized is ridge regression. Kernel Ridge Regression is a non linear regression method that is almost similar to the support vector machine regression. We map the inputs to a higher dimension using a kernel that makes the regression problem linear or almost linear. In case of Kernel Ridge Regression there are no support vectors. You can watch the video below that explains what is Ridge Regression and why it is superior to ordinary regression.

Now you don’t need to go very deep in learning this type of regression in depth. Everything has been coded in mlpy python library and we are going to use it to do the calculations and predictions. You just need to understand in general details what is this type of regression. When we will use python mlpy module, it will take us less than 1 second. You should have Python compiler installed on your computer. We use Anaconda. Anaconda comes with Spyder which is an IDE that we use. Did you download your copy of Camarilla Pivot Point MT4 Indicator FREE?

How To Install Mlpy module

There is a little problem when you try to install mlpy library if your python version is 3.5. If you use Anaconda and tell it to install mlpy with the command:

conda install mlpy

You will get the answer: mlpy library is not available on Anaconda list. The other alternative is to use pip. When you use this command:

pip install mlpy

You will be told that it failed to build the wheel. Wheels are now being used in the latest python version in installing these libraries. We had to spend a few hours to solve this tricky problem. Google is your friend. Just google it and you wlll find the answer. We were able to download the windows binaries for mlpy from this Unofficial Windows Binaries For Python Extension Package website. This was the binary wheel:

mlpy-3.5.0-cp35-none-win_amd64.whl

We download it and saved it in the Python Scripts directory and then told pip to install it with the command:

pip install mlpy-3.5.0-cp35-none-win_amd64.whl

This time we were successful and mlpy was successfully installed. This solved our major problem because without the machine learning library we would never have been able to do the calculations and make the prediction.

Python Code for predicting weekly close gold price

Below is the python code that will do the calculations!

import numpy as np
import mlpy
from mlpy import KernelRidge
import matplotlib.pyplot as plt
#import the gold price data
data1=np.genfromtxt("E:/MarketData/XAUUSD10080.csv", delimiter=',')
data2=data1[941:1066:1,5]
#check data correctly read by numpy
np.shape(data1)
#create the training data arrary
trainData1 = np.arange(125).reshape(-1, 1)
#create the testing data array
testData1=np.arange(126).reshape(-1, 1)
#create training kernel matrix
knl = mlpy.kernel_gaussian(trainData1, trainData1, sigma=1)
#create the testing kernel matrix
knlTest = mlpy.kernel_gaussian(testData1, trainData1, sigma=1)
#create a kernel ridge object
knlRidge = KernelRidge(lmb=0.01, kernel=None)
# use the learn method to traing the kernel ridge regression model
knlRidge.learn(knl, data2)
# use the predict method to make the predictions on the testing data
predictData1 = knlRidge.pred(knlTest)
#plot the data and the prediction
fig = plt.figure(1)
plot1 = plt.plot(trainData1, data2, 'o')
plot2 = plt.plot(testData1, predictData1)
plt.show()

When you run the above code on Spyder, you will get the following result.

>> predictData1
array([ 1237.52938265,  1203.436831  ,  1213.72823065,  1237.31253041,
1246.00774624,  1253.83449892,  1267.99664752,  1245.0463038 ,
1267.43058894,  1317.1106803 ,  1323.53963851,  1324.7607099 ,
1341.13641564,  1380.51336806,  1333.51974288,  1293.41468601,
1303.70946715,  1317.31125504,  1295.65423145,  1301.31859594,
1298.7894028 ,  1288.32859345,  1293.29507997,  1291.67070634,
1252.05795355,  1252.69510211,  1277.69888206,  1314.2146799 ,
1316.0754707 ,  1320.65015452,  1336.57204474,  1311.29687687,
1306.54198721,  1293.83808182,  1310.72870238,  1304.35868175,
1280.35630941,  1286.7177494 ,  1268.59823321,  1230.52294627,
1217.56417014,  1216.52168661,  1192.44149085,  1221.95831804,
1238.9281972 ,  1229.33968845,  1173.02199291,  1175.73234753,
1190.5550056 ,  1199.32779574,  1168.2548438 ,  1191.10804757,
1221.31755196,  1195.11009546,  1195.02272492,  1188.80060623,
1222.86847814,  1275.07949027,  1294.89386957,  1283.23207401,
1235.99512053,  1227.45894743,  1203.82879036,  1211.51277637,
1167.75146065,  1156.27842681,  1183.37057811,  1198.03821538,
1201.92995422,  1208.47527683,  1204.2196476 ,  1180.05364562,
1177.13946648,  1189.5806261 ,  1223.64765557,  1206.63080054,
1189.90427123,  1172.16522365,  1181.59306795,  1200.31603202,
1174.92585286,  1168.49794524,  1164.36904293,  1134.69137595,
1100.49915302,  1096.12240475,  1093.727939  ,  1116.4479687 ,
1158.59967459,  1135.77143748,  1122.51020585,  1109.18622522,
1138.85420597,  1146.77014499,  1138.32744316,  1157.78364001,
1175.85110443,  1165.34739556,  1141.64902799,  1089.91899547,
1083.44585416,  1078.36988254,  1060.24662661,  1087.34098498,
1075.69737379,  1067.47971383,  1075.366362  ,  1062.97346377,
1102.53222038,  1090.9884284 ,  1098.01192859,  1118.46981799,
1173.04032265,  1238.29050992,  1227.75720934,  1223.44858502,
1256.97323484,  1251.30082665,  1253.85535692,  1218.04789771,
1221.08383887,  1240.123214  ,  1233.33909887,  1234.1149606 ,
1293.3834565 ,  1280.2817356 ])

Last one is the predicted weekly close price for gold which is $1280.281. As said above you can use this code for currency pairs like EURUSD, GBPUSD, USDJPY, USDCAD etc. Just replace the input csv file in the beginning of the above code with the appropriate csv file.

Kernel Ridge Regression

You can see above the plot of the closing price. The last price is the predicted weekly close price for gold. Do you know what are Interdealer Brokers? You can also use this to find the high and low for the week. Just replace the closing price with the high price if you want to predict the high price. In the same manner replace closing price with low price if you want to predict the low price. Machine learning is increasing being used now in algorithmic trading. Our ultimate goal is to develop an algorithmic trading strategy that can be used in daily trading. You should learn Python and R is you want to master machine learning. R and Python are increasing being used by the Quants in developing algorithmic trading strategies.

Can We Use Kernel Ridge Regression In Daily Trading?

This is an important question. We predicted gold price above. We modify our code a little. Sigma is an important parameter when we do kernel ridge regression. Higher sigma means more smoothing. Lower sigma means abrupt changes. Sigma is just the usual variance when we deal with a Gaussian distribution. Lower variance means the distribution is closely centered around the mean while a higher variance means the distribution is widely centered around the mean. We use NZDUSD 60 minute data. Make the sigma higher and try to predict price after 20 hours. Below is the python code that does it.

import numpy as np
import mlpy
from mlpy import KernelRidge
import matplotlib.pyplot as plt

#import the gold price data
data1=np.genfromtxt("E:/MarketData/NZDUSD60.csv", delimiter=',')

x=len(data1)

data2=data1[x-205:x-5:1,5]
                       
#check data correctly read by numpy
                       
np.shape(data1)

#create the training data arrary                       
    
trainData1 = np.arange(200).reshape(-1, 1)    

#create the testing data array

testData1=np.arange(220).reshape(-1, 1)

#create training kernel matrix

knl = mlpy.kernel_gaussian(trainData1, trainData1, sigma=20)

#create the testing kernel matrix

knlTest = mlpy.kernel_gaussian(testData1, trainData1, sigma=20)

#create a kernel ridge object
# training kernel matrix

knlRidge = KernelRidge(lmb=0.01, kernel=None)

# use the learn method to traing the kernel ridge regression model
knlRidge.learn(knl, data2)  

# use the predict method to make the predictions on the testing data

predictData1 = knlRidge.pred(knlTest)
predictData1[-1]
data1[-2,5]

#plot the data and the prediction

fig = plt.figure(1)
plot1 = plt.plot(trainData1, data2, 'o')
plot2 = plt.plot(testData1, predictData1)
plt.show()

Below is the plot of the prediction made by Kernel Ridge Regression. 0.69707  is the predicted price. Surprisingly this prediction is quite close. On Friday NZD/USD made a low of 0.70020.

Kernel Ridge Regression Trading

Now you can test the above code for other currency pairs as well. Sometimes the predictions are good and sometimes the prediction is wide off the market. The above code is very fast. I believe with tweaking we can remove those instances when the predictions are wide off the mark.