# An Ensembling Architecture Incorporating Machine Learning Models and Genetic Algorithm Optimization for Forex Trading

^{*}

^{†}

## Abstract

**:**

## 1. Introduction

## 2. Related Works

^{2}score. They reported that the hybrid model outperforms the standalone LSTM and GRU model based on the performance metrics used [18].

## 3. Methods

#### 3.1. Overall Architecture

**System Interface module:**UI interface that traders can use to communicate with AlgoML to obtain trade action recommendation and execute trades through broker websites.**Pre-processing module:**includes the extract, load and transform pipeline together with data cleaning, feature extraction and feature engineering**Action Predictor module:**gives a final predicted buy/sell action output**Trader module:**simulator environment to replicate market trading and execute orders based on model predictions. Also performs actual trade through broker websites**Optimizer module:**optimizes parameters for strategies to improve system profitability

#### 3.2. System Interface Module

#### 3.3. Pre-Processing Module

#### 3.3.1. Data Preparation

#### 3.3.2. Eliminating Trend and Seasonality in Data

#### 3.3.3. Feature Engineering

#### 3.4. Action Predictor Module

- DQN;
- A2C;
- PPO;
- CNN;
- CNN-BILSTM;
- BILSTM;
- Pullback (GA Optimized).

**Highest voting:**uses the prediction with the highest probability score**Average voting:**averages the predictions from each model**Majority voting:**selects the final action which has the majority of predictions

#### 3.5. Trader Module

#### 3.6. Optimizer Module

**Gene:**a feature parameter that defines the strategy (e.g., EMA Period);**Individual/Chromosome:**represents a complete set of feature parameters to define the strategy;**Population:**a collection of possible sets of feature parameters;**Parents:**two sets of feature parameters that are combined to create a new feature set;**Mating pool:**a collection of parents that are used to create the next generation of possible sets of feature parameters;**Fitness:**a function that tells us how good the strategy performs based on the given set of feature parameters, in this case the fitness is defined by the “P&L”;**Mutation:**a way to introduce variation in our population by randomly swapping two feature parameter value between two individuals;**Elitism:**a way to carry the best individuals into the next generation;**CrossOver:**combines parent chromosomes to create new offspring individuals.

## 4. System Model Design

#### 4.1. Deep Learning

#### 4.1.1. Bi-Directional LSTM

#### 4.1.2. Convolutional Neural Network (CNN)

#### 4.1.3. CNN-BILSTM

#### 4.2. Reinforcement Learning

**Value-based RL**: objective is to learn the state-value or state-action-value function to generate the most optimal policy.**Policy-based RL**: aims to learn the policy directly (policy determines what action to take at a particular state, it is a mapping from a state to an action) using parameterized functions.**Actor–critic RL**: aims to learn both value and policy by deploying one neural network as the actor which takes action and another neural network acting as the critic to adjust the action that the actor takes.

#### 4.2.1. Deep Q-Networks (DQN)

#### 4.2.2. Advantage Actor Critic (A2C)

#### 4.2.3. Proximal Policy Optimization (PPO)

#### 4.3. Genetic Algorithm (GA)

#### Pullback Strategy

- If “Close price” > “EMA value” and “RSI value” < “RSI_buy_threshold_value”, THEN “BUY”
- ELIF “RSI value” < “RSI_sell_threshold_value”, THEN “SELL”
- ELSE “BUY”

## 5. Results

#### 5.1. Experimental Setup

#### 5.2. Time-Series Univariate Results Analysis

#### 5.3. Strategies Performance Comparison

## 6. Discussions and Conclusions

## Author Contributions

## Funding

## Institutional Review Board Statement

## Informed Consent Statement

## Data Availability Statement

## Acknowledgments

## Conflicts of Interest

## Abbreviations

SOTA | State of the Art |

SME | Subject Matter Expert |

Forex | Foreign Exchange Market |

OHLCV | Open, High, Low, Close, Volume |

P&L | Profit and Loss |

