Skip to content

Commit

Permalink
add get_xdxr info get finance info
Browse files Browse the repository at this point in the history
  • Loading branch information
rainx committed Jun 21, 2017
1 parent 37e7ecd commit 031818c
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pytdx/hq.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from pytdx.parser.get_history_transaction_data import GetHistoryTransactionData
from pytdx.parser.get_company_info_category import GetCompanyInfoCategory
from pytdx.parser.get_company_info_content import GetCompanyInfoContent
from pytdx.parser.get_xdxr_info import GetXdXrInfo
from pytdx.parser.get_finance_info import GetFinanceInfo

from pytdx.params import TDXParams

Expand Down Expand Up @@ -128,6 +130,16 @@ def get_company_info_content(self, market, code, filename, start, length):
cmd.setParams(market, code, filename, start, length)
return cmd.call_api()

def get_xdxr_info(self, market, code):
cmd = GetXdXrInfo(self.client)
cmd.setParams(market, code)
return cmd.call_api()

def get_finance_info(self, market, code):
cmd = GetFinanceInfo(self.client)
cmd.setParams(market, code)
return cmd.call_api()

if __name__ == '__main__':
import pprint

Expand Down Expand Up @@ -165,6 +177,13 @@ def get_company_info_content(self, market, code, filename, start, length):
log.info("读取公司信息-最新提示")
data = api.get_company_info_content(0, '000001', '000001.txt', 2037337, 101)
pprint.pprint(data)
log.info("读取除权除息信息")
data = api.get_xdxr_info(1, '600300')
pprint.pprint(data)
log.info("读取财务信息")
data = api.get_finance_info(0, '000001')
pprint.pprint(data)

api.disconnect()


Expand Down
128 changes: 128 additions & 0 deletions pytdx/parser/get_finance_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
from pytdx.parser.base import BaseParser
from pytdx.helper import get_datetime, get_volume, get_price
from collections import OrderedDict
import struct


"""
b1cb74000c1f1876006f100091009100010000303030303031b884ce4912000100bcc6330103cf2f018999d149c0f92f48e0bab94a700dd24b000000000000000052b89e3ee52e334f00000000b0c5f74a6078904a203db54800000000000000009a65574c881d464d006dd34b00000000000000004019fb4a802b1549405cdbcc7157d7cc00000000e028fb4ae0a2bd4ae0a2bd4a3f87844c3d0a2f4100004040
市场 证券代码 流通股本 所属省份 所属行业 财务更新
日期 上市日期 总股本 国家股 发起人法人股 法人股 B股 H股
职工股 总资产 流动资产 固定资产 无形资产 股东人数
流动负债 长期负债 资本公积金 净资产 主营收入 主营利润
应收帐款 营业利润 投资收益 经营现金流 总现金流
存货 利润总额 税后利润 净利润 未分利润 保留
保留
0 000001 1691799.000000 18 1 20170428 19910403
1717041.125000 180199.000000 6086000.000000 27532000.000000 0.000000
0.000000 0.310000 3006194944.000000 0.000000 8119000.
000000 4734000.000000 371177.000000 0.000000 0.000000 56465000
.000000 207739008.000000 27712000.000000 0.000000 0.000000
8228000.000000 611000.000000 -115008000.000000 -112901000.000000
0.000000 8230000.000000 6214000.000000 6214000.000000 69483000.000000
10.940000 3.000000
"""

class GetFinanceInfo(BaseParser):

def setParams(self, market, code):
if type(code) is str:
code = code.encode("utf-8")
pkg = bytearray.fromhex(u'0c 1f 18 76 00 01 0b 00 0b 00 10 00 01 00')
pkg.extend(struct.pack(u"<B6s", market, code))
self.send_pkg = pkg

def parseResponse(self, body_buf):
pos = 0
pos += 2 #skip num ,we only query 1 in this case
market, code = struct.unpack(u"<B6s",body_buf[pos: pos+7])
pos += 7

(
liutongguben,
province,
industry,
updated_date,
ipo_date,
zongguben,
guojiagu,
faqirenfarengu,
farengu,
bgu,
hgu,
zhigonggu,
zongzichan,
liudongzichan,
gudingzichan,
wuxingzichan,
gudongrenshu,
liudongfuzhai,
changqifuzhai,
zibengongjijin,
jingzichan,
zhuyingshouru,
zhuyinglirun,
yingshouzhangkuan,
yingyelirun,
touzishouyu,
jingyingxianjinliu,
zongxianjinliu,
cunhuo,
lirunzonghe,
shuihoulirun,
jinglirun,
weifenlirun,
baoliu1,
baoliu2
) = struct.unpack("<IHHIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII", body_buf[pos:])

