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
- Publishing workflow and release checklist:
docs/publishing.md
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e2cf82844933141d1205fab90101b0be94b88e0189f794575d0d5b8ef25896b
|
|
| MD5 |
3995d31c9b166dca575425d6d2a3124a
|
|
| BLAKE2b-256 |
4145f0fe76be6a761244a916efa5955f3645e0de6cffb25c8a856bb1295ec996
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
294409be3787912eb333892f5d2ee9c9225dac2e84848a16e322d39197258a1d
|
|
| MD5 |
90e059aa4610b17df58cb7656f0ab59f
|
|
| BLAKE2b-256 |
7a5214402c3dcdd5d0f351741616be2c68236da7bba1503d0b1279cf5f8a6f42
|