pickle-strategy-sdk 0.5.0__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.
Files changed (31) hide show
  1. pickle_strategy_sdk-0.5.0/LICENSE +202 -0
  2. pickle_strategy_sdk-0.5.0/NOTICE +10 -0
  3. pickle_strategy_sdk-0.5.0/PKG-INFO +219 -0
  4. pickle_strategy_sdk-0.5.0/README.md +202 -0
  5. pickle_strategy_sdk-0.5.0/pyproject.toml +33 -0
  6. pickle_strategy_sdk-0.5.0/setup.cfg +4 -0
  7. pickle_strategy_sdk-0.5.0/src/pickle_strategy/__init__.py +161 -0
  8. pickle_strategy_sdk-0.5.0/src/pickle_strategy/cli.py +206 -0
  9. pickle_strategy_sdk-0.5.0/src/pickle_strategy/codecs.py +1022 -0
  10. pickle_strategy_sdk-0.5.0/src/pickle_strategy/examples/__init__.py +7 -0
  11. pickle_strategy_sdk-0.5.0/src/pickle_strategy/examples/_ladder/__init__.py +1 -0
  12. pickle_strategy_sdk-0.5.0/src/pickle_strategy/examples/_ladder/data.py +332 -0
  13. pickle_strategy_sdk-0.5.0/src/pickle_strategy/examples/_ladder/engine.py +459 -0
  14. pickle_strategy_sdk-0.5.0/src/pickle_strategy/examples/data/ladder/fred_observations.csv +184 -0
  15. pickle_strategy_sdk-0.5.0/src/pickle_strategy/examples/data/ladder/fred_source.json +64 -0
  16. pickle_strategy_sdk-0.5.0/src/pickle_strategy/examples/data/ladder/manifest.json +36 -0
  17. pickle_strategy_sdk-0.5.0/src/pickle_strategy/examples/data/ladder/reference_prices.csv +1241 -0
  18. pickle_strategy_sdk-0.5.0/src/pickle_strategy/examples/data/ladder/security_master.csv +11 -0
  19. pickle_strategy_sdk-0.5.0/src/pickle_strategy/examples/ladder_strategy.py +62 -0
  20. pickle_strategy_sdk-0.5.0/src/pickle_strategy/examples/null_strategy.py +18 -0
  21. pickle_strategy_sdk-0.5.0/src/pickle_strategy/examples/sell_on_friday_strategy.py +53 -0
  22. pickle_strategy_sdk-0.5.0/src/pickle_strategy/loading.py +62 -0
  23. pickle_strategy_sdk-0.5.0/src/pickle_strategy/models.py +280 -0
  24. pickle_strategy_sdk-0.5.0/src/pickle_strategy/service.py +341 -0
  25. pickle_strategy_sdk-0.5.0/src/pickle_strategy/signing.py +530 -0
  26. pickle_strategy_sdk-0.5.0/src/pickle_strategy_sdk.egg-info/PKG-INFO +219 -0
  27. pickle_strategy_sdk-0.5.0/src/pickle_strategy_sdk.egg-info/SOURCES.txt +29 -0
  28. pickle_strategy_sdk-0.5.0/src/pickle_strategy_sdk.egg-info/dependency_links.txt +1 -0
  29. pickle_strategy_sdk-0.5.0/src/pickle_strategy_sdk.egg-info/entry_points.txt +2 -0
  30. pickle_strategy_sdk-0.5.0/src/pickle_strategy_sdk.egg-info/requires.txt +7 -0
  31. pickle_strategy_sdk-0.5.0/src/pickle_strategy_sdk.egg-info/top_level.txt +1 -0
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
202
+
@@ -0,0 +1,10 @@
1
+ Pickle Strategy SDK
2
+ Copyright 2026 PickleTech
3
+
4
+ This product includes software developed by PickleTech.
5
+
6
+ The installed ladder example includes observations retrieved from Federal
7
+ Reserve Economic Data (FRED), Federal Reserve Bank of St. Louis, and credited
8
+ on the cited series pages to the Board of Governors of the Federal Reserve
9
+ System (US). The modeled instruments and reference prices derived by PickleTech
10
+ are educational values, not observed or executable market quotes.
@@ -0,0 +1,219 @@
1
+ Metadata-Version: 2.4
2
+ Name: pickle-strategy-sdk
3
+ Version: 0.5.0
4
+ Summary: Broker-neutral contracts and hosting tools for Pickle strategies
5
+ License-Expression: Apache-2.0
6
+ Requires-Python: >=3.13
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE
9
+ License-File: NOTICE
10
+ Requires-Dist: simplejson<4,>=3.20
11
+ Requires-Dist: starlette<1,>=0.47
12
+ Requires-Dist: uvicorn<1,>=0.34
13
+ Provides-Extra: test
14
+ Requires-Dist: httpx<1,>=0.28; extra == "test"
15
+ Requires-Dist: pytest<10,>=9; extra == "test"
16
+ Dynamic: license-file
17
+
18
+ # Pickle strategy SDK
19
+
20
+ `pickle-strategy-sdk` is the broker-neutral contract and hosting package for
21
+ Pickle strategies. It contains no brokerage, live market-data, backtest,
22
+ calibration, SQLite, SciPy, or pandas code.
23
+
24
+ Python 3.13 or later is required.
25
+
26
+ Install it from PyPI:
27
+
28
+ ```sh
29
+ python -m pip install pickle-strategy-sdk
30
+ ```
31
+
32
+ ## Quickstart
33
+
34
+ A strategy is a plain synchronous callable. The `@strategy_service` decorator
35
+ returns the function unchanged, so direct calls and HTTP calls run the same
36
+ implementation:
37
+
38
+ <!-- readme-quickstart -->
39
+ ```python
40
+ from datetime import UTC, datetime
41
+ from decimal import Decimal
42
+
43
+ from pickle_strategy import BrokerAccountSnapshot, CashBalance, TicketRequest
44
+ from pickle_strategy.examples import ladder_strategy
45
+
46
+ as_of = datetime(2026, 6, 30, 16, tzinfo=UTC)
47
+ snapshot = BrokerAccountSnapshot(
48
+ "exe_quickstart", as_of, "USD", cash=(CashBalance("USD", Decimal("100000")),)
49
+ )
50
+ ticket = ladder_strategy(TicketRequest("req_quickstart", "exe_quickstart", as_of, snapshot))
51
+ ```
52
+
53
+ Serve any installed example over HTTP instead:
54
+
55
+ ```sh
56
+ pickle-strategy serve pickle_strategy.examples.null_strategy:null_strategy \
57
+ --allow-unauthenticated
58
+ pickle-strategy serve pickle_strategy.examples.ladder_strategy:ladder_strategy \
59
+ --hmac-secret-env PICKLE_STRATEGY_KEY
60
+ ```
61
+
62
+ The command listens on `127.0.0.1:8000`. `GET /health` returns
63
+ `{"status":"ok"}`; `POST /strategy` takes a JSON `TicketRequest` and returns a
64
+ `TradeTicket`. The hosted adapter accepts JSON request bodies up to 1 MiB by
65
+ default; embedded hosts can set a different positive byte limit with
66
+ `create_app(strategy, max_request_body_bytes=...)`.
67
+
68
+ ## Authenticating Pickle's calls
69
+
70
+ Serving is not open by default. `serve` requires either
71
+ `--hmac-secret-env NAME`, naming the environment variable that holds the key
72
+ Pickle signs with, or `--allow-unauthenticated`, which is refused on any
73
+ address but loopback. `create_app` makes the same choice explicit and raises
74
+ unless exactly one of `verifier=HmacVerifier(...)` or
75
+ `allow_unauthenticated=True` is given.
76
+
77
+ ```python
78
+ from pickle_strategy import HmacVerifier, create_app
79
+ from pickle_strategy.examples import ladder_strategy
80
+
81
+ app = create_app(ladder_strategy, verifier=HmacVerifier([key]))
82
+ ```
83
+
84
+ An `HmacVerifier` checks an HMAC-SHA256 signature over the raw request body,
85
+ the send time, and `request_id`; enforces a bounded replay window that defaults
86
+ to five minutes; and rejects a signature it has already accepted inside that
87
+ window. It accepts several keys at once so a rotation can deploy the new key
88
+ before Pickle switches to it. Authentication is carried entirely in headers, so
89
+ it adds no field to the `TicketRequest` payload and no schema version.
90
+
91
+ Pickle sends three headers. `X-Pickle-Timestamp` is the RFC 3339 UTC send time
92
+ to seconds precision, `X-Pickle-Request-Id` is the body's `request_id`, and
93
+ `X-Pickle-Signature` is `v1=` followed by the lowercase hex SHA-256 HMAC of
94
+ these four values joined by single newlines, in order:
95
+
96
+ ```text
97
+ pickle-strategy-auth-v1
98
+ <X-Pickle-Timestamp>
99
+ <X-Pickle-Request-Id>
100
+ <raw request body>
101
+ ```
102
+
103
+ The body is signed as the exact bytes on the wire, so a verifier must never
104
+ re-serialize the JSON before checking. A rejected request is answered `401`
105
+ with one of `missing_signature`, `malformed_signature`, `invalid_signature`,
106
+ `expired_signature`, or `replayed_signature`.
107
+
108
+ A signature authenticates a request but does not encrypt it, so serve the
109
+ endpoint over TLS. The verifier never forgets a signature that is still inside
110
+ its replay window; if it cannot track another one safely it answers
111
+ `503 replay_cache_exhausted` rather than dropping a still-valid entry.
112
+
113
+ Since `0.3.0`, `create_app` requires that explicit choice; earlier versions
114
+ had no verifier and served every request unsigned. Callers that relied on that
115
+ pass `allow_unauthenticated=True`.
116
+
117
+ ## Receiving execution reports
118
+
119
+ A deployment with a callback URL receives a signed execution report for each
120
+ material event of its orders. Receiving them is optional, and needs `0.5.0` or
121
+ later. A handler is a
122
+ synchronous function marked `@report_handler`; pass it to `create_app` with a
123
+ `ReportVerifier` built from the deployment's callback key, which is not the
124
+ request key, and the app serves `POST /reports`:
125
+
126
+ ```python
127
+ from pickle_strategy import (
128
+ ExecutionReport,
129
+ HmacVerifier,
130
+ ReportVerifier,
131
+ create_app,
132
+ report_handler,
133
+ )
134
+
135
+ @report_handler
136
+ def record(report: ExecutionReport) -> None:
137
+ store.save_once(report.event_id, report) # idempotent by event_id
138
+
139
+ app = create_app(
140
+ strategy,
141
+ verifier=HmacVerifier([request_key]),
142
+ report_handler=record,
143
+ report_verifier=ReportVerifier([callback_key]),
144
+ )
145
+ ```
146
+
147
+ `create_report_app(record, verifier=ReportVerifier([callback_key]))` serves
148
+ reports alone, for a deployment that runs no hosted strategy. The report route
149
+ has its own development opt-out, `allow_unauthenticated_reports=True`, so
150
+ `allow_unauthenticated` on the strategy route never opens it. From the command
151
+ line, `serve` takes `--report-handler module:function` and
152
+ `--report-secret-env NAME`, and `--allow-unauthenticated`, loopback only, opens
153
+ both routes.
154
+
155
+ A report is signed like a strategy request, in its own context, so a signature
156
+ for one never verifies as the other. It carries `X-Pickle-Timestamp`,
157
+ `X-Pickle-Event-Id`, `X-Pickle-Delivery-Id`, and `X-Pickle-Signature` over:
158
+
159
+ ```text
160
+ pickle-execution-report-v1
161
+ <X-Pickle-Timestamp>
162
+ <X-Pickle-Event-Id>
163
+ <X-Pickle-Delivery-Id>
164
+ <raw request body>
165
+ ```
166
+
167
+ `ReportVerifier.verify(headers, body)` applies the same window and replay rules
168
+ as `HmacVerifier`, decodes the report, and refuses one whose signed
169
+ `X-Pickle-Event-Id` is not the body's `event_id` (`mismatched_event_id`).
170
+ Rejections are answered `401`; a signed body that is not JSON, `400`, and one
171
+ that is not a report, `422`.
172
+
173
+ Delivery is at least once. Pickle retries a delivery your receiver does not
174
+ acknowledge with backoff, each time under a new delivery ID and signature, for
175
+ at least about an hour and a half, and longer while many reports wait for your
176
+ receiver, before it dead-letters the report for an operator to replay. Reports
177
+ about a customer Pickle has deactivated are held back and not sent at all. An
178
+ acknowledgement can be lost after the handler ran, so the same report can
179
+ arrive more than once. Deduplicate by
180
+ `event_id` in your own store, and return only once the report is recorded:
181
+ an exception answers `500`, and Pickle delivers the report again. Order and
182
+ gaps are in the body: `event_sequence` counts the reports for one execution
183
+ and callback URL from 1.
184
+
185
+ ## Contract version
186
+
187
+ Since `0.4.0`, the SDK speaks schema version `3.0`. Its decoders accept any
188
+ `3.x` payload and ignore fields they do not know, so a later additive minor
189
+ version still decodes, and its encoders emit exactly `3.0`. A ticket is
190
+ versioned from the request it answers, so a strategy always answers at a
191
+ version Pickle can read. Pickle reads a ticket strictly: it refuses one at a
192
+ later minor than its request, or with a field its version does not define. A
193
+ request recorded under `1.0` or `2.0` decodes strictly at that version, and a
194
+ strategy's ticket answers it at the same version. The normative schemas are
195
+ under `docs/contracts/v3` in the Pickle repository.
196
+
197
+ ## The ladder example
198
+
199
+ The ladder is backed by frozen public FRED federal-funds and Treasury-yield
200
+ observations plus deterministic modeled prices covering 2026-01-02 through
201
+ 2026-06-30. It serves requests from 2026-01-02T21:00Z, when its first modeled
202
+ prices become available, rejects requests outside that window with
203
+ `422 request_rejected`, and is not suitable for live trading. Its prices are
204
+ not observed or executable. Runtime performs no network or database access and
205
+ adds no SDK dependency.
206
+
207
+ ## License
208
+
209
+ The `pickle-strategy-sdk` distribution is licensed under the Apache License
210
+ 2.0. It may be used in commercial and proprietary strategies without requiring
211
+ those strategies to be open source. Redistributors must follow the license and
212
+ preserve the attribution in the packaged `NOTICE`. Both `LICENSE` and `NOTICE`
213
+ ship inside the wheel and source distribution.
214
+
215
+ This license applies to the SDK distribution rooted in this directory. It does
216
+ not license the rest of the private `PickleTech/pickle` repository. FRED source
217
+ observations retain the attribution and reuse terms identified in the packaged
218
+ source metadata; the Apache-2.0 grant applies to Pickle's code and derived-data
219
+ implementation, not as a claim of ownership over FRED observations.
@@ -0,0 +1,202 @@
1
+ # Pickle strategy SDK
2
+
3
+ `pickle-strategy-sdk` is the broker-neutral contract and hosting package for
4
+ Pickle strategies. It contains no brokerage, live market-data, backtest,
5
+ calibration, SQLite, SciPy, or pandas code.
6
+
7
+ Python 3.13 or later is required.
8
+
9
+ Install it from PyPI:
10
+
11
+ ```sh
12
+ python -m pip install pickle-strategy-sdk
13
+ ```
14
+
15
+ ## Quickstart
16
+
17
+ A strategy is a plain synchronous callable. The `@strategy_service` decorator
18
+ returns the function unchanged, so direct calls and HTTP calls run the same
19
+ implementation:
20
+
21
+ <!-- readme-quickstart -->
22
+ ```python
23
+ from datetime import UTC, datetime
24
+ from decimal import Decimal
25
+
26
+ from pickle_strategy import BrokerAccountSnapshot, CashBalance, TicketRequest
27
+ from pickle_strategy.examples import ladder_strategy
28
+
29
+ as_of = datetime(2026, 6, 30, 16, tzinfo=UTC)
30
+ snapshot = BrokerAccountSnapshot(
31
+ "exe_quickstart", as_of, "USD", cash=(CashBalance("USD", Decimal("100000")),)
32
+ )
33
+ ticket = ladder_strategy(TicketRequest("req_quickstart", "exe_quickstart", as_of, snapshot))
34
+ ```
35
+
36
+ Serve any installed example over HTTP instead:
37
+
38
+ ```sh
39
+ pickle-strategy serve pickle_strategy.examples.null_strategy:null_strategy \
40
+ --allow-unauthenticated
41
+ pickle-strategy serve pickle_strategy.examples.ladder_strategy:ladder_strategy \
42
+ --hmac-secret-env PICKLE_STRATEGY_KEY
43
+ ```
44
+
45
+ The command listens on `127.0.0.1:8000`. `GET /health` returns
46
+ `{"status":"ok"}`; `POST /strategy` takes a JSON `TicketRequest` and returns a
47
+ `TradeTicket`. The hosted adapter accepts JSON request bodies up to 1 MiB by
48
+ default; embedded hosts can set a different positive byte limit with
49
+ `create_app(strategy, max_request_body_bytes=...)`.
50
+
51
+ ## Authenticating Pickle's calls
52
+
53
+ Serving is not open by default. `serve` requires either
54
+ `--hmac-secret-env NAME`, naming the environment variable that holds the key
55
+ Pickle signs with, or `--allow-unauthenticated`, which is refused on any
56
+ address but loopback. `create_app` makes the same choice explicit and raises
57
+ unless exactly one of `verifier=HmacVerifier(...)` or
58
+ `allow_unauthenticated=True` is given.
59
+
60
+ ```python
61
+ from pickle_strategy import HmacVerifier, create_app
62
+ from pickle_strategy.examples import ladder_strategy
63
+
64
+ app = create_app(ladder_strategy, verifier=HmacVerifier([key]))
65
+ ```
66
+
67
+ An `HmacVerifier` checks an HMAC-SHA256 signature over the raw request body,
68
+ the send time, and `request_id`; enforces a bounded replay window that defaults
69
+ to five minutes; and rejects a signature it has already accepted inside that
70
+ window. It accepts several keys at once so a rotation can deploy the new key
71
+ before Pickle switches to it. Authentication is carried entirely in headers, so
72
+ it adds no field to the `TicketRequest` payload and no schema version.
73
+
74
+ Pickle sends three headers. `X-Pickle-Timestamp` is the RFC 3339 UTC send time
75
+ to seconds precision, `X-Pickle-Request-Id` is the body's `request_id`, and
76
+ `X-Pickle-Signature` is `v1=` followed by the lowercase hex SHA-256 HMAC of
77
+ these four values joined by single newlines, in order:
78
+
79
+ ```text
80
+ pickle-strategy-auth-v1
81
+ <X-Pickle-Timestamp>
82
+ <X-Pickle-Request-Id>
83
+ <raw request body>
84
+ ```
85
+
86
+ The body is signed as the exact bytes on the wire, so a verifier must never
87
+ re-serialize the JSON before checking. A rejected request is answered `401`
88
+ with one of `missing_signature`, `malformed_signature`, `invalid_signature`,
89
+ `expired_signature`, or `replayed_signature`.
90
+
91
+ A signature authenticates a request but does not encrypt it, so serve the
92
+ endpoint over TLS. The verifier never forgets a signature that is still inside
93
+ its replay window; if it cannot track another one safely it answers
94
+ `503 replay_cache_exhausted` rather than dropping a still-valid entry.
95
+
96
+ Since `0.3.0`, `create_app` requires that explicit choice; earlier versions
97
+ had no verifier and served every request unsigned. Callers that relied on that
98
+ pass `allow_unauthenticated=True`.
99
+
100
+ ## Receiving execution reports
101
+
102
+ A deployment with a callback URL receives a signed execution report for each
103
+ material event of its orders. Receiving them is optional, and needs `0.5.0` or
104
+ later. A handler is a
105
+ synchronous function marked `@report_handler`; pass it to `create_app` with a
106
+ `ReportVerifier` built from the deployment's callback key, which is not the
107
+ request key, and the app serves `POST /reports`:
108
+
109
+ ```python
110
+ from pickle_strategy import (
111
+ ExecutionReport,
112
+ HmacVerifier,
113
+ ReportVerifier,
114
+ create_app,
115
+ report_handler,
116
+ )
117
+
118
+ @report_handler
119
+ def record(report: ExecutionReport) -> None:
120
+ store.save_once(report.event_id, report) # idempotent by event_id
121
+
122
+ app = create_app(
123
+ strategy,
124
+ verifier=HmacVerifier([request_key]),
125
+ report_handler=record,
126
+ report_verifier=ReportVerifier([callback_key]),
127
+ )
128
+ ```
129
+
130
+ `create_report_app(record, verifier=ReportVerifier([callback_key]))` serves
131
+ reports alone, for a deployment that runs no hosted strategy. The report route
132
+ has its own development opt-out, `allow_unauthenticated_reports=True`, so
133
+ `allow_unauthenticated` on the strategy route never opens it. From the command
134
+ line, `serve` takes `--report-handler module:function` and
135
+ `--report-secret-env NAME`, and `--allow-unauthenticated`, loopback only, opens
136
+ both routes.
137
+
138
+ A report is signed like a strategy request, in its own context, so a signature
139
+ for one never verifies as the other. It carries `X-Pickle-Timestamp`,
140
+ `X-Pickle-Event-Id`, `X-Pickle-Delivery-Id`, and `X-Pickle-Signature` over:
141
+
142
+ ```text
143
+ pickle-execution-report-v1
144
+ <X-Pickle-Timestamp>
145
+ <X-Pickle-Event-Id>
146
+ <X-Pickle-Delivery-Id>
147
+ <raw request body>
148
+ ```
149
+
150
+ `ReportVerifier.verify(headers, body)` applies the same window and replay rules
151
+ as `HmacVerifier`, decodes the report, and refuses one whose signed
152
+ `X-Pickle-Event-Id` is not the body's `event_id` (`mismatched_event_id`).
153
+ Rejections are answered `401`; a signed body that is not JSON, `400`, and one
154
+ that is not a report, `422`.
155
+
156
+ Delivery is at least once. Pickle retries a delivery your receiver does not
157
+ acknowledge with backoff, each time under a new delivery ID and signature, for
158
+ at least about an hour and a half, and longer while many reports wait for your
159
+ receiver, before it dead-letters the report for an operator to replay. Reports
160
+ about a customer Pickle has deactivated are held back and not sent at all. An
161
+ acknowledgement can be lost after the handler ran, so the same report can
162
+ arrive more than once. Deduplicate by
163
+ `event_id` in your own store, and return only once the report is recorded:
164
+ an exception answers `500`, and Pickle delivers the report again. Order and
165
+ gaps are in the body: `event_sequence` counts the reports for one execution
166
+ and callback URL from 1.
167
+
168
+ ## Contract version
169
+
170
+ Since `0.4.0`, the SDK speaks schema version `3.0`. Its decoders accept any
171
+ `3.x` payload and ignore fields they do not know, so a later additive minor
172
+ version still decodes, and its encoders emit exactly `3.0`. A ticket is
173
+ versioned from the request it answers, so a strategy always answers at a
174
+ version Pickle can read. Pickle reads a ticket strictly: it refuses one at a
175
+ later minor than its request, or with a field its version does not define. A
176
+ request recorded under `1.0` or `2.0` decodes strictly at that version, and a
177
+ strategy's ticket answers it at the same version. The normative schemas are
178
+ under `docs/contracts/v3` in the Pickle repository.
179
+
180
+ ## The ladder example
181
+
182
+ The ladder is backed by frozen public FRED federal-funds and Treasury-yield
183
+ observations plus deterministic modeled prices covering 2026-01-02 through
184
+ 2026-06-30. It serves requests from 2026-01-02T21:00Z, when its first modeled
185
+ prices become available, rejects requests outside that window with
186
+ `422 request_rejected`, and is not suitable for live trading. Its prices are
187
+ not observed or executable. Runtime performs no network or database access and
188
+ adds no SDK dependency.
189
+
190
+ ## License
191
+
192
+ The `pickle-strategy-sdk` distribution is licensed under the Apache License
193
+ 2.0. It may be used in commercial and proprietary strategies without requiring
194
+ those strategies to be open source. Redistributors must follow the license and
195
+ preserve the attribution in the packaged `NOTICE`. Both `LICENSE` and `NOTICE`
196
+ ship inside the wheel and source distribution.
197
+
198
+ This license applies to the SDK distribution rooted in this directory. It does
199
+ not license the rest of the private `PickleTech/pickle` repository. FRED source
200
+ observations retain the attribution and reuse terms identified in the packaged
201
+ source metadata; the Apache-2.0 grant applies to Pickle's code and derived-data
202
+ implementation, not as a claim of ownership over FRED observations.
@@ -0,0 +1,33 @@
1
+ [build-system]
2
+ requires = ["setuptools>=77.0.3"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "pickle-strategy-sdk"
7
+ version = "0.5.0"
8
+ description = "Broker-neutral contracts and hosting tools for Pickle strategies"
9
+ readme = "README.md"
10
+ license = "Apache-2.0"
11
+ license-files = ["LICENSE", "NOTICE"]
12
+ requires-python = ">=3.13"
13
+ dependencies = [
14
+ "simplejson>=3.20,<4",
15
+ "starlette>=0.47,<1",
16
+ "uvicorn>=0.34,<1",
17
+ ]
18
+
19
+ [project.optional-dependencies]
20
+ test = [
21
+ "httpx>=0.28,<1",
22
+ "pytest>=9,<10",
23
+ ]
24
+
25
+ [project.scripts]
26
+ pickle-strategy = "pickle_strategy.cli:main"
27
+
28
+ [tool.setuptools.packages.find]
29
+ where = ["src"]
30
+ include = ["pickle_strategy*"]
31
+
32
+ [tool.setuptools.package-data]
33
+ "pickle_strategy.examples" = ["data/ladder/*.csv", "data/ladder/*.json"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+