friction-engine 0.1.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.
- friction_engine-0.1.0/LICENSE +189 -0
- friction_engine-0.1.0/PKG-INFO +150 -0
- friction_engine-0.1.0/README.md +126 -0
- friction_engine-0.1.0/pyproject.toml +39 -0
- friction_engine-0.1.0/setup.cfg +4 -0
- friction_engine-0.1.0/src/friction_engine/__init__.py +48 -0
- friction_engine-0.1.0/src/friction_engine/fees.py +131 -0
- friction_engine-0.1.0/src/friction_engine/liquidity.py +41 -0
- friction_engine-0.1.0/src/friction_engine/mae_mfe.py +184 -0
- friction_engine-0.1.0/src/friction_engine/models.py +88 -0
- friction_engine-0.1.0/src/friction_engine/slippage.py +132 -0
- friction_engine-0.1.0/src/friction_engine.egg-info/PKG-INFO +150 -0
- friction_engine-0.1.0/src/friction_engine.egg-info/SOURCES.txt +18 -0
- friction_engine-0.1.0/src/friction_engine.egg-info/dependency_links.txt +1 -0
- friction_engine-0.1.0/src/friction_engine.egg-info/requires.txt +3 -0
- friction_engine-0.1.0/src/friction_engine.egg-info/top_level.txt +1 -0
- friction_engine-0.1.0/tests/test_fees.py +82 -0
- friction_engine-0.1.0/tests/test_liquidity.py +75 -0
- friction_engine-0.1.0/tests/test_mae_mfe.py +116 -0
- friction_engine-0.1.0/tests/test_slippage.py +86 -0
|
@@ -0,0 +1,189 @@
|
|
|
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 as
|
|
87
|
+
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, provided that You
|
|
91
|
+
meet the following conditions:
|
|
92
|
+
|
|
93
|
+
(a) You must give any other recipients of the Work or
|
|
94
|
+
Derivative Works a copy of this License; and
|
|
95
|
+
|
|
96
|
+
(b) You must cause any modified files to carry prominent notices
|
|
97
|
+
stating that You changed the files; and
|
|
98
|
+
|
|
99
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
100
|
+
that You distribute, all copyright, patent, trademark, and
|
|
101
|
+
attribution notices from the Source form of the Work,
|
|
102
|
+
excluding those notices that do not pertain to any part of
|
|
103
|
+
the Derivative Works; and
|
|
104
|
+
|
|
105
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
106
|
+
distribution, then any Derivative Works that You distribute must
|
|
107
|
+
include a readable copy of the attribution notices contained
|
|
108
|
+
within such NOTICE file, excluding those notices that do not
|
|
109
|
+
pertain to any part of the Derivative Works, in at least one
|
|
110
|
+
of the following places: within a NOTICE text file distributed
|
|
111
|
+
as part of the Derivative Works; within the Source form or
|
|
112
|
+
documentation, if provided along with the Derivative Works; or,
|
|
113
|
+
within a file titled "NOTICE" file in the root directory of
|
|
114
|
+
the Derivative Works; or within a file titled "NOTICE" file in
|
|
115
|
+
the root directory of the Source form. You may copy and
|
|
116
|
+
distribute Derivative Works of the Work only under this License,
|
|
117
|
+
in which the Licensor agrees to place attribution notices
|
|
118
|
+
contained in such NOTICE file. You may reproduce and distribute
|
|
119
|
+
copies of the Work for any purpose, provided that You also meet
|
|
120
|
+
the conditions of the License for Derivative Works as set forth
|
|
121
|
+
in the NOTICE file.
|
|
122
|
+
|
|
123
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
124
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
125
|
+
by You to the Licensor shall be under the terms and conditions of this
|
|
126
|
+
License, without any additional terms or conditions.
|
|
127
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
128
|
+
the terms of any separate license agreement you may have executed
|
|
129
|
+
with Licensor regarding such Contributions.
|
|
130
|
+
|
|
131
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
132
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
133
|
+
except as required for reasonable and customary use in describing the
|
|
134
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
135
|
+
|
|
136
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
137
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
138
|
+
Contributor provides its Work) on an "AS IS" BASIS, WITHOUT
|
|
139
|
+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
|
|
140
|
+
including, without limitation, any warranties or conditions of
|
|
141
|
+
TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
142
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
143
|
+
appropriateness of using or redistributing the Work and assume any
|
|
144
|
+
risks associated with Your exercise of permissions under this License.
|
|
145
|
+
|
|
146
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
147
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
148
|
+
unless required by applicable law (such as deliberate and grossly
|
|
149
|
+
negligent acts) or agreed to in writing, will any Contributor be
|
|
150
|
+
liable to You for any loss or damage, as a result of any such
|
|
151
|
+
Contributor's exercise of permissions under this License, even if
|
|
152
|
+
such Contributor has been advised of the possibility of such damages.
|
|
153
|
+
|
|
154
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
155
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
156
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
157
|
+
or other liability obligations. However, in accepting such
|
|
158
|
+
obligations, You may act only on Your own behalf and on Your sole
|
|
159
|
+
responsibility, not on behalf of any Contributor, and only if You
|
|
160
|
+
agree to indemnify, defend, and hold each Contributor harmless for
|
|
161
|
+
any liability incurred by each such Contributor in performing its
|
|
162
|
+
obligations under this License.
|
|
163
|
+
|
|
164
|
+
END OF TERMS AND CONDITIONS
|
|
165
|
+
|
|
166
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
167
|
+
|
|
168
|
+
To apply the Apache License to your work, attach the following
|
|
169
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
170
|
+
replaced with your own identifying information. (Don't include
|
|
171
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
172
|
+
comment syntax for the file format. We also recommend that a
|
|
173
|
+
file or class name and description of purpose be included on the
|
|
174
|
+
same "printed page" as the copyright notice for easier
|
|
175
|
+
identification within third-party archives.
|
|
176
|
+
|
|
177
|
+
Keith Infinity
|
|
178
|
+
|
|
179
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
180
|
+
you may not use this file except in compliance with the License.
|
|
181
|
+
You may obtain a copy of the License at
|
|
182
|
+
|
|
183
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
184
|
+
|
|
185
|
+
Unless required by applicable law or agreed to in writing, software
|
|
186
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
187
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
188
|
+
See the License for the specific language governing permissions and
|
|
189
|
+
limitations under the License.
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: friction-engine
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Realistic backtest friction modeling: volume-aware slippage, multi-market fees & taxes (US/TW/JP), liquidity constraints, and MAE/MFE trade analytics.
|
|
5
|
+
Author: friction-engine contributors
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/jalano0i9u8y7-lab/friction-engine
|
|
8
|
+
Keywords: backtest,slippage,transaction-costs,MAE,MFE,trading
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Topic :: Office/Business :: Financial :: Investment
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Provides-Extra: dev
|
|
22
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
# friction-engine
|
|
26
|
+
|
|
27
|
+
**Your backtest is lying to you — this library measures how much.**
|
|
28
|
+
|
|
29
|
+
`friction-engine` models the four costs naive backtests ignore, as small,
|
|
30
|
+
composable, fully-auditable pieces:
|
|
31
|
+
|
|
32
|
+
| Module | What it fixes | The naive-backtest lie |
|
|
33
|
+
|---|---|---|
|
|
34
|
+
| `slippage` | Volume-aware slippage (half-spread + square-root impact) | "I filled 200k shares at the close price" |
|
|
35
|
+
| `fees` | Multi-market fees & taxes (US / TW / JP presets, all configurable) | "Commissions are round-off error" |
|
|
36
|
+
| `liquidity` | Participation-rate caps on executable quantity | "The market absorbed my 10× ADV order instantly" |
|
|
37
|
+
| `mae_mfe` | MAE/MFE excursion analytics | "The trade returned +5%" — but it dipped -8% first; would your stop have survived? |
|
|
38
|
+
|
|
39
|
+
Pure Python standard library. **Zero third-party dependencies.** Every model
|
|
40
|
+
is one short file you can read end-to-end before trusting it.
|
|
41
|
+
|
|
42
|
+
## Install
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install friction-engine
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Or from source:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install .
|
|
52
|
+
pip install ".[dev]" # with pytest for running the test suite
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Quick start
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from friction_engine import (
|
|
59
|
+
Market, Order, ParticipationCap, Side, TradePath,
|
|
60
|
+
VolumeShareSlippage, excursion, fee_for,
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
order = Order(symbol="2330.TW", side=Side.BUY, quantity=20_000)
|
|
64
|
+
|
|
65
|
+
# 1. Liquidity: can the market absorb this?
|
|
66
|
+
allowed = ParticipationCap(0.10).cap(order, bar_volume=150_000)
|
|
67
|
+
|
|
68
|
+
# 2. Slippage: volume-aware fill price (not the close you assumed)
|
|
69
|
+
slip = VolumeShareSlippage(spread_bps=5, eta=0.5)
|
|
70
|
+
fill_price = slip.fill_price(order, 100.0, bar_volume=150_000, volatility=0.02)
|
|
71
|
+
|
|
72
|
+
# 3. Fees & taxes: TW stocks = 14.25 bps commission + 30 bps sell tax
|
|
73
|
+
fee, tax = fee_for(order, fill_price * order.quantity, market=Market.TW)
|
|
74
|
+
|
|
75
|
+
# 4. MAE/MFE: what happened inside the trade, not just entry→exit
|
|
76
|
+
path = TradePath(
|
|
77
|
+
entry_price=fill_price, side=Side.BUY,
|
|
78
|
+
highs=[101, 103, 106], lows=[97.5, 99, 102], exit_price=105.0,
|
|
79
|
+
)
|
|
80
|
+
rec = excursion(path)
|
|
81
|
+
print(rec.mae, rec.mfe, rec.giveback) # -0.025 … +0.06 … how much you gave back
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Run the full worked example:
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
python examples/basic_usage.py
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## The models, honestly
|
|
91
|
+
|
|
92
|
+
**Volume-aware slippage** — `fill = ref × (1 + dir × (spread/2 + η·σ·√(q/V)))`.
|
|
93
|
+
The square-root temporary-impact shape is the most widely published
|
|
94
|
+
empirical form in the market-microstructure literature. `η` (eta) is a
|
|
95
|
+
calibration knob: 0.1 gentle, 0.5 typical, 1.0 harsh. If you don't know your
|
|
96
|
+
`η`, run all three and treat the spread of outcomes as your uncertainty.
|
|
97
|
+
|
|
98
|
+
**Fee presets** — US (zero commission + ~0.3 bps sell-side regulatory fees),
|
|
99
|
+
TW stocks (14.25 bps commission both sides + 30 bps sell tax), TW ETF
|
|
100
|
+
(10 bps sell tax), JP (10 bps commission + 10% consumption tax *on the
|
|
101
|
+
commission*, no share transaction tax). These are publicly documented
|
|
102
|
+
typical values as of mid-2026 and **will go stale** — brokers discount,
|
|
103
|
+
regulators re-price. Treat presets as starting points:
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
from friction_engine import FEE_SCHEDULES, Market
|
|
107
|
+
my_tw = FEE_SCHEDULES[Market.TW].with_overrides(commission_bps=6.0, min_commission=20.0)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
**Liquidity cap** — `min(quantity, max_participation × bar_volume)`. Simple,
|
|
111
|
+
brutal, and the single most common way backtests overstate capacity.
|
|
112
|
+
|
|
113
|
+
**MAE/MFE** — descriptive statistics over a trade's post-entry high/low
|
|
114
|
+
path (Sweeney-style excursion analysis). Aggregates (`excursion_stats`)
|
|
115
|
+
answer: *is my stop inside the noise band of my winners?* (`mae_of_winners_mean`)
|
|
116
|
+
and *how much do my losers climb before failing?* (`mfe_of_losers_mean`).
|
|
117
|
+
|
|
118
|
+
## What this library is NOT
|
|
119
|
+
|
|
120
|
+
- Not a backtester. It computes friction; you bring the loop, the data, and
|
|
121
|
+
the point-in-time discipline. (Pair it with `quant-lint` to audit the
|
|
122
|
+
strategy code feeding it.)
|
|
123
|
+
- Not a broker simulator. Fill *decisions* (partial fills, limit queues,
|
|
124
|
+
halts) are out of scope; it prices the fills you assume.
|
|
125
|
+
- Not financial advice, and presets are not a fee-quote service. Verify
|
|
126
|
+
current rates with your broker/exchange before trading real money.
|
|
127
|
+
|
|
128
|
+
## Development
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
python -m pytest # run the test suite
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Layout:
|
|
135
|
+
|
|
136
|
+
```
|
|
137
|
+
src/friction_engine/
|
|
138
|
+
models.py # Order / Fill / Side value types
|
|
139
|
+
slippage.py # Zero / Fixed / VolumeShare slippage models
|
|
140
|
+
fees.py # FeeSchedule + US/TW/JP presets
|
|
141
|
+
liquidity.py # ParticipationCap
|
|
142
|
+
mae_mfe.py # TradePath, mae/mfe, excursion, excursion_stats
|
|
143
|
+
tests/ # pytest suite, no network, no data files
|
|
144
|
+
examples/ # runnable worked example
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## License
|
|
148
|
+
|
|
149
|
+
Apache-2.0 — see [LICENSE](LICENSE). Clean-room provenance: see
|
|
150
|
+
[CLEANROOM.md](CLEANROOM.md).
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# friction-engine
|
|
2
|
+
|
|
3
|
+
**Your backtest is lying to you — this library measures how much.**
|
|
4
|
+
|
|
5
|
+
`friction-engine` models the four costs naive backtests ignore, as small,
|
|
6
|
+
composable, fully-auditable pieces:
|
|
7
|
+
|
|
8
|
+
| Module | What it fixes | The naive-backtest lie |
|
|
9
|
+
|---|---|---|
|
|
10
|
+
| `slippage` | Volume-aware slippage (half-spread + square-root impact) | "I filled 200k shares at the close price" |
|
|
11
|
+
| `fees` | Multi-market fees & taxes (US / TW / JP presets, all configurable) | "Commissions are round-off error" |
|
|
12
|
+
| `liquidity` | Participation-rate caps on executable quantity | "The market absorbed my 10× ADV order instantly" |
|
|
13
|
+
| `mae_mfe` | MAE/MFE excursion analytics | "The trade returned +5%" — but it dipped -8% first; would your stop have survived? |
|
|
14
|
+
|
|
15
|
+
Pure Python standard library. **Zero third-party dependencies.** Every model
|
|
16
|
+
is one short file you can read end-to-end before trusting it.
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pip install friction-engine
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Or from source:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install .
|
|
28
|
+
pip install ".[dev]" # with pytest for running the test suite
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Quick start
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from friction_engine import (
|
|
35
|
+
Market, Order, ParticipationCap, Side, TradePath,
|
|
36
|
+
VolumeShareSlippage, excursion, fee_for,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
order = Order(symbol="2330.TW", side=Side.BUY, quantity=20_000)
|
|
40
|
+
|
|
41
|
+
# 1. Liquidity: can the market absorb this?
|
|
42
|
+
allowed = ParticipationCap(0.10).cap(order, bar_volume=150_000)
|
|
43
|
+
|
|
44
|
+
# 2. Slippage: volume-aware fill price (not the close you assumed)
|
|
45
|
+
slip = VolumeShareSlippage(spread_bps=5, eta=0.5)
|
|
46
|
+
fill_price = slip.fill_price(order, 100.0, bar_volume=150_000, volatility=0.02)
|
|
47
|
+
|
|
48
|
+
# 3. Fees & taxes: TW stocks = 14.25 bps commission + 30 bps sell tax
|
|
49
|
+
fee, tax = fee_for(order, fill_price * order.quantity, market=Market.TW)
|
|
50
|
+
|
|
51
|
+
# 4. MAE/MFE: what happened inside the trade, not just entry→exit
|
|
52
|
+
path = TradePath(
|
|
53
|
+
entry_price=fill_price, side=Side.BUY,
|
|
54
|
+
highs=[101, 103, 106], lows=[97.5, 99, 102], exit_price=105.0,
|
|
55
|
+
)
|
|
56
|
+
rec = excursion(path)
|
|
57
|
+
print(rec.mae, rec.mfe, rec.giveback) # -0.025 … +0.06 … how much you gave back
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Run the full worked example:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
python examples/basic_usage.py
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## The models, honestly
|
|
67
|
+
|
|
68
|
+
**Volume-aware slippage** — `fill = ref × (1 + dir × (spread/2 + η·σ·√(q/V)))`.
|
|
69
|
+
The square-root temporary-impact shape is the most widely published
|
|
70
|
+
empirical form in the market-microstructure literature. `η` (eta) is a
|
|
71
|
+
calibration knob: 0.1 gentle, 0.5 typical, 1.0 harsh. If you don't know your
|
|
72
|
+
`η`, run all three and treat the spread of outcomes as your uncertainty.
|
|
73
|
+
|
|
74
|
+
**Fee presets** — US (zero commission + ~0.3 bps sell-side regulatory fees),
|
|
75
|
+
TW stocks (14.25 bps commission both sides + 30 bps sell tax), TW ETF
|
|
76
|
+
(10 bps sell tax), JP (10 bps commission + 10% consumption tax *on the
|
|
77
|
+
commission*, no share transaction tax). These are publicly documented
|
|
78
|
+
typical values as of mid-2026 and **will go stale** — brokers discount,
|
|
79
|
+
regulators re-price. Treat presets as starting points:
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
from friction_engine import FEE_SCHEDULES, Market
|
|
83
|
+
my_tw = FEE_SCHEDULES[Market.TW].with_overrides(commission_bps=6.0, min_commission=20.0)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
**Liquidity cap** — `min(quantity, max_participation × bar_volume)`. Simple,
|
|
87
|
+
brutal, and the single most common way backtests overstate capacity.
|
|
88
|
+
|
|
89
|
+
**MAE/MFE** — descriptive statistics over a trade's post-entry high/low
|
|
90
|
+
path (Sweeney-style excursion analysis). Aggregates (`excursion_stats`)
|
|
91
|
+
answer: *is my stop inside the noise band of my winners?* (`mae_of_winners_mean`)
|
|
92
|
+
and *how much do my losers climb before failing?* (`mfe_of_losers_mean`).
|
|
93
|
+
|
|
94
|
+
## What this library is NOT
|
|
95
|
+
|
|
96
|
+
- Not a backtester. It computes friction; you bring the loop, the data, and
|
|
97
|
+
the point-in-time discipline. (Pair it with `quant-lint` to audit the
|
|
98
|
+
strategy code feeding it.)
|
|
99
|
+
- Not a broker simulator. Fill *decisions* (partial fills, limit queues,
|
|
100
|
+
halts) are out of scope; it prices the fills you assume.
|
|
101
|
+
- Not financial advice, and presets are not a fee-quote service. Verify
|
|
102
|
+
current rates with your broker/exchange before trading real money.
|
|
103
|
+
|
|
104
|
+
## Development
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
python -m pytest # run the test suite
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Layout:
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
src/friction_engine/
|
|
114
|
+
models.py # Order / Fill / Side value types
|
|
115
|
+
slippage.py # Zero / Fixed / VolumeShare slippage models
|
|
116
|
+
fees.py # FeeSchedule + US/TW/JP presets
|
|
117
|
+
liquidity.py # ParticipationCap
|
|
118
|
+
mae_mfe.py # TradePath, mae/mfe, excursion, excursion_stats
|
|
119
|
+
tests/ # pytest suite, no network, no data files
|
|
120
|
+
examples/ # runnable worked example
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
Apache-2.0 — see [LICENSE](LICENSE). Clean-room provenance: see
|
|
126
|
+
[CLEANROOM.md](CLEANROOM.md).
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "friction-engine"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Realistic backtest friction modeling: volume-aware slippage, multi-market fees & taxes (US/TW/JP), liquidity constraints, and MAE/MFE trade analytics."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "Apache-2.0" }
|
|
12
|
+
authors = [{ name = "friction-engine contributors" }]
|
|
13
|
+
keywords = ["backtest", "slippage", "transaction-costs", "MAE", "MFE", "trading"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Intended Audience :: Financial and Insurance Industry",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"License :: OSI Approved :: Apache Software License",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Topic :: Office/Business :: Financial :: Investment",
|
|
24
|
+
]
|
|
25
|
+
# Zero runtime dependencies by design: pure standard library so the whole
|
|
26
|
+
# package can be audited line-by-line.
|
|
27
|
+
dependencies = []
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
dev = ["pytest>=7"]
|
|
31
|
+
|
|
32
|
+
[project.urls]
|
|
33
|
+
Homepage = "https://github.com/jalano0i9u8y7-lab/friction-engine"
|
|
34
|
+
|
|
35
|
+
[tool.setuptools.packages.find]
|
|
36
|
+
where = ["src"]
|
|
37
|
+
|
|
38
|
+
[tool.pytest.ini_options]
|
|
39
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""friction-engine: realistic backtest friction modeling.
|
|
2
|
+
|
|
3
|
+
Four orthogonal pieces, composable:
|
|
4
|
+
|
|
5
|
+
- :mod:`friction_engine.slippage` -- volume-aware slippage models
|
|
6
|
+
- :mod:`friction_engine.fees` -- multi-market fee & tax schedules (US/TW/JP)
|
|
7
|
+
- :mod:`friction_engine.liquidity` -- participation-rate liquidity constraints
|
|
8
|
+
- :mod:`friction_engine.mae_mfe` -- MAE/MFE excursion analytics
|
|
9
|
+
|
|
10
|
+
Pure standard library, no third-party dependencies.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from .fees import FeeSchedule, Market, fee_for, FEE_SCHEDULES
|
|
14
|
+
from .liquidity import LiquidityConstraint, ParticipationCap
|
|
15
|
+
from .mae_mfe import Excursion, ExcursionStats, TradePath, excursion, excursion_stats, mae, mfe
|
|
16
|
+
from .models import Fill, Order, Side
|
|
17
|
+
from .slippage import (
|
|
18
|
+
FixedSlippage,
|
|
19
|
+
SlippageModel,
|
|
20
|
+
VolumeShareSlippage,
|
|
21
|
+
ZeroSlippage,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
__version__ = "0.1.0"
|
|
25
|
+
|
|
26
|
+
__all__ = [
|
|
27
|
+
"FEE_SCHEDULES",
|
|
28
|
+
"Excursion",
|
|
29
|
+
"ExcursionStats",
|
|
30
|
+
"FeeSchedule",
|
|
31
|
+
"Fill",
|
|
32
|
+
"FixedSlippage",
|
|
33
|
+
"LiquidityConstraint",
|
|
34
|
+
"Market",
|
|
35
|
+
"Order",
|
|
36
|
+
"ParticipationCap",
|
|
37
|
+
"Side",
|
|
38
|
+
"SlippageModel",
|
|
39
|
+
"TradePath",
|
|
40
|
+
"VolumeShareSlippage",
|
|
41
|
+
"ZeroSlippage",
|
|
42
|
+
"excursion",
|
|
43
|
+
"excursion_stats",
|
|
44
|
+
"fee_for",
|
|
45
|
+
"mae",
|
|
46
|
+
"mfe",
|
|
47
|
+
"__version__",
|
|
48
|
+
]
|