On technical analysis and fuzzy logic for mechanical trading
Technical traders realize that technical analysis is more of an art than science. Until you draw a plethora of lines to fill your chart, which would render it practically useless, prices rarely “hit the spot” based on your doodles. This doesn’t matter though because trading is not about charts and guessing numbers. It is about costs and risks (pardon me if you’ve heard this a thousand times already). To whom this would matter though, are the mechanical traders using technical analysis.
It may be obvious to a human to discern price patterns because a trading setup looks like something you know. However, figuring out “obvious” and “looks like” is not trivial in a program. Common ways of tackling this problem is with stochastic algorithms or Bayesian logic. The drawback with using these methods though, is that they are difficult to conjure unless you have some vigorous background in math.
Hereby I suggest an alternative–fuzzy logic.
In it’s simplest implmentation, fuzzy logic are mere if-then statements. Here’s a sample fuzzy logic control system for regulating temperature with a fan (it is also a trick solution to the infamous hysteresis problem in control theory…but I digress),
IF temperature IS very cold THEN stop fan
IF temperature IS cold THEN turn down fan
IF temperature IS normal THEN maintain level
IF temperature IS hot THEN speed up fan
As you can see, there can be more than two values for a result. The classic and ubiquitous TRUE or FALSE boolean is thrown out the window.
Let’s dive right into an example of applying fuzzy logic to technical analysis. Say you are using the RSI as one of your indicators. Typically, you set a pair of threshold values to determine if the instrument is overbought or oversold.
String rsiCondition;
if (RSI > 70) rsiCondition = "Overbought";
if (RSI >= 30 && RSI <= 70) rsiCondition = "Neutral";
if (RSI < 30) rsiCondition = "Oversold";
Then here is what a fuzzy logic implementation would look like.
String rsiCondition;
if (RSI > 90) rsiCondition = "Very overbought!";
if (RSI > 70) rsiCondition = "Overbought";
if (RSI > 60) rsiCondition = "a little overbought";
if (RSI >= 40 && RSI <= 60) rsiCondition = "Neutral";
if (RSI < 40) rsiCondition = "a little oversold";
if (RSI < 30) rsiCondition = "Oversold";
if (RSI < 10) rsiCondition = "Very oversold!";
In fact, you may have noticed that the first RSI example is already a 3-valued fuzzy logic. And you’re right! Fuzzy logic is that easy.
So what can you do with that second RSI example? Well, perhaps you want to implement fuzzy logic on a MACD as a second indicator. Then you can implement a “conviction” algorithm (which isn’t possible with TRUE/FALSE algorithms) by summing the two fuzzy logic (see wiki or google for more information on performing fuzzy logic operations). And then… well.
This is a basic example of exploiting the benefits of fuzzy logic in automated trading strategies. More advanced use of fuzzy logic is demonstrated in this seminal work by Lin and Lee, Neural-network-based fuzzy logic control and decision system, 1991. Fuzzy logic + neural net! Well, as you can see, this is only the tip of the iceberg.
P.S. I have discussed a lot recently about the frontend work that I have been working on. This post is a break for the reader and a glimpse at what I am really doing behind the scenes. Let me know in the comments below if this is your cup of tea and I’ll write more of this type of posts in the future.
read moreFrom Object Oriented Programming to Object Oriented Design
The good news is that I have finished my data scraping module for extracting all the company data listed on the two Toronto Stock Exchanges (TSX and TSV). I presented an alpha version of this data scraping module last week. I added the functionality to download all the historical quotes from Yahoo for all the companies. These data are stored locally in a cool HDF5 format with PyTables. I am very happy about that part.
The bad news is that my module looks more like a hacked job than a piece of software. There is no defined structure for my underlying codes. It is obvious to me that this module would be difficult to extend, which is the whole point of my project. In programming jargon, my code smells (Wikipedia).
Then it finally dawned on me. I am way behind in my programming skills.
For the past few years, my quant work has been confined to MatLab or some proprietary trading platforms. I wrote scripts. Not programs.
My problem with programming is that I am still stuck in a procedural programming mentality because that is what I’ve always used. Even though I updated my knowledge recently by learning Python, learning to program with an object oriented programming language is only the first step. (I am also familiar with C++ and Java from way back)
To use an analogy, programming a software is like constructing a building. The actual coding itself is akin to the construction work. I got that part. But coding without a well designed plan can only go so far.
It is now clear to me that if I am to build the trading software to complement my trading as I have envisioned it, I need to step it up with my software design ability.
Consequently, I am now catching up on object oriented design and analysis, as well as software design patterns with a couple of books. I will try not to get too carried away with these though. My main focus is trading. But this will be very beneficial for my trading in the long run if I can build a more robust trading software that can easily grow with my future needs.
I have many big plans for this side project of mine. I posted a UML diagram showing an architectural overview of my project.
read more

Recent Comments