Skip to main content

Algorithmic trading research utilities for quant teams

Project description

hqg-algorithms

Interfaces and helper types for writing HQG trading strategies.

Install

python3 -m pip install --upgrade pip setuptools wheel
pip install hqg-algorithms

Quick start

Subclass Strategy and implement three methods:

from hqg_algorithms import Strategy, Cadence, Slice, PortfolioView, BarSize, CallPhase


class BuyAndRebalance(Strategy):
    def universe(self) -> list[str]:
        return ["SPY", "IEF"]

    def cadence(self) -> Cadence:
        return Cadence(bar_size=BarSize.DAILY, call_phase=CallPhase.ON_BAR_CLOSE)

    def on_data(self, data: Slice, portfolio: PortfolioView) -> dict[str, float] | None:
        return {"SPY": 0.6, "IEF": 0.4}
Method Purpose
universe() Symbols the platform loads for this strategy
cadence() Bar resolution, trigger timing, and execution delay
on_data() Return target portfolio weights, {} for all cash, or None to skip an update

Slice exposes helpers like data.close("SPY") to read prices. PortfolioView gives read-only access to current equity, cash, positions, and weights.

Example — SMA crossover

from hqg_algorithms import Strategy, Cadence, Slice, PortfolioView, BarSize, CallPhase
from collections import deque


class SimpleSMA(Strategy):
    """Go risk-on when SPY is above its 21-day mean, otherwise hold bonds."""

    def __init__(self):
        self._window = 21
        self._q: deque[float] = deque(maxlen=self._window)

    def universe(self) -> list[str]:
        return ["SPY", "BND"]

    def cadence(self) -> Cadence:
        return Cadence(bar_size=BarSize.DAILY, call_phase=CallPhase.ON_BAR_CLOSE)

    def on_data(self, data: Slice, portfolio: PortfolioView) -> dict[str, float] | None:
        spy_close = data.close("SPY")
        if spy_close is None:
            return None

        self._q.append(spy_close)

        if len(self._q) < self._window:
            return {"BND": 1.0}  # hold bonds while warming up

        sma = sum(self._q) / len(self._q)

        if spy_close > sma:
            return {"SPY": 0.5, "BND": 0.5}  # uptrend
        return {"BND": 1.0}                  # downtrend

Additional docs

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

hqg_algorithms-0.1.1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

hqg_algorithms-0.1.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hqg_algorithms-0.1.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for hqg_algorithms-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2e2cf82844933141d1205fab90101b0be94b88e0189f794575d0d5b8ef25896b
MD5 3995d31c9b166dca575425d6d2a3124a
BLAKE2b-256 4145f0fe76be6a761244a916efa5955f3645e0de6cffb25c8a856bb1295ec996

See more details on using hashes here.

File details

Details for the file hqg_algorithms-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: hqg_algorithms-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for hqg_algorithms-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 294409be3787912eb333892f5d2ee9c9225dac2e84848a16e322d39197258a1d
MD5 90e059aa4610b17df58cb7656f0ab59f
BLAKE2b-256 7a5214402c3dcdd5d0f351741616be2c68236da7bba1503d0b1279cf5f8a6f42

See more details on using hashes here.

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