Skip to main content

The wrapper of `pandas.DataFrame` with stock statistics and indicators support.

Project description

stock-pandas

A wrapper and a subtle class of pandas.DataFrame which supports:

  • stock statistics
  • all kinds of stock indicators

stock-pandas makes automatical trading much easier. stock-pandas requires Python >= 3.6

With the help of stock-pandas and mplfinance, we could easily draw something like:

The code example is available at here.

Install

pip install stock-pandas

Usage

from stock_pandas import StockDataFrame

We also have some examples with annotations in the example directory, you could use JupyterLab or Jupyter notebook to play with them.

StockDataFrame

StockDataFrame inherits from pandas.DataFrame, so if you are familiar with pandas.DataFrame, you are already ready to use stock-pandas

import pandas as pd
stock = StockDataFrame(pd.read_csv('stock.csv'))

As we know, we could use [], which called pandas indexing (a.k.a. __getitem__ in python) to select out lower-dimensional slices. In addition to indexing with colname (column name of the DataFrame), we could also do indexing by directives.

stock[directive]

stock[[directive0, directive1]]

We have an example to show the most basic indexing using [directive]

stock = StockDataFrame({
    'open' : ...,
    'high' : ...,
    'low'  : ...,
    'close': [5, 6, 7, 8, 9]
})

stock['ma:2']

# 0    NaN
# 1    5.5
# 2    6.5
# 3    7.5
# 4    8.5
# Name: ma:2,close, dtype: float64

Which prints the 2-period simple moving average on column "close".

stock.calc(directive: str, create_column: bool=False) -> Series

Calculates series according to the directive.

stock['ma:2']
# is equivalent to:
stock.calc('ma:2', create_column=True)
# This will only calculate without creating a new column in the dataframe
stock.calc('ma:20')

stock.alias(alias: str, name: str) -> None

Defines column alias or directive alias

  • alias str the alias name
  • name str the name of an existing column or the directive string
# Some plot library such as `mplfinance` requires a column named capitalized `Open`,
# but it is ok, we could create an alias.
stock.alias('Open', 'open')

stock.alias('buy_point', 'kdj.j < 0')

Syntax of directive

column_name := command_name | command_name operator expression
operator := '/' | '\' | '<' | '<=' | '==' | '>=' | '>' | '><'
expression := float | command_name
command_name := indicator | indicator : arguments
indicator := alphabets | alphabets.alphabets
arguments := string | arguments , string

directive Example

Here lists several use cases of column names

# The 20-day(default) moving average
#   which is the mid band of bollinger bands
stock['boll']

# kdjj less than 0
stock['kdj.j < 0']

# 9-day kdjk cross up 3-day kdjd
stock['kdj.k:9 / kdj.d:3']

# 5-day simple moving average
stock['ma:5']

# 10-day simple moving average on open prices
stock['ma:10,open']

Built-in Commands

ma, simple moving averages

macd

# macd
stock['macd']
stock['macd.dif']

# macd signal band, which is a shortcut for stock['macd.signal']
stock['macd.s']
stock['macd.signal']
stock['macd.dea']

# macd histogram band, which is equivalent to stock['macd.h']
stock['macd.histogram']
stock['macd.h']
stock['macd.macd']

boll, bollinger bands

# boll
stock['boll']

# bollinger upper band, a shortcut for stock['boll.upper']
stock['boll.u']
stock['boll.upper]

# bollinger lower band, which is equivalent to stock['boll.l']
stock['boll.lower']
stock['boll.l']

Operators

  • /:
  • \:
  • <:
  • <=:
  • ==:
  • >=:
  • >:
  • ><:

Advanced Sections

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stock-pandas-0.3.0.tar.gz (13.1 kB view hashes)

Uploaded Source

Built Distribution

stock_pandas-0.3.0-py3-none-any.whl (12.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page