Fuzzy Logic in Market Trader

written by Sergey Tarasov


lotfi_zadeh.jpg (9587 bytes)

Lotfi Zadeh

The Neural Net technology and Fuzzy Logic are my favorite math methods. This article is devoted to Fuzzy Logic application in Market Trader software. Actually, the Fuzzy Logic math is used only in Neural Net module (Price Events), but this method has given me many other ideas like the idea of the Floating Angle Model (to be precise, the FAM model does not use Fuzzy Logic as such, but it keeps the same style of thinking). To me, the most appealing feature of Fuzzy Logic (FZ) is that it makes formal math logic more close to a human state of mind. Three countries contributed to the development of this idea - Russia (where the inventor of FZ, Lotfi Zadeh, was born), USA (where he worked and proposed FZ), and Japan (where FZ was applied first). This math idea was not accepted by academic science for a long time, though it was accepted by the industry rather quickly. Now the FZ math is used in many things around us - like automatic washing machines, digital cameras, etc.

Fuzzy Logic: very simple explanation

First of all, I should apologize to math lovers who might find this explanation a way too simple. Also, I should apologize to non-mathematicians who will face a necessity to deal with some math, at least. Some definitions concerning FZ application to Neural Net technology are still discussed between the professionals, so I will use definitions according to "Foundations of Neural Networks, Fuzzy Systems, and Knowledge Engineering" by Nikola K. Kasabov (The MIT Press, 1998).

So, what is Fuzzy Logic? Instead of answering this question, let's consider the idea that is absolutely understandable for any human being, but cannot be so easily understand by computers. We try to teach the computer to understand what "short, normal and tall person" means. Everybody knows it, it is quite easy to understand what it means. Looking at the person, we can tell momentarily: "This guy is definitely tall", "This woman is of normal height, though she might be a bit taller than my sister". What will the computer do? Nothing; it has not enough information to make its conclusions.

We have to formalize this task, enter the additional info like this:

The "short" means the height lesser than 150 cm;

Normal height is 150 cm-190 cm;

"Tall" is for the height more than 190 cm.

With this algorithm, the computer can quickly make the conclusion regarding person's height. But the weakest part of this approach is fixed boundaries. According to the proposed algorithm, the person of 149.9 cm height is a short one, while 150.1 cm is normal. The human common sense tells us that there is something wrong with this approach. Our conclusions are more smooth: "definitely tall" or "normal height, may be a bit taller".

Now I will demonstrate how this problem can be solved using Fuzzy Logic math.Look at this diagram:

fuzzy_15.gif (32907 bytes)

Looking at this diagram, we can tell that a person with height of 180 cm is 60% medium and 40% toll. We can tell "medium height though a bit tall".  The brand new of this approach is that this person belongs to two sets simultaneously: "medium" and "tall". This is the logic with "fuzzy boundaries". By the way, what we have just done is called "fuzzification", and the functions shown on the diagram are called "membership functions".


Rules: Fuzzy Logic in Action

The central point of Fuzzy Logic that makes it extremely useful is the possibility to create so called "Fuzzy Rules". Let's write together a program that directs the missile that is chasing another missile.In terms of fuzzy rules, we provide this program with a set of rules like this: "if the target is closer to right and the distance is decreasing very fast -> make a sharp right turn". We can enter different rules covering all possible situations, and the program will follow these rules very fast.

The program will work like this:

a) providing a fuzzification procedure, when the distance to another missile is presented like: "the target is very far", "the target is far", "the target is close", or "the target is very close";

b) applying fuzzy rules applicable for this situation (fuzzy inference mashine).

