Code for automated FX trading
This is a repo where I store code for training and making an automated FX trading bot.
Essentially most of the work done here is about trying to train an accurate price movement classification model. But it as well contains all of the other necessary stuff like downloading historical or recent FX data and live-managing a demo trading account using OANDA's API.
As a default, the training is done using 15 years of hourly data of EUR/USD. The dataset is split into 11 years of training data, 3 years of test data and 1.5 years of cross-validation data (keep this in mind when looking at portfolio value charts). MFeature engineering is mostly done using indicators from ta-lib package.
Once I'm comfortible with data exploration, models and stuff, I should try other pairs as well. Or maybe a grand-model of huge-bunch-of-different-pairs at once!?
Easy way to get historical data. Simple code to download historical (or current) data of selected instruments using OANDA's API.
Live trading portfolio manager. A class to manage real-time trading, open and close positions etc. using OANDA's API wrapper.
Kind of ready to use trading bot. Final script combining everything to live manage a trading portfolio.
First try is a bunch of 'quick and dirty' models with just a few features and some optimization experimentation. I've hand picked a few financial indicators and made sure they do not correlate too much. Additionally I've made a few dummy variables for market hours in major markets.
Predicting price direction.
Predict the direction of price in the next time period. Target values [1, 0, 0] for up, [0, 1, 0] for down [0, 0, 1] for flat (sidenote: the threshold for minimum price change that is still considered flat is determined such that each label of up, down and flat makes roughly 1/3 of full dataset). Train by minimizing cross entropy of error.
logistic regression | lstm net | convolutional net |
---|---|---|
Predicting optimal positions allocation
Instead of predicting price direction, allocate the funds to buy, sell, do not enter positions directly. For instance [0.5, 0.2, 0.3] would indicate to buy 0.5 units, sell 0.2 units and keep in cash 0.3 units. In this case there are no target labels and the model is trained by maximizing objective function (hourly average return).
logistic regression | lstm net | convolutional net |
---|---|---|
Concusions v1:
This time the idea was to:
Plot example of a few features after normalization
After trying multiple ways of combining the features polynomials and PCA, it seems that this approach did not increase the accuracy of the model. Just for future reference I unclude best ROC scores I was able to reach using this approach.
Receiver operating curve
Conclusions v2:
Here is an example of portfolio value given the best models. Unfortunately, the results change dramatically once transaction costs are accounted for.
Portfolio value w\ and w\o transaction costs
Ideas to try out someday: