A Hybrid Artificial Intelligence Approach to Portfolio Management

The tremendous advances in artificial intelligence over the past decade have led to their increasing use in financial markets. In recent years a large number of investment companies and hedge funds have been implementing algorithmic and automated trading on their trading. The speed of decision-making and execution is the most important factor in the success of institutional and individual investors in capital markets. Algorithmic trading using machine learning methods has been able to improve the performance of investors by finding investment opportunities as well as time entry and exit of trading. The purpose of this study is to achieve a better portfolio performance by designing an intelligent and fully automated trading system that investors with the


Introduction
The stock market is always one of the most popular investment sectors due to its high returns. However, some factors have made the behaviour of this market unpredictable. So far, many models have been proposed using different tools and techniques to predict stock market behaviour. Since accurate forecasting of stock price changes can lead to significant benefits, it is important to provide a model for intelligently forecasting the market and allocating a portfolio based on it.
Success in stock trades extensively depends on proper decisions for ontime and correct enters and exits. This issue requires the investigation of information as well as a specialization for exploring investment opportunities. In recent decades, researchers have sought scientific and quantitative methods to obtain better results in financial markets. However, owing to many reasons, the majority of traders are incapable of using scientific analysis in trades. Hence, in recent years, there is a growing need for automated approaches to use financial data effectively to support investment decisions. . One of the most attractive fields for the use of artificial intelligence is stock trading and stock selection systems. The stock trade system, employed as an auxiliary tool in investment decisions, is reckoned as a novel research domain in the world. It enjoys a thorough potential in increasing profitability besides its research potential .
This study aims to develop an intelligent trading system that exploits the conventional technical analysis tools and meta-heuristic algorithms to generate correct trading signals in the return points of trends. Therefore, backed by this system, investors can make timely and correct decisions in their trades and maximize the profitability of their investments .
This study comprises four separate phases. The indicators' parameters will be optimized by the genetic algorithm in the first phase. Each indicator has many input variables that are static for every stock and market. Thus, the computing complexity of this problem compels us to employ one of the metaheuristic algorithms, such as the genetic algorithm to customize and optimize indicators for each stock. In the second phase, the market's current trend will be identified by the fuzzy inference system since a set of technical rules will be selected according to the market regime (neutral or trending market). In the third phase, the variables are aggregated, and with the help of a neural network, the final output signal will be presented as a buy, sell, or hold. Each indicator may generate a different signal. Therefore, a single answer can be generated by the neural network for decision making.
Finally, in the last phase, the portfolio is rebalanced by the candidate list of buying or selling. This optimal allocation is done by optimizing the risk and return along with using a few real constraints.

