mirror of
https://github.com/wassname/pandas-ta.git
synced 2026-06-27 16:10:07 +08:00
71 lines
2.3 KiB
Python
71 lines
2.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
from numpy import NaN as npNaN
|
|
from ..utils import get_offset, verify_series
|
|
|
|
|
|
def ema(close, length=None, offset=None, **kwargs):
|
|
"""Indicator: Exponential Moving Average (EMA)"""
|
|
# Validate Arguments
|
|
close = verify_series(close)
|
|
length = int(length) if length and length > 0 else 10
|
|
# min_periods = kwargs.pop('min_periods', length)
|
|
adjust = kwargs.pop("adjust", False)
|
|
sma = kwargs.pop("sma", True)
|
|
win_type = kwargs.pop("win_type", None)
|
|
offset = get_offset(offset)
|
|
|
|
# Calculate Result
|
|
if sma:
|
|
close = close.copy()
|
|
sma_nth = close[0:length].sum() / length
|
|
close[:length - 1] = npNaN
|
|
close.iloc[length - 1] = sma_nth
|
|
ema = close.ewm(span=length, adjust=adjust).mean()
|
|
|
|
# Offset
|
|
if offset != 0:
|
|
ema = ema.shift(offset)
|
|
|
|
# Name & Category
|
|
ema.name = f"EMA_{length}"
|
|
ema.category = "overlap"
|
|
|
|
return ema
|
|
|
|
|
|
ema.__doc__ = """Exponential Moving Average (EMA)
|
|
|
|
The Exponential Moving Average is more responsive moving average compared to the
|
|
Simple Moving Average (SMA). The weights are determined by alpha which is
|
|
proportional to it's length. There are several different methods of calculating
|
|
EMA. One method uses just the standard definition of EMA and another uses the
|
|
SMA to generate the initial value for the rest of the calculation.
|
|
|
|
Sources:
|
|
https://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:moving_averages
|
|
https://www.investopedia.com/ask/answers/122314/what-exponential-moving-average-ema-formula-and-how-ema-calculated.asp
|
|
|
|
Calculation:
|
|
Default Inputs:
|
|
length=10, adjust=False, sma=True
|
|
if sma:
|
|
sma_nth = close[0:length].sum() / length
|
|
close[:length - 1] = np.NaN
|
|
close.iloc[length - 1] = sma_nth
|
|
EMA = close.ewm(span=length, adjust=adjust).mean()
|
|
|
|
Args:
|
|
close (pd.Series): Series of 'close's
|
|
length (int): It's period. Default: 10
|
|
offset (int): How many periods to offset the result. Default: 0
|
|
|
|
Kwargs:
|
|
adjust (bool, optional): Default: False
|
|
sma (bool, optional): If True, uses SMA for initial value. Default: True
|
|
fillna (value, optional): pd.DataFrame.fillna(value)
|
|
fill_method (value, optional): Type of fill method
|
|
|
|
Returns:
|
|
pd.Series: New feature generated.
|
|
"""
|