ML | Machine Learning |

DL | Deep Learning |

RL | Reinforcement Learning |

DRL | Deep Reinforcement Learning |

RFC | Random Forest Classifer |

SVM | Support Vector Machine |

IID | Independent and identically distributed |

GA | Genetic Algorithm |

CNN | Convolution Neural Network |

LSTM | Long Short-term Memory |

BiLSTM | Bi-Directional Long Short-term Memory |

ETL | Extract, Transform and Load |

ADF | Augmented Dickey-Fuller |

MA | Moving Average |

EMA | Exponential Moving Average |

RSI | Relative Strength Index |

## References

- Folger, J. 10 Ways to Avoid Losing Money in Forex; Investopedia: New York, NY, USA, 2021. [Google Scholar]
- Kuepper, J. Risk Management Techniques for Active Traders; Investopedia: New York, NY, USA, 2021. [Google Scholar]
- Hayes, A. Forex Trading Robot; Investopedia: New York, NY, USA, 2021. [Google Scholar]
- What Is Automated Trading? Available online: https://www.ig.com/sg/trading-platforms/algorithmic-trading/what-is-automated-trading (accessed on 29 January 2022).
- Chou, Y.H.; Kuo, S.Y.; Chen, C.Y.; Chao, H.C. A Rule-Based Dynamic Decision-Making Stock Trading System Based on Quantum-Inspired Tabu Search Algorithm. Access IEEE
**2014**, 2, 883–896. [Google Scholar] [CrossRef] - Wilson, J. How Much Artificial Intelligence Has Changed the Forex Trade. Aithority
**2020**. Available online: https://aithority.com/guest-authors/how-much-artificial-intelligence-has-changed-the-forex-trade/ (accessed on 29 January 2022). - Rabin, K. How Is AI Revolutionizing FX Market in a Way We Didn’t Even Realize; Finextra: London, UK, 2021. [Google Scholar]
- Fetalvero, N. Here’s how tech has revolutionized forex trading. Tech Asia
**2020**. Available online: https://www.techinasia.com/heres-tech-revolutionized-forex-trading (accessed on 29 January 2022). - Escudero, P.; Alcocer, W.; Paredes, J. Recurrent Neural Networks and ARIMA Models for Euro/Dollar Exchange Rate Forecasting. Appl. Sci.
**2021**, 11, 5658. [Google Scholar] [CrossRef] - Deka, A.; Resatoglu, N. Forecasting Foreign Exchange Rate And Consumer Price Index With Arima Model: The Case Of Turkey. Int. J. Sci. Res. Manag.
**2019**, 7, 1254–1275. [Google Scholar] [CrossRef] - Cao, L.; Tay, F. Financial Forecasting Using Support Vector Machines. Neural Comput. Appl.
**2001**, 10, 184–192. [Google Scholar] [CrossRef] - Kamruzzaman, J.; Sarker, R. ANN-Based Forecasting of Foreign Currency Exchange Rates. Neural Inf.-Process.-Lett. Rev.
**2004**, 3, 49–58. [Google Scholar] - Thu, T.N.T.; Xuan, V.D. Using support vector machine in FoRex predicting. In Proceedings of the 2018 IEEE International Conference on Innovative Research and Development (ICIRD), Bangkok, Thailand, 11–12 May 2018; pp. 1–5. [Google Scholar] [CrossRef]
- Juszczuk, P.; Kozak, J.; Trynda, K. Decision Trees on the Foreign Exchange Market; Springer: Cham, Switzerland, 2016; pp. 127–138. [Google Scholar] [CrossRef]
- Nti, I.K.; Adekoya, A.; Weyori, B. A comprehensive evaluation of ensemble learning for stock-market prediction. J. Big Data
**2020**, 7, 1–40. [Google Scholar] [CrossRef] - Qi, L.; Khushi, M.; Poon, J. Event-Driven LSTM for Forex Price Prediction. In Proceedings of the 2020 IEEE Asia-Pacific Conference on Computer Science and Data Engineering (CSDE), Gold Coast, Australia, 16–18 December 2020; pp. 1–6. [Google Scholar]
- Zeng, Z.; Khushi, M. Wavelet Denoising and Attention-based RNN-ARIMA Model to Predict Forex Price. In Proceedings of the 2020 International Joint Conference on Neural Networks (IJCNN), Glasgow, UK, 19–24 July 2020. [Google Scholar]
- Islam, M.; Hossain, E. Foreign exchange currency rate prediction using a GRU-LSTM hybrid network. Soft Comput. Lett.
**2021**, 3, 100009. [Google Scholar] [CrossRef] - Carapuço, J.; Neves, R.; Horta, N. Reinforcement learning applied to Forex trading. Appl. Soft Comput.
**2018**, 73, 783–794. [Google Scholar] [CrossRef] - Théate, T.; Ernst, D. An application of deep reinforcement learning to algorithmic trading. Expert Syst. Appl.
**2021**, 173, 114632. [Google Scholar] [CrossRef] - Yuan, Y.; Wen, W.; Yang, J. Using Data Augmentation Based Reinforcement Learning for Daily Stock Trading. Electronics
**2020**, 9, 1384. [Google Scholar] [CrossRef] - Rundo, F. Deep LSTM with Reinforcement Learning Layer for Financial Trend Prediction in FX High Frequency Trading Systems. Appl. Sci.
**2019**, 9, 4460. [Google Scholar] [CrossRef] [Green Version] - Downey, L. Transaction Costs; Investopedia: New York, NY, USA, 2021. [Google Scholar]
- Maurer, T.A.; Pezzo, L.; Taylor, M.P. Importance of Transaction Costs for Asset Allocations in FX Markets. In Econometric Modeling: Capital Markets—Portfolio Theory eJournal; Elsevier: Amsterdam, The Netherlands, 2019. [Google Scholar]
- Brownlee, J. A Gentle Introduction to Imbalanced Classification. Machine Learning Mastery. 2019. Available online: https://machinelearningmastery.com/what-is-imbalanced-classification/ (accessed on 29 January 2022).
- Tzveta, I. An Introduction to Non-Stationary Processes; Investopedia: New York, NY, USA, 2022. [Google Scholar]
- Brownlee, J. How to Remove Trends and Seasonality with a Difference Transform in Python. 2017. Available online: https://machinelearningmastery.com/remove-trends-seasonality-difference-transform-python/ (accessed on 29 January 2022).
- Palachy, S. Detecting Stationarity in Time Series Data. 2019. Available online: https://towardsdatascience.com/detecting-stationarity-in-time-series-data-d29e0a21e638 (accessed on 29 January 2022).
- Mushtaq, R. Augmented Dickey Fuller Test. SSRN Electron. J.
**2011**. [Google Scholar] [CrossRef] - Ng, R. RAPID Fractional Differencing to Minimize Memory Loss While Making a Time Series Stationary. 2019. Available online: https://medium.com/rapids-ai/rapid-fractional-differencing-to-minimize-memory-loss-while-making-a-time-series-stationary-2052f6c060a4 (accessed on 29 January 2022).
- Baasher, A.; Fakhr, M. Forex trend classification using machine learning techniques. In Proceedings of the 11th WSEAS International Conference on Applied Computer Science, Penang, Malaysia, 3–5 October 2011; World Scientific and Engineering Academy and Society (WSEAS): Stevens Point, WI, USA, 2011; pp. 41–47. [Google Scholar]
- Sezer, O.; Ozbayoglu, M. Algorithmic Financial Trading with Deep Convolutional Neural Networks: Time Series to Image Conversion Approach. Appl. Soft Comput.
**2018**, 70, 525–538. [Google Scholar] [CrossRef] - Yu, L.; Wang, S.; Lai, K. Foreign-Exchange-Rate Forecasting with Artificial Neural Networks; International Series in Operations Research & Management Science; Springer: Berlin/Heidelberg, Germany, 2007. [Google Scholar]
- Peerchemist. Finta. 2021. Available online: https://github.com/peerchemist/finta (accessed on 29 January 2022).
- Twopirllc. Pandas-ta. 2021. Available online: https://github.com/twopirllc/pandas-ta (accessed on 29 January 2022).
- Chollet, F. Deep Learning with Python; Manning Publications: Greenwich, CT, USA, 2017. [Google Scholar]
- Krizhevsky, A.; Sutskever, I.; Hinton, G. ImageNet Classification with Deep Convolutional Neural Networks. In Proceedings of the 25th International Conference on Neural Information Processing Systems–Volume 1, Nevada, CA, USA, 3–6 December 2012. [Google Scholar]
- Gan, K.; On, C.; Anthony, P.; Chang, S. Homogeneous Ensemble FeedForward Neural Network in CIMB Stock Price Forecasting. In Proceedings of the 2018 IEEE International Conference on Artificial Intelligence in Engineering and Technology (IICAIET), Kota Kinabalu, Malaysia, 8 November 2018; pp. 1–6. [Google Scholar] [CrossRef]
- Yang, J.; Rao, R.; Pei, H.; Ding, P. Ensemble Model for Stock Price Movement Trend Prediction on Different Investing Periods. In Proceedings of the 2016 12th International Conference on Computational Intelligence and Security (CIS), Wuxi, China, 16–19 December 2016; pp. 358–361. [Google Scholar] [CrossRef]
- Anzél, K. Top risk management strategies in forex trading. IG
**2020**. Available online: https://www.ig.com/en/trading-strategies/top-risk-management-strategies-in-forex-trading-200630 (accessed on 29 January 2022). - Belk, P. The organisation of foreign exchange risk management: A three-country study. Manag. Financ.
**2002**, 28, 43–52. [Google Scholar] [CrossRef] - Dash, M.; Kodagi, M.; Babu, N. An Empirical Study of Forex Risk Management Strategies. Indian J. Financ.
**2008**, 2. Available online: https://ssrn.com/abstract=1326462 (accessed on 29 January 2022). - Al-Momani, R.; Gharaibeh, M. Foreign exchange risk management practices by Jordanian nonfinancial firms. J. Deriv. Hedge Funds
**2008**, 14, 198–221. [Google Scholar] [CrossRef] [Green Version] - Farley, A. Strategies & Applications behind The 50-Day EMA (INTC, AAPL); Investopedia: New York, NY, USA, 2020. [Google Scholar]
- Evans, C.; Pappas, K.; Xhafa, F. Utilizing Artificial Neural Networks and Genetic Algorithms to Build an Algo-Trading Model for Intra-Day Foreign Exchange Speculation. Adv. Mob. Ubiquitous Cogn. Comput. Math. Comput. Model. J.
**2013**, 58, 1249–1266. [Google Scholar] [CrossRef] - Hirabayashi, A.; Aranha, C.; Iba, H. Optimization of the trading rule in foreign exchange using genetic algorithm. In Proceedings of the 11th Annual Conference on Genetic and Evolutionary Computation, Montreal, QC, Canada, 8–12 July 2009; pp. 1529–1536. [Google Scholar] [CrossRef]
- Mehtab, S.; Sen, J. A Time Series Analysis-Based Stock Price Prediction Using Machine Learning and Deep Learning Models. Int. J. Bus. Forecast. Mark. Intell.
**2020**, 6, 272–335. [Google Scholar] [CrossRef] - Hermans, M.; Schrauwen, B. Training and analyzing deep recurrent neural networks. In Proceedings of the 26th International Conference on Neural Information Processing Systems–Volume 1, Nevada, CA, USA, 5–10 December 2013; pp. 190–198. [Google Scholar]
- Bao, W.; Yue, J.; Rao, Y. A deep learning framework for financial time series using stacked autoencoders and long-short term memory. PLoS ONE
**2017**, 12, e0180944. [Google Scholar] - Schuster, M.; Paliwal, K. Bidirectional recurrent neural networks. Signal Process. IEEE Trans.
**1997**, 45, 2673–2681. [Google Scholar] [CrossRef] [Green Version] - Eapen, J.; Bein, D.; Verma, A. Novel Deep Learning Model with CNN and Bi-Directional LSTM for Improved Stock Market Index Prediction. In Proceedings of the 2019 IEEE 9th Annual Computing and Communication Workshop and Conference (CCWC), Las Vegas, NV, USA, 7–9 January 2019; pp. 264–270. [Google Scholar] [CrossRef]
- Al-Thelaya, K.; El-Alfy, E.S.; Mohammed, S. Evaluation of bidirectional LSTM for short-and long-term stock market prediction. In Proceedings of the 2018 9th International Conference on Information and Communication Systems (ICICS), Irbid, Jordan, 3–5 April 2018; pp. 151–156. [Google Scholar] [CrossRef]
- Di Persio, L.; Honchar, O. Artificial neural networks architectures for stock price prediction: Comparisons and applications. Int. J. Circuits Syst. Signal Process.
**2016**, 10, 403–413. [Google Scholar] - Yu, X.; Li, D. Important Trading Point Prediction Using a Hybrid Convolutional Recurrent Neural Network. Appl. Sci.
**2021**, 11, 3984. [Google Scholar] [CrossRef] - Gudelek, M.U.; Boluk, S.A.; Ozbayoglu, A.M. A deep learning based stock trading model with 2-D CNN trend detection. In Proceedings of the 2017 IEEE Symposium Series on Computational Intelligence (SSCI), Honolulu, HI, USA, 27 November–1 December 2017; pp. 1–8. [Google Scholar] [CrossRef]
- Lecun, Y.; Haffner, P.; Bengio, Y. Object Recognition with Gradient-Based Learning. In Shape, Contour and Grouping in Computer Vision; Springer: Berlin/Heidelberg, Germany, 2000. [Google Scholar]
- Sarraf, S.; Tofighi, G. Classification of Alzheimer’s Disease using fMRI Data and Deep Learning Convolutional Neural Networks. arXiv
**2016**, arXiv:1603.08631. [Google Scholar] - Yang, H.; Zhu, Y.; Huang, Q. A Multi-indicator Feature Selection for CNN-Driven Stock Index Prediction. In Proceedings of the 25th International Conference, ICONIP, Siem Reap, Cambodia, 13–16 December 2018; pp. 35–46. [Google Scholar] [CrossRef]
- Yang, C.; Zhai, J.; Tao, G. Deep Learning for Price Movement Prediction Using Convolutional Neural Network and Long Short-Term Memory. Math. Probl. Eng.
**2020**, 2020, 2746845. [Google Scholar] [CrossRef] - Sutton, R.S.; Barto, A.G. Reinforcement Learning: An Introduction, 2nd ed.; The MIT Press: Cambridge, MA, USA, 2018. [Google Scholar]
- Chen, J. Pullback Definition; Investopedia: New York, NY, USA, 2021. [Google Scholar]
- OANDA. Available online: https://www.oanda.com/sg-en/ (accessed on 29 January 2022).
- Lioudis, N. How Global Events Affect the Forex Market; Investopedia: New York, NY, USA, 2019. [Google Scholar]

