Click here to Login

Custom Indicator: Relative Strength

Updated on 2012-01-10

Relative strength is a measure of how strong a stock is compared to an index, sector, industry or another stock. Relative strength can be calculated by taking the stock's price change over a period and comparing it with the price change of another asset during the same period. The other asset could be the stock's industry index, the S&P 500 index or a competitor.

Three lines of code are necessary to implemented this indicator in QuantShare programming language, but our goal here is to create a custom function so that we can easily use the relative strength indicator when backtesting trading systems for example.

p = 250;
a = GetSeries('^GSPC', close); // Get S&P 500 price series
b = perf(close, p) - perf(a, p);

To avoid typing the above formula each time you want to create a portfolio or backtest a strategy, I will show you how to create a custom indicator and reference the strength index with a single line of code:


a = RS("^GSPC", 20);

Before continuing, have a look at our previous post: The custom trading indicators tool explained . You will learn the basics and the different steps required to create a custom trading indicator.

Now that you know how to create a custom indicator item; create one then name it "RS".

In the rest of this article, I will show you two different ways to implement the relative strength indicator.

Relative Strength Version 1

The first implementation consists of compiling and executing a QuantShare formula and then getting a time-series to return it (or further process it).

The "cFunctions" variable contains two functions that we can use here: "CompileFormula" and "CreateFormula".

CompileFormula: Creates then compiles a QuantShare formula

CreateFormula: Creates a QuantShare formula

The second function should be used in case you want to pass a non-fixed parameter to the underlying formula. In the case of the relative strength, the asset symbol name and the period are fixed and therefore we can directly compile our formula.

QSFormula formula = cFunctions.CompileFormula("p = 250; a = GetSeries('^GSPC', close); b = perf(close, p) - perf(a, p);");

Add two parameters to this indicator by clicking on "Add a parameter" button twice (In "Create Functions" control).

Under "Variables" panel, type "symbol" in the first cell of the first row and then type "period" in the first cell of the second row. Change the parameter type to "String" in the first row.

Here is the complete formula:

QSFormula formula = cFunctions.CompileFormula("p = " + period[0] + "; a = GetSeries('" + symbol[0] + "', close); b = perf(close, p) - perf(a, p);");
result = formula.GetVectorDouble("b");

In the first line, I have replaced the static values (250 and ^GSPC) with the values provided in the indicator parameters (period and symbol). These parameters are vectors and therefore in order to get a numeric and text values, I have used "[0]" to obtain the first value of each one of these vectors.
In the second line, we obtain the relative strength indicator by getting the vector stored in the variable "b".

Relative Strength Version 2:

This version also requires that we add two parameters; follow the previous section to learn how to add a parameter.

The second implementation is based on the "cFunctions" and "TA" variables. The variable "TA" contains a list of build-in indicator functions.

How to get another asset or index price series:

VectorCustomDouble v = cFunctions.GetTimeframeData(symbol[0], cFunctions.Timeframe, "close");

Param 1: We request data for the asset (symbol[0])
Param 2: We request data for the default time frame
Param 3: We request the "close" price series

"VectorCustomDouble" item is different from the "VectorD" item in that it may contain more than one element/value in a specific bar.
For example, in case we are working with weekly data and we use "GetTimeframeData" function to reference a daily period, the "VectorCustomDouble" variable will get five values for each trading bar (Trading data for Monday, Tuesday, Wednesday, Thursday and Friday).

We can convert a "VectorCustomDouble" variable to "VectorD" by calling the "ToVectorD" method.

Here is the complete formula:

VectorCustomDouble v = cFunctions.GetTimeframeData(symbol[0], cFunctions.Timeframe, "close");
VectorD a = v.ToVectorD();
result = TA.Perf(cFunctions.Close, period) - TA.Perf(a, period);

If you ask which method is the most efficient, I will tell you that the second method is the most efficient one in terms of performance and memory consumption.

no comments (Log in)

QuantShare Blog
Search Posts

Recent Posts

Create Graphs using the Grid Tool
Posted 1288 days ago

Profile Graphs
Posted 1393 days ago

Previous Posts

More Posts


Create an account
Affiliate Program
Contact Us
Trading Forum
How-to Lessons
About Us
Terms of Use

Copyright 2024
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
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.