PaperTrade-India 0.1.0.dev0__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.
- papertrade_india/__init__.py +250 -0
- papertrade_india/_context.py +144 -0
- papertrade_india/broker.py +546 -0
- papertrade_india/cli.py +474 -0
- papertrade_india/clock.py +29 -0
- papertrade_india/corporate_actions/__init__.py +10 -0
- papertrade_india/corporate_actions/bonus.py +105 -0
- papertrade_india/corporate_actions/dividends.py +99 -0
- papertrade_india/corporate_actions/rights.py +189 -0
- papertrade_india/corporate_actions/splits.py +111 -0
- papertrade_india/corporate_actions/store.py +247 -0
- papertrade_india/data/nse_holidays_2026.json +22 -0
- papertrade_india/data/nse_holidays_2027.json +9 -0
- papertrade_india/data/nse_universe_sample.csv +31 -0
- papertrade_india/domain/__init__.py +6 -0
- papertrade_india/domain/exceptions.py +183 -0
- papertrade_india/domain/models.py +213 -0
- papertrade_india/domain/rules/__init__.py +5 -0
- papertrade_india/domain/rules/risk.py +165 -0
- papertrade_india/domain/rules/tick_lot_band.py +139 -0
- papertrade_india/events.py +29 -0
- papertrade_india/exceptions.py +29 -0
- papertrade_india/execution/__init__.py +5 -0
- papertrade_india/execution/book.py +385 -0
- papertrade_india/execution/fees.py +213 -0
- papertrade_india/execution/settlement.py +360 -0
- papertrade_india/execution/simulation.py +176 -0
- papertrade_india/execution/slippage.py +120 -0
- papertrade_india/fees.py +29 -0
- papertrade_india/idempotency.py +29 -0
- papertrade_india/infrastructure/__init__.py +6 -0
- papertrade_india/infrastructure/clock.py +123 -0
- papertrade_india/infrastructure/events.py +137 -0
- papertrade_india/infrastructure/holidays.py +138 -0
- papertrade_india/infrastructure/idempotency.py +133 -0
- papertrade_india/infrastructure/ledger.py +141 -0
- papertrade_india/infrastructure/market_hours.py +200 -0
- papertrade_india/infrastructure/migrations.py +622 -0
- papertrade_india/infrastructure/observability.py +195 -0
- papertrade_india/infrastructure/persistence.py +130 -0
- papertrade_india/infrastructure/symbols.py +279 -0
- papertrade_india/infrastructure/watchlist.py +78 -0
- papertrade_india/interface.py +92 -0
- papertrade_india/ledger.py +29 -0
- papertrade_india/limit_orders.py +29 -0
- papertrade_india/market_hours.py +29 -0
- papertrade_india/microstructure.py +57 -0
- papertrade_india/migrations.py +29 -0
- papertrade_india/models.py +29 -0
- papertrade_india/observability.py +29 -0
- papertrade_india/orders/__init__.py +5 -0
- papertrade_india/orders/amo.py +98 -0
- papertrade_india/orders/book_helpers.py +111 -0
- papertrade_india/orders/bracket.py +115 -0
- papertrade_india/orders/limit.py +210 -0
- papertrade_india/orders/market.py +264 -0
- papertrade_india/orders/partial_fills.py +92 -0
- papertrade_india/orders/preopen.py +274 -0
- papertrade_india/orders/state.py +640 -0
- papertrade_india/orders/stop.py +122 -0
- papertrade_india/orders/submit.py +385 -0
- papertrade_india/partial_fills.py +29 -0
- papertrade_india/persistence.py +29 -0
- papertrade_india/preopen.py +29 -0
- papertrade_india/presets.py +120 -0
- papertrade_india/price_feed.py +316 -0
- papertrade_india/providers/__init__.py +123 -0
- papertrade_india/providers/alphavantage.py +180 -0
- papertrade_india/providers/base.py +256 -0
- papertrade_india/providers/cache.py +147 -0
- papertrade_india/providers/composite.py +244 -0
- papertrade_india/providers/dhan.py +266 -0
- papertrade_india/providers/finnhub.py +122 -0
- papertrade_india/providers/health.py +233 -0
- papertrade_india/providers/jugaad.py +142 -0
- papertrade_india/providers/kite.py +222 -0
- papertrade_india/providers/nse_bhavcopy.py +210 -0
- papertrade_india/providers/nsepython.py +98 -0
- papertrade_india/providers/registry.py +226 -0
- papertrade_india/providers/stooq.py +173 -0
- papertrade_india/providers/twelvedata.py +198 -0
- papertrade_india/providers/upstox.py +318 -0
- papertrade_india/providers/upstox_instruments.py +171 -0
- papertrade_india/providers/yfinance.py +205 -0
- papertrade_india/quickstart.py +111 -0
- papertrade_india/reads/__init__.py +6 -0
- papertrade_india/reads/account.py +140 -0
- papertrade_india/reads/orders.py +54 -0
- papertrade_india/reads/positions.py +188 -0
- papertrade_india/risk.py +29 -0
- papertrade_india/settlement.py +29 -0
- papertrade_india/simulation.py +29 -0
- papertrade_india/slippage.py +29 -0
- papertrade_india/symbols.py +29 -0
- papertrade_india/workers/__init__.py +6 -0
- papertrade_india/workers/limit_orders.py +390 -0
- papertrade_india-0.1.0.dev0.dist-info/METADATA +261 -0
- papertrade_india-0.1.0.dev0.dist-info/RECORD +101 -0
- papertrade_india-0.1.0.dev0.dist-info/WHEEL +4 -0
- papertrade_india-0.1.0.dev0.dist-info/entry_points.txt +2 -0
- papertrade_india-0.1.0.dev0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"""Read-only views over positions.
|
|
2
|
+
|
|
3
|
+
Functions here never mutate state. Each takes the broker as its first
|
|
4
|
+
argument so they can reach the persistence handle, the price feed, and
|
|
5
|
+
the ``mark_to_bid`` toggle. The broker's public ``get_positions`` /
|
|
6
|
+
``get_position`` methods are thin delegators over these helpers.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import logging
|
|
12
|
+
from datetime import datetime
|
|
13
|
+
from typing import TYPE_CHECKING
|
|
14
|
+
|
|
15
|
+
from ..domain.models import Exchange, Position
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING: # pragma: no cover
|
|
18
|
+
from ..broker import IndiaPaperBroker
|
|
19
|
+
|
|
20
|
+
logger = logging.getLogger(__name__)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def mark_price(broker: IndiaPaperBroker, symbol: str) -> tuple[float, str]:
|
|
24
|
+
"""Resolve the mark-to-market price for a long position.
|
|
25
|
+
|
|
26
|
+
Returns ``(price, basis)``:
|
|
27
|
+
|
|
28
|
+
- With ``broker.mark_to_bid=True`` and the rich quote exposing a
|
|
29
|
+
bid, returns ``(bid, "bid")``. This matches what a real broker
|
|
30
|
+
uses for unrealized P&L on a long.
|
|
31
|
+
- When bid is missing but we have both bid+ask, mid is used.
|
|
32
|
+
- Otherwise falls back to the legacy last-price behavior.
|
|
33
|
+
|
|
34
|
+
Raises whatever the price feed raises so callers can decide between
|
|
35
|
+
fall-back and propagation.
|
|
36
|
+
"""
|
|
37
|
+
if broker.mark_to_bid:
|
|
38
|
+
try:
|
|
39
|
+
mq = broker.price_feed.get_market_quote(symbol)
|
|
40
|
+
except Exception: # noqa: BLE001
|
|
41
|
+
mq = None
|
|
42
|
+
if mq is not None:
|
|
43
|
+
if mq.bid is not None and mq.bid > 0:
|
|
44
|
+
return float(mq.bid), "bid"
|
|
45
|
+
if mq.bid is not None and mq.ask is not None:
|
|
46
|
+
return float((mq.bid + mq.ask) / 2.0), "mid"
|
|
47
|
+
return broker.price_feed.get_price(symbol), "last"
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def list_all(broker: IndiaPaperBroker) -> list[Position]:
|
|
51
|
+
"""Return every open position for the broker's account."""
|
|
52
|
+
with broker.persistence.read() as conn:
|
|
53
|
+
rows = conn.execute(
|
|
54
|
+
"SELECT symbol, exchange, qty, avg_cost, entry_date "
|
|
55
|
+
"FROM positions WHERE account_id = ?",
|
|
56
|
+
(broker.account_id,),
|
|
57
|
+
).fetchall()
|
|
58
|
+
|
|
59
|
+
positions: list[Position] = []
|
|
60
|
+
for row in rows:
|
|
61
|
+
stale = False
|
|
62
|
+
basis = "last"
|
|
63
|
+
try:
|
|
64
|
+
price, basis = mark_price(broker, row["symbol"])
|
|
65
|
+
except Exception as e: # noqa: BLE001 — network-volatile
|
|
66
|
+
logger.warning(
|
|
67
|
+
"Position %s: price unavailable, using avg_cost. %s",
|
|
68
|
+
row["symbol"], e,
|
|
69
|
+
)
|
|
70
|
+
price = row["avg_cost"]
|
|
71
|
+
stale = True
|
|
72
|
+
|
|
73
|
+
mv = price * row["qty"]
|
|
74
|
+
cb = row["avg_cost"] * row["qty"]
|
|
75
|
+
positions.append(
|
|
76
|
+
Position(
|
|
77
|
+
symbol=row["symbol"],
|
|
78
|
+
exchange=Exchange(row["exchange"]),
|
|
79
|
+
qty=row["qty"],
|
|
80
|
+
avg_cost=row["avg_cost"],
|
|
81
|
+
current_price=price,
|
|
82
|
+
market_value=mv,
|
|
83
|
+
cost_basis=cb,
|
|
84
|
+
unrealized_pl=mv - cb,
|
|
85
|
+
unrealized_pl_percent=(
|
|
86
|
+
((mv - cb) / cb * 100) if cb > 0 else 0.0
|
|
87
|
+
),
|
|
88
|
+
entry_date=datetime.fromisoformat(row["entry_date"]),
|
|
89
|
+
current_price_stale=stale,
|
|
90
|
+
mark_basis=basis,
|
|
91
|
+
)
|
|
92
|
+
)
|
|
93
|
+
return positions
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def get(broker: IndiaPaperBroker, symbol: str) -> Position | None:
|
|
97
|
+
"""Direct O(1) lookup against the (account_id, symbol) primary key."""
|
|
98
|
+
with broker.persistence.read() as conn:
|
|
99
|
+
row = conn.execute(
|
|
100
|
+
"SELECT symbol, exchange, qty, avg_cost, entry_date "
|
|
101
|
+
"FROM positions WHERE account_id = ? AND symbol = ?",
|
|
102
|
+
(broker.account_id, symbol),
|
|
103
|
+
).fetchone()
|
|
104
|
+
if row is None:
|
|
105
|
+
return None
|
|
106
|
+
|
|
107
|
+
stale = False
|
|
108
|
+
basis = "last"
|
|
109
|
+
try:
|
|
110
|
+
price, basis = mark_price(broker, row["symbol"])
|
|
111
|
+
except Exception as e: # noqa: BLE001
|
|
112
|
+
logger.warning(
|
|
113
|
+
"Position %s: price unavailable, using avg_cost. %s",
|
|
114
|
+
row["symbol"], e,
|
|
115
|
+
)
|
|
116
|
+
price = row["avg_cost"]
|
|
117
|
+
stale = True
|
|
118
|
+
|
|
119
|
+
mv = price * row["qty"]
|
|
120
|
+
cb = row["avg_cost"] * row["qty"]
|
|
121
|
+
return Position(
|
|
122
|
+
symbol=row["symbol"],
|
|
123
|
+
exchange=Exchange(row["exchange"]),
|
|
124
|
+
qty=row["qty"],
|
|
125
|
+
avg_cost=row["avg_cost"],
|
|
126
|
+
current_price=price,
|
|
127
|
+
market_value=mv,
|
|
128
|
+
cost_basis=cb,
|
|
129
|
+
unrealized_pl=mv - cb,
|
|
130
|
+
unrealized_pl_percent=(
|
|
131
|
+
((mv - cb) / cb * 100) if cb > 0 else 0.0
|
|
132
|
+
),
|
|
133
|
+
entry_date=datetime.fromisoformat(row["entry_date"]),
|
|
134
|
+
current_price_stale=stale,
|
|
135
|
+
mark_basis=basis,
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def basis_breakdown(broker: IndiaPaperBroker, symbol: str) -> dict | None:
|
|
140
|
+
"""Return the open position's cost basis broken into principal vs fees.
|
|
141
|
+
|
|
142
|
+
Useful for reconciling against a broker contract note: ``avg_cost``
|
|
143
|
+
bakes in prorated buy-side fees, but the ledger has the raw
|
|
144
|
+
components separately. We only count buy-side movements that
|
|
145
|
+
haven't been reversed by a subsequent sell.
|
|
146
|
+
|
|
147
|
+
Returns ``None`` if no open position exists for ``symbol``.
|
|
148
|
+
"""
|
|
149
|
+
pos = get(broker, symbol)
|
|
150
|
+
if pos is None:
|
|
151
|
+
return None
|
|
152
|
+
|
|
153
|
+
with broker.persistence.read() as conn:
|
|
154
|
+
rows = conn.execute(
|
|
155
|
+
"SELECT reason, COALESCE(SUM(amount), 0) AS total "
|
|
156
|
+
"FROM cash_movements "
|
|
157
|
+
"WHERE account_id = ? AND symbol = ? "
|
|
158
|
+
"GROUP BY reason",
|
|
159
|
+
(broker.account_id, symbol),
|
|
160
|
+
).fetchall()
|
|
161
|
+
|
|
162
|
+
by_reason = {r["reason"]: float(r["total"]) for r in rows}
|
|
163
|
+
buy_principal = abs(by_reason.get("buy_principal", 0.0))
|
|
164
|
+
buy_fees = abs(by_reason.get("buy_fees", 0.0))
|
|
165
|
+
sell_principal = abs(by_reason.get("sell_principal", 0.0))
|
|
166
|
+
sell_fees = abs(by_reason.get("sell_fees", 0.0))
|
|
167
|
+
|
|
168
|
+
total_basis = pos.qty * pos.avg_cost
|
|
169
|
+
if buy_principal > 0:
|
|
170
|
+
open_share = total_basis / max(buy_principal + buy_fees, 1e-9)
|
|
171
|
+
fees_in_basis = min(buy_fees * open_share, total_basis)
|
|
172
|
+
else:
|
|
173
|
+
fees_in_basis = 0.0
|
|
174
|
+
principal_in_basis = total_basis - fees_in_basis
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
"qty": pos.qty,
|
|
178
|
+
"principal": principal_in_basis,
|
|
179
|
+
"fees_in_basis": fees_in_basis,
|
|
180
|
+
"total_basis": total_basis,
|
|
181
|
+
"ledger_buy_principal": buy_principal,
|
|
182
|
+
"ledger_buy_fees": buy_fees,
|
|
183
|
+
"ledger_sell_principal": sell_principal,
|
|
184
|
+
"ledger_sell_fees": sell_fees,
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
__all__ = ["list_all", "get", "mark_price", "basis_breakdown"]
|
papertrade_india/risk.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""Deprecated re-export shim — module moved to :mod:`papertrade_india.domain.rules.risk`.
|
|
2
|
+
|
|
3
|
+
Importing from here still works during the deprecation window but emits
|
|
4
|
+
a :class:`DeprecationWarning`. Update imports to the new path; this
|
|
5
|
+
shim will be removed in v0.3.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import warnings as _warnings
|
|
11
|
+
|
|
12
|
+
_warnings.warn(
|
|
13
|
+
"papertrade_india.risk is deprecated; "
|
|
14
|
+
"import from papertrade_india.domain.rules.risk instead.",
|
|
15
|
+
DeprecationWarning,
|
|
16
|
+
stacklevel=2,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
import papertrade_india.domain.rules.risk as _src # noqa: E402
|
|
20
|
+
from papertrade_india.domain.rules.risk import * # noqa: E402, F401, F403
|
|
21
|
+
|
|
22
|
+
# Re-export every attribute defined on the new module — including
|
|
23
|
+
# underscore-prefixed names — so callers that imported private symbols
|
|
24
|
+
# during the refactor window keep working. Dunders are skipped.
|
|
25
|
+
globals().update(
|
|
26
|
+
{name: getattr(_src, name) for name in dir(_src) if not name.startswith("__")}
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
del _src, _warnings
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""Deprecated re-export shim — module moved to :mod:`papertrade_india.execution.settlement`.
|
|
2
|
+
|
|
3
|
+
Importing from here still works during the deprecation window but emits
|
|
4
|
+
a :class:`DeprecationWarning`. Update imports to the new path; this
|
|
5
|
+
shim will be removed in v0.3.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import warnings as _warnings
|
|
11
|
+
|
|
12
|
+
_warnings.warn(
|
|
13
|
+
"papertrade_india.settlement is deprecated; "
|
|
14
|
+
"import from papertrade_india.execution.settlement instead.",
|
|
15
|
+
DeprecationWarning,
|
|
16
|
+
stacklevel=2,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
import papertrade_india.execution.settlement as _src # noqa: E402
|
|
20
|
+
from papertrade_india.execution.settlement import * # noqa: E402, F401, F403
|
|
21
|
+
|
|
22
|
+
# Re-export every attribute defined on the new module — including
|
|
23
|
+
# underscore-prefixed names — so callers that imported private symbols
|
|
24
|
+
# during the refactor window keep working. Dunders are skipped.
|
|
25
|
+
globals().update(
|
|
26
|
+
{name: getattr(_src, name) for name in dir(_src) if not name.startswith("__")}
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
del _src, _warnings
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""Deprecated re-export shim — module moved to :mod:`papertrade_india.execution.simulation`.
|
|
2
|
+
|
|
3
|
+
Importing from here still works during the deprecation window but emits
|
|
4
|
+
a :class:`DeprecationWarning`. Update imports to the new path; this
|
|
5
|
+
shim will be removed in v0.3.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import warnings as _warnings
|
|
11
|
+
|
|
12
|
+
_warnings.warn(
|
|
13
|
+
"papertrade_india.simulation is deprecated; "
|
|
14
|
+
"import from papertrade_india.execution.simulation instead.",
|
|
15
|
+
DeprecationWarning,
|
|
16
|
+
stacklevel=2,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
import papertrade_india.execution.simulation as _src # noqa: E402
|
|
20
|
+
from papertrade_india.execution.simulation import * # noqa: E402, F401, F403
|
|
21
|
+
|
|
22
|
+
# Re-export every attribute defined on the new module — including
|
|
23
|
+
# underscore-prefixed names — so callers that imported private symbols
|
|
24
|
+
# during the refactor window keep working. Dunders are skipped.
|
|
25
|
+
globals().update(
|
|
26
|
+
{name: getattr(_src, name) for name in dir(_src) if not name.startswith("__")}
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
del _src, _warnings
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""Deprecated re-export shim — module moved to :mod:`papertrade_india.execution.slippage`.
|
|
2
|
+
|
|
3
|
+
Importing from here still works during the deprecation window but emits
|
|
4
|
+
a :class:`DeprecationWarning`. Update imports to the new path; this
|
|
5
|
+
shim will be removed in v0.3.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import warnings as _warnings
|
|
11
|
+
|
|
12
|
+
_warnings.warn(
|
|
13
|
+
"papertrade_india.slippage is deprecated; "
|
|
14
|
+
"import from papertrade_india.execution.slippage instead.",
|
|
15
|
+
DeprecationWarning,
|
|
16
|
+
stacklevel=2,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
import papertrade_india.execution.slippage as _src # noqa: E402
|
|
20
|
+
from papertrade_india.execution.slippage import * # noqa: E402, F401, F403
|
|
21
|
+
|
|
22
|
+
# Re-export every attribute defined on the new module — including
|
|
23
|
+
# underscore-prefixed names — so callers that imported private symbols
|
|
24
|
+
# during the refactor window keep working. Dunders are skipped.
|
|
25
|
+
globals().update(
|
|
26
|
+
{name: getattr(_src, name) for name in dir(_src) if not name.startswith("__")}
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
del _src, _warnings
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""Deprecated re-export shim — module moved to :mod:`papertrade_india.infrastructure.symbols`.
|
|
2
|
+
|
|
3
|
+
Importing from here still works during the deprecation window but emits
|
|
4
|
+
a :class:`DeprecationWarning`. Update imports to the new path; this
|
|
5
|
+
shim will be removed in v0.3.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import warnings as _warnings
|
|
11
|
+
|
|
12
|
+
_warnings.warn(
|
|
13
|
+
"papertrade_india.symbols is deprecated; "
|
|
14
|
+
"import from papertrade_india.infrastructure.symbols instead.",
|
|
15
|
+
DeprecationWarning,
|
|
16
|
+
stacklevel=2,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
import papertrade_india.infrastructure.symbols as _src # noqa: E402
|
|
20
|
+
from papertrade_india.infrastructure.symbols import * # noqa: E402, F401, F403
|
|
21
|
+
|
|
22
|
+
# Re-export every attribute defined on the new module — including
|
|
23
|
+
# underscore-prefixed names — so callers that imported private symbols
|
|
24
|
+
# during the refactor window keep working. Dunders are skipped.
|
|
25
|
+
globals().update(
|
|
26
|
+
{name: getattr(_src, name) for name in dir(_src) if not name.startswith("__")}
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
del _src, _warnings
|