#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;}

### Learn How to Create Your Own Technical Analysis Indicators

Updated on 2013-05-21

Today, we will show you how to implement custom functions in QuantShare by creating two popular trading indicators.
Custom functions can be implemented using C# programming language. The result is a function that can be used in the QuantShare programming language. You know, the easy language you use when creating charts, composites, trading systems...

There are several reasons why someone would want to create a technical analysis indicator using C#. Here are few of them:

- To have one single function to use instead of several lines of code
- To create advanced indicators that cannot be created using QuantShare programming language
- To protect the source code of your indicator

Simple Moving Average

The simple moving average (SMA) is a very popular trend following indicator that shows the direction of a trend.

The SMA indicator is already built-in QuantShare, however, we wanted to show you how to implement it from scratch using the custom indicators tool.

The SMA formula is as follows: Average of last N-bar values

To create a new custom indicator:
- Select "Tools -> Create Functions".
- Click on "Add", type the function name "SMA1" then click on "Save".

Note that we cannot use "SMA" as a name for our function because this keyword is reserved (Remember that there is already a built-in function called "SMA").

After you create the indicator/function, you can type the C# script in the right panel. Below that panel, you can define the parameters that will be passed to the function. In our case (Simple moving average), we only need one parameter, which is the lookback period.

Let me show you now the C# code that you must copy in order to create the SMA indicator:

- First of all, add one parameter (set "Period" as name)
- Type the following code:

int p = (int)Period[0]; // All parameters are passed as vectors. Here we are passing a numeric value and thus we can get that value by getting the first element of the "Period" vector
VectorD close = cFunctions.Close; // Gets the close series
for(int i=p-1;i < result.Length;i++) // Loops through each trading bar
{
double sum = 0;
for(int j=i-p+1;j <= i;j++) // Loops through the last "p" elements of each trading bar
{
sum = sum + close[j]; // Sums previous close values
}
result[i] = sum / p; // Calculates the average of the past close values (SMA)
}

Each line of code is followed by a brief description that explains what exactly this line does.
Click on the "Save" button to compile and save your indicator.

Percentage Price Oscillator

Percentage price oscillator is a momentum indicator that simply calculates the difference between two moving averages in percentage.

The PPO formula is as follows: (Short exponential moving average - Long Exponential Moving Average) / (Long Exponential Moving Average)

In QS programming language, PPO (9, 26) can calculated as follows:

ppo2 = (ema(9) - ema(26)) / ema(26);

But because we do want to replace this line by something like (ppo2 = ppo(9, 26);), let us create a custom function to calculate the PPO indicator.

As usual, open the "Create Functions" tool and create a new function (PPO1). Do not use "PPO" keyword because the percentage price oscillator is already built-in QuantShare.

Here is the C# formula of the PPO indicator:

VectorD st = TA.Ema(p1);
VectorD lt = TA.Ema(p2);
result = (st - lt) / lt;

You must also add two parameters (below the script editor).
"p1": Short-term period
"p2": Long-term period.
These parameters will be passed to the function and as you can see, they are used to calculate the different exponential moving averages.

In this example, we have used the "TA" class. This class contains several built-in functions and indicators.

We could have implemented the first example (SMA) by simply typing:
result = TA.Sma(Period);

Note that at any moment, you can use the CONTROL+SPACE shortcut to display the list of available functions/parameters.

How to Reference a Custom Indicator in QuantShare Programming Language

Let us see how to plot a custom indicator in a chart.

- Right click on a chart then select "Create new pane"
- Right click on the new pane then select "Edit Formula"
- Type the following formula:

a = ppo1(9, 26); // This is the call the previously created "ppo1" C# function
plot(a, "PPO", colorBlue);

- Click on "Update Graph" to add the PPO indicator to the chart

How to Protect your Source Code

- In "Create Functions" form, click on "Manage" button (Top bar)
- Select one or several indicator you want to protect/encrypt
- Select "Tools -> Encrypt checked items" (If you do not see the "Tools" button on the top menu, click on "+" icon to expand the menu)
- Click "Yes"
- Enter your password then click on "OK"

Now, each time you select that indicator in "Create Function" control, you will be prompted to enter a password. Otherwise, you will not be able to see the indicator C# code.

Note that even if an indicator is encrypted, you can still use it in your charts, trading systems, composites, watchlists and any other tool.

 3 comments (Log in)

 Search Posts Recent Posts Chart Layouts Explained - With Custom Scripts Posted 1140 days ago Create Graphs using the Grid Tool Posted 1320 days ago Profile Graphs Posted 1425 days ago More about QuantShare Programming Language Posted 1504 days ago Backtest Trading Strategies Programmatically and on Schedule Posted 1650 days ago QuantShare Trading Software: New Features in the 3.7.0 Version Posted 1680 days ago Compare Multiple Securities in a Single Chart Posted 1740 days ago Previous Posts Learn How to Create Your Own Technical Analysis Indicators Posted 4073 days ago How to Pick the Best Trend Indicators Posted 4084 days ago Update Chart Layout based on Active Ticker Symbol - Part 2 Posted 4100 days ago Update Chart Layout based on Active Ticker Symbol Posted 4109 days ago Using Average True Range to Measure Intraday Volatility Posted 4134 days ago How to Backtest Your Trading System for Each Industry Posted 4142 days ago Create Profitable Trading Strategies with Exchange Traded Funds (ETFs) Posted 4155 days ago 3 Market Indicators based on Fundamental Data Posted 4168 days ago How to Send Commands to QuantShare from Excel or Other Applications Posted 4192 days ago How to make sure your Historical Price Data is Split Adjusted Posted 4198 days ago How to Select the Best Market Indicator for your Trading System Posted 4217 days ago 4 Market Composite Indicators Based on Industry Data Posted 4231 days ago Industry Analysis - How to Compare Stocks with their Industries Posted 4239 days ago

 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 Follow us on Twitter

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.