|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Created on 2018/10/31 |
| 4 | +
|
| 5 | +@author: gaoan |
| 6 | +""" |
| 7 | +import json |
| 8 | + |
| 9 | +import six |
| 10 | +import pandas as pd |
| 11 | +from tigeropen.common.util.string_utils import get_string |
| 12 | +from tigeropen.common.response import TigerResponse |
| 13 | + |
| 14 | +# 盘前盘后 |
| 15 | +HOUR_TRADING_COLUMNS = [ |
| 16 | + 'hour_trading_tag', |
| 17 | + 'hour_trading_latest_price', |
| 18 | + 'hour_trading_pre_close', |
| 19 | + 'hour_trading_latest_time', |
| 20 | + 'hour_trading_volume', |
| 21 | + 'hour_trading_timestamp', |
| 22 | +] |
| 23 | +# 下一交易时段信息 |
| 24 | +NEXT_MARKET_STATUS_COLUMNS = [ |
| 25 | + # 市场状态(US:开盘、收盘、盘前交易、盘后交易||CN/HK:开盘、收盘、午间休市) |
| 26 | + 'next_market_status_tag', |
| 27 | + # 开始时间 |
| 28 | + 'next_market_status_begin_time', |
| 29 | +] |
| 30 | +# 拆合股 |
| 31 | +STOCK_SPLIT_COLUMNS = [ |
| 32 | + # 执行日期 yyyy-MM-dd格式 |
| 33 | + 'stock_split_execute_date', |
| 34 | + # 公司行动后的因子 |
| 35 | + 'stock_split_to_factor', |
| 36 | + # 公司行动前的因子 |
| 37 | + 'stock_split_for_factor' |
| 38 | +] |
| 39 | +# 股权信息 |
| 40 | +STOCK_RIGHT_COLUMNS = [ |
| 41 | + # 原股权代码 |
| 42 | + 'stock_right_symbol', |
| 43 | + # 股权代码 |
| 44 | + 'stock_right_rights_symbol', |
| 45 | + # 开始交易日期, YYYY-MM-dd格式,可能为空字符串 |
| 46 | + 'stock_right_first_dealing_date', |
| 47 | + # 最后交易日期, YYYY-MM-dd格式,可能为空字符串 |
| 48 | + 'stock_right_last_dealing_date' |
| 49 | +] |
| 50 | +# 股票代码变更 |
| 51 | +SYMBOL_CHANGE_COLUMNS = [ |
| 52 | + # 新的股票代码 |
| 53 | + 'symbol_change_new_symbol', |
| 54 | + # 执行日期,yyyy-MM-dd格式 |
| 55 | + 'symbol_change_execute_date' |
| 56 | +] |
| 57 | +# 股票公告 |
| 58 | +STOCK_NOTICE_COLUMNS = [ |
| 59 | + # 公告标题 |
| 60 | + 'stock_notice_title', |
| 61 | + # 公告内容 |
| 62 | + 'stock_notice_content', |
| 63 | + # 公告类型 |
| 64 | + 'stock_notice_type' |
| 65 | +] |
| 66 | + |
| 67 | +COLUMNS = ['symbol', 'market', 'exchange', 'sec_type', 'name', 'shortable', 'latest_price', 'pre_close', |
| 68 | + 'adj_pre_close', 'trading_status', 'market_status', 'timestamp', 'latest_time', |
| 69 | + 'open', 'high', 'low', 'volume', 'amount', 'ask_price', 'ask_size', 'bid_price', 'bid_size', 'change', |
| 70 | + 'amplitude', 'halted', 'delay', 'float_shares', 'shares', 'eps', 'etf', 'listing_date', 'adr_rate' |
| 71 | + ] + HOUR_TRADING_COLUMNS + NEXT_MARKET_STATUS_COLUMNS + STOCK_SPLIT_COLUMNS + STOCK_RIGHT_COLUMNS \ |
| 72 | + + SYMBOL_CHANGE_COLUMNS + STOCK_NOTICE_COLUMNS |
| 73 | + |
| 74 | +DETAIL_FIELD_MAPPINGS = {'secType': 'sec_type', 'latestPrice': 'latest_price', 'preClose': 'pre_close', |
| 75 | + 'floatShares': 'float_shares', 'marketStatus': 'market_status', 'latestTime': 'latest_time', |
| 76 | + 'askPrice': 'ask_price', 'askSize': 'ask_size', 'bidPrice': 'bid_price', 'bidSize': 'bid_size', |
| 77 | + 'tradingStatus': 'trading_status', 'adjPreClose': 'adj_pre_close', 'adrRate': 'adr_rate', |
| 78 | + 'listingDate': 'listing_date', 'beginTime': 'begin_time', |
| 79 | + 'nextMarketStatus': 'next_market_status', 'hourTrading': 'hour_trading', |
| 80 | + 'stockSplit': 'stock_split', 'stockRight': 'stock_right', 'symbolChange': 'symbol_change', |
| 81 | + 'executeDate': 'execute_date', 'newSymbol': 'new_symbol', 'forFactor': 'for_factor', |
| 82 | + 'toFactor': 'to_factor', 'rightsSymbol': 'rights_symbol', 'stockNotice': 'stock_notice', |
| 83 | + 'firstDealingDate': 'first_dealing_date', 'lastDealingDate': 'last_dealing_date' |
| 84 | + } |
| 85 | + |
| 86 | +SUB_FIELDS = { |
| 87 | + # 下一交易时段信息 |
| 88 | + 'next_market_status', |
| 89 | + # 盘前盘后信息 |
| 90 | + 'hour_trading', |
| 91 | + # 拆合股 |
| 92 | + 'stock_split', |
| 93 | + # 股权信息 |
| 94 | + 'stock_right' |
| 95 | + # 股票代码变更 |
| 96 | + 'symbol_change', |
| 97 | + # 公告 |
| 98 | + 'stock_notice' |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +class StockDetailsResponse(TigerResponse): |
| 103 | + def __init__(self): |
| 104 | + super(StockDetailsResponse, self).__init__() |
| 105 | + self.details = None |
| 106 | + self._is_success = None |
| 107 | + |
| 108 | + def parse_response_content(self, response_content): |
| 109 | + response = super(StockDetailsResponse, self).parse_response_content(response_content) |
| 110 | + if 'is_success' in response: |
| 111 | + self._is_success = response['is_success'] |
| 112 | + if not self.data: |
| 113 | + return |
| 114 | + detail_data = [] |
| 115 | + data_json = json.loads(self.data) |
| 116 | + for item in data_json.get('items', {}): |
| 117 | + item_values = dict() |
| 118 | + for key, value in item.items(): |
| 119 | + if value is None: |
| 120 | + continue |
| 121 | + if isinstance(value, six.string_types): |
| 122 | + value = get_string(value) |
| 123 | + tag = self._key_to_tag(key) |
| 124 | + if tag in SUB_FIELDS: |
| 125 | + for sub_k, sub_v in value.items(): |
| 126 | + sub_tag = self._key_to_tag(sub_k) |
| 127 | + item_values[self._join_tag(tag, sub_tag)] = sub_v |
| 128 | + else: |
| 129 | + item_values[tag] = value |
| 130 | + |
| 131 | + detail_data.append([item_values.get(tag) for tag in COLUMNS]) |
| 132 | + |
| 133 | + self.details = pd.DataFrame(detail_data, columns=COLUMNS) |
| 134 | + |
| 135 | + @classmethod |
| 136 | + def _key_to_tag(cls, k): |
| 137 | + return DETAIL_FIELD_MAPPINGS[k] if k in DETAIL_FIELD_MAPPINGS else k |
| 138 | + |
| 139 | + @classmethod |
| 140 | + def _join_tag(cls, tag, sub_tag): |
| 141 | + return '_'.join((tag, sub_tag)) |
0 commit comments