TA-LIB
Technical Analysis Library (TA-LIB) Tutorial for C#
The TA-Lib DLL is a framework for technical analysis. This DLL contains over 200 technical indicators, such as ADX, MACD, RSI, Stochastic, and Bollinger Bands. It was originally written in C, but has been translated to 100% native Java and C#. TA-Lib contains little documentation, other than for the C implementation. If you are familiar with the C programming language, it is not too much of a stretch to get TA-Lib working for you. However, this article is meant to show how to create a simple moving average (SMA) with TA-Lib.
First, you will need to obtain the TA-Lib library. This library can be downloaded from the following URL.
You should download the C# DLL for this article. Additionally, you may wish to download the MSVC version. The MSCV version contains full Java and .Net source code.
Also, this article is meant just as an introduction to using TA-Lib. If you are right now asking yourself what Technical Analysis or financial indicators are, you might want to read up on that first. I make no attempt, at least in this article, to demonstrate what technical analysis is. A great starting point is a website called Chart School.
Licensing
Unfortunately, TA-LIB is not released under one of the standard open source licenses. However, it is very much open source. I am not a lawyer, so you should check this for yourself. However, the license sounds very similar to the LGPL, at least to my "non-lawyer" brain. The gist of the license is as follows.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither name of author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
Computing a Simple Moving Average
First, lets see how to calculate a simple moving average (SMA). This is probably the most simple indicator. But most of the more complex indicators work very similar. The source code for my example is shown here.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using TicTacTec.TA.Library; namespace ExampleTALib { public class Program { /** * The total number of periods to generate data for. */ public const int TOTAL_PERIODS = 100; /** * The number of periods to average together. */ public const int PERIODS_AVERAGE = 30; static void Main(string[] args) { double[] closePrice = new double[TOTAL_PERIODS]; double[] output = new double[TOTAL_PERIODS]; int begin; int length; for (int i = 0; i < closePrice.Length; i++) { closePrice[i] = (double)i; } TicTacTec.TA.Library.Core.RetCode retCode = Core.Sma(0, closePrice.Length - 1, closePrice, PERIODS_AVERAGE, out begin, out length, output); if (retCode == TicTacTec.TA.Library.Core.RetCode.Success) { Console.WriteLine("Output Begin:" + begin); Console.WriteLine("Output Begin:" + length); for (int i = begin; i < length; i++) { StringBuilder line = new StringBuilder(); line.Append("Period #"); line.Append(i + 1); line.Append(" close= "); line.Append(closePrice[i]); line.Append(" mov avg="); line.Append(output[i]); Console.WriteLine(line.ToString()); } } else { Console.WriteLine("Error"); } Console.WriteLine("Done"); } } }
All data is passed in through arrays. This indicator only uses closing prices, so the closing prices are one single dimension array. If you were to use open/close prices, these would be additional arrays. This example uses a range of 100 closing prices, and calculates a 30 day SMA. The results are printed at the end. The code should be fairly easy to follow, but I will highlight a few things.
Mainly, the Core object. TA-Lib pretty much took every financial indicator in the non-OOP C-based version of TA-Lib and dumped them into one MASSIVE class. This would be the Core object. You will instantiate one of these and continue to use it. I am not sure that it is threadsafe. I would suggest using one per thread, as it DOES very much appear to hold some "state".
Conclusions
TA-Lib is widely used. This article is meant to just get you started. I in the process of using TA-Lib to provide technical indicators to the Encog Machine Learning Framework. It looks to be a very good library for our purposes.
Code
There is a bug in your code, you arent printing out all the return elements from Core::SMA, change the for loop to this for correct values:
for (int i = 0; i < length; i++)
{
StringBuilder line = new StringBuilder();
line.Append("Period #");
line.Append(i + 1);
line.Append(" close= ");
line.Append(closePrice[begin]);
line.Append(" mov avg=");
line.Append(output[i]);
Console.WriteLine(line.ToString());
++begin;
}
Now, you will see all 71 values computed and they will match to the close values. For example, in your code you show the sma for close=29 = 43.5, when it is actually 14.5, you can verify quickly in Excel. Anyways, hope this helps people.
Also, since the forum on the TA Lib website doesn't work, do you have any ideas on where I can find a reference that discusses the different types of TA Lib Moving Averages? There are quite a few and I don't recognize them by name in the source code. Thanks!