def _get_v(v):
if v == 0:
return 0
else:
return get_volume(v)

return OrderedDict(
[
("market", market),
("code", code),
("liutongguben", _get_v(liutongguben)),
('province', province),
('industry', industry),
('updated_date', updated_date),
('ipo_date', ipo_date),
("zongguben", _get_v(zongguben)),
("guojiagu", _get_v(guojiagu)),
("faqirenfarengu", _get_v(faqirenfarengu)),
("farengu", _get_v(farengu)),
("bgu", _get_v(bgu)),
("hgu", _get_v(hgu)),
("zhigonggu", _get_v(zhigonggu)),
("zongzichan", _get_v(zongzichan)),
("liudongzichan", _get_v(liudongzichan)),
("gudingzichan", _get_v(gudingzichan)),
("wuxingzichan", _get_v(wuxingzichan)),
("gudongrenshu", _get_v(gudongrenshu)),
("liudongfuzhai", _get_v(liudongfuzhai)),
("changqifuzhai", _get_v(changqifuzhai)),
("zibengongjijin", _get_v(zibengongjijin)),
("jingzichan", _get_v(jingzichan)),
("zhuyingshouru", _get_v(zhuyingshouru)),
("zhuyinglirun", _get_v(zhuyinglirun)),
("yingshouzhangkuan", _get_v(yingshouzhangkuan)),
("yingyelirun", _get_v(yingyelirun)),
("touzishouyu", _get_v(touzishouyu)),
("jingyingxianjinliu", _get_v(jingyingxianjinliu)),
("zongxianjinliu", _get_v(zongxianjinliu)),
("cunhuo", _get_v(cunhuo)),
("lirunzonghe", _get_v(lirunzonghe)),
("shuihoulirun", _get_v(shuihoulirun)),
("jinglirun", _get_v(jinglirun)),
("weifenlirun", _get_v(weifenlirun)),
("baoliu1", _get_v(baoliu1)),
("baoliu2", _get_v(baoliu2))
]
)
74 changes: 74 additions & 0 deletions pytdx/parser/get_xdxr_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from pytdx.parser.base import BaseParser
from pytdx.helper import get_datetime, get_volume, get_price, get_time
from collections import OrderedDict
import struct
"""
need to fix
get_volume ?
4098 ---> 3.0
2434.0062499046326 ---> 2.6
1218.0031249523163 ---> 2.3
"""

class GetXdXrInfo(BaseParser):

def setParams(self, market, code):
if type(code) is str:
code = code.encode("utf-8")
pkg = bytearray.fromhex(u'0c 1f 18 76 00 01 0b 00 0b 00 0f 00 01 00')
pkg.extend(struct.pack("<B6s", market, code))
self.send_pkg = pkg

def parseResponse(self, body_buf):
pos = 0

if len(body_buf) < 11:
return []

pos += 9 # skip 9
num = int.from_bytes(body_buf[pos: pos+2], 'little')
pos += 2

rows = []
for i in range(num):
market, code = struct.unpack(u"<B6s", body_buf[:7])
pos += 7
pos += 1 #skip a byte
year, month, day, hour, minite, pos = get_datetime(9, body_buf, pos)
pos += 1 #skip a byte



# b'\x00\xe8\x00G' => 33000.00000
# b'\x00\xc0\x0fF' => 9200.00000
# b'\x00@\x83E' => 4200.0000

cash_raw, peigu_price_raw, songgu_num_raw, peigu_percent_raw = struct.unpack("<IIII", body_buf[pos: pos + 16])
pos += 16

def _get_v(v):
if v == 0:
return 0
else:
return get_volume(v)


row = OrderedDict(
[
('year', year),
('month', month),
('day', day),
('cash', _get_v(cash_raw)),
('peigu_price', _get_v(peigu_price_raw)),
('songgu_num', _get_v(songgu_num_raw)),
('peigu_percent', _get_v(peigu_percent_raw)),
]
)
rows.append(row)

return rows

0 comments on commit 031818c

Please sign in to comment.