Skip to content

Commit 227cda6

Browse files
committed
modified: tigeropen/examples/trade_client_demo.py
new file: tigeropen/trade/response/order_preview_response.py modified: tigeropen/trade/trade_client.py
1 parent dabd085 commit 227cda6

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

tigeropen/examples/trade_client_demo.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ def trade_apis():
9292
new_order = openapi_client.get_order(order_id=order_id)
9393
assert new_order.status == OrderStatus.CANCELLED or new_order.status == OrderStatus.PENDING_CANCEL
9494

95+
# 预览订单 (下单前后保证金要求, 佣金等预览)
96+
result = openapi_client.preview_order(order)
97+
print(result)
98+
9599

96100
if __name__ == '__main__':
97101
get_account_info()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import json
2+
3+
tmp = {"initMarginBefore":324749.22,"commissionCurrency":"USD","maintMargin":279948.41,
4+
"equityWithLoan":704652.64,"minCommission":0.26051,"maintMarginBefore":273257.34,
5+
"initMargin":332121.39,"equityWithLoanBefore":700658.61,"marginCurrency":"USD",
6+
"maxCommission":1.34051}
7+
8+
from tigeropen.common.response import TigerResponse
9+
10+
PREVIEW_ORDER_FIELD_MAPPING = {"initMarginBefore": "init_margin_before", "commissionCurrency": "commission_currency",
11+
"maintMargin": "maint_margin", "equityWithLoan": "equity_with_loan",
12+
"minCommission": "min_commission", "maintMarginBefore": "maint_margin_before",
13+
"initMargin": "init_margin", "equityWithLoanBefore": "equity_with_loan_before",
14+
"marginCurrency": "margin_currency", "maxCommission": "max_commission"}
15+
16+
17+
class PreviewOrderResponse(TigerResponse):
18+
def __init__(self):
19+
super(PreviewOrderResponse, self).__init__()
20+
self.preview_order = dict()
21+
self._is_success = None
22+
23+
def parse_response_content(self, response_content):
24+
response = super(PreviewOrderResponse, self).parse_response_content(response_content)
25+
if 'is_success' in response:
26+
self._is_success = response['is_success']
27+
28+
if self.data:
29+
data_json = json.loads(self.data)
30+
for key, value in data_json.items():
31+
field = PREVIEW_ORDER_FIELD_MAPPING.get(key, key)
32+
self.preview_order[field] = value
33+

tigeropen/trade/trade_client.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010

1111
from tigeropen.trade.response.contracts_response import ContractsResponse
1212
from tigeropen.trade.response.order_id_response import OrderIdResponse
13+
from tigeropen.trade.response.order_preview_response import PreviewOrderResponse
1314
from tigeropen.trade.response.orders_response import OrdersResponse
1415
from tigeropen.tiger_open_client import TigerOpenClient, ApiException
1516
from tigeropen.trade.request.model import ContractParams, AccountsParams, AssetParams, PositionParams, OrdersParams, \
1617
OrderParams, PlaceModifyOrderParams, CancelOrderParams
1718
from tigeropen.quote.request import OpenApiRequest
1819
from tigeropen.trade.response.assets_response import AssetsResponse
1920
from tigeropen.common.consts.service_types import CONTRACTS, ACCOUNTS, POSITIONS, ASSETS, ORDERS, ORDER_NO, \
20-
CANCEL_ORDER, MODIFY_ORDER, PLACE_ORDER, ACTIVE_ORDERS, INACTIVE_ORDERS, FILLED_ORDERS, CONTRACT
21+
CANCEL_ORDER, MODIFY_ORDER, PLACE_ORDER, ACTIVE_ORDERS, INACTIVE_ORDERS, FILLED_ORDERS, CONTRACT, PREVIEW_ORDER
2122

2223
import logging
2324

@@ -412,6 +413,46 @@ def create_order(self, account, contract, action, order_type, quantity, limit_pr
412413

413414
return None
414415

416+
def preview_order(self, order):
417+
"""
418+
预览订单
419+
:param order: Order 对象
420+
:return:
421+
init_margin_before 下单前账户初始保证金
422+
init_margin 预计下单后的账户初始保证金
423+
maint_margin_before 下单前账户的维持保证金
424+
maint_margin 预计下单后的账户维持保证金
425+
margin_currency 保证金货币币种
426+
equity_with_loan_before 下单前账户的含借贷值股权(含贷款价值资产)
427+
equity_with_loan 下单后账户的含借贷值股权(含贷款价值资产)
428+
min_commission 预期最低佣金
429+
max_commission 预期最高佣金
430+
commission_currency 佣金货币币种
431+
"""
432+
params = PlaceModifyOrderParams()
433+
params.account = order.account
434+
params.contract = order.contract
435+
params.action = order.action
436+
params.order_type = order.order_type
437+
params.order_id = order.order_id
438+
params.quantity = order.quantity
439+
params.limit_price = order.limit_price
440+
params.aux_price = order.aux_price
441+
params.trail_stop_price = order.trail_stop_price
442+
params.trailing_percent = order.trailing_percent
443+
params.percent_offset = order.percent_offset
444+
params.time_in_force = order.time_in_force
445+
params.outside_rth = order.outside_rth
446+
request = OpenApiRequest(PREVIEW_ORDER, biz_model=params)
447+
response_content = self.__fetch_data(request)
448+
if response_content:
449+
response = PreviewOrderResponse()
450+
response.parse_response_content(response_content)
451+
if response.is_success():
452+
return response.preview_order
453+
else:
454+
raise ApiException(response.code, response.message)
455+
415456
def place_order(self, order):
416457
"""
417458
下单

0 commit comments

Comments
 (0)