conrich2 1.0.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.
- conrich/Common/__init__.py +0 -0
- conrich/Common/cipher.py +74 -0
- conrich/Common/endpoints.py +49 -0
- conrich/Common/errors.py +29 -0
- conrich/Common/events.py +117 -0
- conrich/Common/framing.py +70 -0
- conrich/Common/gate.py +65 -0
- conrich/Common/normalize.py +46 -0
- conrich/Common/records.py +44 -0
- conrich/Common/service.py +110 -0
- conrich/Common/signer.py +176 -0
- conrich/Common/tagvalue.py +47 -0
- conrich/Common/timeutil.py +35 -0
- conrich/Common/transport.py +171 -0
- conrich/Common/version.py +16 -0
- conrich/Futures/Futures_README.md +71 -0
- conrich/Futures/__init__.py +12 -0
- conrich/Futures/base.py +236 -0
- conrich/Futures/constant.py +63 -0
- conrich/Futures/fields.py +716 -0
- conrich/Futures/foreign_futures.py +53 -0
- conrich/Futures/futures.py +74 -0
- conrich/Futures/messages.py +97 -0
- conrich/Futures/session.py +464 -0
- conrich/Quote/__init__.py +15 -0
- conrich/Quote/event.py +4 -0
- conrich/Quote/quote.py +116 -0
- conrich/Quote/server.py +10 -0
- conrich/Quote/symbols.py +18 -0
- conrich/Stock/__init__.py +10 -0
- conrich/Stock/constant.py +48 -0
- conrich/Stock/fields.py +147 -0
- conrich/Stock/messages.py +179 -0
- conrich/Stock/orders.py +104 -0
- conrich/Stock/results.py +53 -0
- conrich/Stock/session.py +373 -0
- conrich/Stock/stock.py +279 -0
- conrich/__init__.py +38 -0
- conrich2-1.0.0.dev0.dist-info/METADATA +242 -0
- conrich2-1.0.0.dev0.dist-info/RECORD +43 -0
- conrich2-1.0.0.dev0.dist-info/WHEEL +5 -0
- conrich2-1.0.0.dev0.dist-info/licenses/LICENSE +21 -0
- conrich2-1.0.0.dev0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""ForeignFuturesAPI: pure Python client for foreign futures / options (2025 protocol)."""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
|
|
7
|
+
from . import fields
|
|
8
|
+
from .base import FuturesBaseAPI
|
|
9
|
+
from .messages import FOREIGN
|
|
10
|
+
|
|
11
|
+
logger = logging.getLogger('conrich.futures')
|
|
12
|
+
|
|
13
|
+
_TOKENS = {
|
|
14
|
+
'commodity': 'FF_QCommo', 'commodity_ym': 'FF_QCommoYM', 't_order': 'FF_QTOrder', 'h_order': 'FF_QHOrder',
|
|
15
|
+
'offset': 'FF_QQRYCOVEA', 'offset_history': 'FF_QHFFTRH', 'deposit': 'FC_QQRYMNYAL', 'deposit_history': 'FF_QCASHA',
|
|
16
|
+
'position_index': 'FF_QARYKEEPA',
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class ForeignFuturesAPI(FuturesBaseAPI):
|
|
21
|
+
spec = FOREIGN
|
|
22
|
+
|
|
23
|
+
def _tok(self, name: str) -> str:
|
|
24
|
+
return _TOKENS[name]
|
|
25
|
+
|
|
26
|
+
def check_connect(self) -> str:
|
|
27
|
+
"""0.7.x returned the DLL return code string here (not a bool); kept for compatibility."""
|
|
28
|
+
code = self._check_code()
|
|
29
|
+
logger.debug('[FFCheckConnect] (%s) %s', code, fields.RETURN_CODES.get(code, ''))
|
|
30
|
+
return code
|
|
31
|
+
|
|
32
|
+
def exchange(self):
|
|
33
|
+
return self._table('FF_QExchange', [], 'exchange')
|
|
34
|
+
|
|
35
|
+
def exchange_rate(self):
|
|
36
|
+
return self._table('FF_QCURRATE', [], 'exchange_rate')
|
|
37
|
+
|
|
38
|
+
def order(self, action, branch_no, account_no, exchange,
|
|
39
|
+
side1, market_type1, commodity1, commodity_month1, strike_price1, put_call1,
|
|
40
|
+
side2, market_type2, commodity2, commodity_month2, strike_price2, put_call2,
|
|
41
|
+
order_type, price, price_numerator, price_denominator, stop_price, stop_price_numerator,
|
|
42
|
+
stop_price_denominator, quantity, day_trade_flag='N', writeoff='', order_id='', orig_quantity=0) -> str:
|
|
43
|
+
"""Behind the gate; the order composer arrives in Phase 7 (design-2025.md 3.9, 8.2)."""
|
|
44
|
+
self._gate.check(str(account_no))
|
|
45
|
+
raise NotImplementedError('foreign futures order() is not implemented yet (design-2025.md Phase 7)')
|
|
46
|
+
|
|
47
|
+
def margin(self, branch_no, account_no):
|
|
48
|
+
bhno = self._bhno(branch_no)
|
|
49
|
+
return self._table('FF_QRYFINAA', [bhno, account_no], 'margin', (bhno, account_no))
|
|
50
|
+
|
|
51
|
+
def position(self, branch_no, account_no):
|
|
52
|
+
bhno = self._bhno(branch_no)
|
|
53
|
+
return self._table('FF_QTRYKEEPA', [bhno, account_no], 'position', (bhno, account_no))
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""FuturesAPI: pure Python client for domestic futures / options (2025 protocol)."""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
|
|
7
|
+
from ..Common.records import csv_to_dicts, unique_header
|
|
8
|
+
from . import fields
|
|
9
|
+
from .base import FuturesBaseAPI
|
|
10
|
+
from .messages import DOMESTIC, quote_params
|
|
11
|
+
|
|
12
|
+
logger = logging.getLogger('conrich.futures')
|
|
13
|
+
|
|
14
|
+
_TOKENS = {
|
|
15
|
+
'commodity': 'F_QCommo', 'commodity_ym': 'F_QCommoYM', 't_order': 'F_QTOrder', 'h_order': 'F_QHOrder',
|
|
16
|
+
'offset': 'F_QQRYCOVER', 'offset_history': 'F_QHFTRH', 'deposit': 'FC_QQRYMNYAL', 'deposit_history': 'FC_QCASHA',
|
|
17
|
+
'position_index': 'F_QQRYKEEPC',
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class FuturesAPI(FuturesBaseAPI):
|
|
22
|
+
spec = DOMESTIC
|
|
23
|
+
|
|
24
|
+
def _tok(self, name: str) -> str:
|
|
25
|
+
return _TOKENS[name]
|
|
26
|
+
|
|
27
|
+
def check_connect(self) -> bool:
|
|
28
|
+
code = self._check_code()
|
|
29
|
+
logger.debug('[FCheckConnect] (%s) %s', code, fields.RETURN_CODES.get(code, ''))
|
|
30
|
+
return code == '102'
|
|
31
|
+
|
|
32
|
+
def order(self, action, branch_no, account_no, market_type, compound_type, commodity, time_in_force,
|
|
33
|
+
writeoff, order_type, side, quantity, price, orig_net_no='', order_id='') -> str:
|
|
34
|
+
"""Behind the gate; the order composer arrives in Phase 6 (design-2025.md 3.8, 8.2)."""
|
|
35
|
+
self._gate.check(str(account_no))
|
|
36
|
+
raise NotImplementedError('futures order() is not implemented yet (design-2025.md Phase 6)')
|
|
37
|
+
|
|
38
|
+
def margin(self, branch_no, account_no, currency='**', group='', trader=''):
|
|
39
|
+
bhno = self._bhno(branch_no)
|
|
40
|
+
return self._table('F_QQRYSFINA', [bhno, account_no, currency, group, trader], 'margin', (bhno, account_no))
|
|
41
|
+
|
|
42
|
+
def position(self, branch_no, account_no, group='', trader='', compound_type='3'):
|
|
43
|
+
bhno = self._bhno(branch_no)
|
|
44
|
+
return self._table('F_QQRYSKEEP', [bhno, account_no, group, trader, compound_type], 'position', (bhno, account_no))
|
|
45
|
+
|
|
46
|
+
def position_summary(self, branch_no, account_no, compound_type='3'):
|
|
47
|
+
bhno = self._bhno(branch_no)
|
|
48
|
+
return self._table('F_QQRYKEEPA', [bhno, account_no, compound_type], 'position_summary', (bhno, account_no))
|
|
49
|
+
|
|
50
|
+
def quote(self, commodity: str) -> dict[str, str]:
|
|
51
|
+
"""FQueryQuote. UNVERIFIED: the answer layout is unknown (no recording); columns are named
|
|
52
|
+
by HEADERS['Futures']['quote'] when that list is filled in, else 'col1', 'col2', ..."""
|
|
53
|
+
commodity = commodity.strip()
|
|
54
|
+
if not commodity:
|
|
55
|
+
raise ValueError('commodity is empty')
|
|
56
|
+
if not self.is_login:
|
|
57
|
+
self.last_code = '202'
|
|
58
|
+
return {}
|
|
59
|
+
token, params = quote_params(commodity)
|
|
60
|
+
code, msg = self._raw(token, params)
|
|
61
|
+
if code != '000':
|
|
62
|
+
logger.debug('[quote] (%s)', code)
|
|
63
|
+
return {}
|
|
64
|
+
header = fields.HEADERS['Futures'].get('quote') or []
|
|
65
|
+
first = msg.split('\n')[0] if msg else ''
|
|
66
|
+
if not first.strip():
|
|
67
|
+
return {}
|
|
68
|
+
if header:
|
|
69
|
+
rows = csv_to_dicts(first, unique_header(header))
|
|
70
|
+
else:
|
|
71
|
+
import csv
|
|
72
|
+
width = len(next(csv.reader([first])))
|
|
73
|
+
rows = csv_to_dicts(first, [f'col{i + 1}' for i in range(width)])
|
|
74
|
+
return rows[0] if rows else {}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""Futures message composing / parsing (design-2025.md 3.5, 3.6, 3.8, 3.9; design-pure-python.md 2.13).
|
|
3
|
+
|
|
4
|
+
All functions are pure.
|
|
5
|
+
"""
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from datetime import datetime
|
|
10
|
+
from typing import Literal
|
|
11
|
+
|
|
12
|
+
from ..Common import tagvalue
|
|
13
|
+
from ..Common.framing import compose_plain
|
|
14
|
+
from ..Common.signer import SignParts
|
|
15
|
+
from ..Common.timeutil import fmt_date, fmt_timestamp
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True)
|
|
19
|
+
class MarketSpec:
|
|
20
|
+
name: Literal['F', 'FF']
|
|
21
|
+
order_channel: str # 'FOrder' / 'FFOrder'
|
|
22
|
+
receive_channel: str # 'FReceive' / 'FFReceive'
|
|
23
|
+
header_key: str # 'Futures' / 'ForeignFutures'
|
|
24
|
+
source: str # 'futures' / 'foreign_futures'
|
|
25
|
+
event_prefix: str # 'OnF' / 'OnFF'
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
DOMESTIC = MarketSpec('F', 'FOrder', 'FReceive', 'Futures', 'futures', 'OnF')
|
|
29
|
+
FOREIGN = MarketSpec('FF', 'FFOrder', 'FFReceive', 'ForeignFutures', 'foreign_futures', 'OnFF')
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def parse_message(plain: str) -> dict[str, str]:
|
|
33
|
+
"""Futures parser: a repeated tag keeps the first value (ucClient.ParseFIX)."""
|
|
34
|
+
return tagvalue.parse(plain, duplicate='first')
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def login_plain(spec: MarketSpec, id_no: str, pwd_hash: str, version: str, now: datetime, *,
|
|
38
|
+
sign: SignParts, verify_mode: bool = False, account_login: bool = False,
|
|
39
|
+
bhno: str = '', account: str = '') -> str:
|
|
40
|
+
"""Domestic and foreign share one layout (design D5): 20104 / 20048 and the signature fields."""
|
|
41
|
+
pairs = [('35', 'A'), ('52', fmt_timestamp(now)), ('553', id_no), ('554', pwd_hash),
|
|
42
|
+
('555', 'Y' if verify_mode else 'N'), ('556', 'Y' if account_login else 'N'),
|
|
43
|
+
('557', bhno), ('558', account), ('20104', 'N'), ('20048', ''),
|
|
44
|
+
('21003', sign.cert_no), ('21002', sign.signature), ('21001', sign.plain), ('58', version)]
|
|
45
|
+
return compose_plain('|' + tagvalue.compose(pairs))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def is_bulletin_for_futures(plain: str) -> bool:
|
|
49
|
+
"""The DLL only dispatches bulletins whose text contains 20105=F (substring test, ucClient.cs:2478-2503)."""
|
|
50
|
+
return '20105=F' in plain
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def register_code(account_rows: list[str]) -> str:
|
|
54
|
+
"""'<col3><col4>' of every account row joined by ',' (branch + account)."""
|
|
55
|
+
parts = []
|
|
56
|
+
for row in account_rows:
|
|
57
|
+
cols = row.split(',')
|
|
58
|
+
parts.append(cols[3] + cols[4])
|
|
59
|
+
return ','.join(parts)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def register_plain(account_rows: list[str], now: datetime) -> str:
|
|
63
|
+
return compose_plain('|' + tagvalue.compose([('35', 'r'), ('52', fmt_timestamp(now)),
|
|
64
|
+
('20100', register_code(account_rows))]))
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def recover_plain(spec: MarketSpec, bhno: str, cseq: str, seq_start: int | str, seq_end: int | str) -> str:
|
|
68
|
+
pairs = [('35', '2'), ('50', bhno), ('1', cseq)]
|
|
69
|
+
if spec is FOREIGN:
|
|
70
|
+
pairs.append(('20048', ''))
|
|
71
|
+
pairs += [('7', str(seq_start)), ('16', str(seq_end))]
|
|
72
|
+
return compose_plain('|' + tagvalue.compose(pairs))
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def logout_plain(spec: MarketSpec) -> str:
|
|
76
|
+
"""Copied from the DLL: the domestic channels send a 4 digit length field (a DLL slip)."""
|
|
77
|
+
if spec is DOMESTIC:
|
|
78
|
+
return '8=Concords|9=0009|35=5|58='
|
|
79
|
+
return compose_plain('|35=5|58=')
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def quote_params(commodity: str) -> tuple[str, list[str]]:
|
|
83
|
+
"""FQueryQuote request (token, params). UNVERIFIED: only the client side is known."""
|
|
84
|
+
return 'F_QQuoteInfo', [commodity]
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def parse_receive(plain: str) -> tuple[str, str, str]:
|
|
88
|
+
"""Message on the report channel -> (kind, code, msg); kind: heartbeat / order_report / bulletin / other."""
|
|
89
|
+
tags = parse_message(plain)
|
|
90
|
+
t = tags.get('35')
|
|
91
|
+
if plain == '8=Concords|9=00005|35=0' or t in ('0', 'h'):
|
|
92
|
+
return 'heartbeat', '', ''
|
|
93
|
+
if t in ('8', '9'):
|
|
94
|
+
return 'order_report', '000', plain
|
|
95
|
+
if t == 'C':
|
|
96
|
+
return 'bulletin', '000', plain
|
|
97
|
+
return 'other', '', plain
|