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 Calculate number of bars since last condition true https://www.amibroker.com/guide/afl/barslast.html
BARSSINCE Calculate number of bars since first condition true https://www.amibroker.com/guide/afl/barssince.html
BINS Discretize the input into n bins, the ctx.groups() is the number of groups Bins are 0-based index. Same value are assigned to the same bin.
CORR Calculate Correlation over a moving window Correlation = Cov(X, Y) / (StdDev(X) * StdDev(Y))
COUNT Calculate number of periods where condition is true in passed periods window https://www.amibroker.com/guide/afl/count.html
COV Calculate Covariance over a moving window Covariance = (SumXY - (SumX * SumY) / N) / (N - 1)
CROSS For 2 arrays A and B, return true if A[i-1] < B[i-1] and A[i] >= B[i] alias: golden_cross, cross_ge https://www.amibroker.com/guide/afl/cross.html
DMA Exponential Moving Average current = weight * current + (1 - weight) * previous https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
EMA Exponential Moving Average (variant of well-known EMA) weight = 2 / (n + 1) https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
FRET Future Return Calculates the return from the open price of the delayed day (t+delay) to the close price of the future day (t+delay+periods-1). Return = (Close[t+delay+periods-1] - Open[t+delay]) / Open[t+delay] If n=1, delay=1, it calculates (Close[t+1] - Open[t+1]) / Open[t+1]. If is_calc[t+delay] is 0, returns NaN.
HHV Find highest value in a preceding periods window https://www.amibroker.com/guide/afl/hhv.html
HHVBARS The number of periods that have passed since the array reached its periods period high https://www.amibroker.com/guide/afl/hhvbars.html
INTERCEPT Linear Regression Intercept Calculates the intercept of the linear regression line for a moving window.
LLV Find lowest value in a preceding periods window https://www.amibroker.com/guide/afl/llv.html
LLVBARS The number of periods that have passed since the array reached its periods period low https://www.amibroker.com/guide/afl/llvbars.html
LONGCROSS For 2 arrays A and B, return true if previous N periods A < B, Current A >= B
LWMA Linear Weighted Moving Average LWMA = SUM(Price * Weight) / SUM(Weight)
MA Simple Moving Average, also known as arithmetic moving average https://en.wikipedia.org/wiki/Moving_average#Simple_moving_average
NEUTRALIZE Neutralize the effect of a categorical variable on a numeric variable
PRODUCT Calculate product of values in preceding periods window If periods is 0, it calculates the cumulative product from the first valid value.
RANK Calculate rank percentage cross group dimension, the ctx.groups() is the number of groups Same value are averaged
RCROSS For 2 arrays A and B, return true if A[i-1] > B[i-1] and A[i] <= B[i] alias: death_cross, cross_le
REF Right shift input array by periods, r[i] = input[i - periods] https://www.amibroker.com/guide/afl/ref.html
REGBETA Calculate Regression Coefficient (Beta) of Y on X over a moving window Beta = Cov(X, Y) / Var(X)
REGRESI Calculate Regression Residual of Y on X over a moving window Returns the residual of the last point: epsilon = Y - (alpha + beta * X)
RLONGCROSS For 2 arrays A and B, return true if previous N periods A > B, Current A <= B
SLOPE Linear Regression Slope Calculates the slope of the linear regression line for a moving window.
SMA Exponential Moving Average (variant of well-known EMA) weight = m / n https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
STDDEV Calculate Standard Deviation over a moving window
SUM Calculate sum of values in preceding periods window If periods is 0, it calculates the cumulative sum from the first valid value. https://www.amibroker.com/guide/afl/sum.html
SUMBARS Calculate number of periods (bars) backwards until the sum of values is greater than or equal to amount https://www.amibroker.com/guide/afl/sumbars.html
SUMIF Calculate sum of values in preceding periods window where condition is true
TS_RANK Calculate rank in a sliding window with size periods
VAR Calculate Variance over a moving window Variance = (SumSq - (Sum^2)/N) / (N - 1)

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 = np.array([1, 2, None, 4, 5, 6, 7, 8, 9, 10], dtype=np.float64)
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.

Vibe Coding

When you need LLM to help you implement new factor in python, you can let LLM known which functions are available in alpha-lib by providing the list of supported functions as context.

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

Development

To contribute to the development of alpha-lib, you can clone the repository and set up a development environment.

Toolchain requirements:

  • Rust (latest stable)
  • Python (3.11+)
  • maturin (for building Python bindings)

Vibe Coding

