Forecasting Exchange Rates Using ARIMA In Python

Understand Market Data”.

It provides a basic overview of market data.

Exchange rates are dependent on a range of factors such as supply and demand, government policies, country’s growth rates etc.

For more information on economical indicators that can impact exchange rates, please have a look at my article “Everything You Need To Know To Assess And Compare Countries”.

Forecasting Exchange RatesRecently, a number of technological advancements have been introduced that can forecast future time points quickly, efficiently and accurately.

One of them is introduction of statistical and machine learning (ML) models in Python.

I provided an overview of basics of python in my article “Python From Scratch”.

In this article, I will use ARIMA model to forecast exchange rates.

In my blog “How do I predict time series?” and “Understanding Auto Regressive Moving Average Model — ARIMA”, I outlined the concept of time series and how ARIMA works.

In this blog, I will be using Python programming language with Jupyter notebook to explain how to use ARIMA in python to forecast exchange rates.

Please read FinTechExplained disclaimer.

Using Pandas To Load Exchange Rates DataPandas is one of the most popular Python libraries.

It is built on type of Numpy python library and offers a range of features including:Object Creation, Data analysis and data loading.

It has inbuilt Statistic functions, can merge/join/union multiple collections.

It can also help us in grouping, pivoting and plotting the data.

Additionally Pandas is very efficient library to load data from csv, HDF5 and excel.

Lastly a range of styling and formatting can be applied to the data.

We will be using pandas to:1.

Load time series exchange rates from a csv (comma separated) file2.

View first 10 records3.

View basic statistical information on the dataLoad time series exchange rates from a csv fileI have prepared a file which includes daily closing GBP/USD exchange rates since 31/12/1998.

File is stored here.

This file contains data with two columns: Data and GBP/USD CloseEnsure file is saved in the same location as your python notebook.

Type in following lines to load the file and view first 10 records:To import a library, do: Import <library name> as <alias>Note: GetData(fileName) is a method that takes in file name as an argument.

Press Alt + Enter to view basic statistical information ont he dataThe image below shows the first 10 records of the csv file by calling GetData(fileName) method:Let’s get useful statistics out and plot the exchange ratesType in: exchangeRatesSeries.

describe() to see the stats as shown below:describe() shows a number of useful metrics including:count — Number of records, mean — expected value, std — Standard deviation telling us dispersion of data around the mean, min — Minimum value in the set, max — Maximum value in the set along with a range of percentiles.

Percentiles can help us understand probability distribution of our data.

Plotting the loaded dataHtml styling can be added to change feel-and-look in Python.

I am setting the plot colour to green:#plot the time series dataexchangeRatesSeries.

plot(color=’green’)Notice, passing color=’green’ creates a green line graph.

Type in: exchangeRatesSeries.

hist() to show historgram.

Histograms can help us understand distribution of data which in return helps us in forecasting a variable:matplotlib can help us with plotting data.

We can also import matplotlib by writing:from matplotlib import pyplotThen plot time series by writing:pyplot.

plot(exchangeRateSeries)ARIMA With StatsModels PackageStatsModels is a powerful python library that is rich with statistical models.

StatsModels library contains a number of models which can be used to forecast and predict data.

This library holds a number of diagnostic tools too.

We are going to use ARIMA model in StatsModels package to forecast exchange rates.

ARIMA IntroductionARIMA model has 3 parameters:P — Auto regressive feature of the modelD — Differencing orderQ — Moving average feature of the modelOnce we import statsmodels, use the tsa.

arima_model and give it an alias of ARIMA:from statsmodels.

tsa.

arima_model import ARIMAFor in depth details of the parameters, please visit: http://www.

statsmodels.

org/dev/generated/statsmodels.

tsa.

arima_model.

ARIMA.

htmlARIMA parameters can be changed to attain different forecasting behaviour.

I have defined a user defined method that takes in training set and value for each of the three parameter.

The function first creates ARIMA model.

It then executes fit() and then forecast() on the model.

Forecast() returns a predicted value.

Copy and paste these lines into your notebook:ARIMA(…) creates ARIMA model.

Fits() fits ARIMA(p,d,q) model by exact maximum likelihood via Kalman filter and Forecast() returns an estimated value based on the fitted ARIMA model.

We can think of Fit() as a process that generates best fit line curve that gives least error.

More on this topic is covered in my blog: How Good Is My Predicted Model — Regression Analysis?p, q, d parameters can be tweeked to get better results.

This is an example of how we can pass in time series data and use ARIMA to predict a value based on the actual observed data:By running the script, we see the predicted value:We passed in random values as training set.

ARIMA model then fitted itself and predicted the next value as 15.

219305We can also pass in exogenous variables, dates, frequency of time series etc to the ARIMA model.

Lastly, Let’s Use ARIMA In Python To Forecast Exchange RatesNow that we understand how to use python Pandas to load csv data and how to use StatsModels to predict value, let’s combine all of the knowledge acquired in this blog to forecast our sample exchange rates.

Copy and paste this code.

It is a combination of all of the concepts which we have learnt in this blog.

Press Alt+Enter.

Python will start calling ARIMA model in a loop with the actual data.

70% of data is used to train the model and the rest 30% is used to test the accuracy.

Each time a newly predicted value is produced.

Actual and predicted values will be printed on the notebook.

Finally actual vs predicted values will be plotted on the chartView Mean Squared ErrorI have also imported an additional library sklearn which I will be using in my future blogs.

Copy paste this line:“from sklearn.

metrics import mean_squared_error” to import the library.

Finally print Mean Squared Error:Mean squared error calculates an average of the difference between actual and predicted data and tells you how good your model is.

More information can be found in my blog: How Good Is My Predictive Model — Regression AnalysisBy running the code below, we can view the actual, forecasted values along with a line graph and total mean squared error:As you can see, we printed actual and predicted values.

Additionally, we plotted predicted values in red with MSE of 1.

551.

For complete notebook, please visit here.

Further ImprovementsForecasting exchange rates can be improved by:Constantly updating model parametersBy inputting additional factors that impact exchange rates and their correlations into accountModel parameters can also be updated via machine learning and optimisation techniques.

Lastly factors and their correlations can be stressed to ensure forecasted exchange rates take extreme scenarios into account.

Final NotesThis article demonstrated how to use python to forecast exchange rates using ARIMA model.

Financial markets can move in any direction and this makes it very hard, if not impossible, to accurately predict exchange rates.

Having said that, the sole purpose of forecasting exchange rates via ARIMA is to help us in taking calculated decisions that maximise returns and minimise risks.

Forecasted exchange rates are dependent on the assumptions imposed by ARIMA model which are based on auto regression, integrated and moving average concepts.

ARIMA is a simple yet powerful model.

It assumes that the historic values dictate behaviour of present.

It also assumes that the data does not contain anomalies, is stationary and model parameters along with error term is constant.

Although ARIMA does not take stresses in market data as input, economical and political conditions, or correlations of all risk factors to forecast exchange rates but the simple example demonstrated above can be useful for forecasting movements of stable currencies in normal conditions in which past behaviour dictates present and values.

Please let me know if you have any feedback.

.

. More details

Leave a Reply