dnse-sdk-openapi 0.0.1__tar.gz → 0.0.100__tar.gz
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.
- {dnse_sdk_openapi-0.0.1/dnse_sdk_openapi.egg-info → dnse_sdk_openapi-0.0.100}/PKG-INFO +14 -3
- dnse_sdk_openapi-0.0.1/PKG-INFO → dnse_sdk_openapi-0.0.100/README.md +13 -8
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/broker-api/get_list_care_by.py +1 -1
- dnse_sdk_openapi-0.0.100/dnse/__init__.py +26 -0
- dnse_sdk_openapi-0.0.100/dnse/api/_version.py +3 -0
- {dnse_sdk_openapi-0.0.1/dnse → dnse_sdk_openapi-0.0.100/dnse/api}/client.py +169 -14
- {dnse_sdk_openapi-0.0.1/dnse → dnse_sdk_openapi-0.0.100/dnse/api}/common.py +8 -0
- dnse_sdk_openapi-0.0.100/dnse/websocket/_version.py +3 -0
- {dnse_sdk_openapi-0.0.1/websocket-marketdata/trading_websocket → dnse_sdk_openapi-0.0.100/dnse/websocket}/client.py +83 -9
- {dnse_sdk_openapi-0.0.1/websocket-marketdata/trading_websocket → dnse_sdk_openapi-0.0.100/dnse/websocket}/models.py +131 -36
- dnse_sdk_openapi-0.0.1/README.md → dnse_sdk_openapi-0.0.100/dnse_sdk_openapi.egg-info/PKG-INFO +19 -2
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/dnse_sdk_openapi.egg-info/SOURCES.txt +25 -12
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/dnse_sdk_openapi.egg-info/top_level.txt +1 -0
- dnse_sdk_openapi-0.0.100/marketdata-api/get_expected_price.py +22 -0
- dnse_sdk_openapi-0.0.100/marketdata-api/get_foreign_trading.py +22 -0
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/marketdata-api/get_instruments.py +1 -1
- dnse_sdk_openapi-0.0.100/marketdata-api/get_lastest_session.py +22 -0
- dnse_sdk_openapi-0.0.100/marketdata-api/get_latest_quote.py +22 -0
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/marketdata-api/get_latest_trade.py +1 -1
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/marketdata-api/get_ohlc.py +1 -1
- dnse_sdk_openapi-0.0.100/marketdata-api/get_quotes.py +22 -0
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/marketdata-api/get_security_definition.py +1 -1
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/marketdata-api/get_trades.py +1 -1
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/marketdata-api/get_working_dates.py +1 -1
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/pyproject.toml +1 -1
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/trading-api/cancel_order.py +1 -1
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/trading-api/close_position.py +1 -1
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/trading-api/create_trading_token.py +1 -1
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/trading-api/get_accounts.py +1 -1
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/trading-api/get_balances.py +1 -1
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/trading-api/get_close_price.py +1 -1
- dnse_sdk_openapi-0.0.100/trading-api/get_corporate_action_history.py +28 -0
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/trading-api/get_loan_packages.py +1 -1
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/trading-api/get_order_detail.py +1 -1
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/trading-api/get_order_history.py +2 -2
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/trading-api/get_orders.py +3 -1
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/trading-api/get_position_by_id.py +1 -1
- dnse_sdk_openapi-0.0.100/trading-api/get_position_pnl_configs.py +26 -0
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/trading-api/get_positions.py +1 -1
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/trading-api/get_ppse.py +1 -1
- dnse_sdk_openapi-0.0.100/trading-api/post_order.py +45 -0
- dnse_sdk_openapi-0.0.100/trading-api/post_position_pnl_configs.py +48 -0
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/trading-api/put_order.py +1 -1
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/trading-api/send_email_otp.py +1 -1
- dnse_sdk_openapi-0.0.100/websocket-marketdata/estimated_market_index.py +53 -0
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/websocket-marketdata/expected_price.py +6 -2
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/websocket-marketdata/foreign_investor.py +6 -2
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/websocket-marketdata/market_index.py +6 -2
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/websocket-marketdata/ohlc.py +6 -2
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/websocket-marketdata/ohlc_closed.py +6 -2
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/websocket-marketdata/quote.py +6 -2
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/websocket-marketdata/sec_def.py +6 -2
- dnse_sdk_openapi-0.0.100/websocket-marketdata/session.py +54 -0
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/websocket-marketdata/trade.py +6 -2
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/websocket-marketdata/trade_extra.py +7 -3
- dnse_sdk_openapi-0.0.100/websocket-trading/broker_order.py +53 -0
- dnse_sdk_openapi-0.0.100/websocket-trading/broker_position.py +53 -0
- {dnse_sdk_openapi-0.0.1/websocket-marketdata → dnse_sdk_openapi-0.0.100/websocket-trading}/order.py +6 -2
- dnse_sdk_openapi-0.0.100/websocket-trading/position.py +55 -0
- dnse_sdk_openapi-0.0.1/dnse/__init__.py +0 -4
- dnse_sdk_openapi-0.0.1/trading-api/post_order.py +0 -38
- dnse_sdk_openapi-0.0.1/websocket-marketdata/trading_websocket/__init__.py +0 -33
- dnse_sdk_openapi-0.0.1/websocket-marketdata/trading_websocket/_version.py +0 -3
- {dnse_sdk_openapi-0.0.1/websocket-marketdata/trading_websocket → dnse_sdk_openapi-0.0.100/dnse/websocket}/auth.py +0 -0
- {dnse_sdk_openapi-0.0.1/websocket-marketdata/trading_websocket → dnse_sdk_openapi-0.0.100/dnse/websocket}/connection.py +0 -0
- {dnse_sdk_openapi-0.0.1/websocket-marketdata/trading_websocket → dnse_sdk_openapi-0.0.100/dnse/websocket}/encoding.py +0 -0
- {dnse_sdk_openapi-0.0.1/websocket-marketdata/trading_websocket → dnse_sdk_openapi-0.0.100/dnse/websocket}/exceptions.py +0 -0
- {dnse_sdk_openapi-0.0.1/websocket-marketdata/trading_websocket → dnse_sdk_openapi-0.0.100/dnse/websocket}/py.typed +0 -0
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/dnse_sdk_openapi.egg-info/dependency_links.txt +0 -0
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/setup.cfg +0 -0
- {dnse_sdk_openapi-0.0.1 → dnse_sdk_openapi-0.0.100}/trading-api/get_execution_detail.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: dnse-sdk-openapi
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.100
|
|
4
4
|
Summary: DNSE OpenAPI SDK
|
|
5
5
|
Description-Content-Type: text/markdown
|
|
6
6
|
|
|
@@ -34,10 +34,14 @@ and investment applications.
|
|
|
34
34
|
#### Install from PyPI
|
|
35
35
|
|
|
36
36
|
```console
|
|
37
|
-
pip install openapi-sdk
|
|
37
|
+
pip install openapi-sdk
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
Upgrade
|
|
40
|
+
Upgrade:
|
|
41
|
+
|
|
42
|
+
```console
|
|
43
|
+
pip install --upgrade openapi-sdk
|
|
44
|
+
```
|
|
41
45
|
|
|
42
46
|
### Usage
|
|
43
47
|
|
|
@@ -50,12 +54,16 @@ client = DNSEClient(
|
|
|
50
54
|
api_key="your_api_key",
|
|
51
55
|
api_secret="your_api_secret",
|
|
52
56
|
base_url="https://openapi.dnse.com.vn",
|
|
57
|
+
api_version="2026-05-07",
|
|
53
58
|
)
|
|
54
59
|
|
|
55
60
|
status, body = client.get_accounts(dry_run=False)
|
|
56
61
|
print(status, body)
|
|
57
62
|
```
|
|
58
63
|
|
|
64
|
+
The SDK sends the API version in the `version` header. If `api_version` is omitted, it defaults to `2026-01-01`; it can also be set
|
|
65
|
+
with the `DNSE_API_VERSION` environment variable.
|
|
66
|
+
|
|
59
67
|
### Dry Run
|
|
60
68
|
|
|
61
69
|
Set `dry_run=True` to preview the request without sending it to DNSE servers. No network call will be executed.
|
|
@@ -83,9 +91,12 @@ python sdk/python/api/get_accounts.py
|
|
|
83
91
|
| `get_orders.py` | Demonstrates how to retrieve intraday order book. |
|
|
84
92
|
| `get_order_detail.py` | Demonstrates how to retrieve detailed information of a specific order (by ID). |
|
|
85
93
|
| `get_order_history.py` | Demonstrates how to retrieve historical orders. |
|
|
94
|
+
| `get_corporate_action_history.py` | Demonstrates how to retrieve corporate action history. |
|
|
86
95
|
| `get_execution_detail.py` | Demonstrates how to retrieve detailed execution information of an order. |
|
|
87
96
|
| `get_positions.py` | Demonstrates how to retrieve current holding positions. |
|
|
88
97
|
| `get_positions_by_id.py` | Demonstrates how to retrieve detailed information of a specific position (by ID). |
|
|
98
|
+
| `get_position_pnl_configs.py` | Demonstrates how to retrieve PnL configs of a derivative position (by ID). |
|
|
99
|
+
| `post_position_pnl_configs.py` | Demonstrates how to create or update PnL configs of a derivative position (by ID). |
|
|
89
100
|
| `close_position.py` | Demonstrates how to close an existing position (by ID). |
|
|
90
101
|
| `send_email_otp.py` | Demonstrates how to request an OTP sent to your registered email. The OTP is required for generating a trading token. |
|
|
91
102
|
| `create_trading_token.py` | Demonstrates how to generate a Trading Token required for order placement. |
|
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: dnse-sdk-openapi
|
|
3
|
-
Version: 0.0.1
|
|
4
|
-
Summary: DNSE OpenAPI SDK
|
|
5
|
-
Description-Content-Type: text/markdown
|
|
6
|
-
|
|
7
1
|
# DNSE OpenAPI Python SDK
|
|
8
2
|
|
|
9
3
|
Official Python SDK for integrating with DNSE OpenAPI.
|
|
@@ -34,10 +28,14 @@ and investment applications.
|
|
|
34
28
|
#### Install from PyPI
|
|
35
29
|
|
|
36
30
|
```console
|
|
37
|
-
pip install openapi-sdk
|
|
31
|
+
pip install openapi-sdk
|
|
38
32
|
```
|
|
39
33
|
|
|
40
|
-
Upgrade
|
|
34
|
+
Upgrade:
|
|
35
|
+
|
|
36
|
+
```console
|
|
37
|
+
pip install --upgrade openapi-sdk
|
|
38
|
+
```
|
|
41
39
|
|
|
42
40
|
### Usage
|
|
43
41
|
|
|
@@ -50,12 +48,16 @@ client = DNSEClient(
|
|
|
50
48
|
api_key="your_api_key",
|
|
51
49
|
api_secret="your_api_secret",
|
|
52
50
|
base_url="https://openapi.dnse.com.vn",
|
|
51
|
+
api_version="2026-05-07",
|
|
53
52
|
)
|
|
54
53
|
|
|
55
54
|
status, body = client.get_accounts(dry_run=False)
|
|
56
55
|
print(status, body)
|
|
57
56
|
```
|
|
58
57
|
|
|
58
|
+
The SDK sends the API version in the `version` header. If `api_version` is omitted, it defaults to `2026-01-01`; it can also be set
|
|
59
|
+
with the `DNSE_API_VERSION` environment variable.
|
|
60
|
+
|
|
59
61
|
### Dry Run
|
|
60
62
|
|
|
61
63
|
Set `dry_run=True` to preview the request without sending it to DNSE servers. No network call will be executed.
|
|
@@ -83,9 +85,12 @@ python sdk/python/api/get_accounts.py
|
|
|
83
85
|
| `get_orders.py` | Demonstrates how to retrieve intraday order book. |
|
|
84
86
|
| `get_order_detail.py` | Demonstrates how to retrieve detailed information of a specific order (by ID). |
|
|
85
87
|
| `get_order_history.py` | Demonstrates how to retrieve historical orders. |
|
|
88
|
+
| `get_corporate_action_history.py` | Demonstrates how to retrieve corporate action history. |
|
|
86
89
|
| `get_execution_detail.py` | Demonstrates how to retrieve detailed execution information of an order. |
|
|
87
90
|
| `get_positions.py` | Demonstrates how to retrieve current holding positions. |
|
|
88
91
|
| `get_positions_by_id.py` | Demonstrates how to retrieve detailed information of a specific position (by ID). |
|
|
92
|
+
| `get_position_pnl_configs.py` | Demonstrates how to retrieve PnL configs of a derivative position (by ID). |
|
|
93
|
+
| `post_position_pnl_configs.py` | Demonstrates how to create or update PnL configs of a derivative position (by ID). |
|
|
89
94
|
| `close_position.py` | Demonstrates how to close an existing position (by ID). |
|
|
90
95
|
| `send_email_otp.py` | Demonstrates how to request an OTP sent to your registered email. The OTP is required for generating a trading token. |
|
|
91
96
|
| `create_trading_token.py` | Demonstrates how to generate a Trading Token required for order placement. |
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
from .api._version import __version__ as APIVersion
|
|
3
|
+
from .api.client import DNSEClient
|
|
4
|
+
from .websocket._version import __version__ as WSVersion
|
|
5
|
+
from .websocket.client import TradingClient
|
|
6
|
+
from .websocket.exceptions import (
|
|
7
|
+
TradingWebSocketError,
|
|
8
|
+
ConnectionError,
|
|
9
|
+
ConnectionClosed,
|
|
10
|
+
AuthenticationError,
|
|
11
|
+
SubscriptionError,
|
|
12
|
+
EncodingError,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"DNSEClient",
|
|
17
|
+
"APIVersion",
|
|
18
|
+
"WSVersion",
|
|
19
|
+
"TradingClient",
|
|
20
|
+
"TradingWebSocketError",
|
|
21
|
+
"ConnectionError",
|
|
22
|
+
"ConnectionClosed",
|
|
23
|
+
"AuthenticationError",
|
|
24
|
+
"SubscriptionError",
|
|
25
|
+
"EncodingError"
|
|
26
|
+
]
|
|
@@ -5,7 +5,7 @@ from urllib import parse
|
|
|
5
5
|
import urllib3
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
from .common import build_signature, get_date_header_name
|
|
8
|
+
from .common import build_signature, get_api_version, get_date_header_name
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
class DNSEClient:
|
|
@@ -16,12 +16,14 @@ class DNSEClient:
|
|
|
16
16
|
base_url="https://openapi.dnse.com.vn",
|
|
17
17
|
algorithm="hmac-sha256",
|
|
18
18
|
hmac_nonce_enabled=True,
|
|
19
|
+
api_version=None,
|
|
19
20
|
):
|
|
20
21
|
self._api_key = api_key
|
|
21
22
|
self._api_secret = api_secret
|
|
22
23
|
self._base_url = base_url.rstrip("/")
|
|
23
24
|
self._algorithm = algorithm
|
|
24
25
|
self._hmac_nonce_enabled = hmac_nonce_enabled
|
|
26
|
+
self._api_version = api_version or get_api_version()
|
|
25
27
|
|
|
26
28
|
# Tạo PoolManager 1 lần duy nhất, tái sử dụng suốt vòng đời object
|
|
27
29
|
self._http = urllib3.PoolManager(
|
|
@@ -29,8 +31,8 @@ class DNSEClient:
|
|
|
29
31
|
maxsize=10, # Số connections tối đa mỗi pool
|
|
30
32
|
block=False, # Không block khi pool đầy
|
|
31
33
|
timeout=urllib3.Timeout(connect=30.0, read=60.0),
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
cert_reqs = 'CERT_NONE', # Không yêu cầu certificate
|
|
35
|
+
assert_hostname = False # Không kiểm tra hostname
|
|
34
36
|
)
|
|
35
37
|
|
|
36
38
|
def get_accounts(self, dry_run=False):
|
|
@@ -58,18 +60,52 @@ class DNSEClient:
|
|
|
58
60
|
dry_run=dry_run,
|
|
59
61
|
)
|
|
60
62
|
|
|
61
|
-
def get_position_by_id(self, market_type, position_id, dry_run=False):
|
|
63
|
+
def get_position_by_id(self, market_type, position_id, version=None, dry_run=False):
|
|
62
64
|
return self._request(
|
|
63
65
|
"GET",
|
|
64
|
-
f"/
|
|
66
|
+
f"/positions/{position_id}",
|
|
65
67
|
query={"marketType": market_type},
|
|
68
|
+
version=version,
|
|
66
69
|
dry_run=dry_run,
|
|
67
70
|
)
|
|
68
71
|
|
|
69
|
-
def
|
|
72
|
+
def get_position_pnl_configs(self, market_type, position_id, version=None, dry_run=False):
|
|
73
|
+
return self._request(
|
|
74
|
+
"GET",
|
|
75
|
+
f"/positions/{position_id}/pnl-configs",
|
|
76
|
+
query={"marketType": market_type},
|
|
77
|
+
version=version,
|
|
78
|
+
dry_run=dry_run,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
def post_position_pnl_configs(self, market_type, position_id, payload, trading_token, version=None, dry_run=False):
|
|
82
|
+
headers = {"trading-token": trading_token}
|
|
83
|
+
return self._request(
|
|
84
|
+
"POST",
|
|
85
|
+
f"/positions/{position_id}/pnl-configs",
|
|
86
|
+
query={"marketType": market_type},
|
|
87
|
+
body=payload,
|
|
88
|
+
headers=headers,
|
|
89
|
+
version=version,
|
|
90
|
+
dry_run=dry_run,
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
def get_orders(
|
|
94
|
+
self,
|
|
95
|
+
account_no,
|
|
96
|
+
market_type,
|
|
97
|
+
order_category=None,
|
|
98
|
+
page_index=None,
|
|
99
|
+
page_size=None,
|
|
100
|
+
dry_run=False,
|
|
101
|
+
):
|
|
70
102
|
query = {"marketType": market_type}
|
|
71
103
|
if order_category:
|
|
72
104
|
query["orderCategory"] = order_category
|
|
105
|
+
if page_index is not None:
|
|
106
|
+
query["pageIndex"] = page_index
|
|
107
|
+
if page_size is not None:
|
|
108
|
+
query["pageSize"] = page_size
|
|
73
109
|
return self._request(
|
|
74
110
|
"GET",
|
|
75
111
|
f"/accounts/{account_no}/orders",
|
|
@@ -125,6 +161,34 @@ class DNSEClient:
|
|
|
125
161
|
dry_run=dry_run,
|
|
126
162
|
)
|
|
127
163
|
|
|
164
|
+
def get_corporate_action_history(
|
|
165
|
+
self,
|
|
166
|
+
account_no,
|
|
167
|
+
symbol=None,
|
|
168
|
+
ca_type=None,
|
|
169
|
+
ca_status=None,
|
|
170
|
+
page_index=None,
|
|
171
|
+
page_size=None,
|
|
172
|
+
dry_run=False,
|
|
173
|
+
):
|
|
174
|
+
query = {}
|
|
175
|
+
if symbol is not None:
|
|
176
|
+
query["symbol"] = symbol
|
|
177
|
+
if ca_type is not None:
|
|
178
|
+
query["caType"] = ca_type
|
|
179
|
+
if ca_status is not None:
|
|
180
|
+
query["caStatus"] = ca_status
|
|
181
|
+
if page_index is not None:
|
|
182
|
+
query["pageIndex"] = page_index
|
|
183
|
+
if page_size is not None:
|
|
184
|
+
query["pageSize"] = page_size
|
|
185
|
+
return self._request(
|
|
186
|
+
"GET",
|
|
187
|
+
f"/accounts/{account_no}/corporate-action-history",
|
|
188
|
+
query=query if query else None,
|
|
189
|
+
dry_run=dry_run,
|
|
190
|
+
)
|
|
191
|
+
|
|
128
192
|
def get_ppse(self, account_no, market_type, symbol, price, loan_package_id, dry_run=False):
|
|
129
193
|
return self._request(
|
|
130
194
|
"GET",
|
|
@@ -180,7 +244,70 @@ class DNSEClient:
|
|
|
180
244
|
dry_run=dry_run,
|
|
181
245
|
)
|
|
182
246
|
|
|
183
|
-
def
|
|
247
|
+
def get_expected_price(self, symbol, board_id=None, from_date=None, to_date=None, limit=None, order = None, next_page_token=None, dry_run=False):
|
|
248
|
+
query = {}
|
|
249
|
+
if board_id is not None:
|
|
250
|
+
query["boardId"] = board_id
|
|
251
|
+
if from_date is not None:
|
|
252
|
+
query["from"] = from_date
|
|
253
|
+
if to_date is not None:
|
|
254
|
+
query["to"] = to_date
|
|
255
|
+
if limit is not None:
|
|
256
|
+
query["limit"] = limit
|
|
257
|
+
if order is not None:
|
|
258
|
+
query["order"] = order
|
|
259
|
+
if next_page_token is not None:
|
|
260
|
+
query["nextPageToken"] = next_page_token
|
|
261
|
+
return self._request(
|
|
262
|
+
"GET",
|
|
263
|
+
f"/price/{symbol}/expected-price",
|
|
264
|
+
query=query if query else None,
|
|
265
|
+
dry_run=dry_run,
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
def get_quotes(self, symbol, board_id=None, from_date=None, to_date=None, limit=None, order = None, next_page_token=None, dry_run=False):
|
|
269
|
+
query = {}
|
|
270
|
+
if board_id is not None:
|
|
271
|
+
query["boardId"] = board_id
|
|
272
|
+
if from_date is not None:
|
|
273
|
+
query["from"] = from_date
|
|
274
|
+
if to_date is not None:
|
|
275
|
+
query["to"] = to_date
|
|
276
|
+
if limit is not None:
|
|
277
|
+
query["limit"] = limit
|
|
278
|
+
if order is not None:
|
|
279
|
+
query["order"] = order
|
|
280
|
+
if next_page_token is not None:
|
|
281
|
+
query["nextPageToken"] = next_page_token
|
|
282
|
+
return self._request(
|
|
283
|
+
"GET",
|
|
284
|
+
f"/price/{symbol}/quotes",
|
|
285
|
+
query=query if query else None,
|
|
286
|
+
dry_run=dry_run,
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
def get_foreign_trading(self, symbol, board_id=None, from_date=None, to_date=None, limit=None, order = None, next_page_token=None, dry_run=False):
|
|
290
|
+
query = {}
|
|
291
|
+
if board_id is not None:
|
|
292
|
+
query["boardId"] = board_id
|
|
293
|
+
if from_date is not None:
|
|
294
|
+
query["from"] = from_date
|
|
295
|
+
if to_date is not None:
|
|
296
|
+
query["to"] = to_date
|
|
297
|
+
if limit is not None:
|
|
298
|
+
query["limit"] = limit
|
|
299
|
+
if order is not None:
|
|
300
|
+
query["order"] = order
|
|
301
|
+
if next_page_token is not None:
|
|
302
|
+
query["nextPageToken"] = next_page_token
|
|
303
|
+
return self._request(
|
|
304
|
+
"GET",
|
|
305
|
+
f"/price/{symbol}/foreign-trading",
|
|
306
|
+
query=query if query else None,
|
|
307
|
+
dry_run=dry_run,
|
|
308
|
+
)
|
|
309
|
+
|
|
310
|
+
def get_instruments(self, symbol=None, market_id=None, security_group_id=None, index_name=None, limit=None, page=None, version=None, dry_run=False):
|
|
184
311
|
query = {}
|
|
185
312
|
if symbol is not None:
|
|
186
313
|
query["symbol"] = symbol
|
|
@@ -196,8 +323,9 @@ class DNSEClient:
|
|
|
196
323
|
query["page"] = page
|
|
197
324
|
return self._request(
|
|
198
325
|
"GET",
|
|
199
|
-
|
|
326
|
+
"/market/instruments",
|
|
200
327
|
query=query if query else None,
|
|
328
|
+
version=version,
|
|
201
329
|
dry_run=dry_run,
|
|
202
330
|
)
|
|
203
331
|
|
|
@@ -212,6 +340,17 @@ class DNSEClient:
|
|
|
212
340
|
dry_run=dry_run,
|
|
213
341
|
)
|
|
214
342
|
|
|
343
|
+
def get_latest_quote(self, symbol, board_id=None, dry_run=False):
|
|
344
|
+
query = {}
|
|
345
|
+
if board_id is not None:
|
|
346
|
+
query["boardId"] = board_id
|
|
347
|
+
return self._request(
|
|
348
|
+
"GET",
|
|
349
|
+
f"/price/{symbol}/quotes/latest",
|
|
350
|
+
query=query if query else None,
|
|
351
|
+
dry_run=dry_run,
|
|
352
|
+
)
|
|
353
|
+
|
|
215
354
|
def get_close_price(self, symbol, board_id=None, dry_run=False):
|
|
216
355
|
query = {}
|
|
217
356
|
if board_id is not None:
|
|
@@ -237,17 +376,31 @@ class DNSEClient:
|
|
|
237
376
|
dry_run=dry_run,
|
|
238
377
|
)
|
|
239
378
|
|
|
240
|
-
def
|
|
379
|
+
def get_lastest_session(self, tsc_prod_grp_id=None, board_id=None, dry_run=False):
|
|
380
|
+
query = {}
|
|
381
|
+
if tsc_prod_grp_id is not None:
|
|
382
|
+
query["tscProdGrpId"] = tsc_prod_grp_id
|
|
383
|
+
if board_id is not None:
|
|
384
|
+
query["boardId"] = board_id
|
|
385
|
+
return self._request(
|
|
386
|
+
"GET",
|
|
387
|
+
f"/market/trading-session",
|
|
388
|
+
query=query if query else None,
|
|
389
|
+
dry_run=dry_run,
|
|
390
|
+
)
|
|
391
|
+
|
|
392
|
+
def post_order(self, account_no, market_type, payload, trading_token, order_category="NORMAL", version=None, dry_run=False):
|
|
241
393
|
headers = {"trading-token": trading_token}
|
|
242
394
|
query = {"marketType": market_type}
|
|
243
395
|
if order_category:
|
|
244
396
|
query["orderCategory"] = order_category
|
|
245
397
|
return self._request(
|
|
246
398
|
"POST",
|
|
247
|
-
"/accounts/orders",
|
|
399
|
+
f"/accounts/{account_no}/orders",
|
|
248
400
|
query=query,
|
|
249
401
|
body=payload,
|
|
250
402
|
headers=headers,
|
|
403
|
+
version=version,
|
|
251
404
|
dry_run=dry_run,
|
|
252
405
|
)
|
|
253
406
|
|
|
@@ -310,28 +463,30 @@ class DNSEClient:
|
|
|
310
463
|
dry_run=dry_run,
|
|
311
464
|
)
|
|
312
465
|
|
|
313
|
-
def close_position(self, position_id, market_type, trading_token, dry_run=False):
|
|
466
|
+
def close_position(self, position_id, market_type, trading_token, version=None, dry_run=False):
|
|
314
467
|
headers = {"trading-token": trading_token}
|
|
315
468
|
query = {"marketType": market_type}
|
|
316
469
|
return self._request(
|
|
317
470
|
"POST",
|
|
318
|
-
f"/
|
|
471
|
+
f"/positions/{position_id}/close",
|
|
319
472
|
query=query,
|
|
320
473
|
headers=headers,
|
|
474
|
+
version=version,
|
|
321
475
|
dry_run=dry_run,
|
|
322
476
|
)
|
|
323
477
|
|
|
324
|
-
def _request(self, method, path, query=None, body=None, headers=None, dry_run=False):
|
|
478
|
+
def _request(self, method, path, query=None, body=None, headers=None, version=None, dry_run=False):
|
|
325
479
|
debug = os.getenv("DEBUG", "").lower() == "true"
|
|
326
480
|
url = self._build_url(path, query)
|
|
327
481
|
date_value, signature_header_value = self._signature_headers(method, path)
|
|
328
482
|
date_header_name = get_date_header_name()
|
|
329
483
|
|
|
330
|
-
# Build headers dict
|
|
484
|
+
# Build headers dict (per-request `version` overrides the client default)
|
|
331
485
|
req_headers = {
|
|
332
486
|
date_header_name: date_value,
|
|
333
487
|
"X-Signature": signature_header_value,
|
|
334
488
|
"x-api-key": self._api_key,
|
|
489
|
+
"version": version or self._api_version,
|
|
335
490
|
}
|
|
336
491
|
|
|
337
492
|
if body is not None:
|
|
@@ -8,11 +8,17 @@ from datetime import datetime, timezone
|
|
|
8
8
|
from urllib import parse, request
|
|
9
9
|
from uuid import uuid4
|
|
10
10
|
|
|
11
|
+
DEFAULT_API_VERSION = "2026-07-23"
|
|
12
|
+
|
|
11
13
|
|
|
12
14
|
def get_date_header_name():
|
|
13
15
|
return os.getenv("DATE_HEADER", "Date")
|
|
14
16
|
|
|
15
17
|
|
|
18
|
+
def get_api_version():
|
|
19
|
+
return os.getenv("DNSE_API_VERSION") or DEFAULT_API_VERSION
|
|
20
|
+
|
|
21
|
+
|
|
16
22
|
def build_signature(secret, method, path, date_value, algorithm, nonce=None, header_name=None):
|
|
17
23
|
header_name = header_name or get_date_header_name()
|
|
18
24
|
header_key = header_name.lower()
|
|
@@ -50,6 +56,8 @@ def send_signed_request(
|
|
|
50
56
|
hmac_nonce_enabled=True,
|
|
51
57
|
):
|
|
52
58
|
debug = os.getenv("DEBUG", "").lower() == "true"
|
|
59
|
+
headers = dict(headers or {})
|
|
60
|
+
headers.setdefault("version", get_api_version())
|
|
53
61
|
parsed = parse.urlparse(url)
|
|
54
62
|
path = parsed.path
|
|
55
63
|
date_value = datetime.now(timezone.utc).strftime("%a, %d %b %Y %H:%M:%S %z")
|
|
@@ -10,21 +10,22 @@ This module provides the main TradingClient class that handles:
|
|
|
10
10
|
- Graceful shutdown
|
|
11
11
|
"""
|
|
12
12
|
|
|
13
|
-
from typing import Optional, Callable, List, Dict, Any
|
|
14
13
|
import asyncio
|
|
15
14
|
import logging
|
|
16
15
|
import time
|
|
17
|
-
from
|
|
16
|
+
from typing import Optional, Callable, List, Dict, Any
|
|
17
|
+
|
|
18
18
|
from .auth import AuthManager
|
|
19
|
+
from .connection import WebSocketConnection
|
|
19
20
|
from .encoding import MessageEncoder, MessageDecoder
|
|
20
|
-
from .models import Trade, Quote, Ohlc, Order, Position, AccountUpdate, ExpectedPrice, SecurityDefinition, TradeExtra, \
|
|
21
|
-
MarketIndex, ForeignInvestor
|
|
22
21
|
from .exceptions import (
|
|
23
22
|
AuthenticationError,
|
|
24
23
|
ConnectionError,
|
|
25
24
|
SubscriptionError,
|
|
26
25
|
ConnectionClosed,
|
|
27
26
|
)
|
|
27
|
+
from .models import Trade, Quote, Ohlc, Order, AccountUpdate, ExpectedPrice, SecurityDefinition, TradeExtra, \
|
|
28
|
+
MarketIndex, ForeignInvestor, Position, EstimatedMarketIndex, Session
|
|
28
29
|
|
|
29
30
|
logger = logging.getLogger(__name__)
|
|
30
31
|
logger.setLevel(logging.INFO)
|
|
@@ -47,10 +48,13 @@ _MSG_TYPE_MAP = {
|
|
|
47
48
|
"bc": ("ohlc_closed", Ohlc, None),
|
|
48
49
|
"do": ("order_event", Order, "order"),
|
|
49
50
|
"eo": ("order_event", Order, "order"),
|
|
50
|
-
"
|
|
51
|
+
"dp": ("position_event", Position, "position"),
|
|
52
|
+
"ep": ("position_event", Position, "position"),
|
|
51
53
|
"mi": ("market_index", MarketIndex, None),
|
|
54
|
+
"emi": ("estimated_market_index", EstimatedMarketIndex, "marketIndex"),
|
|
52
55
|
"a": ("account", AccountUpdate, None),
|
|
53
56
|
"f": ("foreign", ForeignInvestor, None),
|
|
57
|
+
"s": ("session", Session, None),
|
|
54
58
|
}
|
|
55
59
|
|
|
56
60
|
|
|
@@ -235,7 +239,8 @@ class TradingClient:
|
|
|
235
239
|
await self._subscribe_channel(channel, symbols)
|
|
236
240
|
|
|
237
241
|
if on_trade_extra:
|
|
238
|
-
_handler = self._make_filtered_handler(board_id, "boardId",
|
|
242
|
+
_handler = self._make_filtered_handler(board_id, "boardId",
|
|
243
|
+
on_trade_extra) if board_id is not None else on_trade_extra
|
|
239
244
|
self.on("trade_extra", _handler)
|
|
240
245
|
|
|
241
246
|
async def subscribe_expected_price(
|
|
@@ -251,7 +256,8 @@ class TradingClient:
|
|
|
251
256
|
await self._subscribe_channel(channel, symbols)
|
|
252
257
|
|
|
253
258
|
if on_expected_price:
|
|
254
|
-
_handler = self._make_filtered_handler(board_id, "boardId",
|
|
259
|
+
_handler = self._make_filtered_handler(board_id, "boardId",
|
|
260
|
+
on_expected_price) if board_id is not None else on_expected_price
|
|
255
261
|
self.on("expected_price", _handler)
|
|
256
262
|
|
|
257
263
|
async def subscribe_order_event(
|
|
@@ -265,6 +271,43 @@ class TradingClient:
|
|
|
265
271
|
if on_order_event:
|
|
266
272
|
self.on("order_event", on_order_event)
|
|
267
273
|
|
|
274
|
+
async def subscribe_broker_order_event(
|
|
275
|
+
self,
|
|
276
|
+
investor_id: str,
|
|
277
|
+
market_type="STOCK",
|
|
278
|
+
on_order_event: Optional[Callable[[Order], None]] = None,
|
|
279
|
+
encoding="json"
|
|
280
|
+
) -> None:
|
|
281
|
+
channel = f"order.broker.{market_type}.{investor_id}.{encoding}"
|
|
282
|
+
await self._subscribe_channel(channel, [])
|
|
283
|
+
|
|
284
|
+
if on_order_event:
|
|
285
|
+
self.on("order_event", on_order_event)
|
|
286
|
+
|
|
287
|
+
async def subscribe_position_event(
|
|
288
|
+
self, market_type="STOCK",
|
|
289
|
+
on_position_event: Optional[Callable[[Position], None]] = None,
|
|
290
|
+
encoding="json"
|
|
291
|
+
) -> None:
|
|
292
|
+
channel = f"position.{market_type}.{encoding}"
|
|
293
|
+
await self._subscribe_channel(channel, [])
|
|
294
|
+
|
|
295
|
+
if on_position_event:
|
|
296
|
+
self.on("position_event", on_position_event)
|
|
297
|
+
|
|
298
|
+
async def subscribe_broker_position_event(
|
|
299
|
+
self,
|
|
300
|
+
investor_id: str,
|
|
301
|
+
market_type="STOCK",
|
|
302
|
+
on_position_event: Optional[Callable[[Position], None]] = None,
|
|
303
|
+
encoding="json"
|
|
304
|
+
) -> None:
|
|
305
|
+
channel = f"position.broker.{market_type}.{investor_id}.{encoding}"
|
|
306
|
+
await self._subscribe_channel(channel, [])
|
|
307
|
+
|
|
308
|
+
if on_position_event:
|
|
309
|
+
self.on("position_event", on_position_event)
|
|
310
|
+
|
|
268
311
|
async def subscribe_sec_def(
|
|
269
312
|
self, symbols: List[str], on_sec_def: Optional[Callable[[SecurityDefinition], None]] = None,
|
|
270
313
|
encoding="json", board_id=None
|
|
@@ -278,7 +321,8 @@ class TradingClient:
|
|
|
278
321
|
await self._subscribe_channel(channel, symbols)
|
|
279
322
|
|
|
280
323
|
if on_sec_def:
|
|
281
|
-
_handler = self._make_filtered_handler(board_id, "boardId",
|
|
324
|
+
_handler = self._make_filtered_handler(board_id, "boardId",
|
|
325
|
+
on_sec_def) if board_id is not None else on_sec_def
|
|
282
326
|
self.on("security_definition", _handler)
|
|
283
327
|
|
|
284
328
|
async def subscribe_market_index(
|
|
@@ -292,6 +336,18 @@ class TradingClient:
|
|
|
292
336
|
if on_market_index:
|
|
293
337
|
self.on("market_index", on_market_index)
|
|
294
338
|
|
|
339
|
+
async def subscribe_estimated_market_index(
|
|
340
|
+
self, estimated_market_index: str,
|
|
341
|
+
on_estimated_market_index: Optional[Callable[[EstimatedMarketIndex], None]] = None, encoding="json"
|
|
342
|
+
) -> None:
|
|
343
|
+
channel = f"estimated_market_index.{estimated_market_index}.json"
|
|
344
|
+
if encoding == "msgpack":
|
|
345
|
+
channel = f"estimated_market_index.{estimated_market_index}.msgpack"
|
|
346
|
+
await self._subscribe_channel(channel, [])
|
|
347
|
+
|
|
348
|
+
if on_estimated_market_index:
|
|
349
|
+
self.on("estimated_market_index", on_estimated_market_index)
|
|
350
|
+
|
|
295
351
|
async def subscribe_quotes(
|
|
296
352
|
self, symbols: List[str], on_quote: Optional[Callable[[Quote], None]] = None, encoding="json", board_id=None
|
|
297
353
|
) -> None:
|
|
@@ -342,6 +398,21 @@ class TradingClient:
|
|
|
342
398
|
if on_ohlc:
|
|
343
399
|
self.on("ohlc", on_ohlc)
|
|
344
400
|
|
|
401
|
+
async def subscribe_session(
|
|
402
|
+
self,
|
|
403
|
+
product_group_id: str,
|
|
404
|
+
board_id: str = "*",
|
|
405
|
+
on_session: Optional[Callable[[Session], None]] = None, encoding="json"
|
|
406
|
+
) -> None:
|
|
407
|
+
channel = f"session.{product_group_id}.{board_id}.json"
|
|
408
|
+
|
|
409
|
+
if encoding == "msgpack":
|
|
410
|
+
channel = f"session.{product_group_id}.{board_id}.msgpack"
|
|
411
|
+
await self._subscribe_channel(channel, [])
|
|
412
|
+
|
|
413
|
+
if on_session:
|
|
414
|
+
self.on("session", on_session)
|
|
415
|
+
|
|
345
416
|
async def subscribe_ohlc_closed(
|
|
346
417
|
self,
|
|
347
418
|
symbols: List[str],
|
|
@@ -433,9 +504,11 @@ class TradingClient:
|
|
|
433
504
|
|
|
434
505
|
def _make_filtered_handler(self, board_id: Optional[str], attr: str, handler: Callable) -> Callable:
|
|
435
506
|
"""Wrap handler to only fire when obj.{attr} == board_id."""
|
|
507
|
+
|
|
436
508
|
def _filtered(obj, _b=board_id, _a=attr, _cb=handler):
|
|
437
509
|
if getattr(obj, _a, None) == _b:
|
|
438
510
|
_cb(obj)
|
|
511
|
+
|
|
439
512
|
return _filtered
|
|
440
513
|
|
|
441
514
|
def on(self, event: str, handler: Callable) -> None:
|
|
@@ -501,7 +574,8 @@ class TradingClient:
|
|
|
501
574
|
break
|
|
502
575
|
|
|
503
576
|
delay = min(2 ** (reconnect_attempt - 1), max_reconnect_delay)
|
|
504
|
-
logger.info(
|
|
577
|
+
logger.info(
|
|
578
|
+
f"Connection error detected. Reconnecting in {delay}s (attempt {reconnect_attempt}/{self.max_retries})...")
|
|
505
579
|
|
|
506
580
|
self._emit("reconnecting", {
|
|
507
581
|
"attempt": reconnect_attempt,
|