You are here

Technical Analysis Library (TA-LIB) Tutorial for Java

The TA-Lib JAR is a framework for technical analysis. This JAR 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.

http://ta-lib.org/

You should download the JAVA JAR 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.

import com.tictactec.ta.lib.Core;
import com.tictactec.ta.lib.MInteger;
import com.tictactec.ta.lib.RetCode;

public class ExampleTALib {

  /**
   * The total number of periods to generate data for.
   */
  public static final int TOTAL_PERIODS = 100;

  /**
    * The number of periods to average together.
    */
  public static final int PERIODS_AVERAGE = 30;

    public static void main(String[] args) {
      double[] closePrice = new double[TOTAL_PERIODS];
      double[] out = new double[TOTAL_PERIODS];
      MInteger begin = new MInteger();
      MInteger length = new MInteger();

      for (int i = 0; i < closePrice.length; i++) {
        closePrice[i] = (double) i;
      }

      Core c = new Core();
      RetCode retCode = c.sma(0, closePrice.length - 1, closePrice, PERIODS_AVERAGE, begin, length, out);

      if (retCode == RetCode.Success) {
        System.out.println("Output Begin:" + begin.value);
        System.out.println("Output Begin:" + length.value);

        for (int i = begin.value; i < length.value; 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(out[i]);
          System.out.println(line.toString());
      }
    }
    else {
      System.out.println("Error");
    }
  }
}

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.

First, 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".

Secondly, in C, you can just pass a pointer to an integer(or any other type) and allow the function to return results that way. Pretty much "pass by reference". To give this ability to Java, the MInteger class is used. This allows the SMA function to return a starting index, as well as a length.

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.

Comments

aiv's picture

the average in this example is always above the highest value in the queue:
Output Begin:29
Output Begin:71
Period #30 close= 29.0 mov avg=43.5
Period #31 close= 30.0 mov avg=44.5
Period #32 close= 31.0 mov avg=45.5
Period #33 close= 32.0 mov avg=46.5
Period #34 close= 33.0 mov avg=47.5
Period #35 close= 34.0 mov avg=48.5
Period #36 close= 35.0 mov avg=49.5
Period #37 close= 36.0 mov avg=50.5
Period #38 close= 37.0 mov avg=51.5
Period #39 close= 38.0 mov avg=52.5
Period #40 close= 39.0 mov avg=53.5
Period #41 close= 40.0 mov avg=54.5
Period #42 close= 41.0 mov avg=55.5
Period #43 close= 42.0 mov avg=56.5
Period #44 close= 43.0 mov avg=57.5
Period #45 close= 44.0 mov avg=58.5
Period #46 close= 45.0 mov avg=59.5
Period #47 close= 46.0 mov avg=60.5
Period #48 close= 47.0 mov avg=61.5
Period #49 close= 48.0 mov avg=62.5
Period #50 close= 49.0 mov avg=63.5
Period #51 close= 50.0 mov avg=64.5
Period #52 close= 51.0 mov avg=65.5
Period #53 close= 52.0 mov avg=66.5
Period #54 close= 53.0 mov avg=67.5
Period #55 close= 54.0 mov avg=68.5
Period #56 close= 55.0 mov avg=69.5
Period #57 close= 56.0 mov avg=70.5
Period #58 close= 57.0 mov avg=71.5
Period #59 close= 58.0 mov avg=72.5
Period #60 close= 59.0 mov avg=73.5
Period #61 close= 60.0 mov avg=74.5
Period #62 close= 61.0 mov avg=75.5
Period #63 close= 62.0 mov avg=76.5
Period #64 close= 63.0 mov avg=77.5
Period #65 close= 64.0 mov avg=78.5
Period #66 close= 65.0 mov avg=79.5
Period #67 close= 66.0 mov avg=80.5
Period #68 close= 67.0 mov avg=81.5
Period #69 close= 68.0 mov avg=82.5
Period #70 close= 69.0 mov avg=83.5
Period #71 close= 70.0 mov avg=84.5

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer