Machine learning for algorithmic trading

. The purpose of the study is to confirm the feasibility of using machine learning methods to predict the behavior of the foreign exchange market. The article examines the theoretical and practical aspects of the implementation of artificial neural networks in the process of Internet trading. We studied the features of constructing automated trading advisors that perform trading operations based on the forecast of neural networks in combination with indicator signals. As a result, a hybrid system has been built that has a high-precision forecast and allows you to make a profit with the correct selection of parameters.


Introduction
The study of the dynamics of financial markets today is a rather difficult and urgent task, since the availability of a correct forecast of price changes is the main element of the success of the owner of a financial asset.
Currently, automated trading in the financial market is represented by a number of standing and effective advisors for trading. A huge number of robots are offered in free and paid access, but they all quickly lose their effectiveness. The market is changing and automated trading systems (ATS), which could have been profitable yesterday, are already unprofitable today. To solve this problem, so-called advisors based on neural network technologies have recently appeared. With the ability to make a forecast based on the identified patterns, systems of this kind allow traders to respond in a timely manner to market fluctuations. Neural network automated trading systems are actively discussed in many forums and specialized platforms for traders, since indeed, with the proper approach, they can function in a wide variety of markets [1,3,8].
The relevance of the research topic is due to the lack of a large number of tested and free neural network automatic telephone exchanges. Such technologies have not been sufficiently studied by trading participants, which is explained by the lack of scientific research by specialists in this field, weak interaction of analytical services and, as a result, the prevalence of stereotypes of market behavior based on subjective practical experience and intuition of the participants in the process. The price of paid neural network automatic telephone exchanges ranges from several tens to several thousand dollars, while the source code of the advisor is hidden, it is impossible to trace on the basis of which rules a particular transaction is made, the user can intervene minimally in the trading process by setting the lot size and a number of other key trading parameters.

Similar systems
The use of neural networks as part of trading strategies in the past decade has been widely discussed in scientific publications. The dynamic, complex, evolutionary and chaotic nature of the market clearly demonstrates the limitations of classical statistical methods for forecasting time series of stock prices, and requires more powerful methods to complete the task. In particular, when dealing with market trends, we need methods that can work with a large amount of "noisy" and non-linear data. Given the disadvantages imposed by statistical methods, machine learning methods such as artificial neural networks (ANNs) in combination with heuristic algorithms will be used as an alternative.
Despite the fact that ANNs have become popular methods for forecasting the stock market, their use and evaluation in real scenarios to make correct predictions and make a profit is still insufficiently studied. Most ANNs proposed so far are estimated using the classical model, the metrics of which give us the concept of a generalized model, but not understanding the behavior of the system in the real market. One of the ways to assess the real quality of ANN forecasts is to create a hybrid system, which is an automatic trading advisor operating in real time [5,6,7].
This study is based on the hypothesis that it is advisable to use machine learning methods in combination with heuristic algorithms and technical analysis indicators to derive the relationship between price closing data and when making a forecast about market behavior. In this case, neural network technologies make it possible to identify the patterns of price dynamics in the financial market in the presence of a sufficient amount of historical data.
In this paper, the object of research is the foreign exchange market, in particular, the forecast of the closing price of USD against EUR within a given time interval. The aim of the work is to create a robot-advisor, the trading strategy of which is based on neural network technologies for performing automated trading.

Creation of a library that implements a neural network (NN) model
The library is based on a neural network of the following architecture: fully connected multilayer perceptron of direct propagation. This architecture was chosen due to its ability to approximate complex nonlinear functions, which are the functions of chart behavior in the market [2,4].
To select the NN parameters that are best suited to achieve this goal, a genetic algorithm was chosen that is used to solve optimization and modeling problems by random selection, combination and variation of the desired parameters using mechanisms similar to natural selection in nature.
There are other ways to select parameters, such as a complete enumeration of all values in a certain range with a given step, but this method is considered ineffective in terms of time costs.
During the execution of the genetic algorithm, several specimens with different parameters (genes) were created. Figure 1 shows individual N, which is a set of network parameters:  Inp -the number of neurons in the input layer;  h1 -the number of neurons in the first hidden layer;  h2 -the number of neurons of the second hidden layer;  out -the number of neurons in the output layer;  act_r -learning rate parameter;  b -smoothing factor;  result -the result of the specimen after passing the test. The created population was run on historical data, trained and tested.
To assess the result of the neural network, the following formula was used (1): (1) NW resuit (t) -the result of the neural network for the selected timeframe T; p i (T) -real-time closing price; p i (T) -network predicted price; n -number of values for a specified period (timeframe).
The new population was created by crossing parameters and applying random gene mutations. Natural selection left the specimens with the best results.
Comparing the best specimens of each generation, one cannot fail to notice a significant improvement in the result. Compared to the first generation with the best individual having a score of 0.399, the best individual of the third generation has a score of 0.173.
The result of the genetic algorithm is a vector containing the network parameters with the best result.
Having found the optimal parameters, let's move on to training the model. To train the neural network, the backpropagation method was used. The method consists in calculating all errors up to the input layer based on the error value on the output layer, and then we change the weights using the delta rule. Training consists in reducing the error measure (for this, the gradient descent minimization method is used).
Hyperbolic tangent is selected as the ANN activation function. The training took place on normalized historical data obtained from the finam.ru website for a two-year period -from 11.11.2017 to 11.11.2019 with a timeframe of 1 hour (1H).
The learning algorithm is presented in stages: a vector of closing prices for a certain period is fed to the network input, on the basis of which the prediction is built; calculation of the average absolute error; calculating the error of each neuron and changing the weights. The result of this stage of the research is the ANN trained on historical data.

Creation of an indicator using a trained NN
The indicator created on the basis of the trained ANN makes it possible to obtain a forecast for several hours ahead. Based on the experience of trading, the most optimal for decisionmaking is a forecast 10 hours ahead. The 10-hour forecast consists of 10 network forecasts, each time 1 hour ahead. Having made a forecast one hour ahead, the indicator (network) makes another forecast, but taking into account the previous result. These values were chosen as the best after several launches of the advisor on historical data USD / EUR with a timeframe of 1 hour (1H) from 08/19/2019 to 10/18/2019. After that, the advisor was launched on the EUR / USD chart on the 1 hour timeframe (1H) on 10/19/2019 and worked until 12/21/2019 inclusive, the initial deposit is $ 1000. The work of the advisor is shown in Figure 3. In this figure, the abscissa is time, and the ordinate is the closing prices. At the end of trading, the advisor executed 12 trades (24 deals), of which: 8 with a profit, 4 with a loss, 8 for a buy, 4 for a sale. The EA results are shown in Figure 4.

Conclusion
Today, two-thirds of investors are ready to heed the advice of a computer when investing, and in the future, artificial intelligence may become a major player in the global currency markets.
In this paper, an automatic telephone exchange is proposed, which gives recommendations to the user for buying and selling. The test results allow us to assert that an Expert Advisor based on a trained neural network provides tips to make a profit.
Our conclusions should attract the attention of foreign exchange market participants who trust advisors to trade. The results confirm the existing studies in this area, which generally indicate the impossibility of predicting the movement of the financial market, analyzing only historical prices. To form a reliable picture of the foreign exchange market, a hybrid system is required that operates in accordance with the current trading strategy.
The question remains unclear: how often should the neural network be retrained? The answer to this question requires further research.