Skip to main content

Alpha Library: A high-performance rolling window calculation library implemented in Rust with Python bindings. Used for financial data analysis and factor research.

Project description

Introduction

alpha-lib is a Python library that implements various algorithms and functions commonly used in quantitative finance and algorithmic trading.

For financial data analysis, there are many algorithms required a rolling window calculation. This library provides efficient implementations of these algorithms.

Algorithms

Name Description Ref Link
BARSLAST Bars since last condition true https://www.amibroker.com/guide/afl/barslast.html
BARSSINCE Bars since first condition true https://www.amibroker.com/guide/afl/barssince.html
COUNT Count periods where condition is true https://www.amibroker.com/guide/afl/count.html
CROSS CROSS(A, B): Previous A < B, Current A >= B https://www.amibroker.com/guide/afl/cross.html
DMA Exponential Moving Average https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
HHV Highest High Value https://www.amibroker.com/guide/afl/hhv.html
HHVBARS Bars since Highest High Value https://www.amibroker.com/guide/afl/hhvbars.html
LLV Lowest Low Value https://www.amibroker.com/guide/afl/llv.html
LLVBARS Bars since Lowest Low Value https://www.amibroker.com/guide/afl/llvbars.html
LONGCROSS LONGCROSS(A,B,N): Previous N A < B, Current A >= B
MA Moving Average https://en.wikipedia.org/wiki/Moving_average#Simple_moving_average
RANK rank by group dim
RCROSS RCROSE(A, B): Previous A > B, Current A <= B
REF Reference to value N periods ago https://www.amibroker.com/guide/afl/ref.html
RLONGCROSS RLONGCROSS(A,B,N): Previous N A > B, Current A <= B
SMA Exponential Moving Average (variant of EMA) https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
SUM Sum of value N periods ago https://www.amibroker.com/guide/afl/sum.html
SUMBARS Sums X backwards until the sum is greater than or equal to A https://www.amibroker.com/guide/afl/sumbars.html
TS_RANK rank by ts dim

Usage

Installation

You can install the library using pip:

pip install py-alpha-lib

Simple Example

import alpha as al
import numpy as np

data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dtype=np.float64)

# Calculate 3-period moving average, note that first 2 values are average of available values
result = al.MA(data, 3)
print(result)
# Output: [1.  1.5 2.  3.  4.  5.  6.  7.  8.  9. ]

# Calculate 3-period exponential moving average, first 2 values are NaN
al.set_ctx(flags=al.FLAG_STRICTLY_CYCLE)
result = al.EMA(data, 3)
print(result)
# Output: [ nan  nan 2.  3.  4.  5.  6.  7.  8.  9. ]

# Calculate 3-period exponential moving average, skipping NaN values
al.set_ctx(flags=al.FLAG_SKIP_NAN)
data_with_nan = [1, 2, None, 4, 5, 6, 7, 8, 9, 10]
result = al.MA(data_with_nan, 3)
print(result)
# Output: [1.  1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5]

Environment Context

You may notice that some functions have different behaviors based on the context settings. You can set the context using al.set_ctx() function. The context includes:

  • groups: Number of groups to divide the data into for group-wise operations. groups used calculations multiple stocks(for example) in a single array.
    • Each group is assumed to be of equal size and contiguous in the input array.
    • Each group is processed paralleled and independently. This is why the performance is very good.
    • For rank function, groups is required to be set greater than 1. Because rank is a group-wise operation.
  • start: The starting index for calculations.
    • For some case, this may reduce unnecessary computations.
    • Default is 0.
  • flags: Additional flags to modify function behaviors.
    • FLAG_SKIP_NAN: When this flag is set, functions will skip NaN values during computations.
    • FLAG_STRICTLY_CYCLE: When this flag is set, functions will strictly cycle over the data, meaning that initial periods that do not have enough data will be filled with NaN.
    • You can combine multiple flags using bitwise OR operation, e.g., flags=FLAG_SKIP_NAN | FLAG_STRICTLY_CYCLE.

Factor expression to Python code

You can convert factor expressions to Python code using the lang module. For example:

python -m alpha.lang examples/wq101/alpha101.txt

This will read the factor expressions from examples/wq101/alpha101.txt and generate corresponding Python code using alpha-lib functions.

After generating the code, you may need to adjust the code

  • Fix type conversions between float and bool.
  • Add context settings if needed.

Full Example

WorldQuant 101 famous alpha 101

The WorldQuant 101 alpha factors are a set of quantitative trading signals developed by WorldQuant. There are some implementations of these alpha factors, for example: DolphinDB implementation: , it provides 101 alpha factors implemented in DolphinDB language also with comparative pandas based Python implementation. It's a good starting point for comparing with our alpha-lib.

The full implementation of these 101 alpha factors using alpha-lib can be found in the wq101 folder of this repository. This implementation leverages the efficient algorithms provided by alpha-lib to compute the alpha factors.

  • al: is the factor implemented using alpha-lib.
  • pd_: is the factor implemented using pandas for comparison.
  • Because we can not setup the full featured DolphinDB environment here, we just use it's results.

Run the example

Show help message:

$ examples/wq101/main.py --help
usage: main.py [-h] [-s START] [-e END] [-v] [-d DATA] [-o OUTPUT] [--with-pd] [--with-al] [no ...]

positional arguments:
  no                    alpha numbers to run, e.g., 1 2 3

options:
  -h, --help            show this help message and exit
  -s, --start START     start alpha number
  -e, --end END         end alpha number
  -v, --verbose         enable verbose logging
  -d, --data DATA       data file path
  -o, --output OUTPUT   save output to file
  --with-pd             run pandas implementation
  --with-al             run alpha-lib implementation
# Run specific alpha factors both pandas and alpha-lib implementations
examples/wq101/main.py --with-pd --with-al 1 2 3 4

# Run a range of alpha factors using alpha-lib implementation
examples/wq101/main.py --with-al -s 1 -e 102

Because the pandas implementation is too slow for some factors, below is a 1~14 factors (examples/wq101/main.py --with-al -s 1 -e 15) run time comparison on a sample dataset with 4000 stocks and 261 trading days, total 1,044,000 factors to compute for each factor.

The pandas/DolphinDB is copied from the DolphinDB implementation result

The Value columns are used to verify the correctness of the implementations, they should be the same or very close.

The hardware/soft environment is:

  • CPU: Intel 13th Gen Core i7-13700K (16 cores, 24 threads)
  • RAM: 32GB
  • OS: Ubuntu 22.04 LTS
  • Python: 3.14 without free-threading
  • pandas: 3.0
  • numpy: 2.4
no pandasTime(ms) alphaLibTime(ms) SpeedUp
(pandas/alphaLib)
SpeedUp
(pandas/DolphinDB)
pandasValue alphaLibValue
data 11396 718 15
#001 14231 7 2033 800 0.182125 0.182125
#002 465 14 33 9 -0.64422 -0.326332
#003 430 8 53 14 0.236184 0.236184
#004 55107 6 9184 1193 -8 -8
#005 105 9 11 5 -0.331333 -0.331333
#006 351 2 175 84 0.234518 0.234518
#007 43816 17 2577 486 -1 -1
#008 222 9 24 14 -0.6435 -0.6435
#009 97 9 10 14 17.012321 17.012321
#010 145 11 13 6 0.662 0.662
#011 158 10 15 6 0.785196 0.892723
#012 4 4 1 0.7 -17.012321 -17.012321
#013 446 9 49 8 -0.58 -0.58
#014 398 8 49 18 0.095449 0.095449

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

