I noticed this issue a while ago when I first read the Common Usage: Using TA-Lib section of the Quantopian Overview.
Bottom line: Don't use history windows to generate exponential moving average calculations (e.g. EMA, MACD, etc)
Common Usage: Using TA-Lib
Since history returns a pandas DataFrame, a Series can be extracted and then passed to TA-Lib.
An example EMA calculation:
# Python TA-Lib wrapper
# https://github.com/mrjbq7/ta-lib
import talib
def initialize(context):
context.my_sid = sid(24)
def handle_data(context, data):
price_history = history(bar_count=30, frequency='1d', field='price')
my_sid_series = price_history[context.my_sid]
ema_result = talib.EMA(my_sid_series)
record(ema=ema_result[-1])
All exponential moving averages have permanent 'memory'. In order to calculate a 60 day EMA, for example, I only need the value of the current observations (e.g. closing price), the most recent EMA value (which contains the memory of all past values with exponentially decaying weights), and the decay factor alpha (e.g. 1 / 60 in this example). Although EMA values tend to converge over time (how quickly depends on the alpha parameter), users will obtain different EMA values depending on when they started their observations. Today's observation of the 60-day EMA for SPY will be different if I use one year of SPY history for one example and two year's of history for the other.
The history implementation above, however, will have a subtle drift. When the EMA figure is calculated in TA-Lib, you will get NA until you have enough observations (e.g. 60 data points in a 60 day EMA). On the 60th day, TA-Lib gives you the simple average of these data points for the EMA. This value is used to 'seed' the EMA calculation for all values subsequently returned. Note that the 60-day EMA and simple MA (SMA) will be identical on this initial data point of the 60th observation.
HOWEVER, If one were to use a 60-day history window to generate both the EMA and the SMA in the example above, then the values will be identical throughout the testing window. This is because you are losing the memory properties of the EMA by using a sliding history window and just using the most recent values (which in the extreme simply gives you the simple moving average).
Hope this helps to clarify.