Compare it to the classical math methods application, where it is an extremely difficult task, especially when we have to consider many other conditions (like the wind velocity, weather, etc.). The classical math will apply differential equations (which, I guess, have multiple solutions). Fuzzy Rules application makes this task more intuitively understandable. This is the reason why Fuzzy Logic was accepted by the industry. Instead of differential equations (this might confuse anybody but mathematicians), we have a set of rules where each one of them is understandable for kids. We only have to produce a proper way of the fuzzification procedure (i.e., define the distance, speed, wind velocity, etc.) and define fuzzy rules that cover all possible situations.

Fuzzy Logic + Neural Net

Sometimes, for complicated systems, it is not easy to specify fuzzy rules. It would be good to find the way to automatize the process of finding the best fitting fuzzy rules. This task is resolved by a special kind of Neural Net that is called "Fuzzy Neural Net". 

Let's go back to our example of two missiles chasing one another. Suppose somebody is working on the program for one of the missiles and tries to list different rules regarding to this situation. There are many parameters involved; to cover them all, this person must be a specialist in many things. It can take a lot of time and efforts... But then he sees a kid playing one of the simulator computer games, and this kid does the chasing fantastically fast. This is not surprising as a human brain was formed with the ability to its orientation in the surrounding space several hundred million years ago. As modern biology knows, the part of the brain responsible for orientation ability is one of the biggest and the oldest. During all this time, this ability was developed and improved. We do not use everything collected in it every minute of our lives, but it is still there. Thus, to resolve the chasing task, we could try to imitate actions of this kid: we could create a special program that remembers all steps that the kid does to get the target. This is a proper task for a neural net: inputs are parameters for the missile (such as distance, speed, etc.), outputs are actions of the kid (sorry - the operator) to get the target. Neural net searches for the correspondence between inputs and outputs. Learning these examples, we can teach the neural net to repeat steps made by the kid. We can compare the learned neural net to a memory that keeps the rules how to get the target

Why Fuzzy Logic?

You might ask me: "Why should we use sophiisticated math instead of analyzing the price itself as inputs for Neural Net, without any fuzzification?". The answer is simple: "We try to catch the nonlinear process of stock market changes".

In reality, fuzzy rules might be set this way:

Rule 1: If the price went up very strongly 2 days ago, and then the price went down a bit 1 day ago, there is a high probability that today's price will go down.  

Rule 2: If the price went up strongly 2 days ago, and then the price went down a bit 1 day ago, there is a high probability that today's price will go up.  

The price 2 days ago has changed from up very strongly to up strongly and this can change the price movement today drastically. 

Time to Discuss Technical Issues

The general idea of using the Fuzzy Neural Net in Market Trader is the assumption that some rules exist for the market price movement. It could be something like this:

Rule 1: If 2 days ago the price went up very strongly, and 1 day ago the price went down a bit, there is a high probability that today's price will go down. We do not care now whether this rule (or some other rule/rules) is correct or not - let the neural net specify the rules. The most important thing is the assumption that the possibility exists for the stock market to follow these rules.

Let's go back to our example of two missiles once again. Imagine that we can really see them moving (like two jet planes that leave a white track in the sky). Imagine that we look at them from a big distance. Does not it look like two lines on the diagram intertwined? One of them, a prey, can simbolize the market. The other one, a hunter, is our forecast. The Neural Net learns their movements and corrects the forecast in the direction getting as close to the market as possible. It is how we do it in Market Trader's Neural Net.

Now I would like to demonstrate two examples: the autoregression fuzzy model and the fuzzy stamp model. If I am not mistaken, nobody has proposed the last one before; at least, I did not read about it anywhere though this issue needs more research.

Autoregression Model

This model is based on the assumption that price today is a function of price yesterday, price two days ago, etc. The Astrological Language (the main feature of Market Trader program) allows to define events that can serve as fuzzy events. Open "Neural Net Expert", choose there "Price Events" item, then select "Regression Model":

fuzzy_1.gif (18231 bytes)

You will get these events:

fuzzy_2.gif (8395 bytes)

