Skip to content

Instantly share code, notes, and snippets.

@kemadz
Last active June 2, 2024 17:46
Show Gist options
  • Save kemadz/6181198 to your computer and use it in GitHub Desktop.
Save kemadz/6181198 to your computer and use it in GitHub Desktop.
通达信,同花顺板块导出文件转换
#!/usr/bin/python
# -*- coding: utf-8 -*-
import struct
'''
同花顺自选股文件格式:
0000000: 0900 0721 3030 3039 3731 0711 3630 3036 ...!000971..6006
0000010: 3738 0721 3030 3231 3134 0721 3030 3232 78.!002114.!0022
0000020: 3336 0721 3330 3031 3034 0711 3630 3031 36.!300104..6001
0000030: 3133 0711 3630 3033 3638 0711 3630 3032 13..600368..6002
0000040: 3030 0711 3630 3030 3630 0a 00..600060.
0x00-0x0F 自选股个数,2 bytes, unsigned short,little-endian
0x10-0x17 '\x07'(分隔符)
0x18-0x1F 市场代码: '\x11'(上海) | '\x21'(深圳)
0x20-0x4F 证券代码, 6 bytes
0x50-0x57 '\x07'(分隔符)
0x58-0x5F 市场代码: '\x11'(上海) | '\x21'(深圳)
0x60-0x8F 证券代码, 6 bytes
......
'''
def main():
stocks = []
with open("tdx_in.ebk", 'rb') as filepath:
filepath.readline()
for line in filepath:
line = line.decode().strip()
if line[0] == '1':
stocks.append('\x07\x11' + line[1:])
else:
stocks.append('\x07\x21' + line[1:])
with open('ths_out.sel', 'wb') as filepath:
data = struct.pack('H', len(stocks)).decode() + ''.join(stocks)
filepath.write(data.encode())
if __name__ == '__main__':
main()
#!/usr/bin/python
# -*- coding: utf-8 -*-
import struct
'''
同花顺自选股文件格式:
0000000: 0900 0721 3030 3039 3731 0711 3630 3036 ...!000971..6006
0000010: 3738 0721 3030 3231 3134 0721 3030 3232 78.!002114.!0022
0000020: 3336 0721 3330 3031 3034 0711 3630 3031 36.!300104..6001
0000030: 3133 0711 3630 3033 3638 0711 3630 3032 13..600368..6002
0000040: 3030 0711 3630 3030 3630 0a 00..600060.
0x00-0x0F 自选股个数,2 bytes, unsigned short,little-endian
0x10-0x17 '\x07'(分隔符)
0x18-0x1F 市场代码: '\x11'(上海) | '\x21'(深圳)
0x20-0x4F 证券代码, 6 bytes
0x50-0x57 '\x07'(分隔符)
0x58-0x5F 市场代码: '\x11'(上海) | '\x21'(深圳)
0x60-0x8F 证券代码, 6 bytes
......
'''
def main():
stocks = []
with open("ths_in.sel", 'rb') as filepath:
cnt = struct.unpack('<H', filepath.read(2))[0]
for _ in range(cnt):
data = filepath.readline(8).decode()
exch = '1' if data[1] == '\x11' else '0'
code = data[2:]
stocks.append(exch+code)
with open('tdx_out.sel', 'wb') as filepath:
data = '\r\n' + '\r\n'.join(stocks)
filepath.write(data.encode())
if __name__ == '__main__':
main()
1600570
1600446
1600298
0002129
0002570
0002266
1600976
1600200
0002236
0002415
0300017
0300104
0300251
0300027
0000002
0000001
1600016
1600368
0300024
0000156
1600113
1600060
0000559
0002130
1601727
0000695
1600572
0002114
1600693
0000651
1600613
1600761
1600458
0002117
1600690
0002681
0002079
0300155
0002498
0002655
0002527
0002023
1601567
0000100
1600523
601398!002296601299601766601390!002010600795600216!000983600547600796600160600016!000001
@ruanyl
Copy link

ruanyl commented Nov 22, 2016

你好,请问sel文件格式是怎样的?能不能给个例子?谢谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment