mirror of
https://github.com/wassname/pandas-ta.git
synced 2026-08-17 11:23:27 +08:00
54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
from ..utils import get_offset, verify_series
|
|
|
|
def sma(close, length=None, offset=None, **kwargs):
|
|
"""Indicator: Simple Moving Average (SMA)"""
|
|
# Validate Arguments
|
|
close = verify_series(close)
|
|
length = int(length) if length and length > 0 else 10
|
|
min_periods = int(kwargs['min_periods']) if 'min_periods' in kwargs and kwargs['min_periods'] is not None else length
|
|
offset = get_offset(offset)
|
|
|
|
# Calculate Result
|
|
sma = close.rolling(length, min_periods=min_periods).mean()
|
|
|
|
# Offset
|
|
if offset != 0:
|
|
sma = sma.shift(offset)
|
|
|
|
# Name & Category
|
|
sma.name = f"SMA_{length}"
|
|
sma.category = 'overlap'
|
|
|
|
return sma
|
|
|
|
|
|
|
|
sma.__doc__ = \
|
|
"""Simple Moving Average (SMA)
|
|
|
|
The Simple Moving Average is the classic moving average that is the equally
|
|
weighted average over n periods.
|
|
|
|
Sources:
|
|
https://www.tradingtechnologies.com/help/x-study/technical-indicator-definitions/simple-moving-average-sma/
|
|
|
|
Calculation:
|
|
Default Inputs:
|
|
length=10
|
|
SMA = SUM(close, length) / length
|
|
|
|
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): Default: True
|
|
presma (bool, optional): If True, uses SMA for initial value.
|
|
fillna (value, optional): pd.DataFrame.fillna(value)
|
|
fill_method (value, optional): Type of fill method
|
|
|
|
Returns:
|
|
pd.Series: New feature generated.
|
|
""" |