Literature Review
During the past recent decades, many researchers have employed softcomputing technics, including the genetic algorithm and artificial neural networks in mathematical problems (Goldberg, 1998). These methods were successfully applied to solving diverse problems. Soft-computing technics were employed in making many smart systems. In the meantime, one of their applicative domains is financial problems, such that many researchers are now investigating the applications of soft-computing technics (it is better to use "techniques" in formal settings) in the financial domain (Baba et al., 2001).
The conventional technical analysis, which is the base of many trading systems, usually emerges in the forms of different trading rules and concentrates on the peak and bottom points of prices in trends, and it is extensively prevalent in the world's markets. Unfortunately, the stock trading systems that are based on using merely the technical rules will be no more suitable owing to their dependence on traders' experiences in selecting decision-making rules. This is the biggest drawback of using this type of analysis. The main problem is that the technical rules are based on some parameters. If technical parameters are truly and smartly determined, they will deliver appropriate buy and sell signals for the investor's profitability. Another problem is that different technical rules may simultaneously create contradictory trading signals and problematize making decisions in such conditions. Refenes et al. (1994) modelled the behaviour of stock prices via neural networks and compared its performance to regression models. In this study, the researchers employed neural networks as an alternative for the classic statistical technics to predict the price of stocks. The results of this study showed that neural networks performed better than statistical methods and provided better models. Tan et al. (1995) designed a system that predicted the considerable shortterm changes in the stock price. At first, a pre-processing was conducted on the data, and then the neural network was modelled which estimated trading positions well. Kuo et al. (2001) developed a prediction system regarding stocks hold, sell, and buy in the stock exchange markets. The characteristic of the developed system was that it allowed the quantification of qualitative variables interfering with the prediction of stock prices. These researchers have published a paper with the same title in 1998 without using genetic algorithms. In the mentioned paper, the researchers used a questionnaire with the Fuzzy Delphi Method (FDM) to make use of experts' opinions in predicting stock prices. Yim (2002) conducted a study comparing the econometric methods using ARMA and GARCH Models. The results showed the superiority of neural networks to the classic ARMA and GARCH models. In a study, Nouri et al. (2014) compared artificial neural networks (ANN) and autoregressive integrated moving average model (ARIMA) in the modelling and short-term prediction of the exchange rate in Iran. Their results revealed that the employed neural network enjoyed a better prediction power compared to the ARIMA model. In another study, Foroughi et al (2014) predicted the profit of every stock according to the combination of artificial neural networks and the optimization algorithm of particle aggregative movement. Their results showed that the univariate and multivariate models predicted every stocks' profit with 78% and 92% precisions, respectively. Souto-Mairo (2011) used fuzzy logic to predict the moving direction of the Brazil Stock Price Index, and their predictive results were finally evaluated to be suitable.
Alejandro Rodríguez et al. (2011) exploited neural networks to improve technical analysis indicators. This Study used the genetic algorithm to improve the prediction parameters used in technical analysis and enhance the ENS networks via improved parameters for the purpose of predicting turnover points. The results of studies show that the employed approach performs better than the buy and hold policy (Lin et al., 2011). Rahnamay et al. (2015) adopted a genetic algorithm approach to optimize a portfolio comprising the mutual funds stock of the Tehran Stock Exchange. The results indicated that the genetic algorithm could be more desirable for selecting the portfolios developed by the genetic algorithm. Tehrani and Abbasion (2008) used only a neural network tool to investigate the schedule of entering the stock trades with a technical approach. Their results revealed that the system's performance was only suitable for Bear markets. In Bull markets, there was not a significant difference between the proposed trading system and the hold and buy method. Brogaard et al. (2018) investigate high-frequency trading around extreme price movements, classified as the top 0.1% of the individual stock price changes over 10-s trading intervals. The authors find that the net effect of highfrequency trading liquidity demand and supply is negatively associated with the direction of the price changes. Michell et al. (2020) used An embedded approach of STPG and a Fuzzy inference system to generate trading rules. They used a fuzzy inference system (FIS) and strongly typed genetic programming (STGP) to generate trading rules for the US stock market, a framework called FISTGP. The results show that the proposed model outperforms the Buy and Hold (B&H) strategy by 28.62% in the test period, considering excesses of return, with almost the same risk (1.28% higher). Manahov (2018) studied the effect of high-frequency data with an STGP approach, to see if it is beneficial or not to the market, finding that it is actually harmful to institutional traders, on a millisecond basis. Ha & Moon (2018) proposed GP to identify patterns in the cryptocurrency market. They focus the study on applying the signals in the market to increase investment return. Based on several experiments, they show that GP can create useful rules for trading, performing well in the tests. Rastegar and Sedaghatipour (2019) also presented an algorithmic trading system for gold coin futures contracts based on overnight data. Due to the fact that technical analysis is suitable for two-way markets (buying and selling position), Signal 8 technical tools have been used for the trading system. In order to create a trading system, the MOPSO algorithm has been used with the aim of optimizing the two functions of return and conditional value at risk (CVaR). Also, to complete the risk management system, the optimal profit and loss limit for the futures contract has been set. The results show that the designed trading system has a more return-to-risk ratio than other competing strategies such as buying and holding and selling and holding. Baky et al. (2021) have tried to create an intelligent trading system based on technical analysis Using meta-heuristic algorithms and machine learning. Using a genetic algorithm, they optimized the parameters of technical indicators and using an artificial neural network converted the signals of indicators into buying or sell orders. The results show that there is a statistically significant difference between stocks buy and hold and the trading system presented by their research.