This list represents the results of price fuzzufication. Each line represents a possible price event. Thus, the first line "PRICE DOWN (HIGHEST) 1 TRADE BACK" means: one day ago the price went down very significantly ("highest" stands for the biggest difference between two price points); the next line means that one day ago price went down significantly (though not so much as for the first event), etc.

In other words, the price can go up or down, and the change can be very significant ("highest), significant ("high"), reasonable ("medium"), minor ("low") or not significant at all ("lowest"). The program considers 10 types of possible price movements:

down - highest;

down - high;

down - medium;

down - low;

down - lowest;  

up - highest;

up - high;

up - medium;

up - low;

up - lowest.  

We can define these price movements for one trade ago, two trades ago, three trades ago, etc. The number of events will be increased/decreased accordingly.

All these parameters are defined here:

fuzzy_3.gif (8362 bytes)

Pressing "Fuzzy" button. you can define the fuzzy grade (in our example, we use 5 grades). Also, you can define the maximum lag for the autoregression model.

As for the price events, let's use the relative price oscillator. The forecast given by the Neural Net  looks like this (dark green line):

fuzzy_5.gif (10806 bytes)

By the way, the program provides the ability to use the "Price Bar Structure" events, the fuzzification techniques are applied here as well:

fuzzy_4.gif (6184 bytes)

Here you define the High-Low grade (HL), Close-Open grade (CO) and Open-Low (OL) grade.

For example, look at the "White Candle"

fuzzy_11.gif (5793 bytes)

in terms of of Fuzzy Logic, the events are:

High-Low (HL) grade High or Highest

Close-Open (CO) grade UP High or Highest

Open-Low (OL) grade Low or Lowest

Theoretically, this way we can describe the Japanese Candlestick technique, but this issue needs additional research.


Fuzzy Stamp Technique

This technique is more close to the classical fuzzy neural net. This model concerns mostly price events that we want to predict.

Press fuzzy_6.gif (1045 bytes) button. You will get this window:

fuzzy_7.gif (10509 bytes)


On this diagram, you can see the distribution of the difference between Close and Open. Using these settings we produce the fuzzification of the difference between Close and Open values. The program creates four types of events:

fuzzy_8.gif (1639 bytes)

Now look at this diagram:

fuzzy_9.gif (23460 bytes)

The screen is divided on 4 colored zones: two red zones and two blue zones. For each trading day, we have four colored bars: two red bars corresponding to upward movement (small an big upward movement) and two blue bars corresponding to downward movement (small and big). The brighter is the color, the more active this bar is. During the optimization process, the program finds the appropriate color for each bar. For example, if some day the price makes a very strong upward movement, we will have a very bright up red bar, all other bars will be almost white (not active).

This diagram demonstrates how to read fuzzy stamp diagram:

fuzzy_10.gif (21231 bytes)

By the way, to create the fuzzy stamp we can use as input any type of events: any astrological event, any fixed cycle, price events.

What we should do is just look for the most bright bar for each day. The interesting feature is that sometimes all bars are bright. May be, this corresponds to unstable zones...

This is the "know how" of fuzzy stamp technology. A classical fuzzy neural net supports usually the process of getting the price from these colored bars (it is called "defuzzification"), but I prefer this variant to see the possible ways. 

Parameters to Play

While creating the fuzzy stamp, you can increase the grade (a number of colored bars) for better visualization:

fuzzy_12.gif (3068 bytes)

If you use the autoregression or price bar structure model, it may be worth to vary the number of hidden neurons (H1 parameter). Roughly, it means we increase the number of rules that can be specified by the neural net. Also, you can use the neural net with 2 hidden neurons, but it calculates slowly.

fuzzy_14.gif (6220 bytes)

So, you can see yourself that application of Fuzzy Logic to Neural Net might be promising for the market forecast. 

I would like to discuss it with anybody interested and I would be happy to answer your questions (if I know this answer...)



Sergey and Tatiana Tarasov.

July 10, 2004.

Toronto, Canada