mirror of
https://github.com/wassname/pandas-ta.git
synced 2026-07-19 11:26:11 +08:00
48 lines
1.2 KiB
Python
48 lines
1.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
from ..utils import get_offset, verify_series
|
|
|
|
def variance(close, length=None, offset=None, **kwargs):
|
|
"""Indicator: Variance"""
|
|
# Validate Arguments
|
|
close = verify_series(close)
|
|
length = int(length) if length and length > 1 else 30
|
|
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
|
|
variance = close.rolling(length, min_periods=min_periods).var()
|
|
|
|
# Offset
|
|
if offset != 0:
|
|
variance = variance.shift(offset)
|
|
|
|
# Name & Category
|
|
variance.name = f"VAR_{length}"
|
|
variance.category = 'statistics'
|
|
|
|
return variance
|
|
|
|
|
|
|
|
variance.__doc__ = \
|
|
"""Rolling Variance
|
|
|
|
Sources:
|
|
|
|
Calculation:
|
|
Default Inputs:
|
|
length=30
|
|
VARIANCE = close.rolling(length).var()
|
|
|
|
Args:
|
|
close (pd.Series): Series of 'close's
|
|
length (int): It's period. Default: 30
|
|
offset (int): How many periods to offset the result. Default: 0
|
|
|
|
Kwargs:
|
|
fillna (value, optional): pd.DataFrame.fillna(value)
|
|
fill_method (value, optional): Type of fill method
|
|
|
|
Returns:
|
|
pd.Series: New feature generated.
|
|
""" |