Automated Forex Trading System

I acquired my first forex bank account for attending an international conference in 2018. Lacking the basic concepts for fundamental and technical analysis, I came up with a simple but profound idea: Would it be possible to construct a system that can automatically trade forex and profit from it? Thus started this project of automated forex trading.

System Framework

Fig. 1 illustrates the overall system architecture. Here, the main server is used to autonomously access online forex data, save it into a table, and perform all necessary calculations for generating a trade signal (e.g. buy, sell, do nothing). A trading model is loaded inside the main server, and can be any form of structure as long as it can input history and current forex data and output a trade signal. A client can train the trading model on a remote server, then manually update it on the main server. The trade signal is sent to another server specialized for interacting with an online trading platform.

Figure 1. System architecture of this project.

This architecture has the advantage for clearly distinguishing the functional blocks, where each block can be modified separately. For example, if one plans to implement a trading strategy that utilizes machine learning for prediction of future prices, he/she can pre-train the model beforehand, update the model to the main server, and needn’t worry about system failure as long as the input and output of the trading model corresponds to what the main server requires.

Fig. 2 details the flowchart for the main and transaction server. The servers initialize, connect with each other and other online resources, and enter the main loop for autonomous trading. The main server runs on a Linux-based operating system, where Python is used as the language for the main program. By constructing a database and querying with MySQL, per-minute forex data can be stored structurally. For the transaction server, Python is also used for the main program, and Selenium is used for web control automation. The two servers communicated using SSH protocol.

[Source Code for the main program of the transaction server]

Figure 2. Program flowchart for transaction and main server.

Trading Strategy

In this project, a simple strategy for trading is implemented for verification of the effectiveness of automated trading (see another project <reinforcement learning assisted forex trading> for in-depth investigation on trading strategies):

1. The account initially buys a currency pair at a random point.
2. If the rate increases and reach the take profit value, sell it.
3. If the rate decreases and drops below a defined value, buy twice the previous amount.

Figure 3. Trading strategy used in this project.

As long as the rate reaches the take profit point within the period of a trade, this strategy will always be profitable (though in reality this strategy has several disadvantages and runs at the risk of a stop out due to the absence of a stop loss). By using this strategy, the system can be tested of functionality since it requires every functional block to work properly.

Trading Results on Real Account

After testing a few times on a demo account, I decided to use a real account so that I can obtain practical experience being a real game player, and also be stimulated for all the amount I had invested. The initial balance is set as 500 USD ($) to give me enough capital to test this system, but not too much for losing all hope if I ever get stopped out (Fortunately, this never happened.).

The detailed trading results using a real account between 2018/05/04 and 2019/03/12 can be downloaded here. The major currency pairs are used, and a multiplier of 50 (or leverage of 2%) is used to magnify the trade profit and loss. Table 1 and 2 summarize the overall results.

Table 1. Currency pairs used for trading and their corresponding trade count.

Currency Pair Trades

Table 2. Summary for real account trading using the automated system.

Start Time PointApr 5 2018 11:05
End Time PointMar 12 2019 8:52
Total Trade Amount ($)17074
Total Received Amount ($)17234.26
Total Profit ($)160.26
Average Profit per Trade ($) 0.344

Fig. 4 shows the account balance and profit during the whole period. For the first 396 trades, the balance rises in a slow and steady manner. The 397th trade, which is the trade resulting in a sharp decline of balance, is actually manually closed by myself due to reason that it got stuck in a losing position for too long. I had to close it myself so for maintaining enough equity to keep trading. This clearly demonstrates the disadvantage of the trading strategy I had used. From the 398th to the 597th trade, I increased the buy amount so that a larger change in balance (either gain or loss) is exhibited. At last, the balance arrives at 660.26$ and a total profit of 160.26$ is acquired.

Figure 4. Balance and profit vs trade count (the i-th trade).


In summary, an automated forex trading system is developed and has several features including automatic transaction, forex history data storage, real-time data acquisition and trading model implementation. This system is comparable with another electronic trading platform MetaTrader, however still lacking several other features such as visualization of forex data or economic calendar analysis. I am planning to integrate this system with another project <reinforcement learning assisted forex trading> for optimizing the trading strategy.