py_alpha_lib-0.1.0.tar.gz (100.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

py_alpha_lib-0.1.0-pp311-pypy311_pp73-musllinux_1_2_x86_64.whl (878.5 kB view details)

Uploaded PyPymusllinux: musl 1.2+ x86-64

py_alpha_lib-0.1.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (660.9 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

py_alpha_lib-0.1.0-cp314-cp314t-musllinux_1_2_x86_64.whl (871.2 kB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ x86-64

py_alpha_lib-0.1.0-cp314-abi3-win_amd64.whl (531.2 kB view details)

Uploaded CPython 3.14+Windows x86-64

py_alpha_lib-0.1.0-cp313-cp313t-musllinux_1_2_x86_64.whl (872.1 kB view details)

Uploaded CPython 3.13tmusllinux: musl 1.2+ x86-64

py_alpha_lib-0.1.0-cp311-abi3-musllinux_1_2_x86_64.whl (877.7 kB view details)

Uploaded CPython 3.11+musllinux: musl 1.2+ x86-64

py_alpha_lib-0.1.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (660.6 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ x86-64

py_alpha_lib-0.1.0-cp311-abi3-macosx_11_0_arm64.whl (606.5 kB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

File details

Details for the file py_alpha_lib-0.1.0.tar.gz.

File metadata

  • Download URL: py_alpha_lib-0.1.0.tar.gz
  • Upload date:
  • Size: 100.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for py_alpha_lib-0.1.0.tar.gz
Algorithm Hash digest
SHA256 093b393307800b37e4b0278b62e03f9fddf2f42ef4cd37fc2be550e6bc78f026
MD5 9effcd882fde0994a88563b81e1f623c
BLAKE2b-256 7131a1310d09a4852430ddbe18335536daea637e7ccec4e66764fb240703db8b

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_alpha_lib-0.1.0.tar.gz:

Publisher: CI.yml on msd-rs/py-alpha-lib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py_alpha_lib-0.1.0-pp311-pypy311_pp73-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for py_alpha_lib-0.1.0-pp311-pypy311_pp73-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 c5700100bcacaa494df0a59a40874c8ece1a3e5f9c78f4bd327dff90044b6082
MD5 8c69a7ad09977620e8444679a5f5941d
BLAKE2b-256 eb8383ee8c698b0615d5c0ff5e82ac1b133d7316188e20dba8d3f5566dad116f

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_alpha_lib-0.1.0-pp311-pypy311_pp73-musllinux_1_2_x86_64.whl:

Publisher: CI.yml on msd-rs/py-alpha-lib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py_alpha_lib-0.1.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for py_alpha_lib-0.1.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c2c5548e7533575b42fb43f1890ebee190db2615279bcbdd822c05e740cad9a2
MD5 593c8fcf3863b80150d00bcfa4da61d2
BLAKE2b-256 d3157eb9dd7d86a1cdb618aef18f6dfe9e8ce59b6762130142dc5432d14d2d3d

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_alpha_lib-0.1.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: CI.yml on msd-rs/py-alpha-lib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py_alpha_lib-0.1.0-cp314-cp314t-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for py_alpha_lib-0.1.0-cp314-cp314t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 da5479515199c8ea73100e6c2f7ed8f53ef2ff33802a72d270281414541aa41d
MD5 237f44dcdb0b876e058973647dae52f0
BLAKE2b-256 4140d499b225006c1b9f8bf50b1a12275ba44399b980a870ec372cb70b343cb9

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_alpha_lib-0.1.0-cp314-cp314t-musllinux_1_2_x86_64.whl:

Publisher: CI.yml on msd-rs/py-alpha-lib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py_alpha_lib-0.1.0-cp314-abi3-win_amd64.whl.

File metadata

  • Download URL: py_alpha_lib-0.1.0-cp314-abi3-win_amd64.whl
  • Upload date:
  • Size: 531.2 kB
  • Tags: CPython 3.14+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for py_alpha_lib-0.1.0-cp314-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 df332ee5595fc5808b986d2f1a7035d83d180df8a20585b3e256f32ebd03eda6
MD5 fcce126c2294f3e1144ebf839e8f851e
BLAKE2b-256 7f8b00384177fe420b67dfec65d13f77f14705a4d3794fc9bdeab22723a45c2e

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_alpha_lib-0.1.0-cp314-abi3-win_amd64.whl:

Publisher: CI.yml on msd-rs/py-alpha-lib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py_alpha_lib-0.1.0-cp313-cp313t-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for py_alpha_lib-0.1.0-cp313-cp313t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 1af434063c32dde40b15ba19441203cbb219cdd3aa3dcbd2aa632598742e4355
MD5 950de6440e2444bad1195bc377a0ba65
BLAKE2b-256 73e89b4f78c1602b2be3ae9ac3a899d865a9eb3eebb72ca2bfb97388f10dfd12

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_alpha_lib-0.1.0-cp313-cp313t-musllinux_1_2_x86_64.whl:

Publisher: CI.yml on msd-rs/py-alpha-lib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py_alpha_lib-0.1.0-cp311-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for py_alpha_lib-0.1.0-cp311-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 dad4079f0c72f89ed8fa5ac573d8a862305e0990f1566dda83459afe187912bf
MD5 207ca2167260637586e26754796907db
BLAKE2b-256 1a7dfe9726a1e4ea9aa3d3442ba17fb552ba9a9d0f1bb4eb9f5fac26bbd1050a

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_alpha_lib-0.1.0-cp311-abi3-musllinux_1_2_x86_64.whl:

Publisher: CI.yml on msd-rs/py-alpha-lib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py_alpha_lib-0.1.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for py_alpha_lib-0.1.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 53f9eb0440d71e7a98cf9b3995ffe8215b0eb92d53e8dee35e51e81364ba4d9f
MD5 0cbe88ff19543da6608c74081fa714b5
BLAKE2b-256 d73da51c8c0f9e6b3c4ae26a72ca1a673ddb97afeeadf2682f90bff84947ec43

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_alpha_lib-0.1.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: CI.yml on msd-rs/py-alpha-lib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py_alpha_lib-0.1.0-cp311-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for py_alpha_lib-0.1.0-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9ecc9186c3f9cf9286cab13ea3a5f5fb574bf59afd38bf5880bcf0fedda648e9
MD5 7562d0492ea8fe648d2aef9db9affab0
BLAKE2b-256 e7d0cb46d4d3f01ca0ce8b95ea55de1db5d25ee803d329ba80fefa65603f54f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_alpha_lib-0.1.0-cp311-abi3-macosx_11_0_arm64.whl:

Publisher: CI.yml on msd-rs/py-alpha-lib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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