Research Methodology
This study comprises four separate phases. The variables will be optimized by the genetic algorithm in the first phase. There is a minimum m analytical rule, each of which includes n parameters for presenting signals. Thus, the computing complexity of this problem compels us to employ one of the metaheuristic algorithms such as the genetic algorithm. In the second phase, the market's current trend will be identified by the fuzzy inference system since a set of technical rules will be selected according to the kind (Trending or nontrending). In the third phase, the variables are aggregated, and with the help of a neural network, the final output signal will be presented as a buy, sell, or hold signal.
In the last phase, the candidate list for buying and selling will be allocated and the portfolio will be rebalanced by one of the portfolio optimization models. The general structure of research is described in figure 1 including the construction and training model and figure 2 including the implementation of the trading system.

Parameters Optimization
The first step is to adjust the parameters used in the technical indicators. A change in the used parameters issues varying responses. Commonly, experts determine the parameters empirically and by trial and error based on their personal experiences. For example, a 10-day moving average may offer relatively correct and on-time signals for a certain stock, whereas a longer interval is needed for another stock. Accordingly, we must define an algorithm to properly adjust the parameters of the mentioned variables for every stock. The genetic pattern is one of the most suitable algorithms in terms of discovering correct and quick responses.
Suppose the sequence of the expected trading points in which asset buy and sell signals exist is like T (T1, T2, T3, …, Tn ). For every expected trading point (Ti), we will look for operational signals (Sj) proposed by the different rules of the technical analysis. Evidently, every signal is placed between the two expected trading points (Ti-1 < Sj <Ti+1).
According to the type of the trading point; i.e. a buy trading point and sell trading point, we will have the following conditions for the fitness function: If Ti is an anticipated trading point for asset buying, there will be three conditions for the fitness function as below: If Sj (trading signal) is a buy proposal, the magnitude of the fitness function at the Ti point will be computed as follows: In recent trades: Close (Ti) indicates the closing price at Ti.
Close (Sj) indicates the closing price at Sj.
The more the closing price at Sj is closer to the closing price at Ti, the smaller the magnitude of the fitness function.
If Sj is a proposed sell signal, and the price at Sj is close to the price at Ti, the magnitude of the fitness function will be computed by the below Equation: The closeness term in the current equation is as below: In the above fitness function, max (close (Ti-1: Ti+1)), meaning maximum closing price, is between Ti-1 and Ti+1. Since asset selling in minimum price points of a trend is a wrong trading policy, such a decision will accompany a penalty.
If no operation between Ti-1-1 and Ti+1+1 is proposed, a penalty will be earmarked in the model for losing a trading opportunity. In this case, the fitness function will be as below: - Similarly, if Ti is an expected sell point, the fitness function will be a penalty as below: If Sj is a proposed sell signal, the fitness function at Ti will be as below: If the price at Sj is close to the price at Ti, and Sj is erroneously considered as a buy point, the fitness function will be as below: Such that min (close (Ti-1+1: Ti+1-1)), which means minimum closing price, is between Ti-1+1 and Ti+1-1.
If the trading opportunity is lost in the between Ti-1+1 and Ti+1-1 interval, the fitness function will be as below: Finally, the fitness function of the trading points sequence S= {S1, S2, …, Sn} is defined as below: It is anticipated that the genetic algorithm can deliver an optimal combination of parameters such that the magnitude of the fitness function is minimized.
Below table 1 shows the formula of indicators used in the research as well as their parameters that need to be optimized. Table 1 shows the indicators that have been used as a trending market and table 2 shows indicators that have been used in the non-trending or range market.

Ichimoku
As long as the price is higher than the cloud, the general trend is upward, and if the price is lower than the cloud, it is a downward trend, and if the price is inside the cloud, it is fluctuating.

Period of PH & PL in Conversion
Line and Base Line

Identifying the Market Regime
Every variable used in this study issues a proper signal according to a trending or non-trending market. For example, the stochastic oscillator will not enjoy the necessary efficiency in an up-trending or down-trending market. However, this oscillator can be more properly applied to a non-trending market. Accordingly, it is necessary to determine the kind and current situation of the market before using these kinds of variables.
However, determining a market's situation is reckoned as the main challenge of the capital market activists. It is because there is no firm criterion for it, such that an expert may perceive a market situation as non-trending, and another one may perceive it as a trend. This problem is an intuitive issue, and we can define no definiteness for it. One of the strong mathematical tools extensively used as an uncertainty explanation and description is the fuzzy logic theory. It is a novel tool for solving problems that the probability theory does not offer a solution.

