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 directive
s.
stock[directive] # Gets a series
stock[[directive0, directive1]] # Gets a data frame
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
directive := command | command operator expression
operator := '/' | '\' | '><' | '<' | '<=' | '==' | '>=' | '>'
expression := float | command
command := command_name | command_name : arguments
command_name := main_command_name | main_command_name.sub_command_name
main_command_name := alphabets
sub_command_name := alphabets
arguments := argument | argument , arguments
argument := empty_string | string | ( arguments )
directive
Example
Here lists several use cases of column names
# The 20-period (default) moving average
# which is the mid band of bollinger bands
stock['boll']
# kdjj less than 0
stock['kdj.j < 0']
# 9-period kdjk cross up 3-day kdjd
stock['kdj.k:9 / kdj.d:3']
# 5-period simple moving average
stock['ma:5']
# 10-period simple moving average on open prices
stock['ma:10,open']
# Dataframe of 5-period, 10-period, 30-period ma
stock[[
'ma:5',
'ma:10',
'ma:30'
]]
# Which means we use the default values of the first and the second parameters,
# and specify the third parameter
stock['macd:,,10']
# We should wrap a parameter with parantheses if it contains `,`
stock['increase:(ma:20,close),3']
Built-in Commands of Indicators
Document syntax explanation:
- param0
str
which meansparam0
is a required parameter of typestr
. - param1?
str='close'
which means parameterparam1
is optional with default value'close'
.
Actually, all parameters of a command are of string type, so the str
here means an interger-like string.
ma
, simple Moving Averages
ma:<period>,<column>
Gets the period
-period simple moving average on column named column
.
SMA
is often confused between simple moving average and smoothed moving average.
So stock-pandas
will use ma
for simple moving average and smma
for smoothed moving average.
- period
int
(required) - column?
enum<'open'|'high'|'low'|'close'>='close'
Which column should the calculation based on. Defaults to'close'
# which is equivalent to `stock['ma:5,close']`
stock['ma:5']
stock['ma:10,open']
macd
, Moving Average Convergence Divergence
macd:<fast_period>,<slow_period>
macd.signal:<fast_period>,<slow_period>,<signal_period>
macd.histogram:<fast_period>,<slow_period>,<signal_period>
- fast_period?
int=26
fast period. Defaults to26
. - slow_period?
int=12
slow period. Defaults to12
- signal_period?
int=9
signal period. Defaults to9
# 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:<period>,<times>,<column>
- period?
int=20
- times?
int=2
- column?
str='close'
# 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']
kdj
, stochastic oscillator
kdj.k:<param_k>
kdj.d:<param_d>
kdj.j:<param_j>
- param_k?
int=9
- param_d?
int=9
- param_j?
int=9
# The k series of KDJ 999
stock['kdj.k']
# The KDJ serieses of with parameters 9, 3, and 3
stock[['kdj.k', 'kdj.d:3', 'kdj.j:3']]
Built-in Commands for Statistics
column
column:<name>
Just gets the series of a column. This command is designed to be used together with an operator to compare with another command or as a parameter of some statistics command.
- name
str
the name of the column
increase
increase:<on_what>,<period>,<step>
Gets a bool
-type series each item of is True
if the value of indicator on_what
increases in the last period
-period.
- on_what
str
the command name of an indicator. If we want calculate on a normal column such as'close'
, we should use'column:close'
- period?
int=1
- direction?
1 | -1
the direction of "increase".-1
means decreasing
For example:
# Which means whether the `ma:20,close` line (20-period simple moving average on column `'close'`) has been increasing for 3 periods.
stock['increase:(ma:20,close),3']
# If the close price has been decreasinng for 5 periods(maybe days)
stock['increase:column:close,5,-1']
Operators
left operator right
/
: whetherleft
crosses throughright
from the down side ofright
to the upper side which we call it as "cross up".\
: whetherleft
crosses downright
.><
: whetherleft
crossesright
.<
/<=
/==
/>=
/>
: For a certain record of the same time, whether the value ofleft
is less than / less than or equal to / equal to / larger than or equal to / larger than the value ofright
.
Advanced Sections
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for stock_pandas-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5df91ce278a83cf829ac4953cc9fd9c9b372d23fabd7a4eac20ca33d20fc34d1 |
|
MD5 | 0795e2399c14bab5c7868380d714cbaa |
|
BLAKE2b-256 | b23d400ae08f4b0aaea16fae886d9ead1454960eff94ed0a2230ec494919e121 |