Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci(codspeed): setup codspeed benchmarking #3

Merged
merged 9 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions .github/workflows/codspeed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: codspeed-benchmarks

on:
push:
branches:
- "main" # or "master"
pull_request:
# `workflow_dispatch` allows CodSpeed to trigger backtest
# performance analysis in order to generate initial data.
workflow_dispatch:

env:
UV_SYSTEM_PYTHON: 1

jobs:
benchmarks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install uv
uses: astral-sh/setup-uv@v2

- name: "Set up Python"
uses: actions/setup-python@v5
with:
python-version-file: "pyproject.toml"

- name: Install the project
run: uv sync --all-extras --dev

- name: Run benchmarks
uses: CodSpeedHQ/action@v3
with:
token: ${{ secrets.CODSPEED_TOKEN }}
run: uv run pytest --codspeed
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ venv
__pycache__/
data/
.DS_Store
scratchpad.py
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Polars Trading
[![CodSpeed Badge](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://codspeed.io/ngriffiths13/polars-trading)
9 changes: 9 additions & 0 deletions polars_trading/_testing/labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ def get_daily_vol(close: pd.Series, span0: int = 100) -> pd.Series:
)
df0 = close.loc[df0.index] / close.loc[df0.values].values - 1
return df0.ewm(span=span0).std().fillna(0.0)


def apply_pt_sl_on_t1(
close: pd.Series, events: pd.DataFrame, pt_sl: tuple[float, float]
) -> None:
"""Apply stop loss and profit taking.

AFML pg. 45
"""
4 changes: 4 additions & 0 deletions polars_trading/labels/dynamic_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def daily_vol(
in the last 24 hour period to the current price. It then calculates the exponential
weighted standard deviation of the returns.

This currently fails to account for weekend returns when there is no trading.

Marco Lopez de Prado, Advances in Financial Machine Learning, pg. 44

Args:
----
df (DataFrame): The DataFrame containing the price series.
Expand Down
15 changes: 9 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ classifiers = [
]
dependencies = ["polars[plot]>=1.3"]


[tool.ruff.format]
docstring-code-format = true

Expand Down Expand Up @@ -83,21 +84,21 @@ ignore_missing_imports = true
dev-dependencies = [
"build>=1.2.1",
"commitizen>=3.29.0",
"hypothesis>=6.111.2",
"marimo>=0.8.8",
"maturin>=1.7.1",
"mimesis>=12.1.0",
"mkdocs-gen-files>=0.5.0",
"mkdocstrings[python]>=0.26.1",
"pandas>=2.2.2",
"pip>=24.2",
"pre-commit>=3.8.0",
"pyarrow>=17.0.0",
"pytest-benchmark>=4.0.0",
"pytest>=8.3.2",
"ruff>=0.6.3",
"setuptools>=74.1.0",
"wheel>=0.44.0",
"pandas>=2.2.2",
"pyarrow>=17.0.0",
"pytest>=8.3.2",
"pytest-benchmark[histogram]>=4.0.0",
"pytest-codspeed>=2.2.1",
"mimesis>=12.1.0",
]

[tool.commitizen]
Expand All @@ -109,7 +110,9 @@ update_changelog_on_bump = true
major_version_zero = true

[tool.pytest.ini_options]
markers = ["pandas"]
addopts = [
"--benchmark-disable",
"--benchmark-columns=min,mean,median,max,stddev",
"-m not pandas",
]
1 change: 1 addition & 0 deletions tests/labels/test_dynamic_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def test__daily_vol__polars_benchmark(benchmark, trade_data):
benchmark(daily_vol(trade_data.lazy(), "ts_event", "price", "symbol", 100).collect)


@pytest.mark.pandas
@pytest.mark.benchmark(group="daily_vol")
@pytest.mark.parametrize(
"trade_data",
Expand Down
2 changes: 2 additions & 0 deletions tests/test_bars.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def test__time_bars__polars_benchmark(benchmark, trade_data):
benchmark(time_bars, trade_data, timestamp_col="ts_event", bar_size="1d")


@pytest.mark.pandas
@pytest.mark.benchmark(group="time_bars")
@pytest.mark.parametrize(
"trade_data",
Expand Down Expand Up @@ -135,6 +136,7 @@ def test__tick_bars__polars_benchmark(benchmark, trade_data):
benchmark(tick_bars, trade_data, timestamp_col="ts_event", bar_size=100)


@pytest.mark.pandas
@pytest.mark.benchmark(group="tick_bars")
@pytest.mark.parametrize(
"trade_data",
Expand Down
153 changes: 125 additions & 28 deletions uv.lock

Large diffs are not rendered by default.