marginism 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.
- marginism-0.1.0/LICENSE +21 -0
- marginism-0.1.0/PKG-INFO +209 -0
- marginism-0.1.0/README.md +97 -0
- marginism-0.1.0/marginism/README.md +193 -0
- marginism-0.1.0/marginism/__init__.py +62 -0
- marginism-0.1.0/marginism/__main__.py +4 -0
- marginism-0.1.0/marginism/algorithm.py +175 -0
- marginism-0.1.0/marginism/api.py +198 -0
- marginism-0.1.0/marginism/calculator.py +192 -0
- marginism-0.1.0/marginism/cli.py +93 -0
- marginism-0.1.0/marginism/exposure.py +66 -0
- marginism-0.1.0/marginism/model.py +179 -0
- marginism-0.1.0/marginism/parser.py +263 -0
- marginism-0.1.0/marginism/portfolio.py +98 -0
- marginism-0.1.0/marginism/symbols.py +107 -0
- marginism-0.1.0/marginism.egg-info/PKG-INFO +209 -0
- marginism-0.1.0/marginism.egg-info/SOURCES.txt +21 -0
- marginism-0.1.0/marginism.egg-info/dependency_links.txt +1 -0
- marginism-0.1.0/marginism.egg-info/entry_points.txt +2 -0
- marginism-0.1.0/marginism.egg-info/requires.txt +3 -0
- marginism-0.1.0/marginism.egg-info/top_level.txt +1 -0
- marginism-0.1.0/pyproject.toml +30 -0
- marginism-0.1.0/setup.cfg +4 -0
marginism-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 span-margin contributors
|
|
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.
|
marginism-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: marginism
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: NSE/NSCCL SPAN margin calculator from CME-SPAN .spn files, with an order-basket margins API
|
|
5
|
+
Author: marginism
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/marketcalls/marginism
|
|
8
|
+
Project-URL: Repository, https://github.com/marketcalls/marginism
|
|
9
|
+
Keywords: span,margin,nse,nfo,cds,mcx,options,futures,derivatives
|
|
10
|
+
Requires-Python: >=3.8
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Provides-Extra: dev
|
|
14
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
|
|
17
|
+
# marginism
|
|
18
|
+
|
|
19
|
+
Compute **NSE / NSCCL SPAN margins** directly from the exchange's daily
|
|
20
|
+
CME-SPAN risk-parameter files (`.spn`, XML `fileFormat 4.00`), the same inputs a
|
|
21
|
+
broker's margin calculator uses.
|
|
22
|
+
|
|
23
|
+
`spanrisk.xml` in this folder is the **XSD schema** that documents the `.spn`
|
|
24
|
+
format; this library implements the SPAN algorithm against files that conform to
|
|
25
|
+
it (e.g. `nsccl.YYYYMMDD.s.spn`).
|
|
26
|
+
|
|
27
|
+
## Why this works without an option pricer
|
|
28
|
+
|
|
29
|
+
Each contract in a `.spn` file ships a **precomputed 16-scenario risk array** —
|
|
30
|
+
the per-unit profit/loss under 16 combinations of price move (±1/3, ±2/3, ±3/3
|
|
31
|
+
of the scan range, plus two "extreme" moves) and volatility up/down. SPAN margin
|
|
32
|
+
is therefore pure arithmetic over those arrays; no Black-Scholes is needed at
|
|
33
|
+
calculation time.
|
|
34
|
+
|
|
35
|
+
## The SPAN calculation
|
|
36
|
+
|
|
37
|
+
Per **combined commodity** (one underlying — futures + options margined
|
|
38
|
+
together):
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
span_risk = max( scan_risk
|
|
42
|
+
+ calendar (intra-commodity) spread charge
|
|
43
|
+
+ spot/delivery charge # 0 in NSCCL files
|
|
44
|
+
- inter-commodity spread credit # 0 in NSCCL files
|
|
45
|
+
, short_option_minimum )
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
* **Scan risk** — the largest portfolio loss across the 16 scenarios:
|
|
49
|
+
`max_j Σ (signed_qty × risk_array[j])`.
|
|
50
|
+
* **Calendar spread charge** — scan assumes all expiries move together, so a
|
|
51
|
+
flat charge is added back for the basis risk of long-near / short-far
|
|
52
|
+
positions (`dSpread` definitions, method `F`).
|
|
53
|
+
* **Short option minimum (SOM)** — a floor for short-option books
|
|
54
|
+
(`som_rate = 0` in these NSCCL files).
|
|
55
|
+
|
|
56
|
+
A broker's **initial margin = SPAN margin + Exposure (ELM) margin**. Exposure
|
|
57
|
+
margin is *not* in the SPAN file (it's an exchange % of notional), so it is
|
|
58
|
+
configured in `ExposureConfig` and applied to futures and short options.
|
|
59
|
+
|
|
60
|
+
## Install / layout
|
|
61
|
+
|
|
62
|
+
Pure standard library (Python 3.8+), no dependencies. Drop the `marginism/`
|
|
63
|
+
folder on your path (or `pip install -e .`).
|
|
64
|
+
|
|
65
|
+
## Pointing to your `.spn` file
|
|
66
|
+
|
|
67
|
+
You just give the path. Same folder or a different folder, macOS or Windows:
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
# Same folder as your script
|
|
71
|
+
SPN = "nsccl.20260529.s.spn"
|
|
72
|
+
|
|
73
|
+
# Different folder — macOS / Linux (forward slashes)
|
|
74
|
+
SPN = "/Users/you/Downloads/nsccl.20260529.s.spn"
|
|
75
|
+
|
|
76
|
+
# Different folder — Windows (use a raw string r"..." or forward slashes)
|
|
77
|
+
SPN = r"C:\Users\you\Downloads\nsccl.20260529.s.spn"
|
|
78
|
+
SPN = "C:/Users/you/Downloads/nsccl.20260529.s.spn"
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Quick start
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
from marginism import SpanCalculator, Position
|
|
85
|
+
|
|
86
|
+
calc = SpanCalculator.from_file(
|
|
87
|
+
SPN,
|
|
88
|
+
symbols=["NIFTY", "RELIANCE"], # parse only what you need (fast / light)
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
result = calc.calculate([
|
|
92
|
+
# quantity is entered DIRECTLY in units: NIFTY lot size 65 -> 65 = 1 lot,
|
|
93
|
+
# 130 = 2 lots. long +, short -
|
|
94
|
+
Position("NIFTY", "CE", quantity=-65, expiry="20260630", strike=24000),
|
|
95
|
+
Position("NIFTY", "PE", quantity=-65, expiry="20260630", strike=24000),
|
|
96
|
+
])
|
|
97
|
+
|
|
98
|
+
print(result.summary())
|
|
99
|
+
print(result.marginism, result.exposure_margin, result.total_margin)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Order-style API (single or multiple legs)
|
|
103
|
+
|
|
104
|
+
`RiskEngine` accepts orders by `tradingsymbol` and returns a broker-style dict
|
|
105
|
+
(per-leg + consolidated `initial`/`final` + `margin_benefit`). **Pure local
|
|
106
|
+
computation — no network, no service.**
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
from marginism import RiskEngine
|
|
110
|
+
|
|
111
|
+
eng = RiskEngine.from_file(SPN)
|
|
112
|
+
|
|
113
|
+
# one leg or many — a single order is just a basket of size one
|
|
114
|
+
res = eng.basket([
|
|
115
|
+
{"exchange": "NFO", "tradingsymbol": "NIFTY26JUNFUT",
|
|
116
|
+
"transaction_type": "BUY", "quantity": 65},
|
|
117
|
+
{"exchange": "NFO", "tradingsymbol": "NIFTY26JUN23000PE",
|
|
118
|
+
"transaction_type": "BUY", "quantity": 65},
|
|
119
|
+
])
|
|
120
|
+
data = res["data"]
|
|
121
|
+
print(data["final"]["total"], data["margin_benefit"])
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
`quantity` is entered **directly in units** (e.g. 65 for one NIFTY lot, 130 for
|
|
125
|
+
two); `transaction_type` is `BUY`/`SELL`. The engine is exchange-agnostic — load
|
|
126
|
+
an NFO, CDS, or MCX `.spn` file.
|
|
127
|
+
|
|
128
|
+
### Two symbol formats, plus explicit fields
|
|
129
|
+
|
|
130
|
+
A contract can be named two equivalent ways, and both resolve automatically:
|
|
131
|
+
|
|
132
|
+
| Style | Future | Option |
|
|
133
|
+
|---|---|---|
|
|
134
|
+
| compact | `NIFTY26JUNFUT` | `NIFTY26JUN23700CE` (monthly), `NIFTY2660223700CE` (weekly) |
|
|
135
|
+
| full-date | `NIFTY30JUN26FUT` | `NIFTY30JUN2623700CE` |
|
|
136
|
+
|
|
137
|
+
Or skip tradingsymbols and pass fields directly:
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
eng.basket([
|
|
141
|
+
{"symbol": "NIFTY", "instrument": "CE", "expiry": "2026-06-30",
|
|
142
|
+
"strike": 23700, "transaction_type": "SELL", "quantity": 65},
|
|
143
|
+
])
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Command line
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
python -m marginism <file.spn> --list # all symbols
|
|
150
|
+
python -m marginism <file.spn> --info NIFTY # contracts/expiries
|
|
151
|
+
python -m marginism <file.spn> \
|
|
152
|
+
--pos NIFTY:FUT:-65:20260630 \
|
|
153
|
+
--pos NIFTY:CE:65:20260630:24000 # margin for positions
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Important notes
|
|
157
|
+
|
|
158
|
+
* **Lot sizes are not in the SPAN file.** It works in underlying units, so enter
|
|
159
|
+
`quantity` directly in units (NIFTY 65 = 1 lot, 130 = 2 lots).
|
|
160
|
+
* **No exchange tokens in the file.** Instruments are keyed by *trading symbol*
|
|
161
|
+
(`cc` / `pfCode`, e.g. `RELIANCE`), with internal `pfId`/`cId` ids that are
|
|
162
|
+
**not** NSE tokens. Map a token (e.g. `2885` → `RELIANCE`) via a separate
|
|
163
|
+
instrument master before calling this library.
|
|
164
|
+
* **Exposure rates** in `ExposureConfig` are NSE defaults (index 2%, stock
|
|
165
|
+
~3.5%); override per circular via `overrides={"RELIANCE": 0.05}`.
|
|
166
|
+
* **Long options** carry no exposure margin (risk capped at premium); their
|
|
167
|
+
risk array still participates in the portfolio scan so hedges net correctly.
|
|
168
|
+
* `net_option_value` is the mark-to-market value of option legs (premium):
|
|
169
|
+
negative when net short (premium received), positive when net long.
|
|
170
|
+
|
|
171
|
+
## Module map
|
|
172
|
+
|
|
173
|
+
| Module | Responsibility |
|
|
174
|
+
|-----------------|-----------------------------------------------------------|
|
|
175
|
+
| `parser.py` | streaming `iterparse` of `.spn` → data model (symbol filter) |
|
|
176
|
+
| `model.py` | dataclasses: `SpanFile`, `CombinedCommodity`, contracts, risk arrays |
|
|
177
|
+
| `algorithm.py` | SPAN math: scan risk, calendar spreads, SOM, net option value |
|
|
178
|
+
| `portfolio.py` | `Position` input + expiry normalisation |
|
|
179
|
+
| `exposure.py` | exposure / ELM configuration (index 2% / stock 3.5%) |
|
|
180
|
+
| `calculator.py` | `SpanCalculator` — load once, evaluate many portfolios |
|
|
181
|
+
| `symbols.py` | tradingsymbol ⇄ SPAN contract resolution |
|
|
182
|
+
| `api.py` | `RiskEngine` — `basket()`/`orders()`, single or many legs |
|
|
183
|
+
| `cli.py` | `python -m marginism` |
|
|
184
|
+
|
|
185
|
+
100% standard library, runs fully offline — give it a `.spn` file and call a
|
|
186
|
+
function.
|
|
187
|
+
|
|
188
|
+
## Reference
|
|
189
|
+
|
|
190
|
+
- NSE Clearing — NSCCL SPAN:
|
|
191
|
+
https://www.nseclearing.in/risk-management/equity-derivatives/nsccl-span
|
|
192
|
+
|
|
193
|
+
## Disclaimer
|
|
194
|
+
|
|
195
|
+
The software is provided "as is", without warranty of any kind. The author
|
|
196
|
+
accepts **no responsibility or liability for any errors or inaccuracies in the
|
|
197
|
+
calculations, or for any trading losses, damages, or decisions** arising from its
|
|
198
|
+
use. Margins depend on the SPAN file and exposure rates you supply, may differ
|
|
199
|
+
from your broker's, and must be independently verified before trading. Not
|
|
200
|
+
financial advice; use at your own risk.
|
|
201
|
+
|
|
202
|
+
This is an independent project built by an independent developer. It is **not
|
|
203
|
+
affiliated with, sponsored by, endorsed by, or connected to** NSE, NSE Clearing
|
|
204
|
+
(NSCCL), the Chicago Mercantile Exchange (CME), or any broker or exchange.
|
|
205
|
+
|
|
206
|
+
SPAN® is a registered trademark of Chicago Mercantile Exchange Inc. All other
|
|
207
|
+
trademarks are the property of their respective owners. Any names are used only
|
|
208
|
+
for identification/descriptive purposes (nominative use) and do not imply any
|
|
209
|
+
affiliation, endorsement, or license.
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# marginism
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/marginism/)
|
|
4
|
+
[](https://pypi.org/project/marginism/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
**Know the exact margin for any F&O trade — on your own computer, instantly.**
|
|
8
|
+
|
|
9
|
+
Give it the exchange's daily SPAN file and your position(s); it returns the
|
|
10
|
+
**SPAN margin**, **Exposure margin**, **total margin**, and the **margin you save
|
|
11
|
+
by hedging** — the same numbers a broker's calculator shows. Offline, no login,
|
|
12
|
+
pure Python.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install marginism
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick start
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from marginism import RiskEngine
|
|
24
|
+
|
|
25
|
+
eng = RiskEngine.from_file("nsccl.20260529.s.spn") # load once, reuse
|
|
26
|
+
|
|
27
|
+
result = eng.basket([
|
|
28
|
+
{"tradingsymbol": "NIFTY26JUN23700CE", "transaction_type": "SELL", "quantity": 65},
|
|
29
|
+
])
|
|
30
|
+
print(result["data"]["final"]["total"]) # 172285
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`quantity` is in units (lots × lot size; NIFTY 65 = 1 lot). `transaction_type`
|
|
34
|
+
is `BUY`/`SELL`. Pass one leg or many.
|
|
35
|
+
|
|
36
|
+
## Examples
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
# Short straddle — sell 23700 CE + 23700 PE
|
|
40
|
+
eng.basket([
|
|
41
|
+
{"tradingsymbol": "NIFTY26JUN23700CE", "transaction_type": "SELL", "quantity": 65},
|
|
42
|
+
{"tradingsymbol": "NIFTY26JUN23700PE", "transaction_type": "SELL", "quantity": 65},
|
|
43
|
+
])
|
|
44
|
+
# TOTAL = 2,02,898 | margin benefit = 1,38,269
|
|
45
|
+
|
|
46
|
+
# Protective put — buy future + buy 23000 PE (hedge lowers margin)
|
|
47
|
+
eng.basket([
|
|
48
|
+
{"tradingsymbol": "NIFTY26JUNFUT", "transaction_type": "BUY", "quantity": 65},
|
|
49
|
+
{"tradingsymbol": "NIFTY26JUN23000PE", "transaction_type": "BUY", "quantity": 65},
|
|
50
|
+
])
|
|
51
|
+
# TOTAL = 78,904 | margin benefit = 95,689
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
See [`example.py`](example.py) for 10 ready-made strategies (straddle, strangle,
|
|
55
|
+
covered call, calendar spread, iron condor, …).
|
|
56
|
+
|
|
57
|
+
## Don't use a tradingsymbol? Pass fields instead
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
{"symbol": "NIFTY", "instrument": "CE", "expiry": "2026-06-30", "strike": 23700,
|
|
61
|
+
"transaction_type": "SELL", "quantity": 65}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Getting the SPAN file
|
|
65
|
+
|
|
66
|
+
Download the latest daily SPAN file from your exchange clearing house and point
|
|
67
|
+
the engine at it. For NSE F&O (`nsccl.YYYYMMDD.s.spn`) see NSE Clearing's
|
|
68
|
+
[NSCCL SPAN page](https://www.nseclearing.in/risk-management/equity-derivatives/nsccl-span).
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
eng = RiskEngine.from_file("nsccl.20260529.s.spn") # same folder
|
|
72
|
+
eng = RiskEngine.from_file(r"C:\Users\you\Downloads\file.spn") # Windows
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Works for NFO, currency (CDS), and commodity (MCX) files.
|
|
76
|
+
|
|
77
|
+
## Disclaimer
|
|
78
|
+
|
|
79
|
+
Margin figures are estimates — always confirm with your broker before trading.
|
|
80
|
+
Not financial advice. MIT licensed. Full API details in
|
|
81
|
+
[`marginism/README.md`](marginism/README.md).
|
|
82
|
+
|
|
83
|
+
The software is provided "as is", without warranty of any kind. The author
|
|
84
|
+
accepts **no responsibility or liability for any errors or inaccuracies in the
|
|
85
|
+
calculations, or for any trading losses, damages, or decisions** arising from its
|
|
86
|
+
use. Margins depend on the SPAN file and exposure rates you supply, may differ
|
|
87
|
+
from your broker's, and must be independently verified before trading. **Use at
|
|
88
|
+
your own risk.**
|
|
89
|
+
|
|
90
|
+
This is an independent project built by an independent developer. It is **not
|
|
91
|
+
affiliated with, sponsored by, endorsed by, or connected to** NSE, NSE Clearing
|
|
92
|
+
(NSCCL), the Chicago Mercantile Exchange (CME), or any broker or exchange.
|
|
93
|
+
|
|
94
|
+
SPAN® is a registered trademark of Chicago Mercantile Exchange Inc. All other
|
|
95
|
+
trademarks are the property of their respective owners. Any names are used only
|
|
96
|
+
for identification/descriptive purposes (nominative use) and do not imply any
|
|
97
|
+
affiliation, endorsement, or license.
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# marginism
|
|
2
|
+
|
|
3
|
+
Compute **NSE / NSCCL SPAN margins** directly from the exchange's daily
|
|
4
|
+
CME-SPAN risk-parameter files (`.spn`, XML `fileFormat 4.00`), the same inputs a
|
|
5
|
+
broker's margin calculator uses.
|
|
6
|
+
|
|
7
|
+
`spanrisk.xml` in this folder is the **XSD schema** that documents the `.spn`
|
|
8
|
+
format; this library implements the SPAN algorithm against files that conform to
|
|
9
|
+
it (e.g. `nsccl.YYYYMMDD.s.spn`).
|
|
10
|
+
|
|
11
|
+
## Why this works without an option pricer
|
|
12
|
+
|
|
13
|
+
Each contract in a `.spn` file ships a **precomputed 16-scenario risk array** —
|
|
14
|
+
the per-unit profit/loss under 16 combinations of price move (±1/3, ±2/3, ±3/3
|
|
15
|
+
of the scan range, plus two "extreme" moves) and volatility up/down. SPAN margin
|
|
16
|
+
is therefore pure arithmetic over those arrays; no Black-Scholes is needed at
|
|
17
|
+
calculation time.
|
|
18
|
+
|
|
19
|
+
## The SPAN calculation
|
|
20
|
+
|
|
21
|
+
Per **combined commodity** (one underlying — futures + options margined
|
|
22
|
+
together):
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
span_risk = max( scan_risk
|
|
26
|
+
+ calendar (intra-commodity) spread charge
|
|
27
|
+
+ spot/delivery charge # 0 in NSCCL files
|
|
28
|
+
- inter-commodity spread credit # 0 in NSCCL files
|
|
29
|
+
, short_option_minimum )
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
* **Scan risk** — the largest portfolio loss across the 16 scenarios:
|
|
33
|
+
`max_j Σ (signed_qty × risk_array[j])`.
|
|
34
|
+
* **Calendar spread charge** — scan assumes all expiries move together, so a
|
|
35
|
+
flat charge is added back for the basis risk of long-near / short-far
|
|
36
|
+
positions (`dSpread` definitions, method `F`).
|
|
37
|
+
* **Short option minimum (SOM)** — a floor for short-option books
|
|
38
|
+
(`som_rate = 0` in these NSCCL files).
|
|
39
|
+
|
|
40
|
+
A broker's **initial margin = SPAN margin + Exposure (ELM) margin**. Exposure
|
|
41
|
+
margin is *not* in the SPAN file (it's an exchange % of notional), so it is
|
|
42
|
+
configured in `ExposureConfig` and applied to futures and short options.
|
|
43
|
+
|
|
44
|
+
## Install / layout
|
|
45
|
+
|
|
46
|
+
Pure standard library (Python 3.8+), no dependencies. Drop the `marginism/`
|
|
47
|
+
folder on your path (or `pip install -e .`).
|
|
48
|
+
|
|
49
|
+
## Pointing to your `.spn` file
|
|
50
|
+
|
|
51
|
+
You just give the path. Same folder or a different folder, macOS or Windows:
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
# Same folder as your script
|
|
55
|
+
SPN = "nsccl.20260529.s.spn"
|
|
56
|
+
|
|
57
|
+
# Different folder — macOS / Linux (forward slashes)
|
|
58
|
+
SPN = "/Users/you/Downloads/nsccl.20260529.s.spn"
|
|
59
|
+
|
|
60
|
+
# Different folder — Windows (use a raw string r"..." or forward slashes)
|
|
61
|
+
SPN = r"C:\Users\you\Downloads\nsccl.20260529.s.spn"
|
|
62
|
+
SPN = "C:/Users/you/Downloads/nsccl.20260529.s.spn"
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Quick start
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from marginism import SpanCalculator, Position
|
|
69
|
+
|
|
70
|
+
calc = SpanCalculator.from_file(
|
|
71
|
+
SPN,
|
|
72
|
+
symbols=["NIFTY", "RELIANCE"], # parse only what you need (fast / light)
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
result = calc.calculate([
|
|
76
|
+
# quantity is entered DIRECTLY in units: NIFTY lot size 65 -> 65 = 1 lot,
|
|
77
|
+
# 130 = 2 lots. long +, short -
|
|
78
|
+
Position("NIFTY", "CE", quantity=-65, expiry="20260630", strike=24000),
|
|
79
|
+
Position("NIFTY", "PE", quantity=-65, expiry="20260630", strike=24000),
|
|
80
|
+
])
|
|
81
|
+
|
|
82
|
+
print(result.summary())
|
|
83
|
+
print(result.marginism, result.exposure_margin, result.total_margin)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Order-style API (single or multiple legs)
|
|
87
|
+
|
|
88
|
+
`RiskEngine` accepts orders by `tradingsymbol` and returns a broker-style dict
|
|
89
|
+
(per-leg + consolidated `initial`/`final` + `margin_benefit`). **Pure local
|
|
90
|
+
computation — no network, no service.**
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
from marginism import RiskEngine
|
|
94
|
+
|
|
95
|
+
eng = RiskEngine.from_file(SPN)
|
|
96
|
+
|
|
97
|
+
# one leg or many — a single order is just a basket of size one
|
|
98
|
+
res = eng.basket([
|
|
99
|
+
{"exchange": "NFO", "tradingsymbol": "NIFTY26JUNFUT",
|
|
100
|
+
"transaction_type": "BUY", "quantity": 65},
|
|
101
|
+
{"exchange": "NFO", "tradingsymbol": "NIFTY26JUN23000PE",
|
|
102
|
+
"transaction_type": "BUY", "quantity": 65},
|
|
103
|
+
])
|
|
104
|
+
data = res["data"]
|
|
105
|
+
print(data["final"]["total"], data["margin_benefit"])
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
`quantity` is entered **directly in units** (e.g. 65 for one NIFTY lot, 130 for
|
|
109
|
+
two); `transaction_type` is `BUY`/`SELL`. The engine is exchange-agnostic — load
|
|
110
|
+
an NFO, CDS, or MCX `.spn` file.
|
|
111
|
+
|
|
112
|
+
### Two symbol formats, plus explicit fields
|
|
113
|
+
|
|
114
|
+
A contract can be named two equivalent ways, and both resolve automatically:
|
|
115
|
+
|
|
116
|
+
| Style | Future | Option |
|
|
117
|
+
|---|---|---|
|
|
118
|
+
| compact | `NIFTY26JUNFUT` | `NIFTY26JUN23700CE` (monthly), `NIFTY2660223700CE` (weekly) |
|
|
119
|
+
| full-date | `NIFTY30JUN26FUT` | `NIFTY30JUN2623700CE` |
|
|
120
|
+
|
|
121
|
+
Or skip tradingsymbols and pass fields directly:
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
eng.basket([
|
|
125
|
+
{"symbol": "NIFTY", "instrument": "CE", "expiry": "2026-06-30",
|
|
126
|
+
"strike": 23700, "transaction_type": "SELL", "quantity": 65},
|
|
127
|
+
])
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Command line
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
python -m marginism <file.spn> --list # all symbols
|
|
134
|
+
python -m marginism <file.spn> --info NIFTY # contracts/expiries
|
|
135
|
+
python -m marginism <file.spn> \
|
|
136
|
+
--pos NIFTY:FUT:-65:20260630 \
|
|
137
|
+
--pos NIFTY:CE:65:20260630:24000 # margin for positions
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Important notes
|
|
141
|
+
|
|
142
|
+
* **Lot sizes are not in the SPAN file.** It works in underlying units, so enter
|
|
143
|
+
`quantity` directly in units (NIFTY 65 = 1 lot, 130 = 2 lots).
|
|
144
|
+
* **No exchange tokens in the file.** Instruments are keyed by *trading symbol*
|
|
145
|
+
(`cc` / `pfCode`, e.g. `RELIANCE`), with internal `pfId`/`cId` ids that are
|
|
146
|
+
**not** NSE tokens. Map a token (e.g. `2885` → `RELIANCE`) via a separate
|
|
147
|
+
instrument master before calling this library.
|
|
148
|
+
* **Exposure rates** in `ExposureConfig` are NSE defaults (index 2%, stock
|
|
149
|
+
~3.5%); override per circular via `overrides={"RELIANCE": 0.05}`.
|
|
150
|
+
* **Long options** carry no exposure margin (risk capped at premium); their
|
|
151
|
+
risk array still participates in the portfolio scan so hedges net correctly.
|
|
152
|
+
* `net_option_value` is the mark-to-market value of option legs (premium):
|
|
153
|
+
negative when net short (premium received), positive when net long.
|
|
154
|
+
|
|
155
|
+
## Module map
|
|
156
|
+
|
|
157
|
+
| Module | Responsibility |
|
|
158
|
+
|-----------------|-----------------------------------------------------------|
|
|
159
|
+
| `parser.py` | streaming `iterparse` of `.spn` → data model (symbol filter) |
|
|
160
|
+
| `model.py` | dataclasses: `SpanFile`, `CombinedCommodity`, contracts, risk arrays |
|
|
161
|
+
| `algorithm.py` | SPAN math: scan risk, calendar spreads, SOM, net option value |
|
|
162
|
+
| `portfolio.py` | `Position` input + expiry normalisation |
|
|
163
|
+
| `exposure.py` | exposure / ELM configuration (index 2% / stock 3.5%) |
|
|
164
|
+
| `calculator.py` | `SpanCalculator` — load once, evaluate many portfolios |
|
|
165
|
+
| `symbols.py` | tradingsymbol ⇄ SPAN contract resolution |
|
|
166
|
+
| `api.py` | `RiskEngine` — `basket()`/`orders()`, single or many legs |
|
|
167
|
+
| `cli.py` | `python -m marginism` |
|
|
168
|
+
|
|
169
|
+
100% standard library, runs fully offline — give it a `.spn` file and call a
|
|
170
|
+
function.
|
|
171
|
+
|
|
172
|
+
## Reference
|
|
173
|
+
|
|
174
|
+
- NSE Clearing — NSCCL SPAN:
|
|
175
|
+
https://www.nseclearing.in/risk-management/equity-derivatives/nsccl-span
|
|
176
|
+
|
|
177
|
+
## Disclaimer
|
|
178
|
+
|
|
179
|
+
The software is provided "as is", without warranty of any kind. The author
|
|
180
|
+
accepts **no responsibility or liability for any errors or inaccuracies in the
|
|
181
|
+
calculations, or for any trading losses, damages, or decisions** arising from its
|
|
182
|
+
use. Margins depend on the SPAN file and exposure rates you supply, may differ
|
|
183
|
+
from your broker's, and must be independently verified before trading. Not
|
|
184
|
+
financial advice; use at your own risk.
|
|
185
|
+
|
|
186
|
+
This is an independent project built by an independent developer. It is **not
|
|
187
|
+
affiliated with, sponsored by, endorsed by, or connected to** NSE, NSE Clearing
|
|
188
|
+
(NSCCL), the Chicago Mercantile Exchange (CME), or any broker or exchange.
|
|
189
|
+
|
|
190
|
+
SPAN® is a registered trademark of Chicago Mercantile Exchange Inc. All other
|
|
191
|
+
trademarks are the property of their respective owners. Any names are used only
|
|
192
|
+
for identification/descriptive purposes (nominative use) and do not imply any
|
|
193
|
+
affiliation, endorsement, or license.
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""marginism — compute NSE/NSCCL SPAN margins from CME-SPAN ``.spn`` files.
|
|
2
|
+
|
|
3
|
+
Quick start
|
|
4
|
+
-----------
|
|
5
|
+
>>> from marginism import SpanCalculator, Position
|
|
6
|
+
>>> calc = SpanCalculator.from_file(
|
|
7
|
+
... "nsccl.20260529.s/nsccl.20260529.s.spn", symbols=["NIFTY"])
|
|
8
|
+
>>> res = calc.calculate([
|
|
9
|
+
... Position("NIFTY", "FUT", quantity=65, expiry="20260630"),
|
|
10
|
+
... ])
|
|
11
|
+
>>> print(res.summary())
|
|
12
|
+
|
|
13
|
+
The ``.spn`` file ships precomputed 16-scenario risk arrays, so margin is pure
|
|
14
|
+
arithmetic — no option pricing involved. ``quantity`` is in underlying units
|
|
15
|
+
that you enter directly (NIFTY lot size 65 -> pass 65 for one lot, 130 for two);
|
|
16
|
+
long is positive, short negative.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from .algorithm import CommodityResult, ResolvedPosition, compute_commodity
|
|
20
|
+
from .calculator import MarginResult, PositionResult, SpanCalculator
|
|
21
|
+
from .exposure import ExposureConfig
|
|
22
|
+
from .model import (
|
|
23
|
+
CalendarSpread,
|
|
24
|
+
CombinedCommodity,
|
|
25
|
+
Contract,
|
|
26
|
+
FuturesContract,
|
|
27
|
+
OptionContract,
|
|
28
|
+
RiskArray,
|
|
29
|
+
SpanFile,
|
|
30
|
+
SCENARIO_LABELS,
|
|
31
|
+
)
|
|
32
|
+
from .parser import parse_spn
|
|
33
|
+
from .portfolio import Position, normalize_expiry
|
|
34
|
+
from .api import RiskEngine
|
|
35
|
+
from .symbols import SymbolResolver, build_symbol_index
|
|
36
|
+
|
|
37
|
+
__version__ = "0.1.0"
|
|
38
|
+
|
|
39
|
+
__all__ = [
|
|
40
|
+
"SpanCalculator",
|
|
41
|
+
"MarginResult",
|
|
42
|
+
"PositionResult",
|
|
43
|
+
"Position",
|
|
44
|
+
"ExposureConfig",
|
|
45
|
+
"RiskEngine",
|
|
46
|
+
"SymbolResolver",
|
|
47
|
+
"build_symbol_index",
|
|
48
|
+
"SpanFile",
|
|
49
|
+
"CombinedCommodity",
|
|
50
|
+
"Contract",
|
|
51
|
+
"FuturesContract",
|
|
52
|
+
"OptionContract",
|
|
53
|
+
"CalendarSpread",
|
|
54
|
+
"RiskArray",
|
|
55
|
+
"SCENARIO_LABELS",
|
|
56
|
+
"ResolvedPosition",
|
|
57
|
+
"CommodityResult",
|
|
58
|
+
"compute_commodity",
|
|
59
|
+
"parse_spn",
|
|
60
|
+
"normalize_expiry",
|
|
61
|
+
"__version__",
|
|
62
|
+
]
|