Wednesday 23 October 2013

A New DSP Indicator in MT4

In this post, you will to understand how an MT4 indicator, specifically a Digital Signal Processing (DSP) indicator, is written and also how to interpret it on a chart.

Before I begin, let me digress a little bit. My DSP journey began in 1990s when I studied Computer Engineering at UNSW, Australia. For those not familiar with DSP, it is an engineering tool used to filter noise from signal and it has many practical uses in different industries. In 2011, when I started trading in the forex market, I purchased several books including a DSP trading book, titled "Cybernetic Analysis for Stocks and Futures" by John F. Ehlers. The book requires some familiarity with DSP from a practitioner's perspective, but I was lacking in DSP experience. I enrolled in the Coursera's "Digital Signal Processing" course, but I had to drop the course due to my personal commitments. For those interested, here is the course link.

The DSP indicator that I have written is based on a trading strategy from OneStepRemoved's "Cumulant Ratio" indicator. Although my DSP journey has taken a pause, I will read any interesting articles from a DSP practitioner's perspective.

Here is a snapshot of the USDZAR H1 chart with the CumulantRatio (CR) indicator below (Figure 1). I will explain to you the vertical red lines, which I have drawn on the chart after I explain how the maths work.

Figure 1: USDZAR H1 Chart


The maths is basically one equation:



where N is the period (or number of candles/bars), Xi is the individual price of each bar, Xo is the price of latest bar, and X_ is the average price of all the bars. The price may be the Open, High, Low or Close, in this case we use the Close price.

It is important to note that the numerator, which is a sum over the period N, divided by the denominator, which is also a sum over the period N, is the same result as when you divide the average of period N over the average of period N. I will explain why this is important later when we look at its implementation.

We implement the steps to the above math equation as follows:

(i) X_: this series is calculated using a simple moving average.

(ii) Xo: this series is calculated by using a step of the latest price every N period.

(iii) Numerator, i.e. Xo - Xi: this series is calculated by subtracting individual price of each bar from the second series.

(iv) Denominator, i.e. |Xi - X_|: this series is calculated by subtracting the first series from individual price of each bar.

(v) Equation, i.e. ratio: this series is calculated by dividing a simple moving average of third series by a simple moving average of the fourth series.

After mapping the math equation to the steps above, it is relatively straightforward to implement in MQL source code.

I will summarize the implementation in one paragraph, instead of explaining each step in detail. The simple moving average can be implemented using the iMA() function for prices, e.g. Close[], that are taken from a chart, or using the iMAOnArray() function for prices that are taken from a time series. The second step can be implemented using the MathMod() function and integer divisor (/) operator. The MathAbs() function can be used to calculate the absolute value in the fourth step. Unfortunately, in step five, MQL does not provide a sum on array function for time series, but we noted previously that we could use a simple moving average as the result is equivalent.

How to Interpret the Indicator on a Chart

The indicator has two user inputs:

(a) int CrPeriod: the number of bars to use for period N.

(b) int CrAlpha: a multiplier used to increase the range (enhanced visual and significance of the two lines, but it does not affect the histogram)

Figure 2: Custom Indicator Inputs


The indicator has two lines (green and silver) and one histogram (yellow):

(a) The green line represents the third series (Numerator), which can take any value from -infinity to infinity, but in practice, it has a limited range because it is a deviation from the latest price.

(b) The silver line represents the fourth series (Denominator), which can take any positive value from 0 to infinity, but it has a limited range because it is a deviation from the average price.

(c) The yellow histogram represents the fifth series (Equation), which can take any value from -infinity to infinity, and it has a much larger range than the two lines, because the Denominator is usually small, hence the ratio can be large.

Figure 3: CumulantRatio (CR) Indicator


In the next chart (Figure 4), I have marked three vertical red lines on the chart where the green line crosses the zero level. According to Shaun Overton, the green line
is only positive if the current price is “above” the sum of the last N bars. A negative position means that the current price is “below”.
(i) The first cross up showed a relatively weak up move, but you will note that the prices moved up strongly afterwards.

(ii) The second cross down showed a stronger down move, and the prices followed down strongly two bars later.

(iii) The third cross up weakly, and this is followed by the prices moving up strongly.

Figure 4: The green line crossing zero has preceded every big price move


At this preliminary stage, I do not yet fully understand how the indicator behaves and how to use it in trading. I will continue to monitor the relationship between the CR indicator and the prices. I think that you could use some additional filters before using it for trading, so as to prevent false signals.

Download Indicator

Please read the disclaimer first before downloading the source code or indicator.

You understand and agree to the following terms and conditions for use:
(a) you do not remove any of the copyright information from the source code, or attempt to commercialize the source code or indicator.
(b) you are allowed to modify the source code, but you do not attempt to commercialize any variation of the source code or indicator.
(c) I am not liable for any damages or losses as a result of you using the indicator or any variation of the indicator for any purposes.


The indicator can be downloaded here.

Friday 11 October 2013

To code or not to code - that is the question

Recently, I had a request from a forum member of SteveHopwood.com to code an Expert Advisor (EA) based on an indicator.

I could probably code this EA (with no frills) in less than a day using the shell EA code. Programming has been my bread and butter for more than 15 years and I've grown to understand my clients very well.

First, clients rarely know what they want. In this case, the member is probably after the holy grail, and eventually this will lead to "scope creep" to add in more filters and trade management. Second, their idea is probably not new, and they are just reinventing the wheel. Third, they are probably not traders otherwise they wouldn't be searching for the holy grail. Hence, I would not even bother to code this EA.

However, if certain conditions have been met, I may be interested in coding an EA. First, the client must have used this system manually and has at least doubled his account recently. Second, he must not be reinventing the wheel, such as copying an idea from a commercial EA. Third, he should not request for me to put in place a fancy trade management as it does not make a lousy trading system better, i.e. martingale.

You may think that these conditions are excessive, but I am working on my own EA based on these criteria. First, I have doubled my account in manual trading using my own system. Second, I did not copy the idea or reinvent the wheel, as it is original based on my own system. Third, I do not have a sophisticated trade management to be profitable.

So, if you really think that your EA can be worthy of my time, then prove to me that your system is as good as the above system, and have met my criteria. Otherwise, I suggest you hire a no-frills programmer who has no trading capability.