Giter Club home page Giter Club logo

stockstats's Introduction

Stock Statistics/Indicators Calculation Helper

build & test codecov pypi

VERSION: 0.6.2

Introduction

Supply a wrapper StockDataFrame for pandas.DataFrame with inline stock statistics/indicators support.

Supported statistics/indicators are:

  • delta
  • permutation (zero-based)
  • log return
  • max in range
  • min in range
  • middle = (close + high + low) / 3
  • compare: le, ge, lt, gt, eq, ne
  • count: both backward(c) and forward(fc)
  • cross: including upward cross and downward cross
  • SMA: Simple Moving Average
  • EMA: Exponential Moving Average
  • MSTD: Moving Standard Deviation
  • MVAR: Moving Variance
  • RSV: Raw Stochastic Value
  • RSI: Relative Strength Index
  • KDJ: Stochastic Oscillator
  • Bolling: Bollinger Band
  • MACD: Moving Average Convergence Divergence
  • CR: Energy Index (Intermediate Willingness Index)
  • WR: Williams Overbought/Oversold index
  • CCI: Commodity Channel Index
  • TR: True Range
  • ATR: Average True Range
  • DMA: Different of Moving Average (10, 50)
  • DMI: Directional Moving Index, including
    • +DI: Positive Directional Indicator
    • -DI: Negative Directional Indicator
    • ADX: Average Directional Movement Index
    • ADXR: Smoothed Moving Average of ADX
  • TRIX: Triple Exponential Moving Average
  • TEMA: Another Triple Exponential Moving Average
  • VR: Volume Variation Index
  • MFI: Money Flow Index
  • VWMA: Volume Weighted Moving Average
  • CHOP: Choppiness Index
  • KER: Kaufman's efficiency ratio
  • KAMA: Kaufman's Adaptive Moving Average
  • PPO: Percentage Price Oscillator
  • StochRSI: Stochastic RSI
  • WT: LazyBear's Wave Trend
  • Supertrend: with the Upper Band and Lower Band
  • Aroon: Aroon Oscillator
  • Z: Z-Score
  • AO: Awesome Oscillator
  • BOP: Balance of Power
  • MAD: Mean Absolute Deviation
  • ROC: Rate of Change
  • Coppock: Coppock Curve
  • Ichimoku: Ichimoku Cloud
  • CTI: Correlation Trend Indicator
  • LRMA: Linear Regression Moving Average
  • ERI: Elder-Ray Index
  • FTR: the Gaussian Fisher Transform Price Reversals indicator
  • RVGI: Relative Vigor Index
  • Inertia: Inertia Indicator
  • KST: Know Sure Thing
  • PGO: Pretty Good Oscillator
  • PSL: Psychological Line
  • PVO: Percentage Volume Oscillator
  • QQE: Quantitative Qualitative Estimation

Installation

pip install stockstats

Compatibility

The build checks the compatibility for the last two major releases of python3 and the last release of python2.

License

BSD-3-Clause License

Tutorial

Initialization

StockDataFrame works as a wrapper for the pandas.DataFrame. You need to Initialize the StockDataFrame with wrap or StockDataFrame.retype.

import pandas as pd
from stockstats import wrap

data = pd.read_csv('stock.csv')
df = wrap(data)

Formalize your data. This package takes for granted that your data is sorted by timestamp and contains certain columns. Please align your column name.

  • date: timestamp of the record, optional.
  • close: the close price of the period
  • high: the highest price of the interval
  • low: the lowest price of the interval
  • volume: the volume of stocks traded during the interval

Note these column names are case-insensitive. They are converted to lower case when you wrap the data frame.

By default, the date column is used as the index. Users can also specify the index column name in the wrap or retype function.

Example: DataFrame loaded from CSV.

          Date      Amount  Close   High    Low   Volume
0     20040817  90923240.0  11.20  12.21  11.03  7877900
1     20040818  52955668.0  10.29  10.90  10.29  5043200
2     20040819  32614676.0  10.53  10.65  10.30  3116800
...        ...         ...    ...    ...    ...      ...
2810  20160815  56416636.0  39.58  39.79  38.38  1436706
2811  20160816  68030472.0  39.66  40.86  39.00  1703600
2812  20160817  62536480.0  40.45  40.59  39.12  1567600

After conversion to StockDataFrame

              amount  close   high    low   volume
date
20040817  90923240.0  11.20  12.21  11.03  7877900
20040818  52955668.0  10.29  10.90  10.29  5043200
20040819  32614676.0  10.53  10.65  10.30  3116800
...              ...    ...    ...    ...      ...
20160815  56416636.0  39.58  39.79  38.38  1436706
20160816  68030472.0  39.66  40.86  39.00  1703600
20160817  62536480.0  40.45  40.59  39.12  1567600 

Use unwrap to convert it back to a pandas.DataFrame. Note that unwrap won't reset the columns and the index.

Access the Data

StockDataFrame is a subclass of pandas.DataFrame. All the functions of pandas.DataFrame should work the same as before.

Retrieve the data with symbol

We allow the user to access the statistics directly with some specified column name, such as kdjk, macd, rsi.

The values of these columns are calculated the first time you access them from the data frame. Please delete those columns first if you want the lib to re-evaluate them.

Retrieve the Series

Use macd = stock['macd'] or rsi = stock.get('rsi') to retrieve the Series.

Retrieve the symbol with 2 arguments

Some statistics need the column name and the window size, such as delta, shift, simple moving average, etc. Use this patter to retrieve them: <columnName>_<windowSize>_<statistics>

Examples:

  • 5 periods simple moving average of the high price: high_5_sma
  • 10 periods exponential moving average of the close: close_10_ema
  • 1 period delta of the high price: high_-1_d. The minus symbol means looking backward.

Retrieve the symbol with 1 argument

Some statistics require the window size but not the column name. Use this patter to specify your window: <statistics>_<windowSize>

Examples:

  • 6 periods RSI: rsi_6
  • 10 periods CCI: cci_10
  • 13 periods ATR: atr_13

Some of them have default windows. Check their document for detail.

Initialize all indicators with shortcuts

Some indicators, such as KDJ, BOLL, MFI, have shortcuts. Use df.init_all() to initialize all these indicators.

This operation generates lots of columns. Please use it with caution.

Statistics/Indicators

Some statistics have configurable parameters. They are class-level fields. Change of these fields is global. And they won't affect the existing results. Removing existing columns so that they will be re-evaluated the next time you access them.

Delta of Periods

Using pattern <column>_<window>_d to retrieve the delta between different periods.

You can also use <column>_delta as a shortcut to <column>_-1_d

Examples:

  • df['close_-1_d'] retrieves the close price delta between current and prev. period.
  • df['close_delta'] is the same as df['close_-1_d']
  • df['high_2_d'] retrieves the high price delta between current and 2 days later

Shift Periods

Shift the column backward or forward. It takes 2 parameters:

  • the name of the column to shift
  • periods to shift, can be negative

We fill the head and tail with the nearest data.

See the example below:

In [15]: df[['close', 'close_-1_s', 'close_2_s']]
Out[15]:
          close  close_-1_s  close_2_s
date
20040817  11.20       11.20      10.53
20040818  10.29       11.20      10.55
20040819  10.53       10.29      10.10
20040820  10.55       10.53      10.25
...         ...         ...        ...
20160812  39.10       38.70      39.66
20160815  39.58       39.10      40.45
20160816  39.66       39.58      40.45
20160817  40.45       39.66      40.45

[2813 rows x 3 columns]

RSI has a configurable window. The default window size is 14 which is configurable through set_dft_window('rsi', n). e.g.

  • df['rsi']: 14 periods RSI
  • df['rsi_6']: 6 periods RSI

Logarithmic return = ln( close / last close)

From wiki:

For example, if a stock is priced at 3.570 USD per share at the close on one day, and at 3.575 USD per share at the close the next day, then the logarithmic return is: ln(3.575/3.570) = 0.0014, or 0.14%.

Use df['log-ret'] to access this column.

Count of Non-Zero Value

Count non-zero values of a specific range. It requires a column and a window.

Examples:

  • Count how many typical prices are larger than close in the past 10 periods
In [22]: tp = df['middle']                             
                                                       
In [23]: df['res'] = df['middle'] > df['close']        
                                                       
In [24]: df[['middle', 'close', 'res', 'res_10_c']]    
Out[24]:                                               
             middle  close    res  res_10_c            
date                                                   
20040817  11.480000  11.20   True       1.0            
20040818  10.493333  10.29   True       2.0            
20040819  10.493333  10.53  False       2.0            
20040820  10.486667  10.55  False       2.0            
20040823  10.163333  10.10   True       3.0            
...             ...    ...    ...       ...            
20160811  38.703333  38.70   True       5.0            
20160812  38.916667  39.10  False       5.0            
20160815  39.250000  39.58  False       4.0            
20160816  39.840000  39.66   True       5.0            
20160817  40.053333  40.45  False       5.0            
                                                       
[2813 rows x 4 columns]                                
  • Count ups in the past 10 periods
In [26]: df['ups'], df['downs'] = df['change'] > 0, df['change'] < 0 
                                                                     
In [27]: df[['ups', 'ups_10_c', 'downs', 'downs_10_c']]              
Out[27]:                                                             
            ups  ups_10_c  downs  downs_10_c                         
date                                                                 
20040817  False       0.0  False         0.0                         
20040818  False       0.0   True         1.0                         
20040819   True       1.0  False         1.0                         
20040820   True       2.0  False         1.0                         
20040823  False       2.0   True         2.0                         
...         ...       ...    ...         ...                         
20160811  False       3.0   True         7.0                         
20160812   True       3.0  False         7.0                         
20160815   True       4.0  False         6.0                         
20160816   True       5.0  False         5.0                         
20160817   True       5.0  False         5.0                         
                                                                     
[2813 rows x 4 columns]                                              

Max and Min of the Periods

Retrieve the max/min value of specified periods. They require column and window.
Note the window does NOT simply stand for the rolling window.

Examples:

  • close_-3,2_max stands for the max of 2 periods later and 3 periods ago
  • close_-2~0_min stands for the min of 2 periods ago till now

RSV - Raw Stochastic Value

RSV is essential for calculating KDJ. It takes a window parameter. Use df['rsv'] or df['rsv_6'] to access it.

RSI chart the current and historical strength or weakness of a stock. It takes a window parameter.

The default window is 14. Use set_dft_window('rsi', n) to tune it.

Examples:

  • df['rsi']: retrieve the RSI of 14 periods
  • df['rsi_6']: retrieve the RSI of 6 periods

Stochastic RSI gives traders an idea of whether the current RSI value is overbought or oversold. It takes a window parameter.

The default window is 14. Use set_dft_window('stochrsi', n) to tune it.

Examples:

  • df['stochrsi']: retrieve the Stochastic RSI of 14 periods
  • df['stochrsi_6']: retrieve the Stochastic RSI of 6 periods

Retrieve the LazyBear's Wave Trend with df['wt1'] and df['wt2'].

Wave trend uses two parameters. You can tune them with set_dft_window('wt', (10, 21)).

SMMA - Smoothed Moving Average

It requires column and window.

For example, use df['close_7_smma'] to retrieve the 7 periods smoothed moving average of the close price.

The Price Rate of Change (ROC) is a momentum-based technical indicator that measures the percentage change in price between the current price and the price a certain number of periods ago.

Formular:

ROC = (PriceP - PricePn) / PricePn * 100

Where:

  • PriceP: the price of the current period
  • PricePn: the price of the n periods ago

You need a column name and a period to calculate ROC.

Examples:

  • df['close_10_roc']: the ROC of the close price in 10 periods
  • df['high_5_roc']: the ROC of the high price in 5 periods

The mean absolute deviation of a dataset is the average distance between each data point and the mean. It gives us an idea about the variability in a dataset.

Formular:

  1. Calculate the mean.
  2. Calculate how far away each data point is from the mean using positive distances. These are called absolute deviations.
  3. Add those deviations together.
  4. Divide the sum by the number of data points.

Example:

  • df['close_10_mad']: the MAD of the close price in 10 periods

The triple exponential average is used to identify oversold and overbought markets.

The algorithm is:

TRIX = (TripleEMA - LastTripleEMA) -  * 100 / LastTripleEMA
TripleEMA = EMA of EMA of EMA
LastTripleEMA =  TripleEMA of the last period

It requires column and window. By default, the column is close, the window is 12.

Use set_dft_window('trix', n) to change the default window.

Examples:

  • df['trix'] stands for 12 periods Trix for the close price.
  • df['middle_10_trix'] stands for the 10 periods Trix for the typical price.

Tema is another implementation for the triple exponential moving average.

TEMA=(3 x EMA) - (3 x EMA of EMA) + (EMA of EMA of EMA)

It takes two parameters, column and window. By default, the column is close, the window is 5.

Use set_dft_window('tema', n) to change the default window.

Examples:

  • df['tema'] stands for 12 periods TEMA for the close price.
  • df['middle_10_tema'] stands for the 10 periods TEMA for the typical price.

It is the strength index of the trading volume.

It has a default window of 26. Change it with set_dft_window('vr', n).

Examples:

  • df['vr'] retrieves the 26 periods VR.
  • df['vr_6'] retrieves the 6 periods VR.

Williams Overbought/Oversold index is a type of momentum indicator that moves between 0 and -100 and measures overbought and oversold levels.

It takes a window parameter. The default window is 14. Use set_dft_window('wr', n) to change the default window.

Examples:

  • df['wr'] retrieves the 14 periods WR.
  • df['wr_6'] retrieves the 6 periods WR.

CCI stands for Commodity Channel Index.

It requires a window parameter. The default window is 14. Use set_dft_window('cci', n) to change it.

Examples:

  • df['cci'] retrieves the default 14 periods CCI.
  • df['cci_6'] retrieves the 6 periods CCI.

TR - True Range of Trading

TR is a measure of the volatility of a High-Low-Close series. It is used for calculating the ATR.

