77import json
88
99from tigeropen .common .response import TigerResponse
10+ from tigeropen .common .util .string_utils import camel_to_underline
1011from tigeropen .trade .domain .contract import Contract
11- from tigeropen .trade .response import CONTRACT_FIELDS
1212
13- CONTRACT_FIELD_MAPPINGS = {'secType' : 'sec_type' , 'localSymbol' : 'local_symbol' , 'originSymbol' : 'origin_symbol' ,
14- 'conid' : 'contract_id' , 'contractId' : 'contract_id' ,
15- 'shortMargin' : 'short_margin' , 'shortFeeRate' : 'short_fee_rate' ,
16- 'longInitialMargin' : 'long_initial_margin' , 'contractMonth' : 'contract_month' ,
17- 'longMaintenanceMargin' : 'long_maintenance_margin' , 'primaryExchange' : 'primary_exchange' ,
18- 'tradingClass' : 'trading_class' , 'lastTradingDate' : 'last_trading_date' ,
19- 'minTick' : 'min_tick' , 'firstNoticeDate' : 'first_notice_date' ,
20- 'lastBiddingCloseTime' : 'last_bidding_close_time' }
13+ CONTRACT_FIELD_MAPPINGS = {'conid' : 'contract_id' , 'right' : 'put_call' , 'tradeable' : 'trade' }
2114
2215
2316class ContractsResponse (TigerResponse ):
@@ -37,49 +30,9 @@ def parse_response_content(self, response_content):
3730 for item in data_json ['items' ]:
3831 contract_fields = {}
3932 for key , value in item .items ():
40- if value is None :
41- continue
42- tag = CONTRACT_FIELD_MAPPINGS [key ] if key in CONTRACT_FIELD_MAPPINGS else key
43- if tag in CONTRACT_FIELDS :
44- contract_fields [tag ] = value
45-
46- contract_id = contract_fields .get ('contract_id' )
47- symbol = contract_fields .get ('symbol' )
48- currency = contract_fields .get ('currency' )
49- sec_type = contract_fields .get ('sec_type' )
50- exchange = contract_fields .get ('exchange' )
51- origin_symbol = contract_fields .get ('origin_symbol' )
52- local_symbol = contract_fields .get ('local_symbol' )
53- expiry = contract_fields .get ('expiry' )
54- strike = contract_fields .get ('strike' )
55- put_call = contract_fields .get ('right' )
56- multiplier = contract_fields .get ('multiplier' )
57- name = contract_fields .get ('name' )
58- short_margin = contract_fields .get ('short_margin' )
59- short_fee_rate = contract_fields .get ('short_fee_rate' )
60- shortable = contract_fields .get ('shortable' )
61- long_initial_margin = contract_fields .get ('long_initial_margin' )
62- long_maintenance_margin = contract_fields .get ('long_maintenance_margin' )
63- contract_month = contract_fields .get ('contract_month' )
64- identifier = contract_fields .get ('identifier' )
65- primary_exchange = contract_fields .get ('primary_exchange' )
66- market = contract_fields .get ('market' )
67- min_tick = contract_fields .get ('min_tick' )
68- trading_class = contract_fields .get ('trading_class' )
69- status = contract_fields .get ('status' )
70- continuous = contract_fields .get ('continuous' )
71- trade = contract_fields .get ('trade' )
72- last_trading_date = contract_fields .get ('last_trading_date' )
73- first_notice_date = contract_fields .get ('first_notice_date' )
74- last_bidding_close_time = contract_fields .get ('last_bidding_close_time' )
75- contract = Contract (symbol , currency , contract_id = contract_id , sec_type = sec_type , exchange = exchange ,
76- origin_symbol = origin_symbol , local_symbol = local_symbol , expiry = expiry ,
77- strike = strike , put_call = put_call , multiplier = multiplier , name = name ,
78- short_margin = short_margin , short_fee_rate = short_fee_rate , shortable = shortable ,
79- long_initial_margin = long_initial_margin , contract_month = contract_month ,
80- long_maintenance_margin = long_maintenance_margin , identifier = identifier ,
81- primary_exchange = primary_exchange , market = market , min_tick = min_tick ,
82- trading_class = trading_class , status = status , continuous = continuous , trade = trade ,
83- last_trading_date = last_trading_date , first_notice_date = first_notice_date ,
84- last_bidding_close_time = last_bidding_close_time )
33+ tag = CONTRACT_FIELD_MAPPINGS [key ] if key in CONTRACT_FIELD_MAPPINGS else camel_to_underline (key )
34+ contract_fields [tag ] = value
35+ contract = Contract ()
36+ for k , v in contract_fields .items ():
37+ setattr (contract , k , v )
8538 self .contracts .append (contract )
0 commit comments