Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StochK Indicator Fix #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions pandasta/indicators.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -218,6 +218,24 @@ 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 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
Expand Down Expand Up @@ -260,6 +278,8 @@ class Indicators(Enum):
EMA = ('ema', ExponentialMovingAverage.create)
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)

Expand Down