Fuzzification
In this phase, we consider membership functions for every input variable to transform crisp inputs to fuzzy ones and place them in the fuzzy inference system. The membership functions are of different kinds, such as triangular, trapezoidal, arch, etc. In this study, we use a trapezoidal kind. Here, the three variables, including the distance from moving average (d), the slope of moving average (s), and the Index of average directional movement (that is a kind of Technical indicator) are proposed as input linguistic variables.
All these variables are thermalized in a numerical interval between -1and 1 according to the below Equation. For normalizing or fuzzification of numbers in fuzzy theory we should use the below equation: Where max (v) is the maximum value in the training data, and min (v) is the minimum value in the total training data. The below Figure illustrates the membership function applied in this study, and vara, varb, varc, and vard indicate the linguistic value of the linguistic variables. a1, a2, a3, a4 and b1, b2,b3, b4 are the parameters that specify the shape of the membership function. Table 3 outlines the selected interval for the abovementioned parameters. In the following, we present the membership functions employed in the study to determine the rate of membership degree: Table 4 illustrates the characteristics of all linguistic variables that are employed in this system.

Rule Base
The rule base is defined as a fuzzy "if-then" set, which comprises the heart of the fuzzy inference system. There are two main methods for determining fuzzy rules: one is to use expert knowledge, and the other is to use self-organized knowledge such as novel algorithms and neural networks. This study uses the first method to determine fuzzy rules.

The knowledge base for fuzzy inference in this system is defined as below:
Rule 1: If +d is large and s-is small; then, the trend will be neutral. Rule 2: If d-is small and -s is large; then, the trend will be directional. Rule 3: If d-is small, -s is small, and adx is strong; then, the trend will be directional. Rule 4: If -d is small, s-is small, and adx is weak; then, the trend will be neutral. Rule 5: If d+ is small, -s is large, and adx is strong; then, the trend will be directional. Rule 6: If d+ is small, -s is small, and adx is strong, then, the trend will be directional. Rule 7: If d+ is small, -s is small, and adx is weak, then, the trend will be neutral.
Rule 8: If d-is large, -s is small, and adx id weak, then, the trend will be neutral. Rule 9: If d+ is small and s+ is large, then, the trend will be directional. Rule 10: If d+ is small, s+ is small, and adx is strong, then, the trend will be directional. Rule 11: If d+ is small, s+ is small, and adx is weak, then, the trend will be neutral. Rule 12: If -d is small and +s is large, then, the trend will be directional. Rule 13: If -d is small, +s is small, and adx is strong, then, the trend will be directional. Rule 14: If -d is small, +s is small, and adx is weak, then, the trend will be neutral.

Defuzzification
Defuzzification is a process that converts a fuzzy set into a crisp number. Thus, the input to the defuzzification process is a fuzzy set (the sum of output fuzzy sets), and its output is a number. Many methods are used for this purpose; however, in this study, we use the center of gravity (COG) method to create the crisp output.
The created crisp output is compared by the confidence interval that we have defined. Accordingly, if COG is larger than 0.6, the market will be trending; otherwise, we will face a non-trending market.

Creating the Final Signal
In the previous two phases, we dealt with optimizing and determining the market situation. In the second phase, after determining the market situation, we will take the output signals from the variables with respect to the separation developed among them. The variable kinds can be separated as below: The variables used in a trending market: Double moving average, directional average indicator, parabolic SAR, moving average convergence/divergence (MACD), and OBV indicator.

The variables used in a non-trending market:
Momentum index, relative strength indicator (RSI), stochastic index, MACD indicator, and money flow index (MFI).
Each of the above-mentioned indices will issue a signal that is not necessarily equal to another one. Hence, 5 issued signals should be integrated, and the tool used for this purpose is a neural network. Different indicators may produce different signals. Here, by application of the neural network as a classification problem, different signals are taken from Technical indicators and determine if a correct signal is a buy, sell or hold signal. The input to the network is a buy, sell or no decision signal, which we indicate with -1, 1 and 0 respectively. In fact, the signal of each Technical rule is a member of the network input array. Respectively output neurons are bought (+1), sell (-1) or hold (0).
We are using the Elman network in this Research. An Elman network is a three-layer network (arranged horizontally as x, y, and z in figure .4) with the addition of a set of context units (u in the illustration). The middle (hidden) layer is connected to these context units fixed with a weight of one. At each time step, the input is fed forward and a learning rule is applied. The fixed back-connections save a copy of the previous values of the hidden units in the context units (since they propagate over the connections before the learning rule is applied). Thus the network can maintain a sort of state, allowing it to perform such tasks as sequence-prediction that are beyond the power of a standard multilayer perceptron (15).

