hg-systematic 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hg_systematic-0.0.1/LICENSE +21 -0
- hg_systematic-0.0.1/PKG-INFO +91 -0
- hg_systematic-0.0.1/README.md +54 -0
- hg_systematic-0.0.1/pyproject.toml +130 -0
- hg_systematic-0.0.1/src/hg_systematic/__init__.py +0 -0
- hg_systematic-0.0.1/src/hg_systematic/data/__init__.py +0 -0
- hg_systematic-0.0.1/src/hg_systematic/data/generators.py +72 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Howard Henson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: hg_systematic
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A library and examples to show how HGraph can be used for systematic trading.
|
|
5
|
+
License: MIT
|
|
6
|
+
Keywords: reactive,graph,fpg,forward propogating graph,time series,functional reactive programming,frp,functional,time-series,systematic,trading,strategy
|
|
7
|
+
Author: Howard Henson
|
|
8
|
+
Author-email: howard@henson.me.uk
|
|
9
|
+
Requires-Python: >=3.12
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
18
|
+
Requires-Dist: frozendict (>=2.3.10)
|
|
19
|
+
Requires-Dist: hg_oap (>=0.1.9)
|
|
20
|
+
Requires-Dist: hgraph (>=0.3.26)
|
|
21
|
+
Requires-Dist: matplotlib
|
|
22
|
+
Requires-Dist: numpy (>=1.23)
|
|
23
|
+
Requires-Dist: ordered-set (>=4.1.0)
|
|
24
|
+
Requires-Dist: polars (>=1.0)
|
|
25
|
+
Requires-Dist: sortedcontainers (>=2.4.0)
|
|
26
|
+
Requires-Dist: sphinx (>=7.4)
|
|
27
|
+
Requires-Dist: sphinx-autodoc-typehints (>=2.3.0)
|
|
28
|
+
Requires-Dist: sphinx_rtd_theme (>=2.0.0)
|
|
29
|
+
Requires-Dist: sphinxcontrib-plantuml (>=0.30)
|
|
30
|
+
Project-URL: Changelog, https://github.com/hhenson/hg_systematic/blob/main/CHANGELOG.md
|
|
31
|
+
Project-URL: Documentation, https://github.com/hhenson/hg_systematic/blob/main/README.md
|
|
32
|
+
Project-URL: Homepage, https://github.com/hhenson/hg_systematic
|
|
33
|
+
Project-URL: Issues, https://github.com/hhenson/hg_systematic/blob/main/ISSUES.md
|
|
34
|
+
Project-URL: Repository, https://github.com/hhenson/hg_systematic.git
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
|
|
37
|
+
# HG Systematic
|
|
38
|
+
|
|
39
|
+
This is a library of utilities and examples to highlight how HGraph can be used to implement
|
|
40
|
+
systematic trading strategies.
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
See [this](https://hgraph.readthedocs.io/en/latest/) for more information.
|
|
44
|
+
|
|
45
|
+
## Development
|
|
46
|
+
|
|
47
|
+
The project is currently configured to make use of [Poetry](https://python-poetry.org) for dependency management.
|
|
48
|
+
Take a look at the website to see how best to install the tool.
|
|
49
|
+
|
|
50
|
+
Here are some useful commands:
|
|
51
|
+
|
|
52
|
+
First, this will cause the virtual environment to be installed in the same folder as the project (in .venv folder)
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
poetry config virtualenvs.in-project true
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Use this command to set the version of Python to make use of if you want a specific version of Python.
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
poetry env use 3.12
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Then use the following command to install the project and its dependencies. Note that the ``--with docs`` installs
|
|
65
|
+
the dependencies to build the documentation set which is not required otherwise, also the ``--all-extras`` is only
|
|
66
|
+
required for the adaptors.
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
poetry install --with docs --all-extras
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
If you did not use the first command, you can find the location of the installation using:
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
poetry env info
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
PyCharm can make use of poetry to ``setup`` the project.
|
|
79
|
+
|
|
80
|
+
### Run Tests
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
# No Coverage
|
|
84
|
+
poetry run pytest
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
# Generate Coverage Report
|
|
89
|
+
poetry run pytest --cov=your_package_name --cov-report=xml
|
|
90
|
+
```
|
|
91
|
+
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# HG Systematic
|
|
2
|
+
|
|
3
|
+
This is a library of utilities and examples to highlight how HGraph can be used to implement
|
|
4
|
+
systematic trading strategies.
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
See [this](https://hgraph.readthedocs.io/en/latest/) for more information.
|
|
8
|
+
|
|
9
|
+
## Development
|
|
10
|
+
|
|
11
|
+
The project is currently configured to make use of [Poetry](https://python-poetry.org) for dependency management.
|
|
12
|
+
Take a look at the website to see how best to install the tool.
|
|
13
|
+
|
|
14
|
+
Here are some useful commands:
|
|
15
|
+
|
|
16
|
+
First, this will cause the virtual environment to be installed in the same folder as the project (in .venv folder)
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
poetry config virtualenvs.in-project true
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Use this command to set the version of Python to make use of if you want a specific version of Python.
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
poetry env use 3.12
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Then use the following command to install the project and its dependencies. Note that the ``--with docs`` installs
|
|
29
|
+
the dependencies to build the documentation set which is not required otherwise, also the ``--all-extras`` is only
|
|
30
|
+
required for the adaptors.
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
poetry install --with docs --all-extras
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
If you did not use the first command, you can find the location of the installation using:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
poetry env info
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
PyCharm can make use of poetry to ``setup`` the project.
|
|
43
|
+
|
|
44
|
+
### Run Tests
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
# No Coverage
|
|
48
|
+
poetry run pytest
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# Generate Coverage Report
|
|
53
|
+
poetry run pytest --cov=your_package_name --cov-report=xml
|
|
54
|
+
```
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "hg_systematic"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
description = """ \
|
|
5
|
+
A library and examples to show how HGraph can be used for systematic trading. \
|
|
6
|
+
"""
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
license = "MIT"
|
|
9
|
+
authors = [
|
|
10
|
+
"Howard Henson <howard@henson.me.uk>",
|
|
11
|
+
]
|
|
12
|
+
keywords = [
|
|
13
|
+
"reactive", "graph", "fpg", "forward propogating graph",
|
|
14
|
+
"time series", "functional reactive programming",
|
|
15
|
+
"frp", "functional", "time-series", "systematic", "trading",
|
|
16
|
+
"strategy"
|
|
17
|
+
]
|
|
18
|
+
classifiers = [
|
|
19
|
+
"Development Status :: 3 - Alpha",
|
|
20
|
+
"Programming Language :: Python",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: Implementation :: CPython",
|
|
24
|
+
]
|
|
25
|
+
packages = [
|
|
26
|
+
{ include = "hg_systematic", from = "src" },
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[tool.poetry.dependencies]
|
|
30
|
+
python = ">=3.12"
|
|
31
|
+
hgraph = ">=0.3.26"
|
|
32
|
+
hg_oap = ">=0.1.9"
|
|
33
|
+
frozendict = ">=2.3.10"
|
|
34
|
+
sortedcontainers = ">=2.4.0"
|
|
35
|
+
ordered-set = ">=4.1.0"
|
|
36
|
+
numpy = ">=1.23"
|
|
37
|
+
polars = ">=1.0"
|
|
38
|
+
matplotlib = "*"
|
|
39
|
+
sphinx = { version = ">=7.4", optional = true }
|
|
40
|
+
sphinx_rtd_theme = { version = ">=2.0.0", optional = true }
|
|
41
|
+
sphinx-autodoc-typehints = { version = ">=2.3.0", optional = true }
|
|
42
|
+
sphinxcontrib-plantuml = { version = ">=0.30", optional = true }
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
#[tool.poetry.extras]
|
|
46
|
+
#web = [
|
|
47
|
+
#]
|
|
48
|
+
|
|
49
|
+
[tool.poetry.group.docs]
|
|
50
|
+
optional = true
|
|
51
|
+
|
|
52
|
+
[tool.poetry.group.docs.dependencies]
|
|
53
|
+
sphinx = "*"
|
|
54
|
+
sphinx_rtd_theme = "*"
|
|
55
|
+
sphinx-autodoc-typehints = "*"
|
|
56
|
+
sphinxcontrib-plantuml = "*"
|
|
57
|
+
|
|
58
|
+
[tool.poetry.urls]
|
|
59
|
+
Homepage = "https://github.com/hhenson/hg_systematic"
|
|
60
|
+
Documentation = "https://github.com/hhenson/hg_systematic/blob/main/README.md"
|
|
61
|
+
Repository = "https://github.com/hhenson/hg_systematic.git"
|
|
62
|
+
Issues = "https://github.com/hhenson/hg_systematic/blob/main/ISSUES.md"
|
|
63
|
+
Changelog = "https://github.com/hhenson/hg_systematic/blob/main/CHANGELOG.md"
|
|
64
|
+
|
|
65
|
+
[build-system]
|
|
66
|
+
requires = ["poetry-core"]
|
|
67
|
+
build-backend = "poetry.core.masonry.api"
|
|
68
|
+
|
|
69
|
+
[tool.poetry.group.test.dependencies]
|
|
70
|
+
pytest = "*"
|
|
71
|
+
mypy = "*"
|
|
72
|
+
pytest-cov = "*"
|
|
73
|
+
coverage = "*"
|
|
74
|
+
pytest-xdist = "*"
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
# Configure pytest
|
|
78
|
+
[tool.pytest.ini_options]
|
|
79
|
+
minversion = "7.4.3"
|
|
80
|
+
#addopts = "-n auto -ra -q --dist=loadscope" # " --cov=hgraph --cov-report=term-missing --cov-report=xml"
|
|
81
|
+
testpaths = [
|
|
82
|
+
"tests",
|
|
83
|
+
]
|
|
84
|
+
markers = [
|
|
85
|
+
"serial: mark test to run serially (not in parallel with xdist)",
|
|
86
|
+
]
|
|
87
|
+
|
|
88
|
+
# Configure coverage
|
|
89
|
+
[tool.coverage.run]
|
|
90
|
+
source_pkgs = ["hg_systematic",]
|
|
91
|
+
branch = true
|
|
92
|
+
parallel = true
|
|
93
|
+
omit = [
|
|
94
|
+
"tests/*",
|
|
95
|
+
"examples/*",
|
|
96
|
+
"docs/*",
|
|
97
|
+
]
|
|
98
|
+
|
|
99
|
+
[tool.coverage.paths]
|
|
100
|
+
hg = ["src/hg_systematic",]
|
|
101
|
+
tests = ["tests",]
|
|
102
|
+
|
|
103
|
+
[tool.coverage.report]
|
|
104
|
+
fail_under = 80
|
|
105
|
+
exclude_lines = [
|
|
106
|
+
"no cov",
|
|
107
|
+
"if __name__ == .__main__.:",
|
|
108
|
+
"if TYPE_CHECKING:",
|
|
109
|
+
]
|
|
110
|
+
|
|
111
|
+
[tool.black]
|
|
112
|
+
line-length = 120
|
|
113
|
+
target-version = ['py312']
|
|
114
|
+
unstable = true
|
|
115
|
+
include = '\.pyi?$'
|
|
116
|
+
# 'extend-exclude' excludes files or directories in addition to the defaults
|
|
117
|
+
extend-exclude = '''
|
|
118
|
+
# A regex preceded with ^/ will apply only to files and directories
|
|
119
|
+
# in the root of the project.
|
|
120
|
+
(
|
|
121
|
+
^/foo.py # exclude a file named foo.py in the root of the project
|
|
122
|
+
| .*_pb2.py # exclude autogenerated Protocol Buffer files anywhere in the project
|
|
123
|
+
)
|
|
124
|
+
'''
|
|
125
|
+
|
|
126
|
+
[tool.mypy]
|
|
127
|
+
files = ["src",]
|
|
128
|
+
|
|
129
|
+
[tool.poetry.scripts]
|
|
130
|
+
build-docs = "sphinx.cmd.build:main"
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import math
|
|
2
|
+
from datetime import timedelta, datetime
|
|
3
|
+
from zoneinfo import ZoneInfo
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
from hg_oap.dates.dt_utils import date_tz_to_utc
|
|
7
|
+
from hgraph import generator, TS, EvaluationEngineApi, graph, lag, delayed_binding, feedback, compute_node, \
|
|
8
|
+
RECORDABLE_STATE, TimeSeriesSchema, STATE, SIGNAL
|
|
9
|
+
|
|
10
|
+
__all__ = ["white_noise_generator", "auto_regressive_generator"]
|
|
11
|
+
|
|
12
|
+
@compute_node
|
|
13
|
+
def white_noise_generator(
|
|
14
|
+
signal: SIGNAL,
|
|
15
|
+
loc: float = 0.0,
|
|
16
|
+
scale: float = 1.0,
|
|
17
|
+
size: int = 1000,
|
|
18
|
+
_state: STATE = None
|
|
19
|
+
) -> TS[float]:
|
|
20
|
+
"""
|
|
21
|
+
Generates a stream of white noise at each tick of the ``signal`` input.
|
|
22
|
+
The buffer will be initialised at start, and will be re-initialised if the number of ticks exceeds the buffer
|
|
23
|
+
``size``.
|
|
24
|
+
The noise stream can now be generated based on any input signal, for example, a calendar ticking out business days.
|
|
25
|
+
"""
|
|
26
|
+
out = _state.buffer[_state.ndx]
|
|
27
|
+
_state.ndx += 1
|
|
28
|
+
if size == _state.ndx:
|
|
29
|
+
_state.buffer = np.random.normal(loc, scale, size=size)
|
|
30
|
+
_state.ndx = 0
|
|
31
|
+
return out
|
|
32
|
+
|
|
33
|
+
@white_noise_generator.start
|
|
34
|
+
def white_noise_generator_start(
|
|
35
|
+
loc: float = 0.0,
|
|
36
|
+
scale: float = 1.0,
|
|
37
|
+
size: int = 1000,
|
|
38
|
+
_state: STATE = None
|
|
39
|
+
):
|
|
40
|
+
_state.buffer = np.random.normal(loc, scale, size=size)
|
|
41
|
+
_state.ndx = 0
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class ARState(TimeSeriesSchema):
|
|
46
|
+
previous_terms: TS[tuple[float,...]]
|
|
47
|
+
|
|
48
|
+
@compute_node(
|
|
49
|
+
requires=lambda m, s: len(s["initial_values"]) == (order:=s["order"]) and len(s["coefficients"]) == order+1
|
|
50
|
+
)
|
|
51
|
+
def auto_regressive_generator(
|
|
52
|
+
white_noise: TS[float],
|
|
53
|
+
order: int = 1,
|
|
54
|
+
initial_values: tuple[float, ...] = (1.0,),
|
|
55
|
+
coefficients: tuple[float, ...] = (1.0, 0.5),
|
|
56
|
+
_state: RECORDABLE_STATE[ARState] = None
|
|
57
|
+
) -> TS[float]:
|
|
58
|
+
"""
|
|
59
|
+
An autoregressive generator.
|
|
60
|
+
The order defines how many terms to use.
|
|
61
|
+
The size of the initial values is ``order`` and coefficients must be the size of ``order+1``.
|
|
62
|
+
"""
|
|
63
|
+
result = white_noise.value + coefficients[0]
|
|
64
|
+
prev = _state.previous_terms.value
|
|
65
|
+
result += sum(coefficients[i+1] * prev[i] for i in range(order))
|
|
66
|
+
_state.previous_terms.apply_result((result,) + prev[1:])
|
|
67
|
+
return result
|
|
68
|
+
|
|
69
|
+
@auto_regressive_generator.start
|
|
70
|
+
def autoregressive_generator_start(initial_values: tuple[float, ...], _state: RECORDABLE_STATE[ARState] = None):
|
|
71
|
+
_state.previous_terms.apply_result(initial_values)
|
|
72
|
+
|