segments
¶
Canonical segment vocabulary for the instrument mapping layer.
The vocabulary declares every segment that a broker adapter may classify a raw row into, in the order that segments must appear in each broker's rules file under stock_brokers/instruments/mapping/utilities/rules/. The order is by asset class first — fixed income, equities, currencies, commodities — and within each asset class by instrument kind: simple instruments, futures, options, indices, index futures, index options. The catch-all segments come last: mutual funds, exchange traded funds, investment trusts, and the uncategorised buckets.
Segment values stored in unified.instruments are exchange-prefixed, for example nse_equities and bse_equity_options, produced by segment_value.
MAPPED_BROKERS lives here too, because the order the brokers are mapped in is a fact about mapping rather than about downloading, and several modules of this package import it.
Functions:
| Name | Description |
|---|---|
segment_value |
Build the exchange-prefixed segment value stored in the mapped tables. |
segment_shape |
Look up the shape declared for a bare segment name. |
segment_rank |
Compute the ordering rank of one configured segment. |
split_segment_value |
Split an exchange-prefixed segment value into its exchange and bare name. |
segments_of |
Build the prefixed segment values for every combination of exchange and bare segment. |
is_adjustable |
Whether an exchange-prefixed segment is one whose prices are stored unadjusted with factors. |
segment_value
¶
Build the exchange-prefixed segment value stored in the mapped tables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exchange
|
str
|
Canonical lowercase exchange name, for example "nse". |
required |
bare_segment
|
str
|
Bare segment name from CANONICAL_SEGMENTS, for example "equities". |
required |
Returns:
| Type | Description |
|---|---|
str
|
The prefixed segment value, for example "nse_equities". |
Source code in stock_brokers/instruments/mapping/utilities/segments.py
segment_shape
¶
Look up the shape declared for a bare segment name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bare_segment
|
str
|
Bare segment name from CANONICAL_SEGMENTS. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The segment's shape, one of "security", "future", or "option". |
Raises:
| Type | Description |
|---|---|
ValueError
|
If bare_segment is not part of the canonical vocabulary. |
Source code in stock_brokers/instruments/mapping/utilities/segments.py
segment_rank
¶
Compute the ordering rank of one configured segment.
The rank pairs the segment's position in the canonical vocabulary with the exchange's position in the canonical exchange list, so that a rules file listing segments in canonical order yields non-decreasing ranks. Config validation uses this to reject a rules file whose segments are out of order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exchange
|
str
|
Canonical lowercase exchange name. |
required |
bare_segment
|
str
|
Bare segment name from CANONICAL_SEGMENTS. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
A (segment_position, exchange_position) pair of ints. |
Source code in stock_brokers/instruments/mapping/utilities/segments.py
split_segment_value
¶
Split an exchange-prefixed segment value into its exchange and bare name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Prefixed segment value, for example "nse_equities". |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
An (exchange, bare_segment) pair, for example ("nse", "equities"). |
Source code in stock_brokers/instruments/mapping/utilities/segments.py
segments_of
¶
Build the prefixed segment values for every combination of exchange and bare segment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exchanges
|
str | list[str]
|
One canonical exchange name or several, for example "nse". |
required |
bare_segments
|
tuple[str]
|
Bare segment names, usually one of the families above. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
The prefixed values, for example ["nse_equities", "nse_exchange_traded_funds"]. |
Source code in stock_brokers/instruments/mapping/utilities/segments.py
is_adjustable
¶
Whether an exchange-prefixed segment is one whose prices are stored unadjusted with factors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
segment
|
str
|
A prefixed segment value, for example "nse_equities". |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True for equities, exchange traded funds and investment trusts on any exchange. |