zipline EMA calculate problem
Dear Zipline Maintainers,
I am trying to play with zipline. I notice that It integrates with talib to calculate EMA. I have following code block to get EMA value.
def initialize(context): context.asset = symbol(SYMBOL)
# Add 2 mavg transforms, one with a long window, one with a short window.
context.short_ema_trans = EMA(timeperiod=5)
It seems that zipline only using the 5 bars close price to get the EMA value. for eaxmple, the close price is [34, 32, 35, 33, 40, 44, 45]
so when calculate day 5's EMA it uses the data [34, 32, 35, 33, 40] to get EMA. and day 6 it uses [32, 35, 33, 40, 44] and day 7 using [35, 33, 40, 44, 45] passing to talib.EMA funciton. It doesn't using day 5's EMA to get day 6's EMA.
but from google we know that calculate the exponential moving average. The formula below is for a 10-day EMA. SMA: 10 period sum / 10 Multiplier: (2 / (Time periods + 1) ) = (2 / (10 + 1) ) = 0.1818 (18.18%) EMA: {Close - EMA(previous day)} x multiplier + EMA(previous day).
it seems that zipline uses another method to get EMA value, right?
Sincerely, lambokini
The transform api is deprecated. Instead you can just use talib directly. Here is an updated example using EMA: https://github.com/quantopian/zipline/blob/master/zipline/examples/dual_ema_talib.py
@llllllllll Hil, I am new to zipline. I installed zipline just now and I ran the example dual_ema_talib.py. But I got some error in this example.
File "dual_ema_talib.py", line 38, in handle_data trailing_window = data.history(context.asset, 'price', 40, '1d') AttributeError: 'BarData' object has no attribute 'history'
Is there something changed or something wrong?
@llllllllll even we use talib, the EMA calculation i think is still not correct. since each time it use the last n day's data to calculate EMA. it never uses previous EMA and today's close price to calculate today's EMA.
I know we can use talib.EMA to calculate EMA. for example we have 200 day's data, talib.EMA will calculate n=5 period's EMA.
but for zipline, each time it only uses the last 5 day's data to calculate EMA. it should use data from the beginning to today's data and we extract EMA value for today's EMA.
@saukymo Where did you find the example / how did you install zipline? Master of zipline has been updated but we have not yet done a release so if you pulled the example off of github but pip/conda installed zipline the examples would not run.
@lambokini What I mean is that zipline is no longer doing talib computations for the user. You may call the talib functions directly by getting the data in an array with data.history (on master) or zipline.api.history (on latest release) and then calling talib.EMA
Going to close this due to no response/follow up. Feel free to open a new issue if you still have this problem :)