**Figure 23.**Drawdown level with regards to percentage of the initial capital for each strategy/model ordered left to right from the best to the worst.

**Figure 24.**Composite charts for overall accuracy (1st bar chart, on top), F1 for buy (2nd bar chart, in the middle) and F1 for sell (3rd bar chart, at the bottom) combined with Nett P&L result shown by the yellow line, ordered left to right from the best to the worst as per the measure in each of the respective bar chart.

Parameter | Description | Optimized Value |
---|---|---|

EMA Period | Exponential moving average over n day period | 165 |

RSI Period | Relative strength of market over n day period | 17 |

RSI buy_threshold_value | When RSI value reaches below the this value, a “BUY” signal is triggered | 90 |

RSI_sell_threshold_value | When RSI value reaches above the this value, a “SELL” signal is triggered | 80 |

Parameter | Value | Notes |
---|---|---|

Initial Trading Capital | USD 5,000,000 | - |

Lot Size | USD 10,000 (mini lot) | - |

Maximum Position per trade | 100 mini-lot | Equivalent to USD 1,000,000 or 20% of capital size |

Maximum Drawdown | 50% | If the capital has been reduced below 50%, the simulated trading will be stopped |

Take Profit Pips | 100 | - |

Stop Loss Pips | 100 | - |

Funding Cost | 0 | Cost of holding capital |

