This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 936
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from rainx/master
update
- Loading branch information
Showing
9 changed files
with
280 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# coding=utf-8 | ||
|
||
from pytdx.parser.base import BaseParser | ||
from pytdx.helper import get_datetime, get_volume, get_price | ||
from collections import OrderedDict | ||
import struct | ||
|
||
|
||
class GetHistoryMinuteTimeData(BaseParser): | ||
|
||
def setParams(self, market, code, date): | ||
pkg = bytearray.fromhex("01 01 30 00 01 01 10 00 10 00 0c 24") | ||
code = code.encode("utf-8") | ||
pkg.extend(struct.pack("<IB9s", date, market, code)) | ||
#pkg = bytearray.fromhex('01 01 30 00 01 01 10 00 10 00 0c 24 3b c8 33 01 2f 49 46 4c 30 00 38 ec 2d 00') | ||
self.send_pkg = pkg | ||
|
||
def parseResponse(self, body_buf): | ||
# print('测试', body_buf) | ||
# fileobj = open("//Users//wy//data//a.bin", 'wb') # make partfile | ||
# fileobj.write(body_buf) # write data into partfile | ||
# fileobj.close() | ||
pos = 0 | ||
market, code, _, num = struct.unpack('<B9s8sH', body_buf[pos: pos + 20]) | ||
pos += 20 | ||
# print(market, code.decode(), num) | ||
result = [] | ||
for i in range(num): | ||
|
||
(raw_time, price, avg_price, volume, amount) = struct.unpack("<HffII", body_buf[pos: pos + 18]) | ||
|
||
pos += 18 | ||
hour = raw_time // 60 | ||
minute = raw_time % 60 | ||
|
||
result.append(OrderedDict([ | ||
("hour", hour), | ||
("minute", minute), | ||
("price", price), | ||
("avg_price", avg_price), | ||
("volume", volume), | ||
("open_interest", amount), | ||
])) | ||
|
||
return result | ||
|
||
|
||
if __name__ == '__main__': | ||
from pytdx.exhq import TdxExHq_API | ||
api = TdxExHq_API() | ||
cmd = GetHistoryMinuteTimeData(api) | ||
cmd.setParams(8, "10000843", 20180811) | ||
print(cmd.send_pkg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# coding=utf-8 | ||
|
||
from pytdx.parser.base import BaseParser | ||
from pytdx.helper import get_datetime, get_volume, get_price | ||
from collections import OrderedDict | ||
import struct | ||
|
||
|
||
class GetHistoryTransactionData(BaseParser): | ||
|
||
def setParams(self, market, code, date, start, count): | ||
# if type(code) is six.text_type: | ||
code = code.encode("utf-8") | ||
|
||
# if type(date) is (type(date) is six.text_type) or (type(date) is six.binary_type): | ||
# date = int(date) | ||
|
||
# pkg1 = bytearray.fromhex('01 01 30 00 02 01 16 00 16 00 06 24 3b c8 33 01 1f 30 30 30 32 30 00 00 00 01 00 00 00 00 f0 00') | ||
pkg = bytearray.fromhex('01 01 30 00 02 01 16 00 16 00 06 24') | ||
pkg.extend(struct.pack("<IB9siH", date, market, code, start, count)) | ||
self.send_pkg = pkg | ||
|
||
def parseResponse(self, body_buf): | ||
|
||
pos = 0 | ||
market, code, _, num = struct.unpack('<B9s4sH', body_buf[pos: pos + 16]) | ||
pos += 16 | ||
result = [] | ||
for i in range(num): | ||
|
||
(raw_time, price, volume, zengcang, nature) = struct.unpack("<HIIIH", body_buf[pos: pos + 16]) | ||
|
||
pos += 16 | ||
hour = raw_time // 60 | ||
minute = raw_time % 60 | ||
|
||
result.append(OrderedDict([ | ||
("hour", hour), | ||
("minute", minute), | ||
("price", price), | ||
("volume", volume), | ||
("zengcang", zengcang), | ||
("nature", nature), | ||
])) | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# coding=utf-8 | ||
|
||
from pytdx.parser.base import BaseParser | ||
from pytdx.helper import get_datetime, get_volume, get_price, get_time | ||
from collections import OrderedDict | ||
import struct | ||
import six | ||
|
||
|
||
class GetTransactionData(BaseParser): | ||
|
||
def setParams(self, market, code, start, count): | ||
if type(code) is six.text_type: | ||
code = code.encode("utf-8") | ||
pkg = bytearray.fromhex('01 01 08 00 03 01 12 00 12 00 fc 23') | ||
pkg.extend(struct.pack("<B9siH", market, code, start, count)) | ||
self.send_pkg = pkg | ||
|
||
def parseResponse(self, body_buf): | ||
|
||
pos = 0 | ||
market, code, _, num = struct.unpack('<B9s4sH', body_buf[pos: pos + 16]) | ||
pos += 16 | ||
result = [] | ||
for i in range(num): | ||
|
||
(raw_time, price, volume, zengcang, nature) = struct.unpack("<HIIIH", body_buf[pos: pos + 16]) | ||
|
||
pos += 16 | ||
hour = raw_time // 60 | ||
minute = raw_time % 60 | ||
|
||
result.append(OrderedDict([ | ||
("hour", hour), | ||
("minute", minute), | ||
("price", price), | ||
("volume", volume), | ||
("zengcang", zengcang), | ||
("nature", nature), | ||
])) | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
from pytdx.reader.daily_bar_reader import TdxDailyBarReader, TdxFileNotFoundException, TdxNotAssignVipdocPathException | ||
from pytdx.reader.min_bar_reader import TdxMinBarReader | ||
from pytdx.reader.lc_min_bar_reader import TdxLCMinBarReader | ||
from pytdx.reader.exhq_daily_bar_reader import TdxExHqDailyBarReader | ||
|
||
__all__ = [ | ||
'TdxDailyBarReader', | ||
'TdxFileNotFoundException', | ||
'TdxNotAssignVipdocPathException', | ||
'TdxMinBarReader', | ||
'TdxLCMinBarReader', | ||
'TdxExHqDailyBarReader', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#coding=utf-8 | ||
from __future__ import unicode_literals, division | ||
|
||
import pandas as pd | ||
import os | ||
|
||
import struct | ||
from pytdx.reader.base_reader import TdxFileNotFoundException, TdxNotAssignVipdocPathException | ||
from pytdx.reader.base_reader import BaseReader | ||
|
||
""" | ||
读取通达信数据 | ||
""" | ||
|
||
|
||
class TdxExHqDailyBarReader(BaseReader): | ||
|
||
def __init__(self, vipdoc_path=None): | ||
self.vipdoc_path = vipdoc_path | ||
|
||
def parse_data_by_file(self, fname): | ||
|
||
if not os.path.isfile(fname): | ||
raise TdxFileNotFoundException('no tdx kline data, pleaes check path %s', fname) | ||
|
||
with open(fname, 'rb') as f: | ||
content = f.read() | ||
return self.unpack_records('<IffffIIf', content) | ||
|
||
return [] | ||
|
||
|
||
def get_df(self, code_or_file): | ||
|
||
# 只传入了一个参数 | ||
data = [self._df_convert(row) for row in self.parse_data_by_file(code_or_file)] | ||
|
||
df = pd.DataFrame(data=data, columns=('date', 'open', 'high', 'low', 'close', 'amount', 'volume','jiesuan')) | ||
df.index = pd.to_datetime(df.date) | ||
return df[['open', 'high', 'low', 'close', 'amount', 'volume','jiesuan']] | ||
|
||
def _df_convert(self, row): | ||
t_date = str(row[0]) | ||
datestr = t_date[:4] + "-" + t_date[4:6] + "-" + t_date[6:] | ||
|
||
new_row = ( | ||
datestr, | ||
row[1], | ||
row[2], | ||
row[3], | ||
row[4], | ||
row[5], | ||
row[6], | ||
row[7] | ||
) | ||
|
||
return new_row | ||
|
||
|
||
if __name__ == '__main__': | ||
tdx_reader = TdxExHqDailyBarReader() | ||
try: | ||
print(tdx_reader.get_df("/Users/rainx/tmp/vipdoc/ds/29#A1801.day")) | ||
#print(tdx_reader.get_df("/Volumes/share/transfer/76#AG200.day")) | ||
|
||
except TdxFileNotFoundException as e: | ||
pass | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters