From b7aa845e8d66dd390cadbe98077d0b833f2aef1f Mon Sep 17 00:00:00 2001 From: Preslav Rachev Date: Wed, 31 Oct 2018 08:39:30 +0100 Subject: [PATCH 1/2] computes StochK based on the last closing price only - to prevent calculation issues, the old implementation has been kept and can be referred to using `stochko_` - trello: https://trello.com/c/dp3fHXOB --- pandasta/indicators.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/pandasta/indicators.py b/pandasta/indicators.py index bbf1131..483296b 100644 --- a/pandasta/indicators.py +++ b/pandasta/indicators.py @@ -1,9 +1,9 @@ import numpy as np import pandas as pd import random +from abc import ABCMeta, abstractmethod from dataclasses import dataclass from enum import Enum -from abc import ABCMeta, abstractmethod from pyfinance.ols import PandasRollingOLS from typing import Optional @@ -207,7 +207,7 @@ def create(data: TaDataFrame, period): return pd.ewma(data.get_closing_prices(), span=period, min_periods=period - 1) -class StochasticOscillatorK(Indicator): +class StochasticOscillatorKOld(Indicator): @staticmethod def create(data: TaDataFrame, period): assert type( @@ -218,6 +218,16 @@ def create(data: TaDataFrame, period): return (closing_prices - min_price) / (max_price - min_price) +class StochasticOscillatorK(Indicator): + @staticmethod + def create(data: TaDataFrame, period): + assert type( + period) == int, 'Only an integer number of periods is supported at the moment!' + min_price = data.get_low_prices().rolling(period).min() + max_price = data.get_high_prices().rolling(period).max() + return (data.get_closing_prices() - min_price) / (max_price - min_price) + + class HighLowPriceRatio(Indicator): @staticmethod @@ -260,6 +270,7 @@ class Indicators(Enum): EMA = ('ema', ExponentialMovingAverage.create) HILO = ('hilo', HighLowPriceRatio.create) STOCH_K = ('stochk', StochasticOscillatorK.create) + STOCH_K_OLD = ('stochko', StochasticOscillatorKOld.create) ATR = ('atr', AverageTrueRange.create) TREND = ('trend', LinearTrend.create) From 9db8c141e05954e6fcff403ac0289c10fd912eed Mon Sep 17 00:00:00 2001 From: Preslav Rachev Date: Wed, 31 Oct 2018 10:31:37 +0100 Subject: [PATCH 2/2] Adds a 3-day-smoothed StochD indicator - trello: https://trello.com/c/eFGDlBbp --- pandasta/indicators.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pandasta/indicators.py b/pandasta/indicators.py index 483296b..80d1cca 100644 --- a/pandasta/indicators.py +++ b/pandasta/indicators.py @@ -228,6 +228,14 @@ def create(data: TaDataFrame, period): return (data.get_closing_prices() - min_price) / (max_price - min_price) +class StochasticOscillatorD(Indicator): + @staticmethod + def create(data: TaDataFrame, period): + assert type( + period) == int, 'Only an integer number of periods is supported at the moment!' + return StochasticOscillatorK().create(data, period).rolling(3).mean() + + class HighLowPriceRatio(Indicator): @staticmethod @@ -271,6 +279,7 @@ class Indicators(Enum): HILO = ('hilo', HighLowPriceRatio.create) STOCH_K = ('stochk', StochasticOscillatorK.create) STOCH_K_OLD = ('stochko', StochasticOscillatorKOld.create) + STOCH_D = ('stochd', StochasticOscillatorD.create) ATR = ('atr', AverageTrueRange.create) TREND = ('trend', LinearTrend.create)