mintalib


Namemintalib JSON
Version 0.0.15 PyPI version JSON
download
home_pageNone
SummaryMinimal Technical Analysis Library for Python
upload_time2024-12-21 00:05:10
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseMIT License
keywords python cython finance technical-analysis indicators
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Minimal Technical Analysis Library for Python


This package offers a curated list of technical analysis indicators implemented in cython. It is built around `numpy` arrays and aims to be compatible with `pandas` and also `polars` where applicable.
The library is pre-compiled with `cython` so as not to require the `cython` runtime at installation. Also it does not link with `numpy` and so avoids binary dependency issues.


> **Warning**
> This project is experimental and the interface can change.
> For a similar project with a mature api you may want to look into
> [ta-lib](https://pypi.org/project/TA-Lib/).



## Structure
The `mintalib` package contains three main modules:

- [mintalib.core](https://github.com/furechan/mintalib/blob/main/docs/mintalib.core.md)
    low level calculation rountines implemented in cython
- [mintalib.functions](https://github.com/furechan/mintalib/blob/main/docs/mintalib.functions.md)
    wrapper functions to compute indicators
- [mintalib.indicators](https://github.com/furechan/mintalib/blob/main/docs/mintalib.indicators.md)
    composable interface to indicators

Most calculations are available in three flavors.
- The raw calculation routine is called something like
`calc_sma` and is available from the `mintalib.core` module. This routine implemented in cython.
- A function called something like `SMA` is also available from the `mintalib.functions` module, and includes extra facilities like selection of column (`item`) and wrapping of results.
- Finally an indicator with the same name `SMA` is available from the `mintalib.indicators` and offers a composable interface.


## List of Indicators

| Name       | Description                              |
|:-----------|:-----------------------------------------|
| ADX        | Average Directional Index                |
| ATR        | Average True Range                       |
| AVGPRICE   | Average Price                            |
| BBANDS     | Bollinger Bands                          |
| BOP        | Balance of Power                         |
| CCI        | Commodity Channel Index                  |
| CMF        | Chaikin Money Flow                       |
| CROSSOVER  | Cross Over                               |
| CROSSUNDER | Cross Under                              |
| CURVE      | Curve (time curvilinear regression)      |
| DEMA       | Double Exponential Moving Average        |
| DIFF       | Difference                               |
| EMA        | Exponential Moving Average               |
| EVAL       | Expression Eval (pandas only)            |
| EXP        | Exponential                              |
| FLAG       | Flag for value above zero                |
| FORECAST   | Forecast (time linear regression)        |
| HMA        | Hull Moving Average                      |
| KAMA       | Kaufman Adaptive Moving Average          |
| KELTNER    | Keltner Channel                          |
| KER        | Kaufman Efficiency Ratio                 |
| LAG        | Lag Function                             |
| LOG        | Logarithm                                |
| MA         | Generic Moving Average                   |
| MACD       | Moving Average Convergenge Divergence    |
| MAD        | Mean Absolute Deviation                  |
| MAX        | Rolling Maximum                          |
| MFI        | Money Flow Index                         |
| MIDPRICE   | Mid Price                                |
| MIN        | Rolling Minimum                          |
| MINUSDI    | Minus Directional Index                  |
| NATR       | Average True Range (normalized)          |
| PLUSDI     | Plus Directional Index                   |
| PPO        | Price Percentage Oscillator              |
| PRICE      | Generic Price                            |
| RMA        | Rolling Moving Average (RSI style)       |
| ROC        | Rate of Change                           |
| RSI        | Relative Strength Index                  |
| RVALUE     | RValue (time linear regression)          |
| SAR        | Parabolic Stop and Reverse               |
| SIGN       | Sign                                     |
| SLOPE      | Slope (time linear regression)           |
| SMA        | Simple Moving Average                    |
| STDEV      | Standard Deviation                       |
| STOCH      | Stochastic Oscillator                    |
| STREAK     | Consecutive streak of ups or downs       |
| SUM        | Rolling Sum                              |
| TEMA       | Triple Exponential Moving Average        |
| TRANGE     | True Range                               |
| TYPPRICE   | Typical Price                            |
| UPDOWN     | Flag for value crossing up & down levels |
| WCLPRICE   | Weighted Close Price                     |
| WMA        | Weighted Moving Average                  |


## Using Functions

Functions are available via the `functions` module,
with names like `SMA`, `EMA`, `RSI`, `MACD`, all in **upper case**.
The first parameter of a function is either `prices` or `series` depending on whether
the functions expects a dataframe of prices or a single series.
Functions that expect series data can be applied to a prices dataframe, in which case they use 
the column specified with the `item` parameter or by default the `close` column.

A `prices` dataframe can be a pandas dataframe, a polars dataframe or a dictionary of numpy arrays.
The column names for prices are expected to include `open`, `high`, `low`, `close` all in **lower case**.
A `series` can be a pandas series, a polars series or any iterable compatible with numpy arrays.

Functions automatically wrap their result to match their input, so that for example 
pandas based inputs will yield pandas based results with a matching index.


```python
import yfinance as yf

from mintalib.functions import SMA, MAX

# fetch prices (eg with yfinance)
prices = yf.Ticker('AAPL').history('5y')

# convert column and index names to lower case
prices = prices.rename(columns=str.lower).rename_axis(index=str.lower)

# compute indicators
sma50 = SMA(prices, 50)  # SMA of 'close' with period = 50
sma200 = SMA(prices, 200)  # SMA of 'close' with period = 200
high200 = MAX(prices, 200, item='high')  # MAX of 'high' with period = 200

```


## Using Indicators

Indicators are available via the `indicators` module, with similar names as functions all in **uper case**.

Indicators offer a composable interface where a function is bound with its calculation parameters. When instantiated with parameters an indicator yields a callable that can be applied to prices or series data. Indicators support the `@` operator as syntactic sugar to apply the indicator to data. So for example `SMA(50) @ prices` can be used to compute the 50 period simple moving average on `prices`, insted of `SMA(50)(prices)`.

```python
sma50 = SMA(50) @ prices
sma200 = SMA(200) @ prices
```

The `@` operator can also be used to compose indicators, where for example `ROC(1) @ EMA(20)` means `ROC(1)` applied to `EMA(20)`.

```python
slope = ROC(1) @ EMA(20) @ prices
```


## Using Indicators with Pandas

Prices indicators like `ATR` can only be applied to prices dataframes.

```python
atr = ATR(14) @ prices
```

Series indicators can be applied to a prices dataframe or a series. When applied to prices you must specify a column with the `item` or otherwize the indicator will use the `"close"` column by default.

```python
# SMA on the close column
sma50 = SMA(50) @ prices

# SMA on the volume column
vol50 = SMA(50, item="volume") @ prices

# Which is the same as
vol50 = SMA(50) @ prices.volume 
``` 

With pandas dataframes you can compose and assign multiple indicators in one call using the builtin `assign` method.


```python
import yfinance as yf

from mintalib.indicators import EMA, SMA, ROC, RSI, EVAL

# fetch prices (eg with yfinance)
prices = yf.Ticker('AAPL').history('5y')

# convert column and index names to lower case
prices = prices.rename(columns=str.lower).rename_axis(index=str.lower)

# compute and append indicators to prices
# note that calculations can use results from prior indicators
result = prices.assign(
    sma50 = SMA(50),
    sma200 = SMA(200),
    rsi = RSI(14),
    slope = ROC(1) @ EMA(20),
    uptrend = EVAL("sma50 > sma200")
)
```


## Using Indicators with Polars

Indicators can be applied to polars prices dataframes and series in the same way as with pandas. 

The `@` operator has been extended to also work with polars expressions. This is just syntactic sugar around polars `map_batches`.

In the following example, you can assign multiple columns using polars `with_columns`.

```python
import polars as pl
from polars import col

import yfinance as yf

from mintalib.indicators import EMA, SMA, ROC, RSI, EVAL

# fetch prices (eg with yfinance)
prices = yf.Ticker('AAPL').history('5y')

# convert column and index names to lower case
prices = prices.rename(columns=str.lower).rename_axis(index=str.lower)

# convert to polars dataframe 
prices = pl.from_pandas(prices, include_index=True)

# compute and append indicators to prices
result = prices.with_columns(
    sma20 = SMA(20) @ col('close'),
    sma50 = SMA(50) @ col('close'),
)
```


## Example Notebooks

You can find example notebooks in the `examples` folder. 


## Installation

You can install the current version of this package with pip

```console
python -mpip install git+https://github.com/furechan/mintalib.git
```

## Dependencies

- python >= 3.9
- pandas
- numpy


## Related Projects
- [ta-lib](https://github.com/mrjbq7/ta-lib) Python wrapper for TA-Lib
- [qtalib](https://github.com/josephchenhk/qtalib) Quantitative Technical Analysis Library
- [numpy](https://github.com/numpy/numpy) The fundamental package for scientific computing with Python
- [pandas](https://github.com/pandas-dev/pandas) Flexible and powerful data analysis / manipulation library for Python
- [polars](https://github.com/pola-rs/polars) Fast multi-threaded, hybrid-out-of-core query engine focussing on DataFrame front-ends
- [yfinance](https://github.com/ranaroussi/yfinance) Download market data from Yahoo! Finance's API



            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "mintalib",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "python, cython, finance, technical-analysis, indicators",
    "author": null,
    "author_email": "Furechan <furechan@xsmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/50/0e/b73247d04ab176c3fdb7ff0ed8295d38e3b221a5ce5e591468d403006d6a/mintalib-0.0.15.tar.gz",
    "platform": null,
    "description": "# Minimal Technical Analysis Library for Python\n\n\nThis package offers a curated list of technical analysis indicators implemented in cython. It is built around `numpy` arrays and aims to be compatible with `pandas` and also `polars` where applicable.\nThe library is pre-compiled with `cython` so as not to require the `cython` runtime at installation. Also it does not link with `numpy` and so avoids binary dependency issues.\n\n\n> **Warning**\n> This project is experimental and the interface can change.\n> For a similar project with a mature api you may want to look into\n> [ta-lib](https://pypi.org/project/TA-Lib/).\n\n\n\n## Structure\nThe `mintalib` package contains three main modules:\n\n- [mintalib.core](https://github.com/furechan/mintalib/blob/main/docs/mintalib.core.md)\n    low level calculation rountines implemented in cython\n- [mintalib.functions](https://github.com/furechan/mintalib/blob/main/docs/mintalib.functions.md)\n    wrapper functions to compute indicators\n- [mintalib.indicators](https://github.com/furechan/mintalib/blob/main/docs/mintalib.indicators.md)\n    composable interface to indicators\n\nMost calculations are available in three flavors.\n- The raw calculation routine is called something like\n`calc_sma` and is available from the `mintalib.core` module. This routine implemented in cython.\n- A function called something like `SMA` is also available from the `mintalib.functions` module, and includes extra facilities like selection of column (`item`) and wrapping of results.\n- Finally an indicator with the same name `SMA` is available from the `mintalib.indicators` and offers a composable interface.\n\n\n## List of Indicators\n\n| Name       | Description                              |\n|:-----------|:-----------------------------------------|\n| ADX        | Average Directional Index                |\n| ATR        | Average True Range                       |\n| AVGPRICE   | Average Price                            |\n| BBANDS     | Bollinger Bands                          |\n| BOP        | Balance of Power                         |\n| CCI        | Commodity Channel Index                  |\n| CMF        | Chaikin Money Flow                       |\n| CROSSOVER  | Cross Over                               |\n| CROSSUNDER | Cross Under                              |\n| CURVE      | Curve (time curvilinear regression)      |\n| DEMA       | Double Exponential Moving Average        |\n| DIFF       | Difference                               |\n| EMA        | Exponential Moving Average               |\n| EVAL       | Expression Eval (pandas only)            |\n| EXP        | Exponential                              |\n| FLAG       | Flag for value above zero                |\n| FORECAST   | Forecast (time linear regression)        |\n| HMA        | Hull Moving Average                      |\n| KAMA       | Kaufman Adaptive Moving Average          |\n| KELTNER    | Keltner Channel                          |\n| KER        | Kaufman Efficiency Ratio                 |\n| LAG        | Lag Function                             |\n| LOG        | Logarithm                                |\n| MA         | Generic Moving Average                   |\n| MACD       | Moving Average Convergenge Divergence    |\n| MAD        | Mean Absolute Deviation                  |\n| MAX        | Rolling Maximum                          |\n| MFI        | Money Flow Index                         |\n| MIDPRICE   | Mid Price                                |\n| MIN        | Rolling Minimum                          |\n| MINUSDI    | Minus Directional Index                  |\n| NATR       | Average True Range (normalized)          |\n| PLUSDI     | Plus Directional Index                   |\n| PPO        | Price Percentage Oscillator              |\n| PRICE      | Generic Price                            |\n| RMA        | Rolling Moving Average (RSI style)       |\n| ROC        | Rate of Change                           |\n| RSI        | Relative Strength Index                  |\n| RVALUE     | RValue (time linear regression)          |\n| SAR        | Parabolic Stop and Reverse               |\n| SIGN       | Sign                                     |\n| SLOPE      | Slope (time linear regression)           |\n| SMA        | Simple Moving Average                    |\n| STDEV      | Standard Deviation                       |\n| STOCH      | Stochastic Oscillator                    |\n| STREAK     | Consecutive streak of ups or downs       |\n| SUM        | Rolling Sum                              |\n| TEMA       | Triple Exponential Moving Average        |\n| TRANGE     | True Range                               |\n| TYPPRICE   | Typical Price                            |\n| UPDOWN     | Flag for value crossing up & down levels |\n| WCLPRICE   | Weighted Close Price                     |\n| WMA        | Weighted Moving Average                  |\n\n\n## Using Functions\n\nFunctions are available via the `functions` module,\nwith names like `SMA`, `EMA`, `RSI`, `MACD`, all in **upper case**.\nThe first parameter of a function is either `prices` or `series` depending on whether\nthe functions expects a dataframe of prices or a single series.\nFunctions that expect series data can be applied to a prices dataframe, in which case they use \nthe column specified with the `item` parameter or by default the `close` column.\n\nA `prices` dataframe can be a pandas dataframe, a polars dataframe or a dictionary of numpy arrays.\nThe column names for prices are expected to include `open`, `high`, `low`, `close` all in **lower case**.\nA `series` can be a pandas series, a polars series or any iterable compatible with numpy arrays.\n\nFunctions automatically wrap their result to match their input, so that for example \npandas based inputs will yield pandas based results with a matching index.\n\n\n```python\nimport yfinance as yf\n\nfrom mintalib.functions import SMA, MAX\n\n# fetch prices (eg with yfinance)\nprices = yf.Ticker('AAPL').history('5y')\n\n# convert column and index names to lower case\nprices = prices.rename(columns=str.lower).rename_axis(index=str.lower)\n\n# compute indicators\nsma50 = SMA(prices, 50)  # SMA of 'close' with period = 50\nsma200 = SMA(prices, 200)  # SMA of 'close' with period = 200\nhigh200 = MAX(prices, 200, item='high')  # MAX of 'high' with period = 200\n\n```\n\n\n## Using Indicators\n\nIndicators are available via the `indicators` module, with similar names as functions all in **uper case**.\n\nIndicators offer a composable interface where a function is bound with its calculation parameters. When instantiated with parameters an indicator yields a callable that can be applied to prices or series data. Indicators support the `@` operator as syntactic sugar to apply the indicator to data. So for example `SMA(50) @ prices` can be used to compute the 50 period simple moving average on `prices`, insted of `SMA(50)(prices)`.\n\n```python\nsma50 = SMA(50) @ prices\nsma200 = SMA(200) @ prices\n```\n\nThe `@` operator can also be used to compose indicators, where for example `ROC(1) @ EMA(20)` means `ROC(1)` applied to `EMA(20)`.\n\n```python\nslope = ROC(1) @ EMA(20) @ prices\n```\n\n\n## Using Indicators with Pandas\n\nPrices indicators like `ATR` can only be applied to prices dataframes.\n\n```python\natr = ATR(14) @ prices\n```\n\nSeries indicators can be applied to a prices dataframe or a series. When applied to prices you must specify a column with the `item` or otherwize the indicator will use the `\"close\"` column by default.\n\n```python\n# SMA on the close column\nsma50 = SMA(50) @ prices\n\n# SMA on the volume column\nvol50 = SMA(50, item=\"volume\") @ prices\n\n# Which is the same as\nvol50 = SMA(50) @ prices.volume \n``` \n\nWith pandas dataframes you can compose and assign multiple indicators in one call using the builtin `assign` method.\n\n\n```python\nimport yfinance as yf\n\nfrom mintalib.indicators import EMA, SMA, ROC, RSI, EVAL\n\n# fetch prices (eg with yfinance)\nprices = yf.Ticker('AAPL').history('5y')\n\n# convert column and index names to lower case\nprices = prices.rename(columns=str.lower).rename_axis(index=str.lower)\n\n# compute and append indicators to prices\n# note that calculations can use results from prior indicators\nresult = prices.assign(\n    sma50 = SMA(50),\n    sma200 = SMA(200),\n    rsi = RSI(14),\n    slope = ROC(1) @ EMA(20),\n    uptrend = EVAL(\"sma50 > sma200\")\n)\n```\n\n\n## Using Indicators with Polars\n\nIndicators can be applied to polars prices dataframes and series in the same way as with pandas. \n\nThe `@` operator has been extended to also work with polars expressions. This is just syntactic sugar around polars `map_batches`.\n\nIn the following example, you can assign multiple columns using polars `with_columns`.\n\n```python\nimport polars as pl\nfrom polars import col\n\nimport yfinance as yf\n\nfrom mintalib.indicators import EMA, SMA, ROC, RSI, EVAL\n\n# fetch prices (eg with yfinance)\nprices = yf.Ticker('AAPL').history('5y')\n\n# convert column and index names to lower case\nprices = prices.rename(columns=str.lower).rename_axis(index=str.lower)\n\n# convert to polars dataframe \nprices = pl.from_pandas(prices, include_index=True)\n\n# compute and append indicators to prices\nresult = prices.with_columns(\n    sma20 = SMA(20) @ col('close'),\n    sma50 = SMA(50) @ col('close'),\n)\n```\n\n\n## Example Notebooks\n\nYou can find example notebooks in the `examples` folder. \n\n\n## Installation\n\nYou can install the current version of this package with pip\n\n```console\npython -mpip install git+https://github.com/furechan/mintalib.git\n```\n\n## Dependencies\n\n- python >= 3.9\n- pandas\n- numpy\n\n\n## Related Projects\n- [ta-lib](https://github.com/mrjbq7/ta-lib) Python wrapper for TA-Lib\n- [qtalib](https://github.com/josephchenhk/qtalib) Quantitative Technical Analysis Library\n- [numpy](https://github.com/numpy/numpy) The fundamental package for scientific computing with Python\n- [pandas](https://github.com/pandas-dev/pandas) Flexible and powerful data analysis / manipulation library for Python\n- [polars](https://github.com/pola-rs/polars) Fast multi-threaded, hybrid-out-of-core query engine focussing on DataFrame front-ends\n- [yfinance](https://github.com/ranaroussi/yfinance) Download market data from Yahoo! Finance's API\n\n\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "Minimal Technical Analysis Library for Python",
    "version": "0.0.15",
    "project_urls": {
        "homepage": "https://github.com/furechan/mintalib"
    },
    "split_keywords": [
        "python",
        " cython",
        " finance",
        " technical-analysis",
        " indicators"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "500eb73247d04ab176c3fdb7ff0ed8295d38e3b221a5ce5e591468d403006d6a",
                "md5": "8913b2a4689f4ad2cec0b4283a18e2d1",
                "sha256": "3826d80307b342120864ca974cdc25004fdec25a2bb78bd12633995640ea2dac"
            },
            "downloads": -1,
            "filename": "mintalib-0.0.15.tar.gz",
            "has_sig": false,
            "md5_digest": "8913b2a4689f4ad2cec0b4283a18e2d1",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 865856,
            "upload_time": "2024-12-21T00:05:10",
            "upload_time_iso_8601": "2024-12-21T00:05:10.270707Z",
            "url": "https://files.pythonhosted.org/packages/50/0e/b73247d04ab176c3fdb7ff0ed8295d38e3b221a5ce5e591468d403006d6a/mintalib-0.0.15.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-21 00:05:10",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "furechan",
    "github_project": "mintalib",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "mintalib"
}
        
Elapsed time: 0.42982s