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.

Posted in Methods, Trading Signal | Tagged fuzzy logic | Leave a comment

Number one with 40+ percent return in a month! So am I rich yet? Not really

Preliminary results from the August Dukascopy JForex contest shows that I have won first place! My automated strategy returned over 45% in the month of August. With that honour comes a $5,000 grand prize in real greenbacks.

The setup that I used is a minor updated version of the one I used last month. I continue to adapt my system to expect market conditions of the month ahead. Sometimes I’m right, sometimes I’m wrong.

This contest is turning out to be more than a nerdy pass time for me as I have finished in the top 10 three times already since it started in April. However, I must reiterate the fact that this is not real trading. It’s just a paper trading contest with no real money risked on my part. If I’m really that good at trading to make 45% of return a month, I wouldn’t be writing this post at lunch in my cubicle at this moment.

So yes, back to reality as I continue my engineering work while improving my own trading and developing my quantitative trading system in my spare time. As my blog’s tagline reads, this is an engineer’s training to trade for a living.

Posted in Automated System | Tagged JForex | Leave a comment

Closed USDCAD short @ 1.06525, breakeven

It seem that USDCAD isn’t about to die just yet.  My USDCAD short got stopped out at breakeven this morning.  In fact, the pair is still struggling around the descending resistance line as shown in Figure 1.  It made a higher low so I won’t be surprised if it cracks higher.

USDCAD, 4-hour

The reason why I took this trade was that the opportunity seemed too good to pass on based on my analysis at the time. I always keep an eye on the market even when I’m not trading. Although the basic setup remains intact, the opportunity isn’t so fantastic that I couldn’t say no anymore.

Anyway, back to staring at codes.

Posted in Aussie, Kiwi, Loonie | Tagged CAD, exit, USD | Leave a comment

Short USDCAD @ 1.0654, Stop 1.077

This is my first real forex trade after a year minus one day of studying the market and paper trading.

Shorted USDCAD based on the following intermarket observations:

  1. Yesterday’s equities sell-off held around S&P at 1050
  2. Overnight Asian market seem orderly, not every market followed through, but most did
  3. European markets down move were slow overnight until US open
  4. But perhaps most importantly, EUR/USD made a higher low with an increasing COT

Basically, as much as the sell-off tries to scare off people, the markets don’t seem to be moving in unison.  So it would seem this move (in equities, thus inverse USD) is still a bear trap in an intermediate term bull rally.

Plus this daily chart, Figure 1, for picking USDCAD specifically.

USDCAD daily chart

And this 4-hour chart for trade execution.

USDCAD 4-hour

Size of trade is 1000 unit. Risking about 0.05% of capital with a hard stop at 1.077. Anything above 1.070 would put me on alert though.

Coincidentally, I first got into the forex market by studying the loonie.  Let’s see how this first trade performs.

Posted in Aussie, Kiwi, Loonie | Tagged CAD, entry, short, USD | 1 Comment

Using your idling trading computer for a good cause

Nowadays, computing power is practically free for trading. It does not take much processing power to trade or chart on your computer. The situation is even more wasteful for many mechanical traders obligated to keep their computers on all the time. So what can you do with all that idling power? Through World Community Grid (non-profit), you can help develop new cures for diseases or discover affordable solar energy.

According to Wikipedia, “World Community Grid (WCG) is an effort to create the world’s largest public computing grid to tackle scientific research projects that benefit humanity.” By joining WCG and installing an open-source software, you are enabling your computer’s idling cycles to process data from various research projects as part of a cluster of computers. From the WCG statistics page, over half a million users have signed up. It is one of the three biggest non-profit grid computing efforts in the world (the other being SETI@home and folding@home).

I tried SETI@home almost a decade ago to help search for E.T. But it wasn’t until the folding@home project started when grid computing gained popularity. Tell people that you’re leaving your computer on to search for E.T. and people couldn’t hold their laughs at you. But telling people that you’re leaving your computer on to find cures for diseases … then all of a sudden, it’s philanthropic.

The reasons why I chose to devote my computer resources to WCG instead of the other two are simple.

  1. It offers more than one projects to choose. You can fold proteins, find energy efficient material, simulate climate change, etc.
  2. The projects offered have specific, realistic goals.
  3. Projects do get completed (unlike SETI@home, for example, since no E.T. has been found yet after 10 years).
  4. New projects are added as older ones finish.

In any case, that’s just my personal choice. Take a look at the three projects mentioned for more information. In addition, there’s an interview with a researcher using WCG.

If you decide that WCG is right for you, feel free to join Team Traders and start data crunching together. (Warning: Like any software, use at your own risk)

Posted in Journal | Tagged grid computing, WCG | Leave a comment

A whole year, two hundred and six posts later

Recent lack of actions in the market has given me opportunities to focus less on the day-to-day market action and more on my own trading development. Besides working on my quantitative research, one of the tasks that I like to do is to review what I have thought previously in a similar time. This takes us back to a year ago, August of 2009.

Headlines from my fifteen posts back then shows that I was trying desperately (in hindsight) to short the market. Why would I do that? Take a look at this 3-year long weekly chart of S&P500.

S&P500 weekly chart

On the chart, August 2009 looked like a great shorting opportunity.  The resistance level at 1000 seem too good to be true (it was).  In fact, I said the following words on August 22, 2009:

While there’s been much talk of economic recovery and a new bull market is already with us, I still am not convinced just yet.  True, the bears might be MIA.  But all signs say that this bull we’re seeing is standing on thin ice.

How wrong I was.  Since breaking the 1000 resistance back then, the S&P hitched on the bull train to steam upward steadily on to 1150 (Figure 1).  It took a break there and continued on to 1207.

Looking through my trades back then, I eventually stopped shorting the market after S&P broke above 1025.  That was the good part.

The bad part is that I wasn’t able to change my view and remained stubbornly bearish (but at least didn’t commit to any new shorts) for weeks afterward.

If I were to sum up my lesson in the past year, here’s what I would say to myself back in August 2009.

Trading is not about being right.  It is about knowing when you are wrong and doing something about it.

On a final note, it was in August of last year that I started to paper trade the forex market.  I can’t believe it has been a whole year already!

Posted in Self Assessment | Tagged market review, trading quote | Leave a comment