Reputation: 2639
I'm trying to use TA-Lib for technical analysis. I downloaded TA-Lib-Core Nuget package for .NET. Unfortunately, I can't find any API documentation so some method parameters are a bit of mystery.
I downloaded historical data for AMD between 4/12/2016 and 4/12/2017 here.
This is what I have for RSI and MACD calculations:
int outBegIdx1, outNBElement1;
double[] outReal = new double[data.Count];
int outBegIdx2, outNBElement2;
double[] outMACD = new double[data.Count];
double[] outMACDSignal = new double[data.Count];
double[] outMACDHist = new double[data.Count];
TicTacTec.TA.Library.Core.Rsi(0, data.Count - 1, data.Select(x => x.Close).ToArray(), 14, out outBegIdx1, out outNBElement1, outReal);
TicTacTec.TA.Library.Core.Macd(0, data.Count - 1, data.Select(x => (float)x.Close).ToArray(), 12, 26, 9, out outBegIdx2, out outNBElement2, outMACD, outMACDSignal, outMACDHist);
I'm comparing the results to TradingView's AMD page here. To see RSI and MACD values, please click "Indicators" at the top and select those. Also you should be looking at the 1 year daily chart.
The problem is TA-Lib is outputting vastly different results, and I'm not sure if I'm using these APIs correctly. What I'm seeing is 65.34 for RSI and 0.0431 for MACD Histogram as opposed to TradingView's 39.42 and -0.2165 respectively.
Please note that data[0]
has the close price for 4/12/2016 whereas the last element is for 4/12/2017. Also I have no idea what outBegIdx
and outNBElement
parameters stand for.
How do I return correct values?
Upvotes: 5
Views: 9048
Reputation: 3111
Here is documentation that explains out* variables meanings. In short, your our arrays correspond to original data like that:
for (int i = 0; i < outNbElement; i++){
qDebug() << "Result for day #" << outBegIdx+i << ": outMACD: " << outMACD[i]
<< " outMACDSignal: " << outMACDSignal[i]
<< "outMACDHist: " << outMACDHist[i];
}
For ex, MACD (12,26,9) will return data.Count - 33
(if I recall right, it'll be -33) output values in array as input data's first 33 values will be use to initialize EMA's that MACD uses. For example, if you are looking for 10-days MA (moving average) and passes 10 days data to TA-Lib you should expect only 1 day result value in output arrays (for a last day) just because first 9 days were used to initialize and 10-days MA wasn't ready at this point of time. I'm not sure in exact value of 33 for MACD(12,26,9) - you may find exact value with help of indicator's Lookback
functions. There are such in C++ API and they must be somewhere in C# API too. You can even allocate less space for your result arrays considering lookback value. Anyway you're on a safe side while you pass out* arrays which allocate same size as original data and rely on out*
indexes to iterate it.
If TA-Libs moving averages' results differ significantly from some website results this is usually the effect of moment when you start calculation. For example on site you refer I see that MACD indicator is calculated for 1st month of year period. They couldn't get this MACD data if they calculate it using only last year data like TA-Lib did. Bcs of some data must be wasted to initialize moving averages. This means they started MACD calculation earlier. For example 3 years ago (or for all data they have) and display results for last 12 months only. For ex if you switch to ALL period instead of 1y you'll see that indicator's values are missing for a small piece of graph at very beginning. That's where they actually have started MACD calculation for this scale. I would try to feed TA-Lib 3years data period and compared its last year with a website. It should be well enough for their results to converge.
When you are 100% sure TA-Lib's indicator and website's indicator are calculated on same data then you should expect your results will be equal or slightly differ. This small difference may be due to different implementation of indicator. For example MACD(12,26,9) uses factors 2/(12+1), 2/(26+1), 2/(9+1) in its formula. An value 2/27 may be calculated on fly and used with all available precession. Or it may be rounded to 0.074. Or you could refer to some book in your implementation and found out that they recommend 0.075 like in Technical Analysis of Stock Trends, Tenth Edition by Robert D. Edwards,W.H.C. . TA-Lib calculates these values on fly, but could be forced to use hardcoded (0.075, 0.15, 0.2) via some C++ API tricks.
Upvotes: 6