In this case study, we will see how to use forecasting with R for currency trading. If we know the forecasting for the next few days, we can build our trading system around the trend line of forecasts and can make buying or selling decisions.
DISCLAIMER: This case study is for educational purpose. It is neither a recommendation of trading , any sort of tips and not to be copied in real world currency trading. This article should not be taken as recommendation to trade in USDINR. Currency trading is highly risky and hence, a good financial advisor should be consulted for making investment decisions in currency trading.
The entire process of how to use forecasting with R for currency trading is divided into the following sections---
(A) DATA PREPARATION
(B) MODEL BUILDING
(C) PREDICTION
(A) DATA PREPARATION STAGE
First and foremost, we need to load required libraries as shown in the following chunk of commands---
>library(quantmod)
>library(timeSeries)
>library(forecast)
>library(bizdays)
>library(plotly)
>exr=getSymbols("INR=X", src="yahoo", env=NULL)
NOTE: USDINR is coded as INR=X on Yahoo Finance.
When we download data from Yahoo Finance, it loads open, close, high, low, volume and adjusted price of USDINR. We need only closing price/quote (4th Column) of USDINR---
>exr=exr[,4]
We need to remove any missing values from the data to do time series operations hence we, first, calculate the number of missing values and then remove it by using "na.omit" command---
>sum(is.na(exr))
There are 30 missing values in the data, we shall remove the missing values from data.
>exr=na.omit(exr)
>sum(is.na(exr))
Now, let us observe how the trend line looks in plot---
>plot(exr)
After data preparation, the data is used for developing the forecasting model and followed by prediction.
(B) MODEL BUILDING
If we look at the trendline, it is clearly visible that over the period of time, it changes mean and variance hence this time series is non-stationary. The "auto.arima" command of "forecast" package computes the parameters of the time series model. As we have USDINR daily price quotes, we can introduce frequency 365 to address seasonality in the model building. The major components P, D, and Q are calculated by "auto.arima" function automatically. We first convert dataframe "exr" into time series "exr.ts.365"---
>exr.ts.365=ts(exr, frequency = 365)
Now, we build time series model with "auto.arima" command with seasonality denoted by D=1 ---
>model.365=auto.arima(exr.ts.365,D=1)
Model summary and accuracy of "model.365" is as follows---
> summary(model.365)
Series: exr.ts.365
ARIMA(1,1,2)(0,1,0)[365]
Coefficients:
ar1 ma1 ma2
0.8414 -0.9773 0.1442
s.e. 0.0728 0.0741 0.0197
sigma^2 estimated as 0.194: log likelihood=-1638.48
AIC=3284.95 AICc=3284.97 BIC=3308.61
Training set error measures:
ME RMSE MAE MPE MAPE MASE ACF1
Training set 0.002054559 0.4133998 0.2762706 0.002884244 0.491483 0.05617031 0.001935873
The ARIMA model extracted is (1,1,2)(0,1,0)[365] and it's MAPE is quite low i.e.0.491483.
(C) PREDICTION
Now, we predict the next 200 day prices based on "model.365" by using forecast command---
>predict.365=forecast(model.365, h=200)
Let us see how output of "predict.365" looks like---
>predict.365
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
9.487671 70.17357 69.60910 70.73805 69.31028 71.03687
9.490411 70.02434 69.27832 70.77036 68.88340 71.16528
9.493151 69.59606 68.69539 70.49674 68.21860 70.97353
9.495890 69.64146 68.60203 70.68090 68.05179 71.23114
9.498630 69.60031 68.43319 70.76743 67.81535 71.38527
9.501370 69.75752 68.47118 71.04387 67.79023 71.72482
9.504110 69.62377 68.22505 71.02250 67.48461 71.76294
9.506849 69.80306 68.29772 71.30840 67.50084 72.10527
Ignore the first column and observe the "Point Forecast" column and we find that there is a decline in prices of USDINR.
Let us observe visually when the forecast is attached to the time series---
>plot(predict.365)
In the graph, where the extended line in blue cover is the point forecast line and grey and light grey envelopes are basically levels at 80% and 90% confidence.
After getting the forecast, we need to add series of 200 dates on USDINR trading days only. We need to first create a calendar as per India by "create.calendar" command---
>create.calendar("India/ANBIMA", holidaysANBIMA, weekdays=c("saturday", "sunday"))
Now, we calculate how many trading days between 26/12/2018 and 10/10/2019 and we find it is 200 day by running the "bizdays" command---
>bizdays("2018-12-26", "2019-10-10", "India/ANBIMA")
Now, we shall get a data frame of dates and combine it with "predict.365" to make a new data frame "predict200" containing two columns ( Dates and predict.365)---
>Dates=bizseq("2018-12-26", "2019-10-10", "India/ANBIMA")
>Datess=as.data.frame(Dates)
>dim(Datess)
>View(Datess)
>Dates=Datess[1:200,]
>predict.365=as.data.frame(predict.365)
>dim(predict.365)
>predict200=cbind.data.frame(Dates,predict.365)
Now, we can see how "predict200" data frame looks like---
>View(predict200)
We can download the csv file of "predict200" by following command---
>write.csv(predict200, "predict200.csv")
The "predict200" contains columns of Dates, Point Forecast, Lo80, Hi80, Lo90 and Hi90. For this case study, we will use only point forecast to suggest a trading system for USDINR.
Let us see the predict.200 visualization with plot_ly command---
>plot_ly(predict200, x=predict200$Dates, y=predict200$`Point Forecast`,mode="marker")
Now, we can see that until Oct 2019, the USDINR exchange rate shows range bound movement between 71.25 and 68.5. It is basically mean reversion behavior of exchange rate or in simple words, USDINR rates will be sideways between these two points.
Here, trend following strategies will be less successful and sideways market strategies will be more successful. The USDINR market in this period is more likely going to favor option writers.
No comments:
Post a Comment