#twitter-follow { border: 0;position: fixed; top: 240px; left:0;} #live-support { border: 0;position: fixed; top: 370px; left:0;} #knowledge-base { border: 0;position: fixed; top: 170px; left:0;}

### Custom Indicator: Moving average spread

Updated on 2012-01-03

I know there are many QuantShare users who are new to the programming world and I know how difficult it could be for some of them to implement their own trading indicators.

For this reason, we will start another series of posts that show you how to implement custom trading indicators using the C# programming language. I will try to explain every detail of the implementation and if something is not clear, please post your comments below.

Create a Custom Indicator

- Open QuantShare Trading Software
- Select "Tools" then "Create Functions"
- Click on "Add" to create a new indicator
- Next to "Item Name", type "MASpread" then click on "Save Item"

Moving Average Spread

The Moving average spread indicator consists of calculating the difference between a long-term and a short-term moving average.

The first thing we need to do in our script is to create those moving averages. This can be accomplished using the "TA" class. This class contains methods/functions that correspond to the different trading indicators built-in QuantShare programming language.

For example:

In QS programming language, you can compute the 14-bar relative strength index by typing:

a = Rsi(14);

In custom indicator script, you can create the same RSI by typing:

VectorD a = TA.Rsi(14);

There are three important differences:

1: We must specify the type of the variable "a". All time-series are of type "VectorD".
2: We must precede "rsi" with "TA.". After we type the latter keyword, notice the popup list that appears. This list contains the different functions available in the "TA" class.
3: C# is case-sensitive, while QS language is not. Typing "rsi" instead of "Rsi" will generate an error.

Back to our MA spread example, let us create those two moving averages:

VectorD ma1 = TA.Sma(10);
VectorD ma2 = TA.Sma(100);

The first line creates a 10-Bar moving average (short term) and the second one creates a 100-bar moving average (long term).

Several mathematical operations can be applied to variables "ma1" and "ma2". In fact, you can manipulate these vector variables as if they were double or floating variables.

By taking their differences, we compute the difference of each element of these vectors.

Example:

Each vector contains only four elements.

ma1 = [10, 2, 5, 4] and ma2 = [5, 1, 10, 3]
(ma1 - ma2) = [5, 1, -5, 1]

Here is the complete implementation of the MA spread indicator:

VectorD ma1 = TA.Sma(10);
VectorD ma2 = TA.Sma(100);
result = ma1 - ma2;

As explained in the article (The custom trading indicators tool explained), the vector returned by the script should be associated with the "result" variable.

This variable does not require that we define and initialize it (specify its type) because it is a built-in variable (like "TA", "cFunctions" and "Chart"). The code that defines it (as well as the other variables) is generated automatically by QuantShare and appended to the code you type.

Note:

There are two implementation of the "Sma" indicator. By default, the first one takes only a period parameter and it calculates the moving average of the close series.
The second one requires a vector and a period. The below instructions are equivalent.

VectorD v1 = TA.Sma(cFunctions.Close, 10);
VectorD v2 = TA.Sma(10);

Using the indicator in a Chart

Click on "Save" to compile the indicator and create a new function in the QuantShare programming language.

- Right click on a chart then select "Create a new pane"
- In the new pane, click on "Add Indicator" icon (first icon at the top of the chart)
- Select "All Indicators" tab
- Type "MASpread" in search input box
- Select the custom indicator then click on "OK" to add it to the chart
- Right click on this pane, and then select "Edit formula"

This is the QuantShare language formula that generates the moving average spread line:

a = MASpread();
Plot(a, "MaSpread", colorBlack, chartLine, StyleSymbolNone);

By calling "MASpread", QuantShare will execute the "MASpread" custom indicator we have just created and return the content of the "result" variable.

We could have implemented the indicator directly in QuantShare programming language.

This formula will look like this:

a1 = sma(10);
a2 = sma(100);
a = a1 - a2;

The problem is that the code is a little bit longer and we should type it each time we want to reference the MA spread indicator.

Imagine you want to create a trading system based on the moving average spread indicator:

Strategy 1:

buy = MASpread() > 0;

Strategy 2:

buy = (sma(10) - sma(100)) > 0;

The advantage of using "MASpread" may not be obvious because the implementation of this indicator is too short, but imagine typing 10 or 20 lines of code each time you want to reference a technical analysis indicator.

 no comments (Log in)

 Search Posts Recent Posts Learn How to Create Your Own Technical Analysis Indicators Posted 3 days ago How to Pick the Best Trend Indicators Posted 14 days ago Update Chart Layout based on Active Ticker Symbol - Part 2 Posted 30 days ago Update Chart Layout based on Active Ticker Symbol Posted 39 days ago Using Average True Range to Measure Intraday Volatility Posted 64 days ago How to Backtest Your Trading System for Each Industry Posted 72 days ago Create Profitable Trading Strategies with Exchange Traded Funds (ETFs) Posted 85 days ago Previous Posts Custom Indicator: Moving average spread Posted 507 days ago The custom trading indicators tool explained Posted 513 days ago Diversify your portfolio by investing in stocks from various industries Posted 519 days ago Day Trading: A trading system that combines intraday and EOD data Posted 527 days ago Intermarket Analysis - Correlation and Trading Strategies Posted 534 days ago How to choose which technical indicators to use in your trading system Posted 555 days ago 4 indicators to create adaptive trading systems Posted 564 days ago How to create buy and sell trading rules based on News Data Posted 570 days ago 3 ways to rank stocks in a trading system - Simulator and Potfolio Posted 577 days ago Basic trading system implemented using the money management tool Posted 590 days ago Example of a trading system implemented in QuantShare Software Posted 598 days ago Different periodicities and chart bar types in QuantShare Trading Software Posted 605 days ago

 Company About Us Privacy Terms of Use Copyright © 2012 QuantShare.com Social Media Follow us on Facebook Follow us on Twitter