wisdom_capital
¶
Wisdom Capital historical candle download.
Wisdom Capital runs Symphony's XTS platform, and four things about it are unlike every other broker here. All four were measured on 2026-09-12 rather than taken from documentation.
It needs a different session. XTS splits a broker into two applications with separate
credentials, and the interactive token is refused by the chart endpoint, which lives under
/apimarketdata. WisdomCapitalAPI establishes both sessions when it is constructed and keeps
the market data one in last_login as market_data_access_token, so this module asks that class
for the token - the same one bin/wisdom_capital/instruments/websocket_quotes carries - and sends
it on every request.
The bars are not JSON. They arrive inside one string under a key the platform spells
dataReponse, comma separated between bars and pipe separated within one.
Its timestamps need two corrections. The number is seconds since the epoch computed as though India time were UTC, so decoding it as UTC and relabelling the result as India time gives the India wall clock reading directly. And it names the last second of the bar rather than the first, so the bar's own length less one second is subtracted to reach its start. Both were checked against Flattrade's one minute bars for the same session: the starts line up exactly.
There is no daily interval. A compression of 86,400 seconds returns a rolling twenty four
hour bucket running from 09:15 one day to 09:14:59 the next, not the exchange's session, which
was confirmed by asking for it. A bar like that would sit in price_history looking like a daily
bar and comparing against real ones, so it is not offered at all.
The fifth difference is the dangerous one. An empty response says nothing. An instrument the
platform does not recognise, a window before its history begins, and an expired market data token
all answer with the same envelope: a type of success and an empty dataReponse. So a run of
empty windows cannot be trusted here the way it can elsewhere - a token that expired mid-backfill
would look exactly like tens of thousands of dead instruments, and the base class would retire
every series it touched. That is what _check_still_alive is for: after a run of empty windows it
asks about an instrument known to have bars, and if that comes back empty too it logs in again
and asks once more before concluding the session is the problem and stopping the broker.
Classes:
| Name | Description |
|---|---|
WisdomCapitalCandles |
Downloads Wisdom Capital's historical candles. |
WisdomCapitalCandles
¶
Bases: BrokerCandles
Downloads Wisdom Capital's historical candles.
Wisdom Capital candle downloader.
apiis an authenticatedWisdomCapitalAPI. One is constructed when not supplied. Either way a market data session is established on top of it, because the chart endpoint does not accept the interactive token.
Example programs
"""Takes a market data token, fetches one window of RELIANCE one minute bars from Wisdom Capital, and turns the platform's bar string into bars.
Wisdom Capital runs Symphony's XTS platform, whose chart endpoint refuses the interactive trading token and wants a separate market data token. `after_relogin` takes that token from the API class, which is also what the constructor does, so the program calls it once to arm the downloader. The request then carries the token in its `authorization` header, the segment number and instrument id from the identifier `1|2885`, the window in the platform's own `Sep 11 2026 000000` format, and the bar length in seconds.
The answer is one string, bars separated by commas and fields by pipes. Each timestamp needs two corrections: it counts seconds as though India time were UTC, and it names the bar's last second rather than its first. So `1789031759`, which reads as 09:15:59 on 2026-09-10, is the one minute bar that opened at 09:15 India time. Read as a five minute bar, the same stamp would open four minutes earlier, at 09:11.
A `WisdomCapitalCandles` constructor opens a PostgreSQL connection and logs in, so the program builds the object without running its constructor, as `test_runs/candle_parse.py` does, and gives it a stand-in for `WisdomCapitalAPI`. The stand-in hands out a fixed market data token, records the request, and answers with a bar string shaped like the one the live one minute feed returned, wrapped in the `status_code` and `data` envelope the real API class returns, including the platform's own misspelled key `dataReponse`.
Run it from the project root:
python examples/stock_brokers/instruments/historical/wisdom_capital/WisdomCapitalCandles/example_1_bar_string_and_end_stamps.py
"""
import datetime
from stock_brokers.instruments.historical.wisdom_capital import (
WisdomCapitalCandles,
)
class RecordedWisdomCapitalAPI:
"""A stand-in for `WisdomCapitalAPI` with a fixed market data token and one recorded bar string.
Attributes:
requests (list): The (url, headers, parameters) triples it was asked for.
"""
def __init__(self):
"""Builds the stand-in with no requests recorded.
Returns:
None: This method returns nothing.
"""
self.requests = []
def replace_market_data_session(self, stale_access_token=None):
"""Hands out the market data session in force.
Args:
stale_access_token (str | None): A token that was refused, or None when none was.
Returns:
dict: The session's `access_token` and `user_id`.
"""
return {
'access_token': 'market-data-token-1',
'user_id': 'WC1001',
}
def get(self, url, headers=None, params=None):
"""Records a GET request and answers it with two bars.
Args:
url (str): The URL the downloader asked for.
headers (dict): The headers it sent.
params (dict): The query parameters it sent.
Returns:
dict: The response envelope, with the platform's result under `data`.
"""
self.requests.append((url, headers, params))
return {
'status_code': 200,
'data': {
'dataReponse': '1789031759|1279.5|1285.3|1279.5|1283.2|149743|0|,1789031819|1283.2|1284.0|1282.0|1283.0|51743|0|',
},
}
class BarStringAndEndStampsExample:
"""Arms the downloader with a market data token, fetches one window and parses it.
Attributes:
api (RecordedWisdomCapitalAPI): The stand-in Wisdom Capital API.
candles (WisdomCapitalCandles): The downloader being shown.
"""
def __init__(self):
"""Builds the downloader around the stand-in API without opening a database connection.
Returns:
None: This method returns nothing.
"""
self.api = RecordedWisdomCapitalAPI()
self.candles = object.__new__(WisdomCapitalCandles)
self.candles._api = self.api
self.candles._empty_streak = 0
def run(self):
"""Takes the token, fetches 2026-09-10 and prints the request and the bars.
Returns:
None: This method returns nothing.
"""
self.candles.after_relogin()
day = datetime.date(2026, 9, 10)
payload = self.candles.fetch_candles('1|2885', '1minute', day, day)
url, headers, parameters = self.api.requests[0]
print(f'Requested {url}')
print(f' headers: {headers}')
print(f' parameters: {parameters}')
print(f' answer: {payload}')
for bar in self.candles.parse_response(payload, '1minute'):
bar_time, open_price, high, low, close, volume, open_interest = bar
print(f' {bar_time.isoformat()} open {open_price} high {high} low {low} close {close} volume {volume} oi {open_interest}')
first_as_five_minutes = self.candles.parse_response(payload, '5minute')[0][0]
print(f'The first stamp read as a five minute bar opens at {first_as_five_minutes.isoformat()}')
if __name__ == '__main__':
BarStringAndEndStampsExample().run()
It prints:
Requested https://trade.wisdomcapital.in/apimarketdata/instruments/ohlc
headers: {'authorization': 'market-data-token-1'}
parameters: {'exchangeSegment': '1', 'exchangeInstrumentID': '2885', 'startTime': 'Sep 10 2026 000000', 'endTime': 'Sep 11 2026 000000', 'compressionValue': '60'}
answer: 1789031759|1279.5|1285.3|1279.5|1283.2|149743|0|,1789031819|1283.2|1284.0|1282.0|1283.0|51743|0|
2026-09-10T09:15:00+05:30 open 1279.5 high 1285.3 low 1279.5 close 1283.2 volume 149743 oi 0
2026-09-10T09:16:00+05:30 open 1283.2 high 1284.0 low 1282.0 close 1283.0 volume 51743 oi 0
The first stamp read as a five minute bar opens at 2026-09-10T09:11:00+05:30
"""Builds Wisdom Capital identifiers from its instrument master, reads what each says about its instrument, and classifies the platform's refusals.
One XTS instrument id appears under several segments, so `instruments` joins the segment's number to the instrument id, as in `1|2885`, and turns XTS's numeric instrument type into a word the queue's priority understands (`8` is cash, `1` a future). Rows in a segment the platform has no number for are left out. The program gives the downloader a stand-in database connection whose cursor answers with four rows, one of them in a segment the table does not know. `identifier_parts` splits an identifier again, and `series_context`, a classmethod that needs no downloader, reads the exchange and segments its number names.
The platform raises its conditions with codes of its own. The program fetches through a stand-in for `WisdomCapitalAPI` that raises the `WisdomCapitalAPIException` for each of three: `e-apirl` is a rate limit, `e-session` a dead session and `e-instrument` an instrument the platform will not serve, which retires the series. The stand-in also hands out the market data token that `after_relogin` takes before any request is made.
A `WisdomCapitalCandles` constructor opens a PostgreSQL connection and logs in, so the program builds the object without running its constructor, as `test_runs/candle_parse.py` does.
Run it from the project root:
python examples/stock_brokers/instruments/historical/wisdom_capital/WisdomCapitalCandles/example_2_identifiers_and_refusals.py
"""
import datetime
from stock_brokers.api.wisdom_capital import (
WisdomCapitalAPIException,
)
from stock_brokers.instruments.historical.base import (
CandleError,
)
from stock_brokers.instruments.historical.wisdom_capital import (
WisdomCapitalCandles,
)
class InstrumentMasterCursor:
"""A stand-in database cursor that answers the instrument master query with fixed rows.
Attributes:
rows (list): The rows `fetchall` returns.
"""
def __init__(self, rows):
"""Holds the rows to answer with.
Args:
rows (list): Tuples of (segment name, instrument id, instrument type, expiry).
Returns:
None: This method returns nothing.
"""
self.rows = rows
def __enter__(self):
"""Enters the `with` block.
Returns:
InstrumentMasterCursor: This cursor.
"""
return self
def __exit__(self, exception_type, exception, traceback):
"""Leaves the `with` block without suppressing an exception.
Args:
exception_type (type | None): The exception's class, if one was raised.
exception (BaseException | None): The exception, if one was raised.
traceback (types.TracebackType | None): Its traceback, if one was raised.
Returns:
bool: Always False.
"""
return False
def execute(self, query):
"""Accepts the query without running it.
Args:
query (str): The SQL the downloader sent.
Returns:
None: This method returns nothing.
"""
def fetchall(self):
"""Returns the fixed rows.
Returns:
list: The rows.
"""
return self.rows
class InstrumentMasterConnection:
"""A stand-in PostgreSQL connection over four rows of the XTS instrument master.
Attributes:
rows (list): The rows every cursor answers with.
"""
def __init__(self):
"""Builds the connection with four instrument master rows.
Returns:
None: This method returns nothing.
"""
self.rows = [
(
'NSECM',
'2885',
8,
None,
),
(
'BSECM',
'500325',
8,
None,
),
(
'MCXFO',
'565899',
1,
'2026-10-19T23:59:59',
),
(
'NSEXX',
'777',
8,
None,
),
]
def cursor(self):
"""Opens a cursor.
Returns:
InstrumentMasterCursor: A cursor over the rows.
"""
return InstrumentMasterCursor(self.rows)
def commit(self):
"""Accepts a commit.
Returns:
None: This method returns nothing.
"""
class RefusingWisdomCapitalAPI:
"""A stand-in for `WisdomCapitalAPI` that hands out a market data token and refuses every chart request.
Attributes:
code (str | None): The platform code the next refusal carries.
message (str | None): The message the next refusal carries.
"""
def __init__(self):
"""Builds the stand-in with no refusal chosen yet.
Returns:
None: This method returns nothing.
"""
self.code = None
self.message = None
def replace_market_data_session(self, stale_access_token=None):
"""Hands out the market data session in force.
Args:
stale_access_token (str | None): A token that was refused, or None when none was.
Returns:
dict: The session's `access_token` and `user_id`.
"""
return {
'access_token': 'market-data-token-1',
'user_id': 'WC1001',
}
def get(self, url, headers=None, params=None):
"""Refuses a GET request with the chosen code.
Args:
url (str): The URL the downloader asked for.
headers (dict): The headers it sent.
params (dict): The query parameters it sent.
Returns:
dict: Never returns.
Raises:
WisdomCapitalAPIException: Always, carrying the chosen code and message.
"""
raise WisdomCapitalAPIException(code=self.code, message=self.message)
class IdentifiersAndRefusalsExample:
"""Lists identifiers, reads them, and classifies three refusals.
Attributes:
api (RefusingWisdomCapitalAPI): The stand-in Wisdom Capital API.
candles (WisdomCapitalCandles): The downloader being shown.
"""
def __init__(self):
"""Builds the downloader around the stand-ins without running its constructor.
Returns:
None: This method returns nothing.
"""
self.api = RefusingWisdomCapitalAPI()
self.candles = object.__new__(WisdomCapitalCandles)
self.candles._connection = InstrumentMasterConnection()
self.candles._api = self.api
self.candles._empty_streak = 0
def show_identifiers(self):
"""Lists the instruments and reads each identifier.
Returns:
None: This method returns nothing.
"""
instruments = self.candles.instruments()
print(f'Instruments to download: {len(instruments)} of 4 rows')
for identifier, instrument_type, expiry in instruments:
segment_number, instrument_identifier = self.candles.identifier_parts(identifier)
context = WisdomCapitalCandles.series_context(identifier)
print(f'{identifier}: segment {segment_number}, instrument {instrument_identifier}, {instrument_type}, expiry {expiry}, exchange {context.exchange}, {len(context.segments)} segments to search')
def show_refusals(self):
"""Fetches through three refusals and prints what each became.
Returns:
None: This method returns nothing.
"""
self.candles.after_relogin()
refusals = [
(
'e-apirl-0004',
'Rate limit exceeded',
),
(
'e-session-0002',
'Invalid Token',
),
(
'e-instrument-0001',
'Instrument not found',
),
]
for code, message in refusals:
self.api.code = code
self.api.message = message
try:
self.candles.fetch_candles(
'2|35001',
'5minute',
datetime.date(2025, 1, 1),
datetime.date(2025, 3, 31),
)
outcome = 'answered'
except CandleError as error:
outcome = type(error).__name__
print(f'{code} "{message}": {outcome}')
def run(self):
"""Runs both parts in turn.
Returns:
None: This method returns nothing.
"""
self.show_identifiers()
self.show_refusals()
if __name__ == '__main__':
IdentifiersAndRefusalsExample().run()
It prints:
Instruments to download: 3 of 4 rows
1|2885: segment 1, instrument 2885, EQ, expiry None, exchange nse, 6 segments to search
11|500325: segment 11, instrument 500325, EQ, expiry None, exchange bse, 6 segments to search
51|565899: segment 51, instrument 565899, FUT, expiry 2026-10-19T23:59:59, exchange mcx, 16 segments to search
e-apirl-0004 "Rate limit exceeded": CandleThrottled
e-session-0002 "Invalid Token": CandleAuthenticationError
e-instrument-0001 "Instrument not found": CandleInstrumentUnknown
"""Shows what Wisdom Capital's downloader does after fifty empty windows in a row: it asks about an instrument that always has bars before deciding the session is dead.
The XTS platform answers an unknown instrument, a window before the history begins and an expired market data token in exactly the same way, with an empty bar string. So after fifty empty windows `fetch_candles` asks about NSE cash RELIANCE for the past week. If that is empty too it takes a fresh market data token, handing the refused one back so a token another process already replaced is recognised, and asks once more. Only if the second answer is empty as well does it raise `CandleAuthenticationError` and stop the broker.
The program runs two stand-ins for `WisdomCapitalAPI` through this. In the first, the market data token has expired: every answer is empty until a new token is taken, after which RELIANCE has bars, so the fiftieth window returns normally with the streak reset. In the second, the platform answers everything empty whatever the token, so the fiftieth window raises. The stand-ins count requests and record which stale token each replacement was given; the canary's window depends on today's date, so it is not printed.
A `WisdomCapitalCandles` constructor opens a PostgreSQL connection and logs in, so the program builds the object without running its constructor, as `test_runs/candle_parse.py` does, and supplies the rate limiter and logger the constructor would have made. The limiter is set to a thousand requests a second so the program does not wait, and the logger is silent so the warning the check logs does not clutter the output.
Run it from the project root:
python examples/stock_brokers/instruments/historical/wisdom_capital/WisdomCapitalCandles/example_3_fifty_empty_windows_check_the_session.py
"""
import datetime
import logging
from stock_brokers.instruments.historical.base import (
CandleAuthenticationError,
RateLimiter,
)
from stock_brokers.instruments.historical.wisdom_capital import (
WisdomCapitalCandles,
)
class EmptyAnsweringAPI:
"""A stand-in for `WisdomCapitalAPI` that answers empty until its token is replaced, or for ever.
Attributes:
recovers_after_new_token (bool): True when a fresh token makes the canary answer with bars.
tokens_issued (int): How many market data tokens it has handed out.
stale_tokens (list): The stale token each replacement was given.
requests (int): How many chart requests it answered.
"""
def __init__(self, recovers_after_new_token):
"""Builds the stand-in.
Args:
recovers_after_new_token (bool): True when a fresh token makes the canary answer with bars.
Returns:
None: This method returns nothing.
"""
self.recovers_after_new_token = recovers_after_new_token
self.tokens_issued = 0
self.stale_tokens = []
self.requests = 0
def replace_market_data_session(self, stale_access_token=None):
"""Hands out a new market data token.
Args:
stale_access_token (str | None): The token that was refused, or None when none was.
Returns:
dict: The session's `access_token` and `user_id`.
"""
self.tokens_issued += 1
self.stale_tokens.append(stale_access_token)
return {
'access_token': f'market-data-token-{self.tokens_issued}',
'user_id': 'WC1001',
}
def get(self, url, headers=None, params=None):
"""Answers a chart request, with bars only for the canary on a fresh token when it recovers.
Args:
url (str): The URL the downloader asked for.
headers (dict): The headers it sent.
params (dict): The query parameters it sent.
Returns:
dict: The response envelope, with the platform's result under `data`.
"""
self.requests += 1
bars = ''
is_canary = params['exchangeInstrumentID'] == '2885'
fresh_token = headers['authorization'] != 'market-data-token-1'
if self.recovers_after_new_token and is_canary and fresh_token:
bars = '1789031759|1279.5|1285.3|1279.5|1283.2|149743|0|'
return {
'status_code': 200,
'data': {
'dataReponse': bars,
},
}
class FiftyEmptyWindowsCheckTheSessionExample:
"""Runs fifty empty windows against a recovering platform and against a dead one."""
def build_downloader(self, api):
"""Builds a downloader around a stand-in API without opening a database connection.
Args:
api (EmptyAnsweringAPI): The stand-in API.
Returns:
WisdomCapitalCandles: The downloader, holding its first market data token.
"""
candles = object.__new__(WisdomCapitalCandles)
candles._api = api
candles._empty_streak = 0
candles._relogin_used = False
candles._limiter = RateLimiter(1000.0)
candles._logger = logging.getLogger('example.wisdom_capital')
candles._logger.disabled = True
candles.after_relogin()
return candles
def walk_fifty_empty_windows(self, description, api):
"""Asks for fifty expired contracts' windows and prints how the fiftieth ends.
Args:
description (str): What the stand-in pretends.
api (EmptyAnsweringAPI): The stand-in API.
Returns:
None: This method returns nothing.
"""
candles = self.build_downloader(api)
day = datetime.date(2025, 6, 2)
print(description)
try:
for instrument_number in range(50):
candles.fetch_candles(f'2|{40000 + instrument_number}', '5minute', day, day)
print(' the fiftieth window returned normally')
except CandleAuthenticationError as error:
print(f' {type(error).__name__}: {error}')
print(f' chart requests: {api.requests}')
print(f' market data tokens issued: {api.tokens_issued}, stale tokens handed back: {api.stale_tokens}')
def run(self):
"""Runs the recovering platform, then the dead one.
Returns:
None: This method returns nothing.
"""
self.walk_fifty_empty_windows('Expired market data token:', EmptyAnsweringAPI(True))
self.walk_fifty_empty_windows('Platform answering nothing:', EmptyAnsweringAPI(False))
if __name__ == '__main__':
FiftyEmptyWindowsCheckTheSessionExample().run()
It prints:
Expired market data token:
the fiftieth window returned normally
chart requests: 52
market data tokens issued: 2, stale tokens handed back: [None, 'market-data-token-1']
Platform answering nothing:
CandleAuthenticationError: wisdom_capital answers every window empty, including one for an instrument that always has bars, and a fresh market data login did not change it. Stopping rather than retiring series that may be perfectly good.
chart requests: 52
market data tokens issued: 2, stale tokens handed back: [None, 'market-data-token-1']
Methods:
| Name | Description |
|---|---|
after_relogin |
Take the market data token again after the broker session has been logged in again. |
instruments |
Every instrument Wisdom Capital has published, identified the way the wire needs. |
identifier_parts |
Split a stored Wisdom Capital identifier into its segment number and instrument id. |
series_context |
Read the exchange and segment out of a Wisdom Capital identifier. |
fetch_candles |
Fetch one window of Wisdom Capital candles. |
parse_response |
Turn Wisdom Capital's bar string into bars. |
priority_for |
Where a series sits in the queue. Lower is worked first. |
seed |
Make sure every instrument and interval this broker serves has a progress row. |
claim |
The next series due for a window, or None when there is nothing to do. |
next_window |
The date range to ask for next, as a |
relogin |
Establish a fresh session and rebuild everything built on the old one. |
visit |
Fetch one window for one series and store it, advancing the watermark. |
run |
Work the queue until stopped, out of work, or out of time. |
summary |
How far the download has got, for a status display. |
close |
Close the database connection. |
Source code in stock_brokers/instruments/historical/wisdom_capital.py
_log_in_to_market_data
¶
Take the market data token the broker session holds, replacing it when it has been refused.
Separate from the broker's ordinary session: XTS issues one token for trading and another
for market data, and the chart endpoint refuses the first. WisdomCapitalAPI establishes
both and publishes them in the shared last_login, so this asks that class for the token
rather than minting one - a second market data login invalidates the first, which would
take the live quotes feed's token with it.
refreshreplaces the token in hand, for one that has stopped working. Rotating it is not destructive: whoever reads it next gets the new one.
Source code in stock_brokers/instruments/historical/wisdom_capital.py
after_relogin
¶
Take the market data token again after the broker session has been logged in again.
The chart endpoint takes the market data token, not the interactive one, so a fresh interactive session alone would leave a refused credential in place. Building the API class checks the market data token as well and replaces it only when it has stopped working, so this takes what that check settled on rather than forcing a new login, which would invalidate the token the live quotes feed is using. The empty-window check does force one, because there the market data token is what is suspect.
Example usage
Source code in stock_brokers/instruments/historical/wisdom_capital.py
instruments
¶
Every instrument Wisdom Capital has published, identified the way the wire needs.
The identifier is the segment's number joined to the instrument id, because one instrument id appears under several segments and the request carries both. The segment number comes from the platform's own segment name rather than from anything translated, since the names map one to one onto the numbers.
The instrument type is turned from XTS's number into a word so that priority_for can
tell a future from an option, and the expiry is already ISO-8601 and needs nothing.
Example usage
Source code in stock_brokers/instruments/historical/wisdom_capital.py
identifier_parts
staticmethod
¶
Split a stored Wisdom Capital identifier into its segment number and instrument id.
The identifier is built in instruments and taken apart here and nowhere else, so the two
cannot drift apart.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
identifier
|
str
|
For example "11|500325". |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
The segment number and the exchange instrument id. |
Example usage
segment_number, instrument_identifier = self.candles.identifier_parts(identifier)
Source code in stock_brokers/instruments/historical/wisdom_capital.py
series_context
classmethod
¶
Read the exchange and segment out of a Wisdom Capital identifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
identifier
|
str
|
For example "11|500325". |
required |
Returns:
| Type | Description |
|---|---|
SeriesContext
|
The instrument id, with the exchange and segments its number names. |
Example usage
Source code in stock_brokers/instruments/historical/wisdom_capital.py
fetch_candles
¶
Fetch one window of Wisdom Capital candles.
tokenis thesegmentNumber|instrumentIdidentifier frominstruments.intervalis the stored interval name.start_dateandend_datebound the window, inclusive.
Example usage
payload = self.candles.fetch_candles('1|2885', '1minute', day, day)
self.candles.fetch_candles(
'2|35001',
'5minute',
datetime.date(2025, 1, 1),
datetime.date(2025, 3, 31),
)
candles.fetch_candles(f'2|{40000 + instrument_number}', '5minute', day, day)
Source code in stock_brokers/instruments/historical/wisdom_capital.py
_ask
¶
Make one request and return the bar string it carried, which may be empty.
segment_numberis the XTS exchange segment number.instrument_identifieris the instrument id within that segment.compressionis the bar length in seconds.window_startandwindow_endare India wall clock times bounding the window.
Source code in stock_brokers/instruments/historical/wisdom_capital.py
_check_still_alive
¶
Decide whether a run of empty windows means dead instruments or a dead session.
This platform answers an unknown instrument, a window before the history begins and an expired market data token identically, so the only way to tell them apart is to ask about an instrument that is known to have bars. If that is empty too the token is logged in again and asked once more, because expiry is the likeliest explanation and it is cheap to fix. Only if the second answer is empty as well is the broker stopped.
Source code in stock_brokers/instruments/historical/wisdom_capital.py
_classify
staticmethod
¶
Decide what a Wisdom Capital refusal means for the series being downloaded.
The platform names its conditions in the body with codes of its own, which the API class raises as the exception's code.
exceptionis what the API call raised.
Source code in stock_brokers/instruments/historical/wisdom_capital.py
parse_response
¶
Turn Wisdom Capital's bar string into bars.
Each bar is timestamp|open|high|low|close|volume|openInterest, bars separated by commas,
with a trailing pipe leaving an empty last field.
payloadis the bar stringfetch_candlesreturned.intervalis the stored interval name. It is needed rather than ignored, because the timestamp names the end of the bar and the bar's own length is what converts that into its start.
Example usage
first_as_five_minutes = self.candles.parse_response(payload, '5minute')[0][0]
Source code in stock_brokers/instruments/historical/wisdom_capital.py
_bar_start_time
staticmethod
¶
Turn one of the platform's timestamps into the time the bar opened.
Two corrections, both measured. The number is seconds since the epoch computed as though India time were UTC, so it is decoded as UTC and the result relabelled as India time, which yields the India wall clock reading directly. And it names the bar's last second, so the bar's length less one second is subtracted to reach its start.
stampis the epoch value as the platform reported it.bar_secondsis the length of the bar in seconds.
Source code in stock_brokers/instruments/historical/wisdom_capital.py
priority_for
¶
Where a series sits in the queue. Lower is worked first.
The ordering is the difference between useful data in days and useful data in months. Cash instruments come first because they have the deepest history and are wanted most; expired options come last because there are tens of thousands of them, each holding a few weeks of data, and most will be refused outright. Coarse intervals beat fine ones within each tier, since a day bar covers a year in one request and a minute bar covers two months.
instrument_typeis the broker's own type string.expiryis the contract expiry, or None for cash.intervalis the stored interval name.
Example usage
priority = self.downloader.priority_for(instrument_type, expiry, interval)
Source code in stock_brokers/instruments/historical/base.py
_interval_minutes
staticmethod
¶
Minutes in an interval name like 15minute, or 0 when it is not a minute interval.
intervalis the stored interval name.
Source code in stock_brokers/instruments/historical/base.py
seed
¶
Make sure every instrument and interval this broker serves has a progress row.
Safe to run again: existing rows are left exactly as they are, so re-seeding after a new instrument master picks up what is new without disturbing what is already downloaded. Returns how many rows were added.
Example usage
print(f'Series added by the first seed: {self.downloader.seed()}')
print(f'Series added by a second seed: {self.downloader.seed()}')
Source code in stock_brokers/instruments/historical/base.py
_insert_series
¶
Register one batch of series, leaving any that already exist exactly as they are.
cursoris an open cursor on this broker's connection.batchis a list of(token, interval, priority, seeded_date)tuples.
Source code in stock_brokers/instruments/historical/base.py
claim
¶
The next series due for a window, or None when there is nothing to do.
Ordered by priority and then by how long a series has been waiting. Series that have reached the broker's limit, or that are backing off after failures, are skipped.
Example usage
Source code in stock_brokers/instruments/historical/base.py
next_window
¶
The date range to ask for next, as a (start, end, direction) triple.
The backward walk comes first and runs to completion. A series starts at today and steps back a window at a time until it reaches the broker's earliest date or the broker stops returning anything, and only then does it start filling forward with whatever has happened since.
Doing it the other way round - preferring forward whenever the last stored bar is older than today - looks reasonable and does not work: outside market hours the last bar is always older than today, so the series asks for the same empty recent window forever and never walks back at all.
seriesis a row fromclaim.
Example usage
start_date, end_date, direction = self.downloader.next_window(series)
Source code in stock_brokers/instruments/historical/base.py
_fetch_bars
¶
Fetch and parse one window, logging in once first if the broker refuses the session.
A token expiring does not mean the download is over, and it does not mean this series is at
fault. The rule is to log in first and then carry on: one login, then the same window
again. Only if that login fails, or the fresh session is refused as well, does the error go
back up - where visit and run stop the broker rather than carry on with a dead token.
Both alternatives are worse. Stopping outright leaves the download waiting for systemd to restart it into a login, and recording the refusal against whichever series it was on and moving to the next sends thousands of refused requests an hour - enough for Cloudflare to ban the machine from Fyers.
The login is re-armed by any window that comes back without a session error, so a backfill that runs for weeks gets its one login at each daily expiry.
token,interval,start_dateandend_dateare as forfetch_candles.
Source code in stock_brokers/instruments/historical/base.py
relogin
¶
Establish a fresh session and rebuild everything built on the old one.
Through ensure_session, so it takes the same Redis lock and obeys the same floor on login
attempts as any other process using it - which means it may wait, and may find another
process has already logged in, in which case that session is simply used.
Raises:
| Type | Description |
|---|---|
CandleAuthenticationError
|
If no working session could be established. |
Example usage
Source code in stock_brokers/instruments/historical/base.py
_build_api
¶
Construct this broker's authenticated API class, around whatever session is now stored.
The broker's own class from api_class_for, which is what every downloader builds. A
downloader that builds its API differently overrides this.
Source code in stock_brokers/instruments/historical/base.py
visit
¶
Fetch one window for one series and store it, advancing the watermark.
Returns the number of bars stored. The bars and the watermark are written in the same transaction, so an interruption costs at most the window in flight - and repeating that window is harmless, because bars upsert on their key.
seriesis a row fromclaim.
Example usage
stored = downloader.visit(self.series('738561'))
Source code in stock_brokers/instruments/historical/base.py
_store
¶
Write a window's bars and move the watermark, in one transaction.
tokenis the broker's instrument identifier.intervalis the stored interval name.barsare the parsed(time, open, high, low, close, volume, oi)tuples.requested_fromis the start of the window that was asked for.directionis whether the walk was forward or backward.previous_latestis the newest bar already stored, used to tell a forward window that gained ground from one that returned only what was already known.
Source code in stock_brokers/instruments/historical/base.py
_value_within_bigint
¶
Returns a bar's count, or None when the count cannot fit in a BIGINT column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The broker's instrument identifier. |
required |
interval
|
str
|
The stored interval name. |
required |
bar_time
|
datetime
|
The time of the bar the value belongs to. |
required |
column
|
str
|
The name of the column the value is written to, either "volume" or "oi". |
required |
value
|
int | float | None
|
The count exactly as the broker sent it. |
required |
Returns:
| Type | Description |
|---|---|
int | float | None
|
The value unchanged when it fits in a BIGINT or is not a number, and None when it does not fit. |
Source code in stock_brokers/instruments/historical/base.py
_record_empty
¶
Note that a window came back with nothing, and step past it.
token,intervalidentify the series.streakis how many empty windows in a row this makes.requested_fromis the start of the window that was asked for.directionis whether the walk was forward or backward.
Source code in stock_brokers/instruments/historical/base.py
_finish_backfill
¶
Mark the backward walk complete, leaving the series collecting forward.
Distinct from _retire: the broker has no more past for this series, but it will keep
producing new bars. Retiring it here would quietly stop a live instrument updating.
token,intervalidentify the series.reasonis why the walk stopped, kept for review.
Source code in stock_brokers/instruments/historical/base.py
_defer_until_tomorrow
¶
Put a series aside until the next day.
Reached when a forward window brings nothing new, which outside market hours is every forward window. Without it the highest priority series would be re-fetched continuously and spend the whole rate limit re-reading bars already stored.
token,intervalidentify the series.reasonis recorded as the outcome.
Source code in stock_brokers/instruments/historical/base.py
_record_throttle
¶
Note that the broker asked us to slow down, without holding it against the series.
The window is left exactly as it was, so the same series is claimed again once the limiter's back off has passed and the work is simply redone.
token,intervalidentify the series.reasonis what the broker said.
Source code in stock_brokers/instruments/historical/base.py
_record_failure
¶
Note a failure and push the next attempt out exponentially.
An hour, then two, four, eight and so on, so a series that is permanently broken stops crowding out the ones that are not.
token,intervalidentify the series.reasonis what went wrong, stored for a human to read.
Source code in stock_brokers/instruments/historical/base.py
_retire
¶
Stop asking for a series the broker will not serve.
token,intervalidentify the series.reasonis why, stored so the decision can be reviewed.
Source code in stock_brokers/instruments/historical/base.py
run
¶
Work the queue until stopped, out of work, or out of time.
Returns a (visits, bars) pair.
stop_eventis athreading.Eventthat ends the loop when set.deadline_secondsends the loop after this long, for a job with a nightly window.
Example usage
print(f'A run asked to stop before it starts: {self.downloader.run(stop_event=stop_event)}')
Source code in stock_brokers/instruments/historical/base.py
summary
¶
How far the download has got, for a status display.
Example usage
Source code in stock_brokers/instruments/historical/base.py
close
¶
Close the database connection.
Example usage