mirror of
https://github.com/wassname/pandas-ta.git
synced 2026-08-02 12:50:22 +08:00
BLD added sinwma indicator and tests
This commit is contained in:
@@ -109,7 +109,7 @@ help(pd.DataFrame().ta.log_return)
|
||||
|:--------:|
|
||||
|  |
|
||||
|
||||
## _Overlap_ (23)
|
||||
## _Overlap_ (24)
|
||||
|
||||
* _Double Exponential Moving Average_: **dema**
|
||||
* _Exponential Moving Average_: **ema**
|
||||
@@ -128,6 +128,7 @@ help(pd.DataFrame().ta.log_return)
|
||||
* _Pascal's Weighted Moving Average_: **pwma**
|
||||
* _William's Moving Average_: **rma**
|
||||
* _Simple Moving Average_: **sma**
|
||||
* _Sine Weighted Moving Average_: **sinwma**
|
||||
* _Symmetric Weighted Moving Average_: **swma**
|
||||
* _T3 Moving Average_: **t3**
|
||||
* _Triple Exponential Moving Average_: **tema**
|
||||
@@ -229,6 +230,7 @@ Use parameter: cumulative=**True** for cumulative results.
|
||||
|
||||
|
||||
# Inspiration
|
||||
* TradingView: http://www.tradingview.com
|
||||
* Original TA-LIB: http://ta-lib.org/
|
||||
* Bukosabino: https://github.com/bukosabino/ta
|
||||
|
||||
|
||||
@@ -56,6 +56,7 @@ from .overlap.midprice import midprice
|
||||
from .overlap.ohlc4 import ohlc4
|
||||
from .overlap.pwma import pwma
|
||||
from .overlap.rma import rma
|
||||
from .overlap.sinwma import sinwma
|
||||
from .overlap.sma import sma
|
||||
from .overlap.swma import swma
|
||||
from .overlap.t3 import t3
|
||||
|
||||
@@ -502,6 +502,13 @@ class AnalysisIndicators(BasePandasObject):
|
||||
self._append(result, **kwargs)
|
||||
return result
|
||||
|
||||
def sinwma(self, close=None, length=None, offset=None, **kwargs):
|
||||
close = self._get_column(close, 'close')
|
||||
from .overlap.sinwma import sinwma
|
||||
result = sinwma(close=close, length=length, offset=offset, **kwargs)
|
||||
self._append(result, **kwargs)
|
||||
return result
|
||||
|
||||
def sma(self, close=None, length=None, offset=None, **kwargs):
|
||||
close = self._get_column(close, 'close')
|
||||
from .overlap.sma import sma
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from math import pi
|
||||
from math import sin
|
||||
from pandas import Series
|
||||
from ..utils import get_offset, pascals_triangle, verify_series, weights
|
||||
|
||||
def sinwma(close, length=None, asc=None, offset=None, **kwargs):
|
||||
"""Indicator: Sine Weighted Moving Average (SINWMA) by Everget of TradingView"""
|
||||
# Validate Arguments
|
||||
close = verify_series(close)
|
||||
length = int(length) if length and length > 0 else 14
|
||||
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
|
||||
sines = Series([sin((i + 1) * pi / (length + 1)) for i in range(0, length)])
|
||||
w = sines / sines.sum()
|
||||
|
||||
sinwma = close.rolling(length, min_periods=length).apply(weights(w), raw=True)
|
||||
|
||||
# Offset
|
||||
if offset != 0:
|
||||
sinwma = sinwma.shift(offset)
|
||||
|
||||
# Name & Category
|
||||
sinwma.name = f"SINWMA_{length}"
|
||||
sinwma.category = 'overlap'
|
||||
|
||||
return sinwma
|
||||
|
||||
|
||||
|
||||
sinwma.__doc__ = \
|
||||
"""Sine Weighted Moving Average (SWMA)
|
||||
|
||||
A weighted average using sine cycles. The middle term(s) of the average have the highest
|
||||
weight(s).
|
||||
|
||||
Source:
|
||||
https://www.tradingview.com/script/6MWFvnPO-Sine-Weighted-Moving-Average/
|
||||
Author: Everget (https://www.tradingview.com/u/everget/)
|
||||
|
||||
Calculation:
|
||||
Default Inputs:
|
||||
length=10
|
||||
|
||||
def weights(w):
|
||||
def _compute(x):
|
||||
return np.dot(w * x)
|
||||
return _compute
|
||||
|
||||
sines = Series([sin((i + 1) * pi / (length + 1)) for i in range(0, length)])
|
||||
w = sines / sines.sum()
|
||||
SINWMA = close.rolling(length, min_periods=length).apply(weights(w), raw=True)
|
||||
|
||||
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:
|
||||
fillna (value, optional): pd.DataFrame.fillna(value)
|
||||
fill_method (value, optional): Type of fill method
|
||||
|
||||
Returns:
|
||||
pd.Series: New feature generated.
|
||||
"""
|
||||
@@ -6,7 +6,7 @@ long_description = "An easy to use Python 3 Pandas Extension with 80+ Technical
|
||||
setup(
|
||||
name ="pandas_ta",
|
||||
packages =['pandas_ta', 'pandas_ta.momentum', 'pandas_ta.overlap', 'pandas_ta.performance', 'pandas_ta.statistics', 'pandas_ta.trend', 'pandas_ta.volatility', 'pandas_ta.volume'],
|
||||
version ="0.1.33b",
|
||||
version ="0.1.34b",
|
||||
description =long_description,
|
||||
long_description =long_description,
|
||||
author ="Kevin Johnson",
|
||||
|
||||
@@ -218,6 +218,11 @@ class TestOverlap(TestCase):
|
||||
self.assertIsInstance(result, Series)
|
||||
self.assertEqual(result.name, 'RMA_10')
|
||||
|
||||
def test_sinwma(self):
|
||||
result = pandas_ta.sinwma(self.close)
|
||||
self.assertIsInstance(result, Series)
|
||||
self.assertEqual(result.name, 'SINWMA_14')
|
||||
|
||||
def test_sma(self):
|
||||
result = pandas_ta.sma(self.close)
|
||||
self.assertIsInstance(result, Series)
|
||||
|
||||
@@ -93,6 +93,11 @@ class TestOverlapExtension(TestCase):
|
||||
self.assertIsInstance(self.data, DataFrame)
|
||||
self.assertEqual(self.data.columns[-1], 'RMA_10')
|
||||
|
||||
def test_sinwma_ext(self):
|
||||
self.data.ta.sinwma(append=True)
|
||||
self.assertIsInstance(self.data, DataFrame)
|
||||
self.assertEqual(self.data.columns[-1], 'SINWMA_14')
|
||||
|
||||
def test_sma_ext(self):
|
||||
self.data.ta.sma(append=True)
|
||||
self.assertIsInstance(self.data, DataFrame)
|
||||
|
||||
Reference in New Issue
Block a user