moomoo-api-mcp 0.1.6__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.
@@ -0,0 +1,328 @@
1
+ """Trading tools for order management operations."""
2
+
3
+ from mcp.server.fastmcp import Context
4
+ from mcp.server.session import ServerSession
5
+
6
+ from moomoo_mcp.server import AppContext, mcp
7
+
8
+
9
+ @mcp.tool()
10
+ def place_order(
11
+ ctx: Context[ServerSession, AppContext],
12
+ code: str,
13
+ price: float,
14
+ qty: int,
15
+ trd_side: str,
16
+ order_type: str = "NORMAL",
17
+ time_in_force: str = "DAY",
18
+ adjust_limit: float = 0,
19
+ aux_price: float | None = None,
20
+ trail_type: str | None = None,
21
+ trail_value: float | None = None,
22
+ trail_spread: float | None = None,
23
+ trd_env: str = "REAL",
24
+ acc_id: int = 0,
25
+ remark: str = "",
26
+ ) -> dict:
27
+ """Place a new trading order.
28
+
29
+ CRITICAL: You MUST ask the user for explicit confirmation before calling this
30
+ tool, especially if `trd_env` is 'REAL'. Display the full order details to the
31
+ user for verification including: code, side (BUY/SELL), quantity, price, order
32
+ type, and time in force. Orders placed in REAL environment will use real money.
33
+
34
+ IMPORTANT FOR AI AGENTS:
35
+ - Default is REAL account as per user preference.
36
+ - ALWAYS confirm with user before placing orders.
37
+ - For SIMULATE environment, explicitly set trd_env='SIMULATE'.
38
+
39
+ Args:
40
+ code: Stock code (e.g., 'US.AAPL', 'HK.00700').
41
+ price: Order price. For market orders, this is used as price limit.
42
+ qty: Order quantity (number of shares).
43
+ trd_side: Trade side - 'BUY' or 'SELL'.
44
+ order_type: Order type. Supported values:
45
+ - 'NORMAL': Enhanced limit order (HK), limit order (US/A-share).
46
+ - 'MARKET': Market order.
47
+ - 'ABSOLUTE_LIMIT': Limit order (HK only, exact price match required).
48
+ - 'AUCTION': Auction order (HK).
49
+ - 'AUCTION_LIMIT': Auction limit order (HK).
50
+ - 'SPECIAL_LIMIT': Special limit / Market IOC (HK, partial fill then cancel).
51
+ - 'SPECIAL_LIMIT_ALL': Special limit all-or-none (HK, fill all or cancel).
52
+ - 'STOP': Stop market order.
53
+ - 'STOP_LIMIT': Stop limit order.
54
+ - 'MARKET_IF_TOUCHED': Market if touched (take profit).
55
+ - 'LIMIT_IF_TOUCHED': Limit if touched (take profit).
56
+ - 'TRAILING_STOP': Trailing stop market order.
57
+ - 'TRAILING_STOP_LIMIT': Trailing stop limit order.
58
+ time_in_force: Time in force for the order. Default 'DAY'.
59
+ - 'DAY': Order valid for current trading day only.
60
+ - 'GTC': Good-Til-Cancelled, order remains active until filled or cancelled.
61
+ adjust_limit: Adjust limit percentage (0-100). Default 0.
62
+ aux_price: Trigger price for stop/if-touched order types (required for STOP,
63
+ STOP_LIMIT, MARKET_IF_TOUCHED, LIMIT_IF_TOUCHED).
64
+ trail_type: Trailing type for trailing stop orders (required for TRAILING_STOP,
65
+ TRAILING_STOP_LIMIT). Values: 'RATIO' or 'AMOUNT'.
66
+ trail_value: Trailing value (ratio or amount) for trailing stop types.
67
+ trail_spread: Optional trailing spread for trailing stop limit types.
68
+ trd_env: Trading environment - 'REAL' or 'SIMULATE'. Default REAL.
69
+ acc_id: Account ID from get_accounts(). Required if multiple accounts exist.
70
+ remark: Optional order note/remark.
71
+
72
+ Returns:
73
+ Dictionary with order details including order_id, order_status,
74
+ time_in_force, etc.
75
+ """
76
+ trade_service = ctx.request_context.lifespan_context.trade_service
77
+ return trade_service.place_order(
78
+ code=code,
79
+ price=price,
80
+ qty=qty,
81
+ trd_side=trd_side,
82
+ order_type=order_type,
83
+ time_in_force=time_in_force,
84
+ adjust_limit=adjust_limit,
85
+ aux_price=aux_price,
86
+ trail_type=trail_type,
87
+ trail_value=trail_value,
88
+ trail_spread=trail_spread,
89
+ trd_env=trd_env,
90
+ acc_id=acc_id,
91
+ remark=remark,
92
+ )
93
+
94
+
95
+ @mcp.tool()
96
+ def modify_order(
97
+ ctx: Context[ServerSession, AppContext],
98
+ order_id: str,
99
+ modify_order_op: str,
100
+ qty: int | None = None,
101
+ price: float | None = None,
102
+ adjust_limit: float = 0,
103
+ trd_env: str = "REAL",
104
+ acc_id: int = 0,
105
+ ) -> dict:
106
+ """Modify an existing order.
107
+
108
+ CRITICAL: You MUST ask the user for explicit confirmation before calling this
109
+ tool, especially if `trd_env` is 'REAL'. Display the order_id and the new
110
+ parameters (price, qty) to the user for verification.
111
+
112
+ IMPORTANT FOR AI AGENTS:
113
+ - Default is REAL account as per user preference.
114
+ - ALWAYS confirm with user before modifying orders.
115
+
116
+ Args:
117
+ order_id: Order ID to modify. Get from get_orders().
118
+ modify_order_op: Modification operation:
119
+ - 'NORMAL': Modify price/quantity.
120
+ - 'CANCEL': Cancel the order.
121
+ - 'DISABLE': Disable the order.
122
+ - 'ENABLE': Enable a disabled order.
123
+ - 'DELETE': Delete the order.
124
+ qty: New quantity (optional, for NORMAL operation).
125
+ price: New price (optional, for NORMAL operation).
126
+ adjust_limit: Adjust limit percentage (0-100). Default 0.
127
+ trd_env: Trading environment - 'REAL' or 'SIMULATE'. Default REAL.
128
+ acc_id: Account ID from get_accounts().
129
+
130
+ Returns:
131
+ Dictionary with modified order details.
132
+ """
133
+ trade_service = ctx.request_context.lifespan_context.trade_service
134
+ return trade_service.modify_order(
135
+ order_id=order_id,
136
+ modify_order_op=modify_order_op,
137
+ qty=qty,
138
+ price=price,
139
+ adjust_limit=adjust_limit,
140
+ trd_env=trd_env,
141
+ acc_id=acc_id,
142
+ )
143
+
144
+
145
+ @mcp.tool()
146
+ def cancel_order(
147
+ ctx: Context[ServerSession, AppContext],
148
+ order_id: str,
149
+ trd_env: str = "REAL",
150
+ acc_id: int = 0,
151
+ ) -> dict:
152
+ """Cancel an existing order.
153
+
154
+ CRITICAL: You MUST ask the user for explicit confirmation before calling this
155
+ tool, especially if `trd_env` is 'REAL'. Display the order_id to the user
156
+ for verification before cancellation.
157
+
158
+ IMPORTANT FOR AI AGENTS:
159
+ - Default is REAL account as per user preference.
160
+ - ALWAYS confirm with user before cancelling orders.
161
+
162
+ Args:
163
+ order_id: Order ID to cancel. Get from get_orders().
164
+ trd_env: Trading environment - 'REAL' or 'SIMULATE'. Default REAL.
165
+ acc_id: Account ID from get_accounts().
166
+
167
+ Returns:
168
+ Dictionary with cancelled order details.
169
+ """
170
+ trade_service = ctx.request_context.lifespan_context.trade_service
171
+ return trade_service.cancel_order(
172
+ order_id=order_id,
173
+ trd_env=trd_env,
174
+ acc_id=acc_id,
175
+ )
176
+
177
+
178
+ @mcp.tool()
179
+ def get_orders(
180
+ ctx: Context[ServerSession, AppContext],
181
+ code: str = "",
182
+ status_filter_list: list[str] | None = None,
183
+ trd_env: str = "REAL",
184
+ acc_id: int = 0,
185
+ refresh_cache: bool = False,
186
+ ) -> list[dict]:
187
+ """Get list of today's orders.
188
+
189
+ IMPORTANT FOR AI AGENTS:
190
+ - Default is REAL account. You MUST notify the user clearly that you are
191
+ accessing their REAL trading account before proceeding.
192
+ - Only use SIMULATE if the user explicitly requests it.
193
+
194
+ Args:
195
+ code: Filter by stock code (e.g., 'US.AAPL'). Empty string for all.
196
+ status_filter_list: Filter by order statuses. Options:
197
+ - 'UNSUBMITTED', 'WAITING_SUBMIT', 'SUBMITTING', 'SUBMIT_FAILED'
198
+ - 'SUBMITTED', 'FILLED_PART', 'FILLED_ALL'
199
+ - 'CANCELLING_PART', 'CANCELLING_ALL', 'CANCELLED_PART', 'CANCELLED_ALL'
200
+ - 'REJECTED', 'DISABLED', 'DELETED', 'FAILED', 'NONE'
201
+ trd_env: Trading environment - 'REAL' or 'SIMULATE'. Default REAL.
202
+ acc_id: Account ID from get_accounts().
203
+ refresh_cache: Whether to refresh the cache. Default False.
204
+
205
+ Returns:
206
+ List of order dictionaries with order_id, code, qty, price, trd_side,
207
+ order_type, order_status, created_time, updated_time, etc.
208
+ """
209
+ trade_service = ctx.request_context.lifespan_context.trade_service
210
+ return trade_service.get_orders(
211
+ code=code,
212
+ status_filter_list=status_filter_list,
213
+ trd_env=trd_env,
214
+ acc_id=acc_id,
215
+ refresh_cache=refresh_cache,
216
+ )
217
+
218
+
219
+ @mcp.tool()
220
+ def get_deals(
221
+ ctx: Context[ServerSession, AppContext],
222
+ code: str = "",
223
+ trd_env: str = "REAL",
224
+ acc_id: int = 0,
225
+ refresh_cache: bool = False,
226
+ ) -> list[dict]:
227
+ """Get list of today's deals (executed trades).
228
+
229
+ A deal represents a filled order or partial fill. One order can result in
230
+ multiple deals if filled in parts.
231
+
232
+ IMPORTANT FOR AI AGENTS:
233
+ - Default is REAL account. You MUST notify the user clearly that you are
234
+ accessing their REAL trading account before proceeding.
235
+ - Only use SIMULATE if the user explicitly requests it.
236
+
237
+ Args:
238
+ code: Filter by stock code (e.g., 'US.AAPL'). Empty string for all.
239
+ trd_env: Trading environment - 'REAL' or 'SIMULATE'. Default REAL.
240
+ acc_id: Account ID from get_accounts().
241
+ refresh_cache: Whether to refresh the cache. Default False.
242
+
243
+ Returns:
244
+ List of deal dictionaries with deal_id, order_id, code, qty, price,
245
+ trd_side, create_time, etc.
246
+ """
247
+ trade_service = ctx.request_context.lifespan_context.trade_service
248
+ return trade_service.get_deals(
249
+ code=code,
250
+ trd_env=trd_env,
251
+ acc_id=acc_id,
252
+ refresh_cache=refresh_cache,
253
+ )
254
+
255
+
256
+ @mcp.tool()
257
+ def get_history_orders(
258
+ ctx: Context[ServerSession, AppContext],
259
+ code: str = "",
260
+ status_filter_list: list[str] | None = None,
261
+ start: str = "",
262
+ end: str = "",
263
+ trd_env: str = "REAL",
264
+ acc_id: int = 0,
265
+ ) -> list[dict]:
266
+ """Get historical orders.
267
+
268
+ IMPORTANT FOR AI AGENTS:
269
+ - Default is REAL account. You MUST notify the user clearly that you are
270
+ accessing their REAL trading account before proceeding.
271
+ - Only use SIMULATE if the user explicitly requests it.
272
+
273
+ Args:
274
+ code: Filter by stock code (e.g., 'US.AAPL'). Empty string for all.
275
+ status_filter_list: Filter by order statuses (see get_orders for options).
276
+ start: Start date in 'YYYY-MM-DD' format. Empty for max range.
277
+ end: End date in 'YYYY-MM-DD' format. Empty for today.
278
+ trd_env: Trading environment - 'REAL' or 'SIMULATE'. Default REAL.
279
+ acc_id: Account ID from get_accounts().
280
+
281
+ Returns:
282
+ List of historical order dictionaries.
283
+ """
284
+ trade_service = ctx.request_context.lifespan_context.trade_service
285
+ return trade_service.get_history_orders(
286
+ code=code,
287
+ status_filter_list=status_filter_list,
288
+ start=start,
289
+ end=end,
290
+ trd_env=trd_env,
291
+ acc_id=acc_id,
292
+ )
293
+
294
+
295
+ @mcp.tool()
296
+ def get_history_deals(
297
+ ctx: Context[ServerSession, AppContext],
298
+ code: str = "",
299
+ start: str = "",
300
+ end: str = "",
301
+ trd_env: str = "REAL",
302
+ acc_id: int = 0,
303
+ ) -> list[dict]:
304
+ """Get historical deals (executed trades).
305
+
306
+ IMPORTANT FOR AI AGENTS:
307
+ - Default is REAL account. You MUST notify the user clearly that you are
308
+ accessing their REAL trading account before proceeding.
309
+ - Only use SIMULATE if the user explicitly requests it.
310
+
311
+ Args:
312
+ code: Filter by stock code (e.g., 'US.AAPL'). Empty string for all.
313
+ start: Start date in 'YYYY-MM-DD' format. Empty for max range.
314
+ end: End date in 'YYYY-MM-DD' format. Empty for today.
315
+ trd_env: Trading environment - 'REAL' or 'SIMULATE'. Default REAL.
316
+ acc_id: Account ID from get_accounts().
317
+
318
+ Returns:
319
+ List of historical deal dictionaries.
320
+ """
321
+ trade_service = ctx.request_context.lifespan_context.trade_service
322
+ return trade_service.get_history_deals(
323
+ code=code,
324
+ start=start,
325
+ end=end,
326
+ trd_env=trd_env,
327
+ acc_id=acc_id,
328
+ )