Figure 4. A View from Elman Network
Assume that is an input vector, so the activation of hidden state is updated according to: Where is the internal activation function (typically sigmoid functions). Also, , are parameter metrics and vectors. The network output is obtained through the following equation: Where is the output activation function, The main difference between the two-layered Elman network and the other two-layered networks is that the connection in the first layer of the network is recurrent. The delay that occurs in this connection type triggers the storage of the past values as well as their uses in later steps. The feedback mechanism in this network is such that even two Elman networks with similar weights, biases, and inputs can have varying outputs.

Portfolio Rebalancing
Candidate stocks in the previous step must be allocated to enter the portfolio. Therefore, the portfolio should be re-weighed or so-called Rebalanced based on the amounts of money obtained from the sale of outgoing stocks and candidate stock for entry. For this purpose, the traditional Markovitz model is used.
In this research, to make the model more practical, the model has been improved by considering several real constraints. On the other hand with considering coefficient λ in the objective function we tried to include both risk and return criteria in the objective function to Maximize returns while minimizing risk. In fact, λ is only one weighting parameter whose value varies in the range (01) and varies by investor risk preferences. The constraints used in the model are described in Table 5. Table 5. Constraints used in the model

Parameter definition Equation Constraint
Z i : 0 , 1 parameter Limit the number of equity in the portfolio K min : the minimum amount of assets in the portfolio K max : maximum amount of assets in the portfolio Ꜫ i :the minimum amount allowed per share W i ≤ δ i Z i ≥ i Z i Ꜫ Above and below allowable limit per share δ i :the maximum amount allowed per share ADV: average daily trading volume in last month W i ≤ Z i . a .ADV Liquidity limitation for controlling the market impact a: percent of daily volume Finally proposed model is rewritten as Equation (17):

Research Findings
In this section, the results of the implementation and execution of the proposed system are presented and compared. In addition to comparing with TEDPIX, the performance of the model is also compared with the performance of 3 of the best mutual funds that had the highest returns during the period under review.
For this comparison, in addition to the return criterion during the period, 4 well-known portfolio performance evaluation criteria have been used. 'Sharpe ratio', 'Treynor ratio', 'Sortino' as well as 'maximum Drawdown' have been used for this purpose. Also, to evaluate the performance and efficiency of the second and fourth phases of the research, including the use of fuzzy logic to determine the current market trend and also portfolio rebalancing, The model was performed once without the second phase and once without the fourth phase and the results were compared with the complete algorithmic model.

Assumptions
The assumptions used in the research include two parts. Assumptions during construction and training the model, and assumptions of testing and implementing the model.
The assumptions of the Training section are as follows: 1) To implement the model, data from the beginning of 1390(Solar Hijri) to the end of 1396 have been used as training data. In this period, we have witnessed a strong upward, downward, volatile and neutral market. The same period was divided into two parts of the Trending market and non-Trending market so that the indicators can be optimized correctly. From 1391/01/01 to 1391/06/31 as well as 1395/10/01 to 1396/06/31 were considered as a market without trend and 1391/07/01 to 1392/09/30 as well as 1394/12/29 as a market with a strong trend. This classification is more intuitive based on the Tehran Stock Index chart (figure 5). Accordingly, when the market was trending, we only optimized the indicators that followed the trend, and in the non-trending periods, we only optimized the oscillators.
2) In this research, the amount of 500 billion Rials is considered as the initial amount of the portfolio. This amount has been considered due to its comparability with other mutual funds.
3) To make the model more realistic, like mutual funds, when stocks are sold and there are unallocated resources to stocks in a bear market, we allocate them in fixed income securities in order to make the best use of resources. We set the bond rate at 20 percent. The minimum and maximum government bonds rates in this period were 19 to 25 percent and most of the time around 20. So we have considered 20 percent. The assumptions of the second running model are as follows: 1) There will be weekly portfolio review and Rebalancing 2) The evaluation period of portfolio performance is from the beginning of 1397 to mid-1399.
3) The minimum number of portfolio assets is 10 and the maximum is 20.
4) The minimum allowable ratio for an asset is 3% of the total capital and the maximum is 10% (according to the investment fund management instructions by Securities & Exchange Organization). 5) 10% of the average daily trading volume of one symbol per month (ADV) is used as the liquidity constraint in the model.
6) The transaction fee is 0.5% in purchase transactions and 1% in sales transactions.

