|
|
C/C++ Application Program Interface2.0 How to build and link to TA-Lib 3.0 Technical Analysis Functions
1.0 IntroductionAll functions available to the end-user are documented here.
2.0 How to build and link to TA-LibTo use the library in C/C++ project, you just need to #include "ta_libc.h" and link to the static library corresponding to your type of application. All the required header file are in ta-lib/c/include. Header files in other directories should never be included by your application directly. 2.1 Windows - MSVC and Visual StudioHere is the list of variant of the static library currently supported:
Pre-compiled version of these libraries is part of the MSVC package. If you wish to re-build yourself the static libraries, makefiles can be found in ta-lib/c/make/<ENV>/win32/msvc. These MSVC makefiles also works with Visual Studio 2005. To rebuild from scratch do "nmake clean" and then "nmake" again.
2.2 Windows/Free C++ Borland Compiler
2.3 Other Platforms2.3.1 Linux Static LibrariesThe SVN repository and the Win32 packages contains multiple makefiles generated for multiple platforms, including Linux. The makefiles are found in ta-lib/c/make/<ENV>/linux/g++. The <ENV> is a 3 letter sub-directories (cmd,cmr,csd,csr) allowing to select an environment of development applicable to your application (see section 2.1). The cdd and cdr type does not apply to Linux. Just type "make clean" and "make" to build all targets. The generated target will be found in ta-lib/c/lib and ta-lib/c/bin. You will need to link to 3 static libraries: ta-abstract, ta-func and ta-common 2.3.2 All Unix Flavors Shared LibrariesDownload the source code tar.gz package and perform the following as root: ./configure TA-Lib is contained in a single shared library called "libta-lib" (name will vary depending of your platform). With gcc you link using the switch "-lta-lib". 2.4 Regression Testing (All Platform)When building the complete source tree, an application called "ta_regtest" is created in the ta-lib/c/bin directory. This is a suite of tests to validate that the library you did compiled is behaving as expected within your environment. Whenever you re-compile the TA-Lib libraries, it is suggested to re-run ta_regtest. An internet connection is required since web data fetching is one feature being tested.
3.0 Technical Analysis FunctionsMake sure TA_Initialize was called once (and only once) prior to any other API functions. The individual TA function can be directly called. User who would like to integrate the TA functions without prior knowledge of their parameters, should consider the abstraction layer interface. The source code of all the TA functions is in ta-lib/c/src/ta_func. 3.1 Direct call to a TA FunctionDirect call could be done through the interface defined in ta-lib/c/include/ta_func.h All the TA functions are simple mathematical function. You provides the inputs with an array, and the function simply store the output in a caller provided output array. The TA functions are NOT allocating any space for the caller. The number of data in the output will NEVER exceed the number of elements requested to be calculated (with the startIdx and endIdx explained below). Here is an example: We will dissect the TA_MA function allowing to calculate a simple moving average. TA_RetCode TA_MA( int startIdx, At first it appears that there is a lot of parameters, but do not be discourage, all functions are consistent and share the same parameter structure. The parameters are provided in 4 sections:
This structure of parameters gives a lot of flexibility to make the function calculate ONLY the portion of required data. It is slightly complex, but it allows demanding user to manage efficiently the memory and the CPU processing. Lets say you wish to calculate a 30 day moving average using closing prices. The function call could look as follow: TA_Real closePrice[400]; TA_Real out[400]; TA_Integer outBeg; TA_Integer outNbElement; /* ... initialize your closing price here... */ retCode = TA_MA( 0, 399, &closePrice[0], 30,TA_MAType_SMA, &outBeg, &outNbElement, &out[0] ); /* The output is displayed here */ for( i=0; i < outNbElement; i++ ) printf( "Day %d = %f\n", outBeg+i, out[i] ); One important aspect of the output are the outBeg and outNbElement. Even if it was requested to calculate for the whole range (from 0 to 399), the moving average is not valid until the 30th day. Consequently, the outBeg will be 29 (zero base) and the outNbElement will be 400-29 = 371. Meaning only the first 371 elements of out are valid, and these could be calculated only starting at the 30th element of the input. As an alternative example, if you would have requested to calculate only in the "125 to 225" range (with startIdx and endIdx), the outBeg will be 125 and outNbElement will be 100. (the "30" minimum required is not an issue because we dispose of 125 closing price before the start of the requested range...). As you may have already understand, the "out" array will be written only for its first 100 elements. The rest will be left untouched. Here is another example. In that case we want to calculate a 14 bars exponential moving average only for 1 price bar in particular (say the last day of 300 price bar): TA_Real closePrice[300]; TA_Real out; TA_Integer outBeg; TA_Integer outNbElement; /* ... initialize your closing price here... */ retCode = TA_MA( 299, 299, &closePrice[0], 14, TA_MAType_EMA, &outBeg, &outNbElement, &out ); In that example: outBeg will be 299, outNbElement will be 1, and only one value gets written into out. In the case that you do not provide enough data to even being able to calculate at least one value, outNbElement will be 0 and outBeg shall be ignored. If the input and output of a TA function are of the same type, the caller can re-use the input buffer for storing one of the output of the TA function. The following example will work: #define BUFFER_SIZE 100 TA_Real buffer[BUFFER_SIZE]; ... retCode = TA_MA( 0, BUFFER_SIZE-1, &buffer[0], 30, TA_MAType_SMA, &outBeg, &outNbElement, &buffer[0] ); Of course, the input is overwritten, but this capability diminish needs for temporary memory allocation for certain application. You can assume this capability is true for all TA functions. 3.2 Output SizeIt is important that the output array is large enough. Depending of your needs, you might find one of the following method useful to determine the output allocation size. All these methods are consistent and works with all TA functions:
A function TA_XXXX_Lookback is provided for each TA function. Example: For TA_SMA, there is a TA_SMA_Lookback. The lookback function indicates how many inputs are consume before the first output can be calculated. Example: A simple moving average (SMA) of period 10 will have a lookback of 9. 3.3 Abstraction LayerAll the TA Function can be called using the interface defined in ta-lib/c/include/ta_abstract.h The abstraction layer is particularly interesting for an application who wishes to support the complete list of TA functions without having to re-write new code each time a new function is added to the TA-LIB. If you wish to simply integrate in your application a small number of specific function, you may be better to simply call these directly (see previous section). 3.4 Software CompatibilitySurprisingly, some indicators are implemented differently in certain commercial softwares. Because compatibility is sometimes desirable, a global parameter can be set to make many of TA functions to behave similar to the approach chosen by existing popular software. By default all functions use what we believe is the original calculation by the indicator's author. See TA_SetCompatibility and TA_GetCompatibility in ta-lib/c/include/ta_func.h 3.5 Unstable PeriodSome TA functions provides different results depending of the "starting point" of the data being involve. This is often referred as a function having memories. An example of such function is the Exponential Moving Average. It is possible to control the unstable period (the amount of data to strip off) with TA_SetUnstablePeriod and TA_GetUnstablePeriod. 3.6 Input Type: float vs. doubleFor each technical analysis algorithm, there is one version of the function accepting the input as array of float and another accepting array of double. The float version has a "TA_S_" suffix e.g. for TA_MA there is an equivalent TA_S_MA function. TA_RetCode TA_MA( int startIdx, Both version do all the calculation using double e.g. when an element of a float array is accessed, it is changed to double-precision. Consequently, both function will yield the same result. It is typical that users have their price bar data as float and maintain all their intermediate calculation as double. Having direct support for both type in TA-Lib is more memory efficient. With only one type, the user would be potentially forced to duplicate their input data in a new array of a different type prior to a TA function call. |
|