Transaction Cost | 1 pip for each mini lot | Equivalent to USD 100 per transaction |

No | ML-Type | Model | Training Data | Gross P&L | Nett P&L | %Nett P&L | Max Drawdown from Open in % | Max Drawdown from Peak % | Overall Accuracy | F1 for Buy | F1 for Sell |
---|---|---|---|---|---|---|---|---|---|---|---|

1 | RL | DQN | 2017–2019 | 207,930 | 156,830 | 3.14 | −3.65 | −7.25 | 0.51 | 0.66 | 0.12 |

2 | RL | A2C | 2017–2019 | 212,630 | 151,230 | 3.02 | −1.20 | −4.43 | 0.51 | 0.59 | 0.38 |

3 | RL | PPO | 2017–2019 | 196,440 | 137,140 | 2.74 | −1.14 | −4.34 | 0.51 | 0.63 | 0.28 |

4 | DL | CNN | 2006–2018 | 287,120 | 233,720 | 4.93 | −2.41 | −7.51 | 0.54 | 0.61 | 0.43 |

5 | DL | CNN-BILSTM | 2006–2018 | 289,560 | 232,360 | 4.90 | −0.46 | −7.30 | 0.54 | 0.59 | 0.46 |

6 | DL | BILSTM | 2006–2018 | 321,120 | 263,920 | 5.53 | −0.34 | −7.43 | 0.55 | 0.61 | 0.48 |

7 | GA | Pullback | 2006–2018 | 536,470 | 497,770 | 10.55 | −3.38 | −12.60 | 0.56 | 0.71 | 0.01 |

8 | Ensemble | Majority Vote | N.A | 515,920 | 468,420 | 9.97 | −0.68 | −9.69 | 0.55 | 0.69 | 0.19 |

Publisher’s Note: MDPI stays neutral with regard to jurisdictional claims in published maps and institutional affiliations. |

© 2022 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (https://creativecommons.org/licenses/by/4.0/).

## Share and Cite

**MDPI and ACS Style**

Loh, L.K.Y.; Kueh, H.K.; Parikh, N.J.; Chan, H.; Ho, N.J.H.; Chua, M.C.H.
An Ensembling Architecture Incorporating Machine Learning Models and Genetic Algorithm Optimization for Forex Trading. *FinTech* **2022**, *1*, 100-124.
https://doi.org/10.3390/fintech1020008

**AMA Style**

Loh LKY, Kueh HK, Parikh NJ, Chan H, Ho NJH, Chua MCH.
An Ensembling Architecture Incorporating Machine Learning Models and Genetic Algorithm Optimization for Forex Trading. *FinTech*. 2022; 1(2):100-124.
https://doi.org/10.3390/fintech1020008

**Chicago/Turabian Style**

Loh, Leonard Kin Yung, Hee Kheng Kueh, Nirav Janak Parikh, Harry Chan, Nicholas Jun Hui Ho, and Matthew Chin Heng Chua.
2022. "An Ensembling Architecture Incorporating Machine Learning Models and Genetic Algorithm Optimization for Forex Trading" *FinTech* 1, no. 2: 100-124.
https://doi.org/10.3390/fintech1020008