golang实现ema及解决pandas计算ema与talib差异
文章目录
解决ema计算差异问题
- pandas用法:
- talib用法:
- golang使用(代码在后面)
python中计算ema两种方式:
- pandas使用ewm这个窗口函数
- 使用talib.EMA函数
- 参考: https://www.joinquant.com/view/community/detail/3d88c84f05e5a3bd72f728a40e54edf4
- c源码: https://github.com/TA-Lib/ta-lib/blob/master/src/ta_func/ta_EMA.c#L291
但是计算完结果有差异,原因是talib的c源码中有3种实现
/* The first EMA is calculated differently. It
* then become the seed for subsequent EMA.
*
* The algorithm for this seed vary widely.
* Only 3 are implemented here:
*
* TA_MA_CLASSIC:
* Use a simple MA of the first 'period'.
* This is the approach most widely documented.
*
* TA_MA_METASTOCK:
* Use first price bar value as a seed
* from the begining of all the available data.
*
* TA_MA_TRADESTATION: !!! not supported yet.
* Use 4th price bar as a seed, except when
* period is 1 who use 2th price bar or something
* like that... (not an obvious one...).
*/
python中默认是TA_MA_CLASSIC,需要使用talib.set_compatibility
进行设置
import talib as ta
print(ta.get_compatibility()) # DEFAULT == 0
ta.set_compatibility(1) # METASTOCK == 1
print(ta.get_compatibility())
golang版本
查看了很多talib的golang的ema实现,都没有支持METASTOCK这种模式,经过仔细对比talib的c源码,实现了golang版本