This project is co-created with Gemini-3.0-Pro , when you want add new algo, use skill add_algo.md let AI to do correct code change for you.

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.1.tar.gz (132.8 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.1-pp311-pypy311_pp73-musllinux_1_2_x86_64.whl (961.7 kB view details)

Uploaded PyPymusllinux: musl 1.2+ x86-64

py_alpha_lib-0.1.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (741.5 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

py_alpha_lib-0.1.1-cp314-cp314t-musllinux_1_2_x86_64.whl (955.9 kB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ x86-64

py_alpha_lib-0.1.1-cp314-abi3-win_amd64.whl (617.6 kB view details)

Uploaded CPython 3.14+Windows x86-64

py_alpha_lib-0.1.1-cp313-cp313t-musllinux_1_2_x86_64.whl (956.6 kB view details)

Uploaded CPython 3.13tmusllinux: musl 1.2+ x86-64

py_alpha_lib-0.1.1-cp311-abi3-musllinux_1_2_x86_64.whl (961.2 kB view details)

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

py_alpha_lib-0.1.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (741.5 kB view details)

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

py_alpha_lib-0.1.1-cp311-abi3-macosx_11_0_arm64.whl (684.2 kB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: py_alpha_lib-0.1.1.tar.gz
  • Upload date:
  • Size: 132.8 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.1.tar.gz
Algorithm Hash digest
SHA256 0d605315ba5445c30f06c2c4f2ad8b8f23f5101a66eec325122097abeb281bac
MD5 405895e21a6bf620af97b7f963e4ec9e
BLAKE2b-256 ab42fa33d606c95728c35c7832ba4cbf4efe3d5f1214718ea0d19595d6fa81af

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_alpha_lib-0.1.1.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.1-pp311-pypy311_pp73-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for py_alpha_lib-0.1.1-pp311-pypy311_pp73-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 f2aad4638fefbaa667e87b4ef3eac7b23556c919e89b9d20565fff519cf52329
MD5 2c6a3c1a35aaad9ea675d189a82586a3
BLAKE2b-256 28d3b117c8e1b493431b6a6a50aac5a28cae2da77431f48a18abc97258d46033

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_alpha_lib-0.1.1-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.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for py_alpha_lib-0.1.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4afda0ace3371e8b4dac1915eb10f7db06b778b90735c0aa44ecf1f35cd06a92
MD5 8a171df4c36f55226d85bda8da765103
BLAKE2b-256 1430d6a70c5aad6dca785b7aa8c86be78e797cd68ad5148c3076df0e3d4c993c

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_alpha_lib-0.1.1-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.1-cp314-cp314t-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for py_alpha_lib-0.1.1-cp314-cp314t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d2e39256cb3177243ca50797b47578916dfa2b6e88c565956d0ca6a85dd342e0
MD5 24f2d6ad621611e4b1bbd478a9fc2770
BLAKE2b-256 353a817547ae7de06ebd9169469be9e0236c63c0822e2f9c69072e432c49fced

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_alpha_lib-0.1.1-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.1-cp314-abi3-win_amd64.whl.

File metadata

  • Download URL: py_alpha_lib-0.1.1-cp314-abi3-win_amd64.whl
  • Upload date:
  • Size: 617.6 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.1-cp314-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 670f12a5e371f09c6cfc90290ad0d880243f06c5c6660bc8d773613c9978b32d
MD5 00fc9ec22e35dfc40c321fb085d18f19
BLAKE2b-256 98692ede9f7962a96aea676c9f55b2ed7b61c111cd62cd0d78900df851ee5ec0

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_alpha_lib-0.1.1-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.1-cp313-cp313t-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for py_alpha_lib-0.1.1-cp313-cp313t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 4c023a136d926462aaa36c3ee3141bede1576bd6a6ae025a361b41f891f16fd4
MD5 c54fbce7f32373402c7e2768f9bcdc87
BLAKE2b-256 99655c085bde942969ee99093bf5dc60d5e59ef88c3f0b8e885cd0f06243951e

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_alpha_lib-0.1.1-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.1-cp311-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for py_alpha_lib-0.1.1-cp311-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e6c21b53c899f8f3563fcefeece32e73a47c686ea39b6cb68d64c28d9224ac5b
MD5 5598bc28fa29a9ad88c709452819567c
BLAKE2b-256 73896e53a92a9c2d907a5cf2f288b689932f41ab9f92193d0549a6040f684727

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_alpha_lib-0.1.1-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.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for py_alpha_lib-0.1.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b9fb0344f353d594536722fb91383314a6529973a1137ce4582c0b7daf51fb93
MD5 3d5065ec96fd8453329690813fc81657
BLAKE2b-256 c4c98d7763df84048a7b478526d572e30a5e80c4233e67eabef2fdc9533173d6

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_alpha_lib-0.1.1-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.1-cp311-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for py_alpha_lib-0.1.1-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c1cb435bc2b3fb099d0d6234a49022715d69706558d0ee2c29e2ad3297ff16b6
MD5 0eede5707c447d13ba580f1be7409f4c
BLAKE2b-256 27e464749488e3447e839d72100a197fb82c33a5fd7126a964ca62091a79168d

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_alpha_lib-0.1.1-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