【FX, AI】 Forex forecasting with simple deep learning


Until the last time, i performed simple chart display and technical analysis(japanese).

This time, let’s make a prediction of FX with neural network (deep learning).

Specifically, input the information for the past n days from the present time and predict the value of the next day (n + 1).

Neural Network is

To put it simply, an (artificial) neural network is a simple model of the neural network of the human brain.

The following is an example of a neural network.


This network is a model that inputs values ​​from the left and propagates from there to the right.
Speaking of this exchange example, input the data of the chart for the past n days from the left and output the expected value of the exchange for the next day n + 1 from the right.
Deep learning refers to the network layer that has been added (deepened).

Neural network processing is mainly divided into a learning phase and an inference phase.
In the learning phase, data is input to the neural network and trained.
In the inference phase, you actually enter unknown information and make predictions.


This time, I will use TensorFlow as a library for building neural networks.
If you have not installed it, install it with the following command.

pip install tensorflow

Also, I use TensorFlow version 2.0, so please update if you have an older version.

Let’s implement it.
Source code is consisted of
  • Get raw currency data
  • Processing raw data into learning data
  • Learning neural networks
  • Prediction using neural networks

We will implement them in order.

Import the necessary modules etc. as usual.
Library related to Tensorflow and oanda.

import numpy as np
import pandas as pd
from oandapyV20 import API
import oandapyV20.endpoints.instruments as instruments
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation, Flatten
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error

Also declare constants.

COUNT = 5000
NB_ITR = 10
EPOCHS = 100
ACCESS_TOKEN = "************"    # access token of OANDA API
api = API(access_token=ACCESS_TOKEN, environment="practice")

params = {
    "granularity": GRANULARITY,
    "count": COUNT,
    "price": "B",

Get raw data

Get exchange data.

This time, we use USD / JPY 5 minute data.
Prepare a method to get the foot data first and return it as a pandas DataFrame type.
Since normal OANDA API operation can only take 5000 data at a time, you can get more than 5000 data by making a method and performing this process many times.

def get_candles(instrument="USD_JPY", params=None, api=None):
    instruments_candles = instruments.InstrumentsCandles(
        instrument=instrument, params=params)

    response = instruments_candles.response

    df = pd.DataFrame(response["candles"])

    return df

Get data using the method defined above.

candles = None
for i in range(NB_ITR):
    new_candles = get_candles(instrument=INSTRUMENT, params=params, api=api)
    params["to"] = new_candles["time"].iloc[0]
    candles = pd.concat([new_candles, candles])

Here, we get the data by calling the get_candles method for NB_ITR.
I will omit the detailed explanation, but I take the data and add it to the existing data.
This completes the raw data acquisition.

Processing of raw data into learning data

This time, only the Close data is used among the acquired candlestick chart data, so only Close will be extracted.

prices = np.array([x["c"] for x in candles["bid"].values])
prices = prices.astype(np.float64)
prices = prices.reshape(-1, 1)

Next, We standardize the acquired data. In other words, all data has changed to a value between 0 and 1.
(Because learning efficiency is improved by standardization)
In this article, a machine learning library called scikit-learn is used. MinMaxScaler is used by for standardization with fit_transform method.

sc = MinMaxScaler(feature_range=(0, 1))
prices = sc.fit_transform(prices)

Generate data (X, Y) that can be read into the neural network.
Each data becomes X [0] = [t1, t2,…, tn], Y [0] = tn + 1.

X, Y = [], []
for i, _ in enumerate(prices):
    if (i+INPUT_LEN+1 >= len(prices)):

X = np.array(X)
Y = np.array(Y)

Separate the data for neural network learning and evaluation.

split_index = int(len(prices)*0.7)
X_train = X[:split_index]
Y_train = Y[:split_index]
X_test = X[split_index:]
Y_test = Y[split_index:]

Neural network construction and learning

Next, we will build and learn neural networks.

Building a neural network is easy with Keras of TensorFlow. The construction is completed with the following code.

model = Sequential()
model.add(Flatten(input_shape=[INPUT_LEN, 1]))


Build the network in order from the input layer.
Add layers in order by using the add method of model.
This time, we predict (regress) the value, so the output will be one.

Next, we will learn the built neural network.

model.fit(X_train, Y_train, batch_size=BATCH_SIZE, epochs=EPOCHS)

You can easily learn using the model’s fit function. We will learn using the training data (X_train, Y_train) created earlier.

Evaluation and prediction using neural networks

The learning is over here.
Next, we will evaluate the learned model.
First, let’s evaluate using the data (X_test, Y_test) prepared for the test.
This is also easy if you use the evaluate method on the model.

print(model.evaluate(X_test, Y_test))

The following is output.

[5.802867302361515e-05, 0.00013352917612498332]

We only need to look at the left element (loss) this time. The left element is the square error for the test data.

You can see that the error has been reduced.

Next, we see how it is predicted by actually plotting it.

predicted_y = model.predict(X_test)
fig, ax = plt.subplots()

Y_test = sc.inverse_transform(Y_test)
ax.plot(np.arange(len(Y_test)), Y_test)

predicted_y = sc.inverse_transform(predicted_y)
ax.plot(np.arange(len(predicted_y)), predicted_y)

Since we standardized the data earlier, we are also working on restoring the original values.
The following is the output after running the above code.


It looKs like a simple neural network is predicting to some extent!
However, this time, we look at the data one point before the point we want to predict, so if you output a value close to the value one point before, it may be set to hit almost


In the article, We used a simple neural network to predict the exchange rate.
I feel that simple neural networks are giving good results.

Next, let’s learn using a recurrent neural network that can handle time series data well.