The Average True Range is an N-period smoothed moving average (SMMA) of the true range value.
Default to 14 periods.

Users can modify the default window with set_dft_window('atr', n).

Example:

  • df['atr'] retrieves the 14 periods ATR.
  • df['atr_5'] retrieves the 5 periods ATR.

Supertrend indicates the current trend.
We use the algorithm described here. It includes 3 lines:

  • df['supertrend'] is the trend line.
  • df['supertrend_ub'] is the upper band of the trend
  • df['supertrend_lb'] is the lower band of the trend

It has 2 parameters:

  • StockDataFrame.SUPERTREND_MUL is the multiplier of the band, default to 3.
  • the default window size is 14. Change it with set_dft_window('supertrend', n)

DMA - Difference of Moving Average

df['dma'] retrieves the difference of 10 periods SMA of the close price and the 50 periods SMA of the close price.

The directional movement index (DMI) identifies in which direction the price of an asset is moving.

It has several lines:

  • df['pdi'] is the positive directional movement line (+DI)
  • df['ndi'] is the negative directional movement line (-DI)
  • df['dx'] is the directional index (DX)
  • df['adx'] is the average directional index (ADX)
  • df['adxr'] is an EMA for ADX

It has several parameters.

  • default window for +DI is 14, change it with set_dft_window('pdi', n)
  • default window for -DI is 14, change it with set_dft_window('ndi', n)
  • StockDataFrame.DX_SMMA - window for DX, default to 14
  • StockDataFrame.ADX_EMA - window for ADX, default to 6
  • StockDataFrame.ADXR_EMA - window for ADXR, default to 6

The stochastic oscillator is a momenxtum indicator that uses support and resistance levels.

It includes three lines:

  • df['kdjk'] - K series
  • df['kdjd'] - D series
  • df['kdjj'] - J series

The default window is 9. Use set_dft_window('kdjk', n) to change it. Use df['kdjk_6'] to retrieve the K series of 6 periods.

KDJ also has two configurable parameters named StockDataFrame.KDJ_PARAM. The default value is (2.0/3.0, 1.0/3.0)

The Energy Index (Intermediate Willingness Index) uses the relationship between the highest price, the lowest price and yesterday's middle price to reflect the market's willingness to buy and sell.

It contains 4 lines:

  • df['cr'] - the CR line
  • df['cr-ma1'] - StockDataFrame.CR_MA[0] periods of the CR moving average, the default window is 5
  • df['cr-ma2'] - StockDataFrame.CR_MA[1] periods of the CR moving average, the default window is 10
  • df['cr-ma3'] - StockDataFrame.CR_MA[2] periods of the CR moving average, the default window is 20

It's the average of high, low and close. Use df['middle'] to access this value.

When amount is available, middle = amount / volume This should be more accurate because amount represents the total cash flow.

The Bollinger bands includes three lines

  • df['boll'] is the baseline
  • df['boll_ub'] is the upper band
  • df['boll_lb'] is the lower band

The default window of boll is 20. You can also supply your window with df['boll_10']. It will also generate the boll_ub_10 and boll_lb_10 column.

The default period of the Bollinger Band can be changed with set_dft_window('boll', n). The width of the bands can be turned with StockDataFrame.BOLL_STD_TIMES. The default value is 2.

