Skip to content

Commit afdcff6

Browse files
committed
Merge branch 'dev' of git.tigerbrokers.net:server/openapi/openapi-python-sdk into dev
2 parents 0f8b626 + 88465b7 commit afdcff6

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
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()

tigeropen/quote/response/future_contract_response.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
from tigeropen.common.response import TigerResponse
1212

1313
COLUMNS = ['contract_code', 'symbol', 'type', 'name', 'contract_month', 'multiplier', 'exchange', 'currency',
14-
'first_notice_date', 'last_bidding_close_time', 'last_trading_date', 'trade', 'continuous']
14+
'first_notice_date', 'last_bidding_close_time', 'last_trading_date', 'trade', 'continuous', 'min_tick']
1515
CONTRACT_FIELD_MAPPINGS = {'contractCode': 'contract_code', 'exchangeCode': 'exchange', 'ibCode': 'symbol',
1616
'contractMonth': 'contract_month', 'firstNoticeDate': 'first_notice_date',
17-
'lastBiddingCloseTime': 'last_bidding_close_time', 'lastTradingDate': 'last_trading_date'}
17+
'lastBiddingCloseTime': 'last_bidding_close_time', 'lastTradingDate': 'last_trading_date',
18+
'minTick': 'min_tick'}
1819

1920

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

tigeropen/trade/trade_client.py

Lines changed: 45 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,49 @@ 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: dict. 字段如下
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+
若无法下单, 返回的 dict 中仅有如下字段:
433+
warning_text 无法下单的原因
434+
"""
435+
params = PlaceModifyOrderParams()
436+
params.account = order.account
437+
params.contract = order.contract
438+
params.action = order.action
439+
params.order_type = order.order_type
440+
params.order_id = order.order_id
441+
params.quantity = order.quantity
442+
params.limit_price = order.limit_price
443+
params.aux_price = order.aux_price
444+
params.trail_stop_price = order.trail_stop_price
445+
params.trailing_percent = order.trailing_percent
446+
params.percent_offset = order.percent_offset
447+
params.time_in_force = order.time_in_force
448+
params.outside_rth = order.outside_rth
449+
request = OpenApiRequest(PREVIEW_ORDER, biz_model=params)
450+
response_content = self.__fetch_data(request)
451+
if response_content:
452+
response = PreviewOrderResponse()
453+
response.parse_response_content(response_content)
454+
if response.is_success():
455+
return response.preview_order
456+
else:
457+
raise ApiException(response.code, response.message)
458+
415459
def place_order(self, order):
416460
"""
417461
下单

0 commit comments

Comments
 (0)