dnse-sdk-openapi 2.4.0__tar.gz → 2.4.1__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.
Files changed (100) hide show
  1. dnse_sdk_openapi-2.4.1/PKG-INFO +628 -0
  2. dnse_sdk_openapi-2.4.1/README.md +622 -0
  3. dnse_sdk_openapi-2.4.1/dnse_sdk_openapi.egg-info/PKG-INFO +628 -0
  4. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/pyproject.toml +1 -1
  5. dnse_sdk_openapi-2.4.0/PKG-INFO +0 -144
  6. dnse_sdk_openapi-2.4.0/README.md +0 -138
  7. dnse_sdk_openapi-2.4.0/dnse_sdk_openapi.egg-info/PKG-INFO +0 -144
  8. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/broker-api/get_list_care_by.py +0 -0
  9. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/dnse/__init__.py +0 -0
  10. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/dnse/api/_version.py +0 -0
  11. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/dnse/api/client.py +0 -0
  12. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/dnse/api/common.py +0 -0
  13. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/dnse/websocket/_version.py +0 -0
  14. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/dnse/websocket/auth.py +0 -0
  15. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/dnse/websocket/client.py +0 -0
  16. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/dnse/websocket/connection.py +0 -0
  17. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/dnse/websocket/encoding.py +0 -0
  18. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/dnse/websocket/exceptions.py +0 -0
  19. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/dnse/websocket/models.py +0 -0
  20. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/dnse/websocket/py.typed +0 -0
  21. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/dnse_sdk_openapi.egg-info/SOURCES.txt +0 -0
  22. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/dnse_sdk_openapi.egg-info/dependency_links.txt +0 -0
  23. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/dnse_sdk_openapi.egg-info/top_level.txt +0 -0
  24. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/env_util.py +0 -0
  25. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/indicators.py +0 -0
  26. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/log_util.py +0 -0
  27. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/market_utils.py +0 -0
  28. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/otp_email.py +0 -0
  29. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/position_manager.py +0 -0
  30. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/position_store.py +0 -0
  31. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/reference/orders-email-otp.py +0 -0
  32. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/reference/orders-smart-otp.py +0 -0
  33. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/reference/smart-otp-handoff-flow.py +0 -0
  34. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/strategy_base.py +0 -0
  35. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/strategy_ichimoku.py +0 -0
  36. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/strategy_price_action.py +0 -0
  37. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/strategy_scalping.py +0 -0
  38. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/token_store.py +0 -0
  39. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/use-cases/auto-trader.py +0 -0
  40. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/use-cases/market-data.py +0 -0
  41. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/use-cases/order-history.py +0 -0
  42. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/use-cases/place-a-trade.py +0 -0
  43. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/examples/use-cases/portfolio-check.py +0 -0
  44. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/marketdata-api/get_expected_price.py +0 -0
  45. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/marketdata-api/get_foreign_trading.py +0 -0
  46. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/marketdata-api/get_instruments.py +0 -0
  47. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/marketdata-api/get_lastest_session.py +0 -0
  48. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/marketdata-api/get_latest_quote.py +0 -0
  49. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/marketdata-api/get_latest_trade.py +0 -0
  50. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/marketdata-api/get_market_index.py +0 -0
  51. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/marketdata-api/get_ohlc.py +0 -0
  52. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/marketdata-api/get_quotes.py +0 -0
  53. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/marketdata-api/get_security_definition.py +0 -0
  54. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/marketdata-api/get_trades.py +0 -0
  55. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/marketdata-api/get_trades_volume_profile.py +0 -0
  56. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/marketdata-api/get_working_dates.py +0 -0
  57. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/setup.cfg +0 -0
  58. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/tests/test_client.py +0 -0
  59. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/tests/test_smart_otp_handoff.py +0 -0
  60. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/cancel_order.py +0 -0
  61. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/close_position.py +0 -0
  62. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/create_smart_otp_handoff.py +0 -0
  63. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/create_trading_token.py +0 -0
  64. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/get_account_pnl_configs.py +0 -0
  65. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/get_accounts.py +0 -0
  66. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/get_balances.py +0 -0
  67. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/get_close_price.py +0 -0
  68. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/get_corporate_action_history.py +0 -0
  69. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/get_execution_detail.py +0 -0
  70. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/get_loan_packages.py +0 -0
  71. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/get_loans.py +0 -0
  72. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/get_order_detail.py +0 -0
  73. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/get_order_history.py +0 -0
  74. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/get_orders.py +0 -0
  75. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/get_position_by_id.py +0 -0
  76. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/get_position_pnl_configs.py +0 -0
  77. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/get_positions.py +0 -0
  78. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/get_ppse.py +0 -0
  79. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/patch_account_pnl_configs.py +0 -0
  80. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/post_order.py +0 -0
  81. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/post_position_pnl_configs.py +0 -0
  82. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/put_order.py +0 -0
  83. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/reverse_position.py +0 -0
  84. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/trading-api/send_email_otp.py +0 -0
  85. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/websocket-marketdata/estimated_market_index.py +0 -0
  86. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/websocket-marketdata/expected_price.py +0 -0
  87. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/websocket-marketdata/foreign_investor.py +0 -0
  88. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/websocket-marketdata/market_index.py +0 -0
  89. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/websocket-marketdata/market_index_influence.py +0 -0
  90. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/websocket-marketdata/ohlc.py +0 -0
  91. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/websocket-marketdata/ohlc_closed.py +0 -0
  92. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/websocket-marketdata/quote.py +0 -0
  93. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/websocket-marketdata/sec_def.py +0 -0
  94. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/websocket-marketdata/session.py +0 -0
  95. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/websocket-marketdata/trade.py +0 -0
  96. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/websocket-marketdata/trade_extra.py +0 -0
  97. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/websocket-trading/broker_order.py +0 -0
  98. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/websocket-trading/broker_position.py +0 -0
  99. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/websocket-trading/order.py +0 -0
  100. {dnse_sdk_openapi-2.4.0 → dnse_sdk_openapi-2.4.1}/websocket-trading/position.py +0 -0
