What is a trading system?

For some traders, building a trading system means that they focus on finding the best indicators they could get their hands on or the one trading setup that is backtested to work 99% of the time. All I can say is, to each his own.

For me, the keyword of a trading system lies not with trading, but with system. Remember, it is a system. First of all, it is a system for trading. Not a system to trade. What do I mean by that?

Consider this saying, “traders don’t go broke by missing opportunities, they go broke by taking too many.” A trading system needs to be selective. It needs prudent risk management.

Secondly, you need to generate your own market view to trade. Rather it be technical, fundamental, sentimental, or just following some joe, you trade based on your view. Trading is a test of your market hypothesis using money. If you think the market will rise, you buy. If you think the market will fall, you short.

Lastly, a trading system needs to execute trades. Otherwise you’d be an anlayst. It needs to time entries and exits. Again, this could be as simple as on a whim or as complicated as some n-th order algorithm.

Thus we have the three pillars of a trading system.

  1. Risk Management
  2. Market Analysis
  3. Trade Execution

What’s more, if we combine some of these pillars we get some well known concepts. For example, market analysis + trade execution = trading signals, wherein you derive signals based on market conditions to execute trades. Or risk management + trade execution = position management, wherein you manage your holdings by manipulating your trades based on your risk profile.

All these relationships and composition of a trading system can be summarized with a Venn diagram. So, this is my view of a trading system:

Trading system Venn diagram

read more

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 more

JForex Example: Dual-Timeframe Moving Averages Setup

This post describes the setup for my Dukascopy JForex automated trading strategy in July. Note that this strategy is built for competing in a contest and not for real trading (i.e., it’s purely a no cost gamble).

Here is the step by step process of the setup for a long position:

  1. Determine the current trend by the relative position of current price to its moving average in a higher time frame. In particular, a price above the moving average in the higher time frame is deemed as bullish.
  2. Entry: Price moves above a moving average in the current time frame (red line in Figure 1) with a higher high, low, and close (HLC). As illustrated in Figure 1, the two bars circled.
  3. Exit: Crossing below a regular moving average (blue dotted line).

EURUSD setup example

As you can see, the setup itself is simple. Less than a fifth of the source code is devoted to the entry and exit. Most of this strategy involves automated position sizing and risk management to not let profits turn into losses.

The complete source code of this strategy is available in the introductory post.

read more

Why should I care about other markets if I only trade ___?

A benefit of having traded different markets and instruments is that I have gained different perspectives on the capital markets. I realize that all markets are ultimately related. Rather it is fundamental, technical, or quantitative analysis, traders are essentially analyzing the supply and demand of the instrument that they are trading. But supply and demand is influenced solely by money flow in the capital markets. As such, it is the dynamics of money flow that drives the movement of something as big as EUR/USD or as small as a penny stock.

Secondly, money do not appear out of thin air or do they evaporate. Money moving out of one thing end up somewhere else, and vice versa. Consider a dollar in your wallet. Where does it come from? Where will it go? You probably have a good idea of that yourself. Similarly, money moving in and out of a stock comes from somewhere and go somewhere too. That somewhere can be as close as the next stock in that same exchange. Or, it can be moved to another market (e.g. bonds or commodities) entirely. This happens more often than I used to think because this is how the institutions and the pros play. They don’t limit themselves to any particular exchange or any market. They simply go where the money is.

As you can see through this simplistic mental exercise,

  1. money flow is the only driver of prices
  2. money has to come from somewhere and go somewhere

That is the reason why I say all markets are related. It is also the reason why even if you trade a local or a specialized market, it is essential that you are at least aware of what’s happening around. Not doing so is just willingly ignoring a large part of valuable data.

Bill Cara has a saying that “markets do not operate in a vacuum”. In his weekly review on March 14, 2010,

Global equity markets do not trade in a vacuum. It is important to be watching these markets move through a trend juncture together, pushed and pulled by global currency and commodity strength or weakness as well as local and regional economic forces.

I like to visualize the capital markets (stocks, commodities, forex, etc.) as an interconnected web. Granted, some points are connected more than others. But push or pull on any point and you can see the effect throughout the web.

One practical use of this concept in trading is that if one market is moving up or down, you can observe its impact on the global financial web to estimate (using historical patterns or some blackbox model) will that move pick up momentum or will it retrace back to where it was.

This is the essence of intermarket analysis.

read more

A leading indicator to spot financial market Black Swan Events?

Black Swan Events or 4-sigma moves (e.g. 1987 Black Monday and 2008 crash) in the markets are by definition hard to predict. Hard, but not impossible. Experienced traders utilize prudent risk management to shield themselves from monstrous losses, and some can even profit immensely from them. However, for us average folks, is there some kind of simple warning signs that could give us a heads up to these sort of extraordinary moves? In this post, I will introduce a simple leading indicator that seems to do the job quite well. I will talk about what it is and why it seems to work. Of course, this is merely based on limited observations and is not to be construed as a vigorous analysis.

The particular indicator I am referring to is the Google Investing Index (GII). Figure 1 below shows the GII vs. S&P 500 from 2004. The blue line is the GII, the red line is the S&P 500. What I have observed is that rapid (in a month’s time) rise from a negative reading to +20% reading of GII correspond with a shift in sentiment in the market rather well. Referring to Figure 1 below, this condition occurred 4 times since 2004. I marked these 4 occurrences on the graph with green dots. As you can see, they correspond with the double top on S&P, first dive in the crash, the big one in October 2008, and then even confirming the 666 reversal in March 2009. It seems to be a leading indicator for down moves but lagging on upward reveral.

Mostly noticeably though, is that the GII gave extreme readings, and in the direction of our condition (negative to positive jump), days before the big crash in October 2008.

Google Investing Index vs. S&P 500

Google Investing Index vs. S&P 500 (green dots mark signals)

While this can be mere coincidence, here is why I think this index holds some truth. The GII measures the amount of search traffic for topics relating to investing in the U.S. And with Google dominating the online searching market (65% as of October 2009 according to comScore), we can assume GII to measure what the general public is interested in. Now, as we probably know, market extremes are driven by irrational emotions. There’s an old saying that goes like this, on market highs, when even the taxi driver is giving you stock advice, it’s time to get out of the market.

When all the people have invested already, who’s left to buy and drive the price even higher? This is ultimately a supply and demand effect. While we can’t count heads in the market, we can estimate market sentiment. The GII is just one more way of doing that. And it does so by giving us a reading of the market sentiment according to the general public. You know, the last ones in the party to pickup the mess the pros have have left behind. That is exactly what sets the GII apart from the plethora of existing market sentiment indicators. It is a market sentiment indicator on mass psychology, but a crude one at that.

Scoring 4 out of 4 positives is good, but the small sample size render this a mere observation of interest. I do not recommend trading on this indicator. However, it does seem promising to serve as an additonal input to an analytical system…

In summary, here is the condition of this GIS (for Google Investing index for market Sentiment) indicator.

  • the condition is true for a turn in the market when GII jumps from a negative to +20% or more within a month.

Lastly, notice that the GII is still low at the moment while many are calling the top is imminent, or even that it has happened at 1120 in recent weeks. Thus, according to GIS indicator, this market rally is still intact.

read more