Do you like the article? Share it with others - post a link to it! Use new possibilities of MetaTrader 5. The modern-day trading cannot be imagined without automated trading systems usually called Expert Advisors or robots. Most, if not all, of them feature a clear, hard-coded trading strategy and money management system. Their main advantage is a rigid algorithm excluding the human factor.
However, this advantage is also their main drawback since trading robots lack flexibility. Regardless of the market conditions, an Expert Advisor always applies the same trading strategy with the same strictly categorized parameters. In other words, the system always acts rigidly: Unlike an automated system, human traders think in fuzzy categories and may have different opinions on similar market entry signals. They are often doubtful and keep asking themselves if the trend is moderate or strong.
And even if the trend is significant, is it strong enough to enter in two lots? Such fuzzy categories can be handled by the fuzzy logic. The fuzzy logic does not set rigid boundaries between the categories. Instead, it "blurs" them making a trading system more flexible and combining the rigidness of a trading robot with the flexibility of a human mind.
The article provides examples of applying the fuzzy logic system in trading by means of MQL4. Read the article "An Introduction to Fuzzy Logic" to grasp the general concepts of the fuzzy logic theory. Also, learn the basics of FuzzyNet library for MQL4 , since it is used for the implementation of the examples. As its name suggests, this is a triangle-shaped membership function. This is a simple and most frequently used function defined by the following analytic formula:.
It is generally used to specify the following types of uncertainties: The triangular membership function parameters are usually interpreted as follows:. The trapezium-shaped membership function defined by the following formula:. The trapezoidal membership function parameters are interpreted as follows:. The membership function in the form of symmetrical bell-shaped curve defined by the formula:.
The function is calculated using the following formula and applied when setting monotonous membership functions:.
ADX operation and implemented division by a trend power. These three rigidly defined categories have some drawbacks caused by their clear and strict classification logic:. As already mentioned, the fuzzy logic "blurs" fuzzifies the specified borders. The border values of rigidly set categories are assigned to both categories at once but with varying degrees of membership.
A sample description in that case may look as follows: A human trader would describe this as follows: I believe, this is the main advantage of the fuzzy logic. It is flexible and variable when dealing with rigidly specified parameters.
I have selected the following membership functions for our example with ADX indicator:. More complex systems containing numerous categories can be described using other functions available in the FuzzyNet library. Currently, the library contains over a dozen of functions. The graphical representation of our example is shown below:. Describing a trend using the fuzzy logic.
As we can see, the graph now has the areas featuring two trend categories simultaneously. The trend in the area is weak and average, while in it is average and strong.
Thus, we have defined a term set with the predetermined membership functions for the three categories. Now that we have the ADX inputs described by the membership functions, we should define what we consider an output value and defuzzification result, as well as select a fuzzy logical output algorithm. For our example, I have selected the deposit risk percentage relative to the initially specified fuzzy trend strength variable.
In other words, the stronger the trend, the higher the risk and deposit percentage applied in trading. I have chosen Mamdani as the logical output algorithm.
Like with the trend strength, let's introduce three distinct categories according to the degree of risk:. As a result, we obtain the following graphical description by means of fuzzy logic:. Describing a degree of risk by means of the fuzzy logic.
The histogram bars' height displays a numerical value of a risk degree at various trend strength within the limits described above.
Let's examine the code in details. Next, connect the library for creating systems according to the Mamdani algorithm and add variables for visualizing the amount of bars beginning from the zero one and adjusted ADX period. When initializing, we should set the indicator to be in the form of a histogram. In the main code, we define the basic readings of ADX indicator.
Next, let's define a trend direction based on the r variable sign and place mamdani function into a predetermined value. Next, we should add the fuzzy terms described above Fig.
Pass the steps for the output value: Now, let's create a set of three fuzzy rules representing our system:. Create the lists for input and output variables and add v input to be the mamdani function argument. Thus, the entire fuzzy logic system with specified input and output fuzzy variables is set for the entire mamdani function, while ADX indicator value is used as an input.
The resulting function value is res variable the histogram is based upon. As we can see, the indicator shows the presence of a trend using a histogram color, while a bar height shows a recommended risk percentage of the deposit. The obvious question arises — what would be the difference if the indicator was implemented with clear intervals?
To answer it, let's consider the following section in more details Fig. The green arrow shows the histogram bar, while its numerical value and ADX trend strength are displayed to the left. As we can clearly see in Fig. However, it is equal to 5. Showing the differences between fuzzy and standard logic. Here we can see the fuzzy logic in action. It has defined that even though the value is less than 70, the trend in the area is rather strong than average.
We can see this ourselves by examining the Fig. When the X axis value shows Here I want to show the difference in the Expert Advisor operation in case of clearly defined conditions and fuzzy logic elements.
To make the comparison as well-grounded as possible, I decided to use the Expert Advisor from my other article "Trading ideas based on prices' direction and movement speed" , which describes in details the idea of the trading robot. In order to avoid excessive repetitions, I will use this EA as a basis with the following changes:. The idea behind the connection is simple. If RSI and AC are movement parameters, then the higher the speed, the higher the movement persistence, and therefore it is reasonable to place a greater take profit.
If the movement speed is low, a target profit should be set more tightly in order not to run into a roll-back or a trend reversal. Applying the fuzzy logic in the EA. As is the case with the indicator, let's describe the membership functions for both fuzzy models. The first one is a fuzzy model of calculating RSI index where the input is the indicator value. Let's divide the necessary values into three categories:.
Let's select the membership function to describe the specified categories:. Using the membership functions to describe the categories of RSI values. The output of this fuzzy model is RSI index. The following categories and membership functions are used to describe it:. Using the membership functions to describe the categories of RSI index values. Next, let's describe the second fuzzy model from Fig.
The model's inputs have already been described as the outputs of the first model RSI fuzzy index. A take profit value is used as an output here.
Let's define concise categories for it:. Using the membership functions to describe the categories of take profit values. Now that all parameters are defined, it is time to implement the idea into the trading robot. We will add two fuzzy models for successive calculation of stop loss and take profit based on RSI readings. An entry is performed if they match Buy or Sell. Next, if the conditions are met, the fuzzy model operation result is assigned to mdm parameter which uses the current RSI value as an input and a fuzzy index as an output.
In turn, the fuzzy index value is used as an input for another system, in which the output is a take profit value in points. Take profit value is assigned to tkp variable.
The ratio of 0.More...