We use the close price to calculate the MACD lines.

  • df['macd'] is the difference between two exponential moving averages.
  • df['macds] is the signal line.
  • df['macdh'] is he histogram line.

The period of short, long EMA and signal line can be tuned with set_dft_window('macd', (short, long, signal)). The default windows are 12 and 26 and 9.

The Percentage Price Oscillator includes three lines.

  • df['ppo'] derives from the difference of 2 exponential moving average.
  • df['ppos] is the signal line.
  • df['ppoh'] is he histogram line.

The period of short, long EMA and signal line can be tuned with set_dft_window('ppo', (short, long, signal)). The default windows are 12 and 26 and 9.

Follow the pattern <columnName>_<window>_sma to retrieve a simple moving average.

Follow the pattern <columnName>_<window>_mstd to retrieve the moving STD.

Follow the pattern <columnName>_<window>_mvar to retrieve the moving VAR.

It's the moving average weighted by volume.

It has a parameter for window size. The default window is 14. Change it with set_dft_window('vwma', n).

Examples:

  • df['vwma'] retrieves the 14 periods VWMA
  • df['vwma_6'] retrieves the 6 periods VWMA

The Choppiness Index determines if the market is choppy.

It has a parameter for window size. The default window is 14. Change it with set_dft_window('chop', n).

Examples:

  • df['chop'] retrieves the 14 periods CHOP
  • df['chop_6'] retrieves the 6 periods CHOP

The Money Flow Index identifies overbought or oversold signals in an asset.

It has a parameter for window size. The default window is 14. Change it with set_dft_window('mfi', n).

Examples:

  • df['mfi'] retrieves the 14 periods MFI
  • df['mfi_6'] retrieves the 6 periods MFI

The Elder-Ray Index contains the bull and the bear power. Both are calculated based on the EMA of the close price.

The default window is 13.

Formular:

  • Bulls Power = High - EMA
  • Bears Power = Low - EMA
  • EMA is exponential moving average of close of N periods

Examples:

  • df['eribull'] retrieves the 13 periods bull power
  • df['eribear'] retrieves the 13 periods bear power
  • df['eribull_5'] retrieves the 5 periods bull power
  • df['eribear_5'] retrieves the 5 periods bear power

The Efficiency Ratio (ER) is calculated by dividing the price change over a period by the absolute sum of the price movements that occurred to achieve that change.

The resulting ratio ranges between 0 and 1 with higher values representing a more efficient or trending market.

The default column is close.

The default window is 10.

Formular:

  • window_change = ABS(close - close[n])
  • last_change = ABS(close-close[1])
  • volatility = moving sum of last_change in n
  • KER = window_change / volatility

Examples:

  • df['ker'] retrieves the 10 periods KER of the close price
  • df['high_5_ker'] retrieves 5 periods KER of the high price

Kaufman's Adaptive Moving Average is designed to account for market noise or volatility.

It has 2 optional parameters and 2 required parameters

  • fast - optional, the parameter for fast EMA smoothing, default to 5
  • slow - optional, the parameter for slow EMA smoothing, default to 34
  • column - required, the column to calculate
  • window - required, rolling window size

The default value for window, fast and slow can be configured with set_dft_window('kama', (10, 5, 34))

Examples:

  • df['close_10,2,30_kama'] retrieves 10 periods KAMA of the close price with fast = 2 and slow = 30
  • df['close_2_kama'] retrieves 2 periods KAMA of the close price with default fast and slow

Cross Upwards and Cross Downwards

Use the pattern <A>_xu_<B> to check when A crosses up B.

Use the pattern <A>_xd_<B> to check when A crosses down B.

Use the pattern <A>_x_<B> to check when A crosses B.

Examples:

  • kdjk_x_kdjd returns a series that marks the cross of KDJK and KDJD
  • kdjk_xu_kdjd returns a series that marks where KDJK crosses up KDJD
  • kdjk_xd_kdjd returns a series that marks where KDJD crosses down KDJD

The Aroon Oscillator measures the strength of a trend and the likelihood that it will continue.

The default window is 25.

  • Aroon Oscillator = Aroon Up - Aroon Down
  • Aroon Up = 100 * (n - periods since n-period high) / n
  • Aroon Down = 100 * (n - periods since n-period low) / n
  • n = window size

Examples:

  • df['aroon'] returns Aroon oscillator with a window of 25
  • df['aroon_14'] returns Aroon oscillator with a window of 14

Z-score is a statistical measurement that describes a value's relationship to the mean of a group of values.

There is no default column name or window for Z-Score.

The statistical formula for a value's z-score is calculated using the following formula:

z = ( x - μ ) / σ

Where:

  • z = Z-score
  • x = the value being evaluated
  • μ = the mean
  • σ = the standard deviation

Examples:

  • df['close_75_z'] returns the Z-Score of close price with a window of 75

The AO indicator is a good indicator for measuring the market dynamics, it reflects specific changes in the driving force of the market, which helps to identify the strength of the trend, including the points of its formation and reversal.

Awesome Oscillator Formula

  • MEDIAN PRICE = (HIGH+LOW)/2
  • AO = SMA(MEDIAN PRICE, 5)-SMA(MEDIAN PRICE, 34)

Examples:

  • df['ao'] returns the Awesome Oscillator with default windows (5, 34)
  • df['ao_3,10'] returns the Awesome Oscillator with a window of 3 and 10

Balance of Power (BOP) measures the strength of the bulls vs. bears.

Formular:

BOP = (close - open) / (high - low)

Example:

  • df['bop'] returns the Balance of Power

The Chande Momentum Oscillator (CMO) is a technical momentum indicator developed by Tushar Chande.

The formula calculates the difference between the sum of recent gains and the sum of recent losses and then divides the result by the sum of all price movements over the same period.

The default window is 14.

Formular:

CMO = 100 * ((sH - sL) / (sH + sL))

where:

  • sH=the sum of higher closes over N periods
  • sL=the sum of lower closes of N periods

Examples:

  • df['cmo'] returns the CMO with a window of 14
  • df['cmo_5'] returns the CMO with a window of 5

Coppock Curve is a momentum indicator that signals long-term trend reversals.

Formular:

Coppock Curve = 10-period WMA of (14-period RoC + 11-period RoC) WMA = Weighted Moving Average RoC = Rate-of-Change

Examples:

  • df['coppock'] returns the Coppock Curve with default windows
  • df['coppock_5,10,15'] returns the Coppock Curve with WMA window 5, fast window 10, slow window 15.

The Ichimoku Cloud is a collection of technical indicators that show support and resistance levels, as well as momentum and trend direction.

In this implementation, we only calculate the delta between lead A and lead B (which is the width of the cloud).

It contains three windows:

  • window for the conversion line, default to 9
  • window for the baseline and the shifts, default to 26
  • window for the leading line, default to 52

Formular:

  • conversion line = (PH9 + PL9) / 2
  • baseline = (PH26 + PL26) / 2
  • leading span A = (conversion line + baseline) / 2
  • leading span B = (PH52 + PL52) / 2
  • result = leading span A - leading span B

Where:

  • PH = Period High
  • PL = Period Low

Examples:

  • df['ichimoku'] returns the ichimoku cloud width with default windows
  • df['ichimoku_7,22,44'] returns the ichimoku cloud width with window sizes 7, 22, 44

Linear regression works by taking various data points in a sample and providing a “best fit” line to match the general trend in the data.

Implementation reference:

https://github.com/twopirllc/pandas-ta/blob/main/pandas_ta/overlap/linreg.py

Examples:

  • df['close_10_lrma'] linear regression of close price with window size 10

Correlation Trend Indicator is a study that estimates the current direction and strength of a trend.

Implementation is based on the following code:

https://github.com/twopirllc/pandas-ta/blob/main/pandas_ta/momentum/cti.py

Examples:

  • df['cti'] returns the CTI of close price with window 12
  • df['high_5_cti'] returns the CTI of high price with window 5

The Gaussian Fisher Transform Price Reversals indicator, dubbed FTR for short, is a stat based price reversal detection indicator inspired by and based on the work of the electrical engineer now private trader John F. Ehlers.

https://www.tradingview.com/script/ajZT2tZo-Gaussian-Fisher-Transform-Price-Reversals-FTR/

Implementation reference:

https://github.com/twopirllc/pandas-ta/blob/084dbe1c4b76082f383fa3029270ea9ac35e4dc7/pandas_ta/momentum/fisher.py#L9

Formular:

  • Fisher Transform = 0.5 * ln((1 + X) / (1 - X))
  • X is a series whose values are between -1 to 1

Examples:

  • df['ftr'] returns the FTR with window 9
  • df['ftr_20'] returns the FTR with window 20

The Relative Vigor Index (RVI) is a momentum indicator used in technical analysis that measures the strength of a trend by comparing a security's closing price to its trading range while smoothing the results using a simple moving average (SMA).

Formular

  • NUMERATOR= (a+(2×b)+(2×c)+d) / 6
  • DENOMINATOR= (e+(2×f)+(2×g)+h) / 6
  • RVI= SMA-N of DENOMINATOR / SMA-N of NUMERATOR
  • Signal Line = (RVI+(2×i)+(2×j)+k) / 6

where:

  • a=Close−Open
  • b=Close−Open One Bar Prior to a
  • c=Close−Open One Bar Prior to b
  • d=Close−Open One Bar Prior to c
  • e=High−Low of Bar a
  • f=High−Low of Bar b
  • g=High−Low of Bar c
  • h=High−Low of Bar d
  • i=RVI Value One Bar Prior
  • j=RVI Value One Bar Prior to i
  • k=RVI Value One Bar Prior to j
  • N=Minutes/Hours/Days/Weeks/Months

Examples:

  • df['rvgi'] retrieves the RVGI line of window 14
  • df['rvgis'] retrieves the RVGI signal line of window 14
  • df['rvgi_5'] retrieves the RVGI line of window 5
  • df['rvgis_5'] retrieves the RVGI signal line of window 5

In financial markets, the concept of inertia was given by Donald Dorsey in the 1995 issue of Technical Analysis of Stocks and Commodities through the Inertia Indicator. The Inertia Indicator is moment-based and is an extension of Dorsey’s Relative Volatility Index (RVI).

Formular:

  • inertia = n periods linear regression of RVGI

Examples:

  • df['inertia'] retrieves the inertia of 20 periods linear regression of 14 periods RVGI
  • df['inertia_10'] retrieves the inertia of 10 periods linear regression of 14 periods RVGI

The Know Sure Thing (KST) is a momentum oscillator developed by Martin Pring to make rate-of-change readings easier for traders to interpret.

Formular:

  • KST=(RCMA1×1)+(RCMA2×2) + (RCMA3×3)+(RCMA4×4)

Where:

  • RCMA1=10-period SMA of 10-period ROC
  • RCMA2=10-period SMA of 15-period ROC
  • RCMA3=10-period SMA of 20-period ROC
  • RCMA4=15-period SMA of 30-period ROC

Example:

  • df['kst'] retrieves the KST.

The Pretty Good Oscillator indicator by Mark Johnson measures the distance of the current close from its N-day simple moving average, expressed in terms of an average true range over a similar period.

Formular:

  • PGO = (Close - SMA) / (EMA of TR)

Example:

  • df['pgo'] retrieves the PGO with default window 14.
  • df['pgo_10'] retrieves the PGO with window 10.

The Psychological Line indicator is the ratio of the number of rising periods over the total number of periods.

Formular:

  • PSL = (Number of Rising Periods) / (Total Number of Periods) * 100

Example:

  • df['psl'] retrieves the PSL with default window 12.
  • df['psl_10'] retrieves the PSL with window 10.
  • df['high_12_psl'] retrieves the PSL of high price with window 10.

The Percentage Volume Oscillator (PVO) is a momentum oscillator for volume. The PVO measures the difference between two volume-based moving averages as a percentage of the larger moving average.

Formular:

  • Percentage Volume Oscillator (PVO): ((12-day EMA of Volume - 26-day EMA of Volume)/26-day EMA of Volume) x 100
  • Signal Line: 9-day EMA of PVO
  • PVO Histogram: PVO - Signal Line

Example:

  • df['pvo'] derives from the difference of 2 exponential moving average.
  • df['pvos] is the signal line.
  • df['pvoh'] is he histogram line.

The period of short, long EMA and signal line can be tuned with set_dft_window('pvo', (short, long, signal)). The default windows are 12 and 26 and 9.

The Qualitative Quantitative Estimation (QQE) indicator works like a smoother version of the popular Relative Strength Index (RSI) indicator. QQE expands on RSI by adding two volatility based trailing stop lines. These trailing stop lines are composed of a fast and a slow moving Average True Range (ATR). These ATR lines are smoothed making this indicator less susceptible to short term volatility.

Implementation reference: https://github.com/twopirllc/pandas-ta/blob/main/pandas_ta/momentum/qqe.py

Example:

  • df['qqe'] retrieves the QQE with RSI window 14, MA window 5.
  • df['qqel'] retrieves the QQE long
  • df['qqes'] retrieves the QQE short
  • df['qqe_10,4'] retrieves the QQE with RSI window 10, MA window 4
  • df['qqel_10,4'] retrieves the QQE long with customized windows. Initialized by retrieving df['qqe_10,4']
  • df['qqes_10,4'] retrieves the QQE short with customized windows Initialized by retrieving df['qqe_10,4']

The period of short, long EMA and signal line can be tuned with set_dft_window('qqe', (rsi, rsi_ma)). The default windows are 14 and 5.

Issues

We use Github Issues to track the issues or bugs.

Others

MACDH Note:

In July 2017 the code for MACDH was changed to drop an extra 2x multiplier on the final value to align better with calculation methods used in tools like cryptowatch, tradingview, etc.

Contact author:

stockstats's People

Contributors

fniko avatar gleammerray avatar jealous avatar jhmenke avatar kingslayer2205 avatar mcowger avatar royopa avatar shaunscott12 avatar xandrade avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

stockstats's Issues

Can't get DMI

Traceback (most recent call last):
  File "bollinger.py", line 150, in <module>
    main()
  File "bollinger.py", line 146, in main
    print(execute(symbol))
  File "bollinger.py", line 74, in execute
    sdf['pdi'] = sdf._get_pdi(sdf, 14)
  File "/Library/Python/2.7/site-packages/stockstats.py", line 545, in _get_pdi
    df[pdi_column] = df[pdm_column] / df[tr_column] * 100
  File "/Library/Python/2.7/site-packages/stockstats.py", line 925, in __getitem__
    self.init_columns(self, item)
  File "/Library/Python/2.7/site-packages/stockstats.py", line 858, in init_columns
    StockDataFrame.__init_column(obj, columns)
  File "/Library/Python/2.7/site-packages/stockstats.py", line 917, in __init_column
    StockDataFrame.__init_not_exist_column(df, key)
  File "/Library/Python/2.7/site-packages/stockstats.py", line 909, in __init_not_exist_column
    getattr(cls, func_name)(df, r)
  File "/Library/Python/2.7/site-packages/stockstats.py", line 480, in _get_pdm
    um, dm = df['um'], df['dm']
  File "/Library/Python/2.7/site-packages/stockstats.py", line 925, in __getitem__
    self.init_columns(self, item)
  File "/Library/Python/2.7/site-packages/stockstats.py", line 858, in init_columns
    StockDataFrame.__init_column(obj, columns)
  File "/Library/Python/2.7/site-packages/stockstats.py", line 917, in __init_column
    StockDataFrame.__init_not_exist_column(df, key)
  File "/Library/Python/2.7/site-packages/stockstats.py", line 883, in __init_not_exist_column
    cls._get_um_dm(df)
  File "/Library/Python/2.7/site-packages/stockstats.py", line 464, in _get_um_dm
    hd = df['high_delta']
  File "/Library/Python/2.7/site-packages/stockstats.py", line 925, in __getitem__
    self.init_columns(self, item)
  File "/Library/Python/2.7/site-packages/stockstats.py", line 858, in init_columns
    StockDataFrame.__init_column(obj, columns)
  File "/Library/Python/2.7/site-packages/stockstats.py", line 917, in __init_column
    StockDataFrame.__init_not_exist_column(df, key)
  File "/Library/Python/2.7/site-packages/stockstats.py", line 895, in __init_not_exist_column
    cls._get_delta(df, key)
  File "/Library/Python/2.7/site-packages/stockstats.py", line 831, in _get_delta
    df[key] = df[key_to_delta].diff()
  File "/Library/Python/2.7/site-packages/pandas/core/series.py", line 1412, in diff
    result = algorithms.diff(_values_from_object(self), periods)
  File "/Library/Python/2.7/site-packages/pandas/core/algorithms.py", line 1635, in diff
    out_arr[res_indexer] = arr[res_indexer] - arr[lag_indexer]
TypeError: unsupported operand type(s) for -: 'unicode' and 'unicode'
sdf['pdi'] = sdf.get('pdi')

Issue retrieving rsi_14

I get the following error when I try to retrieve rsi_14. My dataframe looks like this:

          open      high       low     close    volume

date
20191113 261.1300 264.7800 261.0700 264.4200 20815595
20191112 261.5500 262.7900 260.9200 261.9600 21826100
20191111 258.3000 262.4700 258.2800 262.2000 20455300
20191108 258.6900 260.4400 256.8500 260.1400 17496600
20191107 258.7400 260.3500 258.1100 259.4300 23735100
... ... ... ... ... ...
20190701 203.1700 204.4900 200.6500 201.5500 27253000
20190628 198.6800 199.4950 197.0500 197.9200 31110600
20190627 200.2900 201.5700 199.5700 199.7400 20899700
20190626 197.7700 200.9900 197.3494 199.8000 26067500
20190625 198.4300 199.2600 195.2900 195.5700 21070300

FutureWarning: Currently, 'apply' passes the values as ndarrays (raw=False)

anaconda3/lib/python3.6/site-packages/stockstats.py:387: FutureWarning: Currently, 'apply' passes the values as ndarrays to the applied function. In the future, this will change to passing it as Series objects. You need to specify 'raw=True' to keep the current behaviour, and you can pass 'raw=False' to silence this warning
lambda x: np.fabs(x - x.mean()).mean())

Obviously, this is a quick fix. Has anyone else encountered it and is there a patch so we can pip install -u stockstats to resolve this issue?

Indicators limited by hardcoded periods

MACD should not have hardcoded EMA periods- they need to be variable inputs.

Infact none of these functions should have hardcoded periods, it's severely limits the usefulness of an otherwise stellar wrapper.

Append new data

Hi! I was wondering if there's a method to append new data. I would like to do something like that:

new_frame = pd.DataFrame(data=[[date, open, high, low, close]], columns=['DateTime', 'Open', 'High', 'Low', 'Close'])
Data = Data.append(new_frame, ignore_index=True)

This code will throw you an exception because you have to marge the different columns. But, it would be great if we just required to add the OHLC data so that the StockDataFrame automatically recalculates the statistics and indicators.
Are there different alternatives to append new data?

Thanks in advance,

Request method to pull earnings per share

I use this package. Ultimately I want to train neural nets with the data. Would really like to see an incorporation of the basic datas: P/E , EPS, P/B, PEG, etc. I can get this from yahoo finance (I think). But would like a one stop shop for all may data in a common format. Willing to help, but would need to be brought up to speed with the data frames.

Generating indicators for different frequencies

Just came across your repo and it looks very promising for what I'm doing! Really impressive that you generated all of these indicators.

It would be great to be able to generate indicators for different frequencies - month, week, hour, minute. Have you looked into this at all? This would be useful for day trading. Then users could do:

dataframe.get('rsi_1_H')
dataframe.get('rsi_30_min')
dataframe.get('rsi_15_min')

It could be a backwards compatible change in that if no frequency is specified, daily frequency is used. Otherwise specify the frequency using the Pandas offset alias format.

The data frame would have to have the highest frequency asked for (minute-level for minute frequency, etc.). The library would have to be able to determine the frequency of the data,
and resample the data to generate indicators for the higher time series.

I can look into this more also if you can offer thoughts on how feasible you think this is and what pieces of code I should look at.

rsi doesn't seem to work with pandas 1.0.0

Hi,

Pandas recently released 1.0.0 and since then, rsi calculations seem to run into KeyError for some reason.

Here's very simple sample code:

#!/usr/bin/env python3

import pandas_datareader.data as web
from stockstats import StockDataFrame

spx = web.DataReader('^GSPC', 'yahoo')
dataframe = StockDataFrame.retype(spx)
print(dataframe['rsi_14']['2020-01-31'])

With pandas 0.25.3, it works fine:

Successfully installed certifi-2019.11.28 chardet-3.0.4 idna-2.8 int-date-0.1.8 lxml-4.5.0 numpy-1.18.1 pandas-0.25.3 pandas-datareader-0.8.1 python-dateutil-2.8.1 pytz-2019.3 requests-2.22.0 six-1.14.0 stockstats-0.3.0 urllib3-1.25.8
$ ./test.py
venv/lib/python3.6/site-packages/pandas/core/indexing.py:205: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._setitem_with_indexer(indexer, value)
43.43522572721968

However, with pandas 1.0.0, KeyError occurs:

Successfully installed certifi-2019.11.28 chardet-3.0.4 idna-2.8 int-date-0.1.8 lxml-4.5.0 numpy-1.18.1 pandas-1.0.0 pandas-datareader-0.8.1 python-dateutil-2.8.1 pytz-2019.3 requests-2.22.0 six-1.14.0 stockstats-0.3.0 urllib3-1.25.8
$ ./test.py 
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._setitem_with_indexer(indexer, value)
Traceback (most recent call last):
  File "venv/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2646, in get_loc
    return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1622, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'rsi_14'

The problem could be on pandas side, though they bumped the major version so I suspect stockstats might want to support retyping of pandas 1.0.0 and on as well.

Cant get EMA

Hi, thanks for your contribution.
I was running the package and failed to get EMA with both

stock.get("ema")
and
stock.get("EMA")

May I know how you get this?

Get all indicators

I think this would be an interesting feature that I'd be willing to help out with.

Ideally you could call a function similar to df.get_stock_stats() that returns a data frame with every single indicator.

any advice?

is TEMA and TRIX adjustable?

are TEMA and TRIX adjustable like other metrics such as .get('tema_20') || .get('trix_35')

Couldn't find on stackoverflow or any other forums so I am at the source : D

Awesome lib!

speeding up kdj

Hi,

kdj is terribly slow, probably because the result is created step by step with list.append().
I did some speed tuning which gives about 60 times speed up, code see below.
I tried to vectorize as much as possible and to have as little array accesses as possible.
Works only with python 3 because I used nonlocal in the inline function.
I guess it could be made even faster with something like scipy.signal.lfilter.

    @classmethod
    def _get_kdjk(cls, df, n_days):
        """ Get the K of KDJ
        Overwrite stockstats.StockDataFrame methods to speed up.

        K = 2/3 × (prev. K) +1/3 × (curr. RSV)
        2/3 and 1/3 are the smooth parameters.
        :param df: data
        :param n_days: calculation range
        :return: None
        """
        rsv_column = 'rsv_{}'.format(n_days)
        k_column = 'kdjk_{}'.format(n_days)

        KDJ_PARAM0, KDJ_PARAM1 = cls.KDJ_PARAM
        prev_k = 50.0

        def __add(t):
            nonlocal prev_k
            prev_k = KDJ_PARAM0 * prev_k + t
            return prev_k

        df[k_column] = [__add(t) for t in KDJ_PARAM1 * df.get(rsv_column).as_matrix()]

    @classmethod
    def _get_kdjd(cls, df, n_days):
        """ Get the D of KDJ

        D = 2/3 × (prev. D) +1/3 × (curr. K)
        2/3 and 1/3 are the smooth parameters.
        :param df: data
        :param n_days: calculation range
        :return: None
        """
        k_column = 'kdjk_{}'.format(n_days)
        d_column = 'kdjd_{}'.format(n_days)

        KDJ_PARAM0, KDJ_PARAM1 = cls.KDJ_PARAM
        prev_d = 50.0

        def __add(t):
            nonlocal prev_d
            prev_d = KDJ_PARAM0 * prev_d + t
            return prev_d

        df[d_column] = [__add(t) for t in KDJ_PARAM1 * df.get(k_column).as_matrix()]

WR result is positive

WR results a positive number. Should the formula be multiplied with -100 rather than 100?

vwap

hi there. is there any indicator that can be used instead of VWAP (Volume-weighted average price) ?

[SettingWithCopyWarning] for getting ATR

my code:

    @classmethod
    def get_atr(cls, candles):
        stock = StockDataFrame.retype(TCandle.to_df(candles))
        return list(stock.get('atr'))

I got the following warning:

  /Users/yurenji/.conda/envs/tangle/lib/python3.6/site-packages/pandas/core/indexing.py:189: SettingWithCopyWarning: 
  A value is trying to be set on a copy of a slice from a DataFrame
  
  See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
    self._setitem_with_indexer(indexer, value)

-- Docs: http://doc.pytest.org/en/latest/warnings.html

I tried 'macdh' and 'sma', they don't have this issue.

About ADX

Talking about the ADX index, I checked several programs and found the ADXs are all different. For stockstats, the ADX looks a bit higher than others. I am quite confused about this index calculation.

MACDH seems to have extraneous 2x?

Looking at the various definitions I can find on the MACD histogram, Everything I can find simply defines it as the difference between the MACD and the 9 period signal line. Various tools like tradingview, cryptowatch, etc. use this as the definition, as does investopedia.

However in comparing the data I get from stockstats, it appears that a doubling modifier is added:

df['macdh'] = 2 * (df['macd'] - df['macds'])

Why is that? I can't find anything to support such a formula. If you agree, I'm happy to submit my PR.

SettingWithCopyWarning when calculating RSI

Hi,
When I use the rsi indicator pandas gives me this warning:

/home/martin/PycharmProjects/test/venv/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._setitem_with_indexer(indexer, value)

The code:

import stockstats
import pandas as pd


def test(csv, indicator):
    sdf = stockstats.StockDataFrame.retype(pd.read_csv(csv))
    value = list(sdf.loc[:, indicator])


test("BTC-USD.csv", "rsi_14")

BTC-USD.csv:

Data,Low,High,Open,Close,Volume
1581857700,9906.32,9917.98,9915.37,9917.98,3.66869252
1581857760,9917.45,9926.23,9917.45,9924.99,2.95763819
1581857820,9925.13,9927,9925.13,9926.58,0.2517662
1581857880,9922.74,9926.93,9926.59,9923.85,0.16580884
1581857940,9922.33,9923.85,9923.85,9923.8,0.75820216
1581858000,9919.35,9923.8,9923.8,9920.32,3.27751825
1581858060,9910.68,9919.35,9919.35,9910.68,0.27416278
1581858120,9906.32,9914.46,9910.68,9906.46,0.77074594
1581858180,9906.32,9906.39,9906.38,9906.33,0.09115752
1581858240,9891,9905,9905,9891,3.82049405
1581858300,9891,9905.95,9893.8,9903.12,5.00440502
1581858360,9900.53,9910.95,9903.12,9905.35,5.5605933
1581858420,9901.92,9903.36,9903.36,9903.19,0.21576107
1581858480,9903.19,9903.64,9903.19,9903.56,0.34238175
1581858540,9897.87,9901.04,9901.04,9898.3,4.69393022
1581858600,9884.31,9898.64,9898.63,9887.94,3.21781626
1581858660,9884,9899.49,9884.01,9898.27,7.51834045
1581858720,9871.02,9899.49,9898.28,9882.2,34.62721212
1581858780,9875.24,9891.88,9882.21,9885,4.54068467
1581858840,9883.78,9886.09,9885.01,9885.41,2.93844096
1581858900,9885.64,9908.07,9885.64,9903.08,6.1523356
1581858960,9896.28,9900,9900,9896.28,1.45623205
1581859020,9896.28,9907.83,9896.28,9907.83,3.23573508
1581859080,9905.94,9907.85,9905.97,9905.95,0.35000316
1581859140,9897.68,9905.95,9905.95,9897.68,0.9411693
1581859200,9895,9896.04,9895.27,9895.11,0.17042081
1581859260,9886.43,9895.11,9895.11,9886.43,1.52060855
1581859320,9878.51,9889.2,9886.42,9886.32,7.34562765
1581859380,9878.5,9888.37,9888.37,9880.19,1.2896627

pandas is 1.0.1 and stockstats is 0.3.1
I know it's just a warning, but I hope it would be fixed.

tips for fixing [SettingWithCopyWarning] and suggestion for MACDH warning

In def _get_s, change the last line of code (StockDataFrame.set_nan(df[shifted_key], shift)) to

a=df[shifted_key].copy()
StockDataFrame.set_nan(a, shift)
df[shifted_key]=a.copy()

Same for def _get_d, change the last line of code (StockDataFrame.set_nan(df[column_name], shift)) to

a=df[column_name].copy()
StockDataFrame.set_nan(a, shift)
df[column_name]=a.copy()

Should be the same for def _get_p, but I don't know which indicators are using this function, so I didn't test it. should change the last two lines of code:

StockDataFrame.set_nan(indices, shifts)
df[column_name] = indices

Also suggest to provide a flag to disable MACDH warning.

Question about _p option

Could you please explain to me what something like this would do:
close_-3,-1,+2_p
open_-1_d_-1,-3_p

I looked at your tests, the main .py file and for the life of mine, can't figure out what _p does.

Please advise.

be able to use "retype" with multi-index dataframe indexed with 'ticker' and 'date'

this is more a feature request.

Basically, it's really easy to pull these bulk download sheets of daily stock data from sources like quandl that have the ticker as a column, then the date for each line of data. If I want to use stockstats properly to calculate DMI or whatever, it looks like I need to use a for loop to iterate through each stock one by one. It'd be much cooler if I could simply use 'retype()' on a dataframe indexed with both ticker and date. I am actually able to do this now (surprisingly), and it runs very quickly even over 15k tickers spanning several years, but the problem is the 14 period moving averages and things like that seem to use the previous stock's data. Since I'm looking at several years of data, this is only an issue for stocks that IPO'd recently, but it would still be nice if the code had some 'if' statements or whatever to check whether a line should be included in the moving average calculations when used this way. It'd be a cool way to make this even easier to use, in my opinion.

How to install / setup up stockstats

Hey,

Wondering if somebody could help a guy out in getting this installed and setup up to use? I'm pretty new at this so any advice would be wonderful.

I have python installed, downloaded the files and I researched that I can get the "pandas.DataFrame" through Anaconda. Not sure where to go from here.

Thanks

AMA indicator in stockstats

Hi,

Is there an Adaptive Moving Average (AMA) indicator in stockstats, which means a moving median for a said period?

Thanks

Cross lines indicator error

Hi,
What a wonderful tool you have created!
I'm facing the following error using cross line indicator:

_ = chart['close_x_close_30_ema']

C:\Users\grafeer\AppData\Local\Continuum\Anaconda3\lib\site-packages\numpy\lib\function_base.py in diff(a, n, axis)

   1924         return diff(a[slice1]-a[slice2], n-1, axis=axis)
   1925     else:
-> 1926         return a[slice1]-a[slice2]
   1927 
   1928 

TypeError: numpy boolean subtract, the - operator, is deprecated, use the bitwise_xor, the ^ operator, or the logical_xor function instead.

I'm using numpy 1.13.0 version

MACD histogram

Why is the MACD histogram being multiplied by 2?:

df['macdh'] = 2 * (df['macd'] - df['macds'])

Every time I see it explained, it's defined as the difference between the MACD line and the signal line.
Thanks

MACDH doubled?

Is there a reason why the MACD histogram value is doubled? From the resources I've seen and the trading client I use, it seems to be calculated as only the difference instead of double the difference.

(And thanks for your work on this library! It has saved me quite a bit of work on a side project of mine.)

Decimal precision of the results

Hello everyone. I am trying to use Stockstats to analyze data from Cryptocurrencies which have 8 decimals positions.

The results only use a precision of 6 decimals. Is there a way to configure stockstats to use more decimals?

If I convert the original data to float it reduces the precision to 6.

Imported Data (Pandas):
datetime open high low close
0 2018-01-22 21:32:00 0.00000632 0.00000633 0.00000631 0.00000632
1 2018-01-22 21:33:00 0.00000631 0.00000632 0.00000631 0.00000631
2 2018-01-22 21:34:00 0.00000631 0.00000632 0.00000631 0.00000632

dtype: object
datetime datetime64[ns]
open object
high object
low object
close object

Results using Stockstats of Bollinger Bands:
close_20_mstd boll boll_ub boll_lb close_13_ema close_34_ema
0 NaN 0.000006 NaN NaN 0.000006 0.000006
1 7.071068e-09 0.000006 0.000006 0.000006 0.000006 0.000006
2 5.773503e-09 0.000006 0.000006 0.000006 0.000006 0.000006
3 5.000000e-09 0.000006 0.000006 0.000006 0.000006 0.000006

close_20_sma float64
close_20_mstd float64
boll float64
boll_ub float64
boll_lb float64
close_13_ema float64
close_34_ema float64

Can you count forward?

First, thank you very much for the great work!

It seems that the counting only applies to a number of days in the past. Would it be nice to have the capability to count forward? Say, 'close_3.5_ge_-5_c' means to count the number of times in the future 5 days where close price is more than $3.5.

Hope this makes sense. Thanks!

Unable to extract SMA data

I was trying to extract SMA values from the dataframe, but the code is throwing error. Pasted below is my code and error details.

results = Sdf.retype(get_history(symbol =AAPL,start=date(2018,03,27),end=date(2018,06,05),index=False))
data_sma=results['open_2_sma']
print (data_sma[-1])

Error message:
No handlers could be found for logger "stockstats"

Traceback (most recent call last):
File "C:\Python27\Practises\Investing\StockStats\Training.py", line 35, in
print ("SMA:",results['data_sma'][-1])
File "C:\Python27\lib\site-packages\stockstats.py", line 929, in getitem
super(StockDataFrame, self).getitem(item))
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2139, in getitem
return self._getitem_column(key)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2146, in _getitem_column
return self._get_item_cache(key)
File "C:\Python27\lib\site-packages\pandas\core\generic.py", line 1842, in _get_item_cache
values = self._data.get(item)
File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 3843, in get
loc = self.items.get_loc(item)
File "C:\Python27\lib\site-packages\pandas\core\indexes\base.py", line 2527, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas_libs\index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
File "pandas_libs\index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
File "pandas_libs\hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas_libs\hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'data_sma'

kdj columns and rsv_9 all NaN

Hi,

there are cases where the kdj columns are all NaN. This seems to be related to rsv_9 beeing NaN.

Here is sample data (the first 20 lines of ASML stock quote) which shows the problem:

date        adj_close     close      high       low      open     volume
2010-01-01   29.28827  23.99998  23.99998  23.99998  23.99998        0.0
2010-01-04   29.60560  24.26000  24.31999  23.89002  23.90003  1563900.0
2010-01-05   29.66047  24.30497  24.62498  24.05996  24.11501  1550300.0
2010-01-06   29.97780  24.56500  24.56500  24.18000  24.20503  1133900.0
2010-01-07   29.42866  24.11501  24.45004  23.80001  24.45004  2648700.0
2010-01-08   28.44013  23.30497  24.08498  23.27502  23.99998  3064200.0
2010-01-11   27.37239  22.43002  23.45497  22.43002  23.31999  4640500.0
2010-01-12   27.82389  22.80001  22.92998  22.60004  22.65001  3098000.0
2010-01-13   28.28762  23.18000  23.39999  22.75003  22.80001  3732600.0
2010-01-14   28.26319  23.15998  23.59996  23.09499  23.46998  1851800.0
2010-01-15   27.89709  22.85999  23.43002  22.69498  23.28996  2738400.0
2010-01-18   27.93985  22.89503  22.98504  22.62499  22.90003  1132900.0
2010-01-19   27.67129  22.67496  22.91997  22.62499  22.80501  2392200.0
2010-01-20   28.53165  23.37997  23.87000  22.87501  22.98997  6490400.0
2010-01-21   29.00759  23.76998  24.06997  23.65001  23.74503  4068900.0
2010-01-22   28.53165  23.37997  23.82003  23.33500  23.53998  3842600.0
2010-01-25   27.78725  22.76998  23.18501  22.69998  22.70999  3091000.0
2010-01-26   28.34861  23.22998  23.37004  22.81002  22.99998  2716300.0
2010-01-27   28.12290  23.04502  23.13503  22.71500  23.08999  2130900.0
2010-01-28   27.86656  22.83497  23.73001  22.83497  23.44003  3445800.0

if rsv_9 is calculated with stockstats, the first value is NaN. This leads to all kdj columns beeing NaN as well. I guess this is due to a division by zero error in _get_rsv in line 251:

df[column_name] = ((df['close'] - low_min) /
                   (high_max - low_min).astype('float64') * 100)  

How should the code be modified so that this bug doesn't appear?
(or what's the correct value for rsv if high_max - low_min == 0?)

RSI 10 calculation

Hello!
Question regarding how RSI 10 is calculated ... When providing a pandas dataframe

df = pd.DataFrame(list)
recs = StockDataFrame.retype(df)
recs['rsi_10']

If I print out the dataframe:

close 970.27
id 34156
volume 184.7046024000001694
close_-1_s NaN
close_-1_d NaN
rs_10 NaN
rsi_10 NaN
Name: 0, dtype: object
close 970.49
id 34157
volume 148.3538014100001874
close_-1_s 970.27
close_-1_d 0.22
rs_10 inf
rsi_10 100
Name: 1, dtype: object
close 967.95
id 34158
volume 108.4361391200002487
close_-1_s 970.49
close_-1_d -2.54
rs_10 0.0779528
rsi_10 7.23156
Name: 2, dtype: object
close 968.14
id 34159
volume 125.0943135599999323
close_-1_s 967.95
close_-1_d 0.19
rs_10 0.161067
rsi_10 13.8724
...

The RSI 10 is already being calculated in the third row - isn't RSI 10 supposed to take the first 10 rows to calculate the initial RSI?

Thanks! And sorry if this is not the place for questions, I searched on Stackoverflow and couldn't find any topic or keyword for stockstats

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.