while25 0.1.0__py3-none-any.whl
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.
- while25/__init__.py +341 -0
- while25/_costs/brokers/alpaca.json +96 -0
- while25/_costs/brokers/binance.json +34 -0
- while25/_costs/brokers/coinbase.json +34 -0
- while25/_costs/brokers/ibkr.json +39 -0
- while25/_costs/brokers/upstox.json +175 -0
- while25/_costs/brokers/zerodha.json +175 -0
- while25/_costs/crypto_alpaca.json +27 -0
- while25/_costs/global_crypto.json +18 -0
- while25/_costs/in_nse_equity.json +238 -0
- while25/_costs/in_nse_etf.json +352 -0
- while25/_costs/in_nse_fo.json +450 -0
- while25/_costs/us_crypto.json +18 -0
- while25/_costs/us_equity.json +175 -0
- while25/_costs/us_options.json +172 -0
- while25/_days.py +123 -0
- while25/_figures.py +233 -0
- while25/_runtime.py +49 -0
- while25/_universes/banknifty.json +80 -0
- while25/_universes/ndx100.json +520 -0
- while25/_universes/nifty50.json +260 -0
- while25/_universes/nifty500.json +2505 -0
- while25/_universes/sp500.json +2525 -0
- while25/assume.py +263 -0
- while25/backtest.py +881 -0
- while25/barline.py +160 -0
- while25/combo_legs.py +238 -0
- while25/errors.py +77 -0
- while25/fonts/IBMPlexMono-Regular.ttf +0 -0
- while25/fonts/IBMPlexMono-SemiBold.ttf +0 -0
- while25/fonts/Inter-Bold.ttf +0 -0
- while25/fonts/Inter-Regular.ttf +0 -0
- while25/fonts/Inter-SemiBold.ttf +0 -0
- while25/fonts/OFL-IBMPlexMono.txt +93 -0
- while25/fonts/OFL-Inter.txt +93 -0
- while25/future_pick.py +77 -0
- while25/inputs.py +215 -0
- while25/instruments.py +488 -0
- while25/modules.py +397 -0
- while25/money.py +24 -0
- while25/option_pick.py +221 -0
- while25/plots.py +147 -0
- while25/report.py +644 -0
- while25/ui.py +680 -0
- while25/universes.py +70 -0
- while25-0.1.0.dist-info/METADATA +3194 -0
- while25-0.1.0.dist-info/RECORD +50 -0
- while25-0.1.0.dist-info/WHEEL +5 -0
- while25-0.1.0.dist-info/licenses/LICENSE +201 -0
- while25-0.1.0.dist-info/top_level.txt +1 -0
while25/future_pick.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""Turning a futures root into one dated contract (2026-10-24).
|
|
2
|
+
|
|
3
|
+
The pure half of `while25.future()`: reading which contract the author asked
|
|
4
|
+
for ("front", "next", "2027-03") and choosing it from the root's live
|
|
5
|
+
contracts as the engine lists them. Nothing here talks to the terminal, so
|
|
6
|
+
the rules are tested on their own:
|
|
7
|
+
|
|
8
|
+
- "front" is the contract the chart's continuous line for the root is on now,
|
|
9
|
+
so it rolls when that line rolls: on IBKR and Upstox after the first full
|
|
10
|
+
session in which the next contract traded more (at the latest after its last
|
|
11
|
+
trading day), on Zerodha at expiry. A venue that draws no such line has no
|
|
12
|
+
front to follow, and is never given a guessed one.
|
|
13
|
+
- "next" is the first listed contract of a later month than the front.
|
|
14
|
+
- A month ("2027-03", "202703") is that month's contract, as its key names it.
|
|
15
|
+
|
|
16
|
+
Malformed input raises ValueError (the author's mistake, nothing was asked);
|
|
17
|
+
a lookup that cannot be answered raises While25Error with a code — never a
|
|
18
|
+
neighbouring contract instead.
|
|
19
|
+
"""
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
import re
|
|
23
|
+
|
|
24
|
+
from .errors import While25Error
|
|
25
|
+
|
|
26
|
+
_MONTH = re.compile(r"^(\d{4})-?(\d{2})$")
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def parse_expiry(expiry) -> str:
|
|
30
|
+
""""front", "next", or a contract month as "YYYY-MM" ("2027-03" or
|
|
31
|
+
"202703"), in any capitals."""
|
|
32
|
+
text = "front" if expiry is None else str(expiry).strip().lower()
|
|
33
|
+
if text in ("front", "next"):
|
|
34
|
+
return text
|
|
35
|
+
m = _MONTH.match(text)
|
|
36
|
+
if m and 1 <= int(m.group(2)) <= 12:
|
|
37
|
+
return f"{m.group(1)}-{m.group(2)}"
|
|
38
|
+
raise ValueError(
|
|
39
|
+
f'expiry must be "front", "next" or a contract month like "2027-03", not {expiry!r}')
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _months(contracts: list) -> str:
|
|
43
|
+
return ", ".join(c["month"] for c in contracts) or "none"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def pick(spec: str, contracts: list, front_key: str, has_line: bool, root: str) -> dict:
|
|
47
|
+
"""The one contract `spec` names among `contracts` (live ones, by contract
|
|
48
|
+
month, as the engine lists them)."""
|
|
49
|
+
if spec in ("front", "next"):
|
|
50
|
+
if not has_line:
|
|
51
|
+
raise While25Error(
|
|
52
|
+
f"the connected venue draws no continuous line for {root}, so there is no "
|
|
53
|
+
f"front contract to follow: name a month ({_months(contracts)})", "NO_FRONT")
|
|
54
|
+
at = next((i for i, c in enumerate(contracts) if c["key"] == front_key), None)
|
|
55
|
+
if at is None:
|
|
56
|
+
raise While25Error(
|
|
57
|
+
f"the chart's continuous {root} is on {front_key or 'no contract yet'}, which the "
|
|
58
|
+
f"venue does not list as live - try again", "NOT_LISTED")
|
|
59
|
+
front = contracts[at]
|
|
60
|
+
if spec == "front":
|
|
61
|
+
return front
|
|
62
|
+
later = [c for c in contracts[at + 1:] if c["month"] > front["month"]]
|
|
63
|
+
if not later:
|
|
64
|
+
raise While25Error(
|
|
65
|
+
f"no {root} contract after {front['symbol']} ({front['month']}) is listed yet",
|
|
66
|
+
"NO_NEXT")
|
|
67
|
+
return later[0]
|
|
68
|
+
same = [c for c in contracts if c["month"] == spec]
|
|
69
|
+
if not same:
|
|
70
|
+
raise While25Error(
|
|
71
|
+
f"{root} lists no {spec} contract: it lists {_months(contracts)}", "NOT_LISTED")
|
|
72
|
+
if len(same) > 1:
|
|
73
|
+
raise While25Error(
|
|
74
|
+
f"{root} lists more than one contract in {spec} "
|
|
75
|
+
f"({', '.join(c['symbol'] for c in same)}): order one of them by its key",
|
|
76
|
+
"AMBIGUOUS_MONTH")
|
|
77
|
+
return same[0]
|
while25/inputs.py
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"""Typed input specs.
|
|
2
|
+
|
|
3
|
+
Declared on a module class; the terminal renders them as a config dialog and
|
|
4
|
+
rejects out-of-range values BEFORE a run, rather than at line 40 of someone's
|
|
5
|
+
compute.
|
|
6
|
+
"""
|
|
7
|
+
import math
|
|
8
|
+
from dataclasses import dataclass, field
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class InputSpec:
|
|
13
|
+
name: str
|
|
14
|
+
|
|
15
|
+
def validate(self, value: Any) -> Any: # pragma: no cover - interface
|
|
16
|
+
raise NotImplementedError
|
|
17
|
+
|
|
18
|
+
def describe(self) -> dict: # pragma: no cover - interface
|
|
19
|
+
raise NotImplementedError
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass
|
|
23
|
+
class Int(InputSpec):
|
|
24
|
+
name: str
|
|
25
|
+
default: int = 0
|
|
26
|
+
minimum: int | None = None
|
|
27
|
+
maximum: int | None = None
|
|
28
|
+
# The increment the terminal's config dialog steps by, matching what native
|
|
29
|
+
# indicators declare. Absent means the dialog picks a sensible default.
|
|
30
|
+
step: int | None = None
|
|
31
|
+
label: str = ""
|
|
32
|
+
|
|
33
|
+
def __post_init__(self) -> None:
|
|
34
|
+
# A default outside its own bounds is a declaration bug; catching it
|
|
35
|
+
# here beats catching it at run time when nobody is looking at the file.
|
|
36
|
+
self.validate(self.default)
|
|
37
|
+
|
|
38
|
+
def validate(self, value: Any) -> int:
|
|
39
|
+
try:
|
|
40
|
+
v = int(value)
|
|
41
|
+
except (TypeError, ValueError):
|
|
42
|
+
raise ValueError(f"{self.name}: {value!r} is not a whole number")
|
|
43
|
+
if self.minimum is not None and v < self.minimum:
|
|
44
|
+
raise ValueError(f"{self.name}: {v} is below the minimum {self.minimum}")
|
|
45
|
+
if self.maximum is not None and v > self.maximum:
|
|
46
|
+
raise ValueError(f"{self.name}: {v} is above the maximum {self.maximum}")
|
|
47
|
+
return v
|
|
48
|
+
|
|
49
|
+
def describe(self) -> dict:
|
|
50
|
+
return {"name": self.name, "type": "int", "default": self.default,
|
|
51
|
+
"minimum": self.minimum, "maximum": self.maximum,
|
|
52
|
+
"step": self.step, "label": self.label or self.name}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@dataclass
|
|
56
|
+
class Float(InputSpec):
|
|
57
|
+
name: str
|
|
58
|
+
default: float = 0.0
|
|
59
|
+
minimum: float | None = None
|
|
60
|
+
maximum: float | None = None
|
|
61
|
+
step: float | None = None
|
|
62
|
+
label: str = ""
|
|
63
|
+
|
|
64
|
+
def __post_init__(self) -> None:
|
|
65
|
+
self.validate(self.default)
|
|
66
|
+
|
|
67
|
+
def validate(self, value: Any) -> float:
|
|
68
|
+
try:
|
|
69
|
+
v = float(value)
|
|
70
|
+
except (TypeError, ValueError):
|
|
71
|
+
raise ValueError(f"{self.name}: {value!r} is not a number")
|
|
72
|
+
# NaN and infinity parse as floats and slip past both bounds, because
|
|
73
|
+
# every comparison with NaN is False: "nan" went through as a valid
|
|
74
|
+
# length and the indicator drew nothing (2026-09-19).
|
|
75
|
+
if not math.isfinite(v):
|
|
76
|
+
raise ValueError(f"{self.name}: {value!r} is not a finite number")
|
|
77
|
+
if self.minimum is not None and v < self.minimum:
|
|
78
|
+
raise ValueError(f"{self.name}: {v} is below the minimum {self.minimum}")
|
|
79
|
+
if self.maximum is not None and v > self.maximum:
|
|
80
|
+
raise ValueError(f"{self.name}: {v} is above the maximum {self.maximum}")
|
|
81
|
+
return v
|
|
82
|
+
|
|
83
|
+
def describe(self) -> dict:
|
|
84
|
+
return {"name": self.name, "type": "float", "default": self.default,
|
|
85
|
+
"minimum": self.minimum, "maximum": self.maximum,
|
|
86
|
+
"step": self.step, "label": self.label or self.name}
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@dataclass
|
|
90
|
+
class Bool(InputSpec):
|
|
91
|
+
name: str
|
|
92
|
+
default: bool = False
|
|
93
|
+
# What the terminal's settings form calls it. Every input type takes one
|
|
94
|
+
# (2026-10-17): a Start or Deploy window names each field, and only Int and
|
|
95
|
+
# Float could be named anything but their key.
|
|
96
|
+
label: str = ""
|
|
97
|
+
|
|
98
|
+
def validate(self, value: Any) -> bool:
|
|
99
|
+
if isinstance(value, bool):
|
|
100
|
+
return value
|
|
101
|
+
if isinstance(value, str):
|
|
102
|
+
return value.strip().lower() in {"1", "true", "yes", "on"}
|
|
103
|
+
return bool(value)
|
|
104
|
+
|
|
105
|
+
def describe(self) -> dict:
|
|
106
|
+
return {"name": self.name, "type": "bool", "default": self.default,
|
|
107
|
+
"label": self.label or self.name}
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
@dataclass
|
|
111
|
+
class Str(InputSpec):
|
|
112
|
+
name: str
|
|
113
|
+
default: str = ""
|
|
114
|
+
label: str = ""
|
|
115
|
+
|
|
116
|
+
def validate(self, value: Any) -> str:
|
|
117
|
+
return "" if value is None else str(value)
|
|
118
|
+
|
|
119
|
+
def describe(self) -> dict:
|
|
120
|
+
return {"name": self.name, "type": "str", "default": self.default,
|
|
121
|
+
"label": self.label or self.name}
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
@dataclass
|
|
125
|
+
class Enum(InputSpec):
|
|
126
|
+
name: str
|
|
127
|
+
choices: list[str] = field(default_factory=list)
|
|
128
|
+
default: str = ""
|
|
129
|
+
label: str = ""
|
|
130
|
+
|
|
131
|
+
def __post_init__(self) -> None:
|
|
132
|
+
if self.default and self.default not in self.choices:
|
|
133
|
+
raise ValueError(
|
|
134
|
+
f"{self.name}: default {self.default!r} is not one of {self.choices}")
|
|
135
|
+
|
|
136
|
+
def validate(self, value: Any) -> str:
|
|
137
|
+
v = str(value)
|
|
138
|
+
if v not in self.choices:
|
|
139
|
+
raise ValueError(f"{self.name}: {v!r} is not one of {self.choices}")
|
|
140
|
+
return v
|
|
141
|
+
|
|
142
|
+
def describe(self) -> dict:
|
|
143
|
+
return {"name": self.name, "type": "enum", "default": self.default,
|
|
144
|
+
"choices": list(self.choices), "label": self.label or self.name}
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
@dataclass
|
|
148
|
+
class Symbol(InputSpec):
|
|
149
|
+
name: str
|
|
150
|
+
default: str = ""
|
|
151
|
+
label: str = ""
|
|
152
|
+
|
|
153
|
+
def validate(self, value: Any) -> str:
|
|
154
|
+
v = str(value or "").strip()
|
|
155
|
+
if not v:
|
|
156
|
+
raise ValueError(f"{self.name}: a symbol is required")
|
|
157
|
+
return v
|
|
158
|
+
|
|
159
|
+
def describe(self) -> dict:
|
|
160
|
+
return {"name": self.name, "type": "symbol", "default": self.default,
|
|
161
|
+
"label": self.label or self.name}
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def spec_from_description(d: dict) -> InputSpec | None:
|
|
165
|
+
"""Rebuild an input spec from its own describe() output.
|
|
166
|
+
|
|
167
|
+
The terminal's runtime holds no module classes, only the registry's
|
|
168
|
+
descriptions of them, yet it has to judge a value BEFORE it answers the
|
|
169
|
+
chart. Rebuilding the spec lets it run the very same validate() the worker
|
|
170
|
+
runs, rather than a second copy of the rules that could drift. None for a
|
|
171
|
+
type this build does not know, which the caller then leaves unchecked.
|
|
172
|
+
"""
|
|
173
|
+
kind = d.get("type")
|
|
174
|
+
name = d.get("name") or ""
|
|
175
|
+
if not name:
|
|
176
|
+
return None
|
|
177
|
+
try:
|
|
178
|
+
if kind == "int":
|
|
179
|
+
return Int(name, d.get("default", 0), d.get("minimum"), d.get("maximum"))
|
|
180
|
+
if kind == "float":
|
|
181
|
+
return Float(name, d.get("default", 0.0), d.get("minimum"), d.get("maximum"))
|
|
182
|
+
if kind == "bool":
|
|
183
|
+
return Bool(name, d.get("default", False))
|
|
184
|
+
if kind == "str":
|
|
185
|
+
return Str(name, d.get("default", ""))
|
|
186
|
+
if kind == "enum":
|
|
187
|
+
return Enum(name, list(d.get("choices") or []), d.get("default", ""))
|
|
188
|
+
if kind == "symbol":
|
|
189
|
+
return Symbol(name, d.get("default", ""))
|
|
190
|
+
except (TypeError, ValueError):
|
|
191
|
+
# A description that no longer rebuilds is a registry problem, not the
|
|
192
|
+
# user's value; the worker still applies the real spec on its own.
|
|
193
|
+
return None
|
|
194
|
+
return None
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def check_values(descriptions: list, raw: dict) -> list[str]:
|
|
198
|
+
"""Every declared input in `raw` that its own spec refuses, as messages.
|
|
199
|
+
|
|
200
|
+
Empty means every value the caller sent is acceptable. An input the
|
|
201
|
+
caller did not send takes its declared default later and is not judged
|
|
202
|
+
here; an undeclared key rides through untouched, as it does in the worker.
|
|
203
|
+
"""
|
|
204
|
+
problems: list[str] = []
|
|
205
|
+
for d in descriptions or []:
|
|
206
|
+
if not isinstance(d, dict) or d.get("name") not in (raw or {}):
|
|
207
|
+
continue
|
|
208
|
+
spec = spec_from_description(d)
|
|
209
|
+
if spec is None:
|
|
210
|
+
continue
|
|
211
|
+
try:
|
|
212
|
+
spec.validate(raw[d["name"]])
|
|
213
|
+
except ValueError as exc:
|
|
214
|
+
problems.append(str(exc))
|
|
215
|
+
return problems
|