Click here to Login




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)

QuantShare Blog
QuantShare
Search Posts




QuantShare
Recent Posts

Create Graphs using the Grid Tool
Posted 1237 days ago

Profile Graphs
Posted 1342 days ago

QuantShare
Previous Posts

More Posts

Back







QuantShare
Product
QuantShare
Features
Create an account
Affiliate Program
Support
Contact Us
Trading Forum
How-to Lessons
Manual
Company
About Us
Privacy
Terms of Use

Copyright © 2024 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
Follow us on Google+
RSS Trading Items



Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.