Skip to main content

Stock Statistics/Indicators Calculation Helper

image

image

image

VERSION: 0.3.1

Introduction

Supply a wrapper StockDataFrame based on the pandas.DataFrame with inline stock statistics/indicators support.

Supported statistics/indicators are:

  • change (in percent)
  • 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)
  • 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: including upper band and lower band.
  • MACD: moving average convergence divergence. Including signal and histogram. (see note)
  • CR:
  • WR: Williams Overbought/Oversold index
  • CCI: Commodity Channel Index
  • TR: true range
  • ATR: average true range
  • line cross check, cross up or cross down.
  • 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: Volatility Volume Ratio

Installation

pip install stockstats

Compatibility

Please check the setup.py file.

Note that pandas add some type check after version 1.0. One type assert is skipped in StockDataFrame. Check ISSUE-50 for detail.

License

BSD

Tutorial

  • Initialize the StockDataFrame with the retype function which convert a pandas.DataFrame to a StockDataFrame.
stock = StockDataFrame.retype(pd.read_csv('stock.csv'))
  • Formalize your data. This package takes for granted that your data is sorted by timestamp and contains certain columns. Please align your column name.
    • open: the open price of the interval
    • close: the close price of the interval
    • high: the highest price of the interval
    • low: the lowest price of the interval
    • volume: the volume of stocks traded during the interval
    • amount: the amount of the stocks during the interval
  • There are some shortcuts for frequent used statistics/indicators like kdjk, boll_hb, macd, etc.
  • The indicators/statistics are generated on the fly when they are accessed. If you are accessing through Series, it may return not found error. The fix is to explicitly initialize it by accessing it like below:
_ = stock['macd']
# or
stock.get('macd')
  • Using get item to access the indicators. The item name following the pattern: {columnName_window_statistics}. Some statistics/indicators has their short cut. See examples below:
# volume delta against previous day
stock['volume_delta']

# open delta against next 2 day
stock['open_2_d']

# open price change (in percent) between today and the day before yesterday
# 'r' stands for rate.
stock['open_-2_r']

# CR indicator, including 5, 10, 20 days moving average
stock['cr']
stock['cr-ma1']
stock['cr-ma2']
stock['cr-ma3']

# volume max of three days ago, yesterday and two days later
stock['volume_-3,2,-1_max']

# volume min between 3 days ago and tomorrow
stock['volume_-3~1_min']

# KDJ, default to 9 days
stock['kdjk']
stock['kdjd']
stock['kdjj']

# three days KDJK cross up 3 days KDJD
stock['kdj_3_xu_kdjd_3']

# 2 days simple moving average on open price
stock['open_2_sma']

# MACD
stock['macd']
# MACD signal line
stock['macds']
# MACD histogram
stock['macdh']

# bolling, including upper band and lower band
stock['boll']
stock['boll_ub']
stock['boll_lb']

# close price less than 10.0 in 5 days count
stock['close_10.0_le_5_c']

# CR MA2 cross up CR MA1 in 20 days count
stock['cr-ma2_xu_cr-ma1_20_c']

# count forward(future) where close price is larger than 10
stock['close_10.0_ge_5_fc']

# 6 days RSI
stock['rsi_6']
# 12 days RSI
stock['rsi_12']

# 10 days WR
stock['wr_10']
# 6 days WR
stock['wr_6']

# CCI, default to 14 days
stock['cci']
# 20 days CCI
stock['cci_20']

# TR (true range)
stock['tr']
# ATR (Average True Range)
stock['atr']

# DMA, difference of 10 and 50 moving average
stock['dma']

# DMI
# +DI, default to 14 days
stock['pdi']
# -DI, default to 14 days
stock['mdi']
# DX, default to 14 days of +DI and -DI
stock['dx']
# ADX, 6 days SMA of DX, same as stock['dx_6_ema']
stock['adx']
# ADXR, 6 days SMA of ADX, same as stock['adx_6_ema']
stock['adxr']

# TRIX, default to 12 days
stock['trix']
# TRIX based on the close price for a window of 3
stock['close_3_trix']
# MATRIX is the simple moving average of TRIX
stock['trix_9_sma']
# TEMA, another implementation for triple ema
stock['tema']
# TEMA based on the close price for a window of 2
stock['close_2_tema']

# VR, default to 26 days
stock['vr']
# MAVR is the simple moving average of VR
stock['vr_6_sma']
Following options are available for tuning. Note that all of them are class level options and MUST be changed before any calculation happens.

:   -   

        KDJ

        :   -   KDJ\_WINDOW: default to 9

    -   

        BOLL

        :   -   BOLL\_WINDOW: default to 20
            -   BOLL\_STD\_TIMES: default to 2

    -   

        MACD

        :   -   MACD\_EMA\_SHORT: default to 12
            -   MACD\_EMA\_LONG: default to 26
            -   MACD\_EMA\_SIGNAL: default to 9

    -   

        PDI, MDI, DX & ADX

        :   -   PDI\_SMMA: default to 14
            -   MDI\_SMMA: default to 14
            -   DX\_SMMA: default to 14
            -   ADX\_EMA: default to 6
            -   ADXR\_EMA: default to 6

    -   

        CR

        :   -   CR\_MA1: default to 5
            -   CR\_MA2: default to 10
            -   CR\_MA3: default to 20

    -   

        Triple EMA

        :   -   TRIX\_EMA\_WINDOW: default to 12
            -   TEMA\_EMA\_WINDOW: default to 5

    -   

        ATR

        :   -   ATR\_SMMA: default to 14

To file issue, please visit:

https://github.com/jealous/stockstats

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:

Metadata

Release files for stock-dataframe 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stock-dataframe 0.1.0
File Size Uploaded Provenance
stock-dataframe-0.1.0.tar.gz 19.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for stock-dataframe 0.1.0
File Interpreter ABI Platform
stock_dataframe-0.1.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 32.0 kB

Release files / stock-dataframe-0.1.0.tar.gz

Download URL stock-dataframe-0.1.0.tar.gz
Size 19.4 kB
Tags Source
SHA-256 checksum
How to use checksums
2840b75228b64e6e8f642d583fbd5712738e6b78afe67d3519be5447b86a9de2
BLAKE2b-256 checksum
How to use checksums
b161341632b3e298120f26a6055cd8f63ab62666fc7c61e10b30ee21d4c28f35
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

Release files / stock_dataframe-0.1.0-py2.py3-none-any.whl

Download URL stock_dataframe-0.1.0-py2.py3-none-any.whl
Size 12.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
e8a730e5c7f067b943664057ffc7e7b4ef182c742488592aa17be7c19e3b8bcc
BLAKE2b-256 checksum
How to use checksums
bfe40782cae4d007baede400b8a42dc4199a95e9e0374151b6e04b47ae59e269
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

Security for stock-dataframe 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page