@@ -0,0 +1,628 @@
1
+ Metadata-Version: 2.4
2
+ Name: dnse-sdk-openapi
3
+ Version: 2.4.1
4
+ Summary: DNSE OpenAPI SDK
5
+ Description-Content-Type: text/markdown
6
+
7
+ # DNSE OpenAPI Python SDK
8
+
9
+ Official Python SDK for integrating applications with **DNSE OpenAPI**.
10
+
11
+ The SDK provides a convenient Python interface for accessing DNSE trading, account, broker, and market data services, including REST APIs, real-time market data, and real-time trading events.
12
+
13
+ ---
14
+
15
+ ## Table of Contents
16
+
17
+ * [Overview](#overview)
18
+ * [Features](#features)
19
+ * [Requirements](#requirements)
20
+ * [Installation](#installation)
21
+ * [Configuration](#configuration)
22
+
23
+ * [Environments](#environments)
24
+ * [Initialize the Client](#initialize-the-client)
25
+ * [Configuration Parameters](#configuration-parameters)
26
+ * [Environment Variables](#environment-variables)
27
+ * [Authentication](#authentication)
28
+ * [Basic Usage](#basic-usage)
29
+ * [Dry Run](#dry-run)
30
+ * [Trading API](#trading-api)
31
+ * [Broker API](#broker-api)
32
+ * [Market Data API](#market-data-api)
33
+ * [WebSocket Market Data](#websocket-market-data)
34
+ * [WebSocket Trading Events](#websocket-trading-events)
35
+ * [Examples](#examples)
36
+ * [Response and Error Handling](#response-and-error-handling)
37
+ * [Security Best Practices](#security-best-practices)
38
+ * [Support](#support)
39
+
40
+ ---
41
+
42
+ # Overview
43
+
44
+ **DNSE OpenAPI** is an API-first trading platform that enables applications to integrate brokerage, trading, account, and market data services.
45
+
46
+ The **DNSE OpenAPI Python SDK** provides a Python client for interacting with DNSE OpenAPI.
47
+
48
+ The SDK handles common API communication requirements, including:
49
+
50
+ * Request authentication
51
+ * Request signing
52
+ * API version configuration
53
+ * HTTP communication
54
+ * Trading API requests
55
+ * Market data requests
56
+ * Real-time market data subscriptions
57
+ * Real-time trading event subscriptions
58
+
59
+ This allows developers to focus on building trading systems, investment applications, automation strategies, and market data applications.
60
+
61
+ ---
62
+
63
+ # Features
64
+
65
+ | Module | Capabilities |
66
+ | ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
67
+ | **Trading API** | Account information, balances, loan packages, buying/selling power, orders, executions, positions, PnL configuration, close position, and reverse position |
68
+ | **Broker API** | Broker-specific account and care-by information |
69
+ | **Authentication** | API Key, API Secret, request signing, Email OTP, Smart OTP, and Trading Token |
70
+ | **Market Data API** | Security definitions, instruments, trades, quotes, OHLC, expected prices, foreign trading, market indices, volume profiles, trading sessions, and working dates |
71
+ | **WebSocket Market Data** | Security definitions, quotes, trades, OHLC, expected prices, foreign investor data, market indices, sessions, and related market events |
72
+ | **WebSocket Trading Events** | Order and position events, including broker order and broker position events |
73
+ | **Dry Run** | Preview API requests without sending network requests |
74
+
75
+ ---
76
+
77
+ # Requirements
78
+
79
+ * Python **3.8+**
80
+ * A valid DNSE OpenAPI **API Key** and **API Secret**
81
+
82
+ API credentials are provided when you register for the corresponding DNSE OpenAPI environment.
83
+
84
+ ---
85
+
86
+ # Installation
87
+
88
+ Install the SDK from PyPI:
89
+
90
+ ```console
91
+ pip3 install dnse-sdk-openapi
92
+ ```
93
+
94
+ To upgrade to the latest version:
95
+
96
+ ```console
97
+ pip3 install --upgrade dnse-sdk-openapi
98
+ ```
99
+
100
+ Verify the installation:
101
+
102
+ ```python
103
+ from dnse import DNSEClient
104
+ ```
105
+
106
+ If no import error is returned, the SDK has been installed successfully.
107
+
108
+ ---
109
+
110
+ # Configuration
111
+
112
+ ## Environments
113
+
114
+ DNSE OpenAPI provides separate **Production** and **Sandbox** environments.
115
+
116
+ | | Production | Sandbox |
117
+ | --------------- | ------------------------------- | --------------------------------- |
118
+ | **REST API** | `https://openapi.dnse.com.vn` | `https://sb-openapi.dnse.com.vn` |
119
+ | **WebSocket** | `wss://ws-openapi.dnse.com.vn` | `wss://ws-sb-openapi.dnse.com.vn` |
120
+ | **API Version** | `2026-07-23` | `2026-07-23` |
121
+ | **Credentials** | Production API Key & API Secret | Sandbox API Key & API Secret |
122
+
123
+ > **Important:** Production and Sandbox use **different API credentials**. Use the API Key and API Secret provided for the corresponding environment.
124
+
125
+ Do not use Production credentials with Sandbox endpoints or Sandbox credentials with Production endpoints.
126
+
127
+ ---
128
+
129
+ ## Initialize the Client
130
+
131
+ ### Production
132
+
133
+ ```python
134
+ from dnse import DNSEClient
135
+
136
+ client = DNSEClient(
137
+ api_key="your_production_api_key",
138
+ api_secret="your_production_api_secret",
139
+ base_url="https://openapi.dnse.com.vn",
140
+ api_version="2026-07-23",
141
+ )
142
+ ```
143
+
144
+ ### Sandbox
145
+
146
+ ```python
147
+ from dnse import DNSEClient
148
+
149
+ client = DNSEClient(
150
+ api_key="your_sandbox_api_key",
151
+ api_secret="your_sandbox_api_secret",
152
+ base_url="https://sb-openapi.dnse.com.vn",
153
+ api_version="2026-07-23",
154
+ )
155
+ ```
156
+
157
+ ---
158
+
159
+ ## Configuration Parameters
160
+
161
+ | Parameter | Required | Description |
162
+ | ------------- | -------- | ------------------------------------------------------------------------------------ |
163
+ | `api_key` | Yes | API Key for the selected environment. Production and Sandbox use different API Keys. |
164
+ | `api_secret` | Yes | API Secret corresponding to the API Key and selected environment. |
165
+ | `base_url` | Yes | REST API endpoint of the selected DNSE environment. |
166
+ | `api_version` | No | API version sent in the `version` request header. |
167
+
168
+ ### API Credentials
169
+
170
+ Your API Key and API Secret are provided when you register for DNSE OpenAPI.
171
+
172
+ Make sure to use the credentials corresponding to the environment you are connecting to.
173
+
174
+ | Environment | API Key | API Secret |
175
+ | ----------- | ------------------ | --------------------- |
176
+ | Production | Production API Key | Production API Secret |
177
+ | Sandbox | Sandbox API Key | Sandbox API Secret |
178
+
179
+ > **Security:** Never expose your API Secret in public repositories, frontend applications, logs, or other publicly accessible locations.
180
+
181
+ ---
182
+
183
+ ## Environment Variables
184
+
185
+ For production applications, avoid hard-coding credentials in source code.
186
+
187
+ ```console
188
+ export DNSE_API_KEY="your_api_key"
189
+ export DNSE_API_SECRET="your_api_secret"
190
+ export DNSE_API_VERSION="2026-07-23"
191
+ ```
192
+
193
+ The API Key and API Secret should correspond to the environment configured in `base_url`.
194
+
195
+ ---
196
+
197
+ # Authentication
198
+
199
+ The SDK handles the authentication and request signing required when communicating with DNSE OpenAPI.
200
+
201
+ Developers provide the API credentials when creating the client:
202
+
203
+ ```python
204
+ client = DNSEClient(
205
+ api_key="your_api_key",
206
+ api_secret="your_api_secret",
207
+ base_url="https://openapi.dnse.com.vn",
208
+ api_version="2026-07-23",
209
+ )
210
+ ```
211
+
212
+ The SDK automatically handles the authentication-related request details.
213
+
214
+ ## Trading Authentication
215
+
216
+ Trading operations that require a **Trading Token** can use the following examples:
217
+
218
+ | Example | Description |
219
+ | ----------------------------- | --------------------------------------------------------- |
220
+ | `send_email_otp.py` | Request an OTP to be sent to the registered email address |
221
+ | `create_smart_otp_handoff.py` | Create a Smart OTP handoff |
222
+ | `create_trading_token.py` | Generate a Trading Token required for trading operations |
223
+
224
+ ---
225
+
226
+ # Basic Usage
227
+
228
+ Once the client has been initialized, API methods can be called directly.
229
+
230
+ For example, retrieve the trading accounts associated with the API Key:
231
+
232
+ ```python
233
+ from dnse import DNSEClient
234
+
235
+ client = DNSEClient(
236
+ api_key="your_api_key",
237
+ api_secret="your_api_secret",
238
+ base_url="https://openapi.dnse.com.vn",
239
+ api_version="2026-07-23",
240
+ )
241
+
242
+ status, body = client.get_accounts()
243
+
244
+ print(status)
245
+ print(body)
246
+ ```
247
+
248
+ The SDK returns:
249
+
250
+ ```text
251
+ status
252
+ body
253
+ ```
254
+
255
+ where:
256
+
257
+ * `status` contains the HTTP response status.
258
+ * `body` contains the API response payload.
259
+
260
+ ---
261
+
262
+ # Dry Run
263
+
264
+ The SDK supports **Dry Run** mode for previewing requests without sending them to DNSE servers.
265
+
266
+ Set:
267
+
268
+ ```python
269
+ dry_run=True
270
+ ```
271
+
272
+ Example:
273
+
274
+ ```python
275
+ status, body = client.get_accounts(dry_run=True)
276
+
277
+ print(status)
278
+ print(body)
279
+ ```
280
+
281
+ When Dry Run is enabled:
282
+
283
+ * The request is prepared by the SDK.
284
+ * No network request is sent.
285
+ * No request reaches DNSE servers.
286
+ * Developers can inspect or validate request construction during development.
287
+
288
+ ---
289
+
290
+ # Trading API
291
+
292
+ The Trading API provides access to account information, order management, positions, executions, and trading configuration.
293
+
294
+ ## Account & Portfolio
295
+
296
+ | Example | Description |
297
+ | ----------------------- | ----------------------------------------------------------- |
298
+ | `get_accounts.py` | Retrieve all trading sub-accounts managed under the API Key |
299
+ | `get_balances.py` | Retrieve asset balances of a trading sub-account |
300
+ | `get_loan_packages.py` | Retrieve available loan package codes |
301
+ | `get_loans.py` | Retrieve loan information |
302
+ | `get_ppse.py` | Retrieve buying power and selling power |
303
+ | `get_positions.py` | Retrieve current holding positions |
304
+ | `get_position_by_id.py` | Retrieve detailed information of a specific position |
305
+
306
+ ## Order Management
307
+
308
+ | Example | Description |
309
+ | --------------------------------- | ------------------------------------------------- |
310
+ | `get_orders.py` | Retrieve the intraday order book |
311
+ | `get_order_detail.py` | Retrieve detailed information of a specific order |
312
+ | `get_order_history.py` | Retrieve historical orders |
313
+ | `get_execution_detail.py` | Retrieve execution information of an order |
314
+ | `get_corporate_action_history.py` | Retrieve corporate action history |
315
+ | `post_order.py` | Submit a new trading order |
316
+ | `put_order.py` | Modify an existing order |
317
+ | `cancel_order.py` | Cancel an existing order |
318
+
319
+ ## Position Management
320
+
321
+ | Example | Description |
322
+ | ----------------------- | ------------------------------------------- |
323
+ | `get_positions.py` | Retrieve current holding positions |
324
+ | `get_position_by_id.py` | Retrieve detailed information of a position |
325
+ | `close_position.py` | Close an existing position |
326
+ | `reverse_position.py` | Reverse an existing derivative position |
327
+
328
+ ## PnL Configuration
329
+
330
+ | Example | Description |
331
+ | ------------------------------ | ----------------------------------------------------------- |
332
+ | `get_position_pnl_configs.py` | Retrieve PnL configuration of a derivative position |
333
+ | `get_account_pnl_configs.py` | Retrieve PnL configuration of a derivative sub-account |
334
+ | `post_position_pnl_configs.py` | Create or update PnL configuration of a derivative position |
335
+ | `patch_account_pnl_configs.py` | Update PnL configuration of a derivative sub-account |
336
+
337
+ ## Trading Authentication
338
+
339
+ | Example | Description |
340
+ | ----------------------------- | -------------------------------------------------------- |
341
+ | `send_email_otp.py` | Request an OTP sent to the registered email address |
342
+ | `create_smart_otp_handoff.py` | Create a Smart OTP handoff |
343
+ | `create_trading_token.py` | Generate a Trading Token required for trading operations |
344
+
345
+ ---
346
+
347
+ # Broker API
348
+
349
+ The Broker API provides broker-specific functionality available through the SDK.
350
+
351
+ | Example | Description |
352
+ | --------------------- | ---------------------------------------- |
353
+ | `get_list_care_by.py` | Retrieve the list of care-by information |
354
+
355
+ ---
356
+
357
+ # Market Data API
358
+
359
+ The Market Data API provides access to reference and historical market data.
360
+
361
+ | Example | Description |
362
+ | ------------------------------ | --------------------------------------------------------- |
363
+ | `get_security_definition.py` | Retrieve security definition and instrument details |
364
+ | `get_instruments.py` | Retrieve available trading instruments and their metadata |
365
+ | `get_trades.py` | Retrieve trade data for a specific instrument |
366
+ | `get_latest_trade.py` | Retrieve the latest trade for a specific instrument |
367
+ | `get_trades_volume_profile.py` | Retrieve trade volume profile |
368
+ | `get_quotes.py` | Retrieve quote data |
369
+ | `get_latest_quote.py` | Retrieve the latest quote |
370
+ | `get_ohlc.py` | Retrieve OHLC data for a specified time range |
371
+ | `get_expected_price.py` | Retrieve expected price data |
372
+ | `get_foreign_trading.py` | Retrieve foreign investor trading data |
373
+ | `get_market_index.py` | Retrieve market index data |
374
+ | `get_latest_session.py` | Retrieve the latest trading session |
375
+ | `get_close_price.py` | Retrieve the latest closing price |
376
+ | `get_working_dates.py` | Retrieve trading working dates |
377
+
378
+ ### Example
379
+
380
+ Retrieve the latest trade data:
381
+
382
+ ```python
383
+ status, body = client.get_latest_trade(
384
+ symbol="VNM"
385
+ )
386
+ print(status, body)
387
+ ```
388
+
389
+ For complete request parameters, supported values, and response schemas, refer to the DNSE OpenAPI API Reference.
390
+
391
+ ---
392
+
393
+ # WebSocket Market Data
394
+
395
+ The SDK provides WebSocket support for subscribing to real-time market data.
396
+
397
+ WebSocket is suitable for applications that require continuously updated market information instead of periodically polling REST APIs.
398
+
399
+ | Example | Description |
400
+ | --------------------------- | ------------------------------------------------------------- |
401
+ | `sec_def.py` | Subscribe to real-time security definition updates |
402
+ | `quote.py` | Subscribe to real-time quote updates |
403
+ | `trade.py` | Subscribe to real-time trade updates |
404
+ | `trade_extra.py` | Subscribe to real-time trade data with additional information |
405
+ | `ohlc.py` | Subscribe to real-time OHLC updates |
406
+ | `ohlc_closed.py` | Subscribe to completed OHLC candle updates |
407
+ | `expected_price.py` | Subscribe to real-time expected price updates |
408
+ | `foreign_investor.py` | Subscribe to foreign investor trading updates |
409
+ | `market_index.py` | Subscribe to real-time market index updates |
410
+ | `estimated_market_index.py` | Subscribe to estimated market index updates |
411
+ | `market_index_influence.py` | Subscribe to market index influence updates |
412
+ | `session.py` | Subscribe to trading session updates |
413
+
414
+ ---
415
+
416
+ # WebSocket Trading Events
417
+
418
+ The SDK supports real-time trading event notifications through WebSocket.
419
+
420
+ | Example | Description |
421
+ | -------------------- | ----------------------------------- |
422
+ | `order.py` | Subscribe to order events |
423
+ | `position.py` | Subscribe to position events |
424
+ | `broker_order.py` | Subscribe to broker order events |
425
+ | `broker_position.py` | Subscribe to broker position events |
426
+
427
+ Order and position events can be used to monitor trading activity in real time.
428
+
429
+ ---
430
+
431
+ # Examples
432
+
433
+ The `examples/` directory contains scripts organized into two groups:
434
+
435
+ - `reference/` — authentication flows: obtaining a trading token via Email OTP, SmartOTP manual entry, or SmartOTP programmatic handoff.
436
+ - `use-cases/` — end-to-end trading flows: portfolio check, market data, order history, place a trade, and a full signal-driven auto-trader with TP/SL management.
437
+
438
+ For individual API operations, refer to the `trading-api/`, `broker-api/`, `marketdata-api/`, `websocket-marketdata/`, and `websocket-trading/` directories at the root of the SDK.
439
+
440
+ For setup instructions, environment variables, and a full file guide, see examples/README.md.
441
+
442
+ All scripts are configured via environment variables. Copy `.env.example` to `.env` and fill in your credentials before running.
443
+
444
+ ---
445
+
446
+ ## Trading Token — Authentication Flows
447
+
448
+ Every order mutation (place, modify, cancel) requires a **trading token** — a short-lived credential valid for 8 hours. The scripts in `examples/reference/` each cover one way to obtain a token, then run a full order lifecycle (place → modify → cancel) to confirm everything works end-to-end.
449
+
450
+ Choose the flow that matches your integration type:
451
+
452
+ | Script | OTP source | Best for |
453
+ |---|---|---|
454
+ | `orders-email-otp.py` | Email inbox | Integrations where the end user has access to their registered email |
455
+ | `orders-smart-otp.py` | DNSE mobile app (manual entry) | Integrations where the end user manually copies the OTP from the app |
456
+ | `smart-otp-handoff-flow.py` | DNSE mobile app (in-app confirmation) | Integrations that trigger the OTP request programmatically and let the user approve via an app popup |
457
+
458
+ ### Email OTP
459
+
460
+ Calls `send_email_otp()` to trigger a passcode to your registered email, then prompts you to enter it. After you submit it, the script exchanges it for a trading token via `create_trading_token(otp_type="email_otp")` and runs the order lifecycle against `HPG` as a test symbol.
461
+
462
+ The `otp_email.py` helper can auto-fetch the OTP from a configured mailbox if you want to remove the manual input step in test environments.
463
+
464
+ ```
465
+ python examples/reference/orders-email-otp.py
466
+ ```
467
+
468
+ ### Smart OTP — Manual Entry
469
+
470
+ For accounts with SmartOTP enabled. Generate the OTP in the DNSE mobile app, enter it when prompted, and the script calls `create_trading_token(otp_type="smart_otp")`. No email step needed. The order lifecycle that follows is identical to the Email OTP example.
471
+
472
+ ```
473
+ python examples/reference/orders-smart-otp.py
474
+ ```
475
+
476
+ ### Smart OTP — Programmatic Handoff
477
+
478
+ Calls `create_smart_otp_handoff()` to trigger an OTP request from the server side. DNSE then pushes a confirmation popup to the user's DNSE mobile app — the user approves it there, and the API returns the SmartOTP in the same response. The script immediately passes that value to `create_trading_token()` to obtain a trading token.
479
+
480
+ This is the recommended flow for integrations that want to keep the user in-app rather than switching to email, while still driving the OTP request programmatically from your backend.
481
+
482
+ ```
483
+ python examples/reference/smart-otp-handoff-flow.py
484
+ ```
485
+
486
+ ---
487
+
488
+ ## Use Cases
489
+
490
+ The scripts in `examples/use-cases/` combine token management, REST calls, and WebSocket events into complete trading flows. Each is a working starting point you can adapt to your own integration.
491
+
492
+ ### Place a Trade
493
+
494
+ Covers the core integration pattern: place an order and confirm it via real-time events.
495
+
496
+ **Flow:**
497
+ 1. Fetch the current price band for the target symbol via `get_security_definition()`.
498
+ 2. Retrieve a valid `loanPackageId` via `get_loan_packages()` — required for all order placements, both stock and derivative.
499
+ 3. Obtain a trading token (cached if still valid, otherwise triggers OTP).
500
+ 4. Subscribe to order and position events over WebSocket — *before* placing the order to avoid missing the confirmation event.
501
+ 5. Place a limit buy order via `post_order()`.
502
+ 6. Wait for either an order event with a live status (`PENDINGNEW`, `NEW`, `ACTIVE`, ...) or a position event for the symbol (indicates a fill). Timeout defaults to 15 seconds.
503
+ 7. Cancel the order at the end of the demo.
504
+
505
+ Step 4 is the key detail: subscribing before placing ensures the WebSocket is ready to receive the exchange acknowledgment, which arrives asynchronously and can precede the REST response in some cases.
506
+
507
+ ```
508
+ python examples/use-cases/place-a-trade.py
509
+ ```
510
+
511
+ ### Auto-Trader: Signal-Driven Position Management
512
+
513
+ Demonstrates a complete signal-to-exit lifecycle: receive a trading signal, size the position, place the entry order, monitor it via WebSocket events, then exit automatically when price hits the take-profit or stop-loss level.
514
+
515
+ This use case is built from three cooperating modules:
516
+
517
+ **`position_manager.py`** is the execution layer. `PositionManager` exposes two entry points depending on whether a position is being opened or resumed:
518
+
519
+ - `run(signal)` — takes a `Signal` (side, entry price, stop-loss, take-profit), places the entry order, and waits up to `entry_fill_timeout` seconds for a fill confirmation via the order event stream. Once filled, it hands off to the exit monitor.
520
+ - `resume(saved)` — re-attaches to an existing open position loaded from disk, skipping the entry step and going directly to TP/SL monitoring. Used on application restart.
521
+
522
+ Exit logic runs on every price tick delivered via `feed_price()`, called by the application's live OHLC WebSocket stream. When price crosses the take-profit or stop-loss level, a closing order is placed immediately for the full filled quantity. An optional `manage_timeout` parameter forces a close after a fixed number of seconds regardless of price — useful for end-of-session risk control.
523
+
524
+ Position sizing is handled by `size_by_risk(entry, stop_loss, risk_points)`, which calculates quantity such that a full stop-out loses approximately `risk_points` in total value, keeping risk consistent across trades with varying volatility.
525
+
526
+ **`position_store.py`** handles crash recovery. When an entry fills, the full position state (symbol, side, quantity, average price, stop-loss, take-profit, entry order ID) is written to a local file. On the next startup, the application detects this file and calls `resume()` instead of opening a new trade. The file is deleted when the position closes.
527
+
528
+ **`strategy_base.py`** defines the `Signal` dataclass and the base `Strategy` interface. The bundled strategy scripts (`strategy_ichimoku.py`, `strategy_price_action.py`, `strategy_scalping.py`) each implement a `Strategy` subclass that emits a `Signal` when entry conditions are met. Swap in any of these — or write your own — without changing the position management layer.
529
+
530
+ ---
531
+
532
+ # Response and Error Handling
533
+
534
+ SDK API methods return the HTTP status and response body:
535
+
536
+ ```python
537
+ status, body = client.get_accounts()
538
+
539
+ if status == 200:
540
+ print(body)
541
+ else:
542
+ print("Request failed:", status, body)
543
+ ```
544
+
545
+ * `status` contains the HTTP response status.
546
+ * `body` contains the API response payload.
547
+
548
+ Applications should handle unsuccessful responses appropriately.
549
+
550
+ For production trading applications, consider implementing:
551
+
552
+ * HTTP status validation
553
+ * API error handling
554
+ * Request retry strategy where appropriate
555
+ * Logging
556
+ * Timeout handling
557
+ * Order status verification
558
+ * WebSocket reconnect handling
559
+
560
+ > **Important:** Do not automatically retry trading requests unless the operation is safe to retry. For order-related operations, verify the order status before retrying a failed request.
561
+
562
+ ---
563
+
564
+ # Security Best Practices
565
+
566
+ ## Keep Credentials Secure
567
+
568
+ Do not hard-code API credentials in source code that is committed to a public or shared repository.
569
+
570
+ Use environment variables or a secure secret-management solution instead.
571
+
572
+ ## Do Not Expose API Credentials
573
+
574
+ Never expose the following in:
575
+
576
+ * Frontend applications
577
+ * Public Git repositories
578
+ * Client-side JavaScript
579
+ * Logs
580
+ * Screenshots
581
+ * Public documentation
582
+
583
+ ```text
584
+ API Key
585
+ API Secret
586
+ Trading Token
587
+ OTP
588
+ ```
589
+
590
+ ## Use Appropriate API Permissions
591
+
592
+ Configure the API Key with only the permissions required by your application.
593
+
594
+ For example, an application that only consumes market data does not need trading permissions.
595
+
596
+ ## Use the Correct Environment
597
+
598
+ Always make sure that the credentials and endpoints belong to the same environment.
599
+
600
+ | Environment | REST API | Credentials |
601
+ | ----------- | -------------------------------- | ---------------------- |
602
+ | Production | `https://openapi.dnse.com.vn` | Production credentials |
603
+ | Sandbox | `https://sb-openapi.dnse.com.vn` | Sandbox credentials |
604
+
605
+ ---
606
+
607
+ # Support
608
+
609
+ For detailed information about:
610
+
611
+ * Available endpoints
612
+ * Request parameters
613
+ * Authentication
614
+ * API permissions
615
+ * Response schemas
616
+ * WebSocket topics
617
+
618
+ refer to the **DNSE OpenAPI Documentation**.
619
+
620
+ Before using trading functionality in a production application, make sure that:
621
+
622
+ * The API Key has been registered successfully.
623
+ * Required API permissions have been enabled.
624
+ * The correct environment is configured.
625
+ * API credentials are stored securely.
626
+ * Trading authentication requirements have been completed.
627
+ * API errors are handled appropriately.
628
+ * WebSocket disconnections and reconnections are handled appropriately.