Performance evaluation
In this research as mentioned, five criteria of return, 'Sharpe ratio', 'Treynor ratio', 'Sortino' and 'maximum drawdown' have been used.
The Sharpe ratio is known for its reward to risk ratio. This ratio is based on capital market theory.
Where TRp is the average return on the total portfolio over a period of time, Rf is the average risk-free rate of return over the period under review, SDp is the standard deviation of the daily returns on the portfolio over the period under review, and Excess Return is the additional return. Sharp ratio measures stock portfolio surplus returns per unit of risk, and the higher the ratio, the better portfolio performance. Treynor introduced a similar criterion called the reward-to-volatility ratio as below: Where TRp is the average return on the entire stock portfolio over a period of time, βp is the systematic risk and indicates an investor's return on a unit of systematic risk. If the adverse risk criterion is used in the performance evaluation instead of the standard deviation, the Sortino index is achieved. In fact, if x is a portfolio return variable and the function f(x) is a function of the probability density of this variable, and µ the mean and r the minimum acceptable rate of return, the Sortino index can be shown as a (20) ratio: (20) In this study, the following equation has been used to calculate the adverse risk: The maximum drawdown is the maximum amount that the portfolio loses its value from a peak value. This changing value in the portfolio can be expressed as a percentage according to Equation: The weekly Time frame is used to calculate return and risk. The basis of the comparison period is from 1397/01/01 to 1398/06/30. The results according to the table below show the superiority of the algorithmic portfolio over other funds as well as the TEDPIX index. Also, the efficiency of the algorithm when using both the portfolio optimization model and fuzzy logic shows its complete superiority over the non-use mode. Figure 6.1 and 6.2 And Table 6 shows a comparative graph of the cumulative returns of the proposed algorithmic portfolio (in three different modes) as well as the TEDPIX index, the index of 50 top companies and the top three funds.  Table 7 compares the portfolio performance of the proposed model and the top 3 funds, as well as the total index and the index of 50 companies in all 5 criteria.

Conclusion
In this study, we tried to improve the decision parameters in the technical rules by using the meta-heuristic algorithm in the first place, and then these rules become the basis for predicting and identifying signals. Also, using the power of fuzzy logic, we tried to determine the correct direction of the market situation (trend market or neutral). Finally, using an artificial neural network, the trading signals generated by each of the technical rules are converted into a signal to decide whether to buy or sell an asset. Finally, the candidate stock list was optimized through one of the traditional portfolio optimization models.
As obtained from the results, the algorithmic portfolio proposed in the research had a much higher performance in both efficiency and risk factors. The return of the algorithmic portfolio during the test period was 2987%, which had the best performance among all mutual funds and with a much better distance from the TEDPIX index and the index of the top 50 companies. During the same period, the total index has experienced 1560% growth. On the other hand, algorithmic portfolio status has been completely superior in all portfolio performance measurement criteria except Treynor. The maximum drawdown in the algorithmic portfolio during this period was about 8%, while in the top funds it was on average 17 and in the overall index 18.5%.
The subject of algorithmic trading and trading systems is very wide and diverse. The most significant suggestive topics for future studies can be described as below: 1. The proposed mathematical model and solution method have many capabilities for development. Therefore, in the first stage, using other metaheuristic models is recommended to replace the genetic algorithm for finding a faster and more efficient answer and compare it with the model in our research. 2. The power of fuzzy logic was used to solve the problem of finding trends in the market in the existing research. However, other mathematical methods can be suggested. 3. An artificial neural network was used to weigh the final stock selection criteria. It is possible to achieve better performance by changing the type and structure of the neural network. 4. The biggest limitation in solving similar problems in algorithmic trading is the impossibility of covering the whole market stocks. Providing a model to shorten the time to reach the answer will be helpful.
Provide a model to solve the problem of market impact at the time of ordering.