Since Charles Babbage’s invention of the first computer we know our electronic friends use simple binary logic based on “true/false” system. It’s quite enough for mathematical calculations and most financial indicators, but when we try to construct a complex indicator or a robot based on our human experience we may encounter a problem. How to tell a computer such simple human terms like “slightly more…”, “too fast…”, “practically nothing…”?
However, it’s possible with fuzzy logic theory, or rather “membership functions”. Let’s define a membership function to describe a human term “hot coffee”.
Initially, we should consider the temperature from 0 to 100 degrees Celsius due to the fact that at the temperatures below 0 coffee turns to ice and above 100 – steam. Obviously a cup of coffee with the temperature of 20 degrees cannot be referred to as “hot” (and our membership function “Hot Coffee” equals 0), while coffee with the temperature over 70 is clearly “hot” and our membership function equals 1. As for values, that exist between these two extremes, the situation is contraversial. The cup of coffee with the temperature of 55 degrees would be “hot” for one person and “not too hot” for another. This is the “fuzziness”.
Nevertheless we can imagine some form of our membership function – it is “monotonically increasing”.
Therefore our function may be expressed by the following analytical equation:
The main purpose of any technical indicator, one way or another, is the definition of the market condition at any given moment (flat, uptrend or downtrend) as well as the generation of signals to enter new position or exit it. How can membership function help us to do it? Easy enough.
Firstly we need to define the boundary conditions. Let’s identify “100% uptrend” as intersection of short EMA (with period of 2) built on typical price (H+L+C)/3 with upper band of the Moving Averages Envelope built with period of 8 and 0.08 deviation and “100% downtrend” as intersection with lower band. We assume “flat” in all other situations. For better results let’s add another envelope with parameters 32 and 0.15. That’s how it looks on the chart:
As a result we have two identical membership functions. Since the signal indicator would look more informative on the subchart with typical range from -1 to 1, then signal to buy and signal to sell occur when both functions are equal to 1 and -1 respectively. So our membership function looks like the following:
And it can be expressed with the following analytical equation:
where “a” and “b” are upper and lower envelope bands respectively, and “x” is an EMA(2) value. Finally, we’ve got our aggregate function for indicator from arithmetic average of two membership functions, i.e. F(x) = [ f1(x) + f2(x) ] / 2.
I’ll continue with programming this indicator in the next part of this article.