diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 8268eaf..ad3ec43 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -1,4 +1,4 @@ -# This file is autogenerated by maturin v1.1.0 +# This file is autogenerated by maturin v1.7.4 # To update, run # # maturin generate-ci github @@ -20,101 +20,162 @@ permissions: jobs: linux: - runs-on: ubuntu-latest + runs-on: ${{ matrix.platform.runner }} strategy: matrix: - target: [x86_64, x86, aarch64, armv7, s390x, ppc64le] + platform: + - runner: ubuntu-latest + target: x86_64 + - runner: ubuntu-latest + target: x86 + - runner: ubuntu-latest + target: aarch64 + - runner: ubuntu-latest + target: armv7 + - runner: ubuntu-latest + target: s390x + - runner: ubuntu-latest + target: ppc64le steps: - - uses: actions/checkout@v3 - - uses: actions/setup-python@v4 + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 with: - python-version: '3.10' + python-version: 3.x - name: Build wheels uses: PyO3/maturin-action@v1 with: - target: ${{ matrix.target }} + target: ${{ matrix.platform.target }} args: --release --out dist --find-interpreter sccache: 'true' manylinux: auto - name: Upload wheels - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 + with: + name: wheels-linux-${{ matrix.platform.target }} + path: dist + + musllinux: + runs-on: ${{ matrix.platform.runner }} + strategy: + matrix: + platform: + - runner: ubuntu-latest + target: x86_64 + - runner: ubuntu-latest + target: x86 + - runner: ubuntu-latest + target: aarch64 + - runner: ubuntu-latest + target: armv7 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: 3.x + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.platform.target }} + args: --release --out dist --find-interpreter + sccache: 'true' + manylinux: musllinux_1_2 + - name: Upload wheels + uses: actions/upload-artifact@v4 with: - name: wheels + name: wheels-musllinux-${{ matrix.platform.target }} path: dist windows: - runs-on: windows-latest + runs-on: ${{ matrix.platform.runner }} strategy: matrix: - target: [x64, x86] + platform: + - runner: windows-latest + target: x64 + - runner: windows-latest + target: x86 steps: - - uses: actions/checkout@v3 - - uses: actions/setup-python@v4 + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 with: - python-version: '3.10' - architecture: ${{ matrix.target }} + python-version: 3.x + architecture: ${{ matrix.platform.target }} - name: Build wheels uses: PyO3/maturin-action@v1 with: - target: ${{ matrix.target }} + target: ${{ matrix.platform.target }} args: --release --out dist --find-interpreter sccache: 'true' - name: Upload wheels - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: - name: wheels + name: wheels-windows-${{ matrix.platform.target }} path: dist macos: - runs-on: macos-latest + runs-on: ${{ matrix.platform.runner }} strategy: matrix: - target: [x86_64, aarch64] + platform: + - runner: macos-12 + target: x86_64 + - runner: macos-14 + target: aarch64 steps: - - uses: actions/checkout@v3 - - uses: actions/setup-python@v4 + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 with: - python-version: '3.10' + python-version: 3.x - name: Build wheels uses: PyO3/maturin-action@v1 with: - target: ${{ matrix.target }} + target: ${{ matrix.platform.target }} args: --release --out dist --find-interpreter sccache: 'true' - name: Upload wheels - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: - name: wheels + name: wheels-macos-${{ matrix.platform.target }} path: dist sdist: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Build sdist uses: PyO3/maturin-action@v1 with: command: sdist args: --out dist - name: Upload sdist - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: - name: wheels + name: wheels-sdist path: dist release: name: Release runs-on: ubuntu-latest - if: "startsWith(github.ref, 'refs/tags/')" - needs: [linux, windows, macos, sdist] + if: ${{ startsWith(github.ref, 'refs/tags/') || github.event_name == 'workflow_dispatch' }} + needs: [linux, musllinux, windows, macos, sdist] + permissions: + # Use to sign the release artifacts + id-token: write + # Used to upload release artifacts + contents: write + # Used to generate artifact attestation + attestations: write steps: - - uses: actions/download-artifact@v3 + - uses: actions/download-artifact@v4 + - name: Generate artifact attestation + uses: actions/attest-build-provenance@v1 with: - name: wheels + subject-path: 'wheels-*/*' - name: Publish to PyPI + if: "startsWith(github.ref, 'refs/tags/')" uses: PyO3/maturin-action@v1 env: MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }} with: command: upload - args: --skip-existing * + args: --non-interactive --skip-existing wheels-*/* diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..2cd7613 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,41 @@ +name: test + +on: + push: + branches: [ master, "release-*" ] + pull_request: + branches: [ master, "release-*" ] + +jobs: + build: + + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + python-version: ["3.9", "3.10", "3.11", "3.12"] + + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Run maturin build + uses: PyO3/maturin-action@v1 + with: + manylinux: auto + command: build + args: --release --sdist -o dist --interpreter python${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install -r requirements-test.txt + - name: Install package + run: python3 -m pip install --user dist/kelly*.tar.gz + - name: Lint with black + run: | + black . --check + - name: Test with pytest + run: | + pytest \ No newline at end of file diff --git a/README.md b/README.md index 4e42db8..d7f40f3 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Scenarios to be covered in the near future include: ## Installation -Requires Python 3.7 or above +Requires Python 3.9 or above ``` pip install git+https://github.com/mberk/kelly.git diff --git a/pyproject.toml b/pyproject.toml index f91b457..59b2baa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "maturin" [project] name = "kelly" -requires-python = ">=3.7" +requires-python = ">=3.9" classifiers = [ "Programming Language :: Rust", "Programming Language :: Python :: Implementation :: CPython", diff --git a/requirements-test.txt b/requirements-test.txt new file mode 100644 index 0000000..1ee5da5 --- /dev/null +++ b/requirements-test.txt @@ -0,0 +1,2 @@ +black==24.8.0 +pytest==8.3.3 \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index bc22f2a..a18ccac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -222,6 +222,11 @@ fn calculate_kelly_stake( kelly_fraction: f64, verbose: bool, ) -> PyResult { + let upper_bound = if is_back { + bankroll + } else { + bankroll / (price - 1.0) + }; let kelly_stake = bounded_minimisation( |stake| { -calculate_log_expected_wealth( @@ -236,7 +241,7 @@ fn calculate_kelly_stake( ) }, 0.0, - bankroll, // TODO: Fix upper bound; this should depend on current position and might even exceed the bankroll if green on all selections + upper_bound, // TODO: Fix upper bound; this should depend on current position and might even exceed the bankroll if green on all selections verbose, ); Ok(kelly_stake * kelly_fraction) diff --git a/tests/test_kelly.py b/tests/test_kelly.py new file mode 100644 index 0000000..13ec684 --- /dev/null +++ b/tests/test_kelly.py @@ -0,0 +1,17 @@ +import kelly + + +def test_calculate_kelly_stake(): + stake = kelly.calculate_kelly_stake( + price=100, + is_back=False, + probability=1 / 100, + other_probabilities=[99 / 100], + position=0.0, + other_positions=[0.0], + bankroll=100.0, + kelly_fraction=1.0, + verbose=True, + ) + assert stake > 0 + assert round(stake, 2) == 0.0