From 5cd911d9bd500dbb4f7589b47fd6cfa2b9fa8017 Mon Sep 17 00:00:00 2001 From: fasiondog Date: Sun, 7 Apr 2019 18:16:44 +0800 Subject: [PATCH] Current financial information download support --- hikyuu/data/pytdx_finance_to_sqlite.py | 22 +++---- hikyuu/data/sqlite_upgrade/0002.sql | 67 +++++++++++---------- hikyuu/gui/HikyuuTDX.py | 6 +- hikyuu/gui/data/ImportWeightToSqliteTask.py | 28 +++++++-- 4 files changed, 73 insertions(+), 50 deletions(-) diff --git a/hikyuu/data/pytdx_finance_to_sqlite.py b/hikyuu/data/pytdx_finance_to_sqlite.py index 53caf1305..c01aaff79 100644 --- a/hikyuu/data/pytdx_finance_to_sqlite.py +++ b/hikyuu/data/pytdx_finance_to_sqlite.py @@ -23,7 +23,7 @@ # SOFTWARE. from hikyuu.data.common import MARKETID, STOCKTYPE -from hikyuu.data.common_sqlite3 import get_marketid +from hikyuu.data.common_sqlite3 import get_marketid, create_database def pytdx_import_finance(db_connect, pytdx_connect, market): @@ -34,21 +34,21 @@ def pytdx_import_finance(db_connect, pytdx_connect, market): cur = db_connect.cursor() all_list = cur.execute(sql).fetchall() - connect.commit() + db_connect.commit() records = [] for stk in all_list: x = pytdx_connect.get_finance_info(1 if stk[1] == MARKETID.SH else 0, stk[2]) - #print(stk[2], x['code']) + #print(stk[2]) if x is not None and x['code'] == stk[2]: cur.execute("select updated_date from stkfinance where stockid={} and updated_date={}".format(stk[0], x['updated_date'])) a = cur.fetchall() a = [x[0] for x in a] if a: - print(a) + #print(a) continue - else: - print(market, stk[2]) + #else: + # print(market, stk[2]) records.append((stk[0], x['updated_date'], x['ipo_date'], @@ -115,7 +115,7 @@ def pytdx_import_finance(db_connect, pytdx_connect, market): zhuyinglirun, \ yingshouzhangkuan, \ yingyelirun, \ - touzishouru, \ + touzishouyu, \ jingyingxianjinliu, \ zongxianjinliu, \ cunhuo, \ @@ -130,7 +130,7 @@ def pytdx_import_finance(db_connect, pytdx_connect, market): ?,?,?,?,?,?,?,?,?,?, \ ?,?,?,?,?,?)", records) - connect.commit() + db_connect.commit() cur.close() return len(records) @@ -143,18 +143,18 @@ def pytdx_import_finance(db_connect, pytdx_connect, market): starttime = time.time() - dest_dir = "d:\\stock" + dest_dir = "c:\\stock" tdx_server = '119.147.212.81' tdx_port = 7709 connect = sqlite3.connect(dest_dir + "\\stock.db") - #create_database(connect) + create_database(connect) from pytdx.hq import TdxHq_API, TDXParams api = TdxHq_API() api.connect(tdx_server, tdx_port) - x = pytdx_import_finance(connect, api, "SH") + x = pytdx_import_finance(connect, api, "SZ") print(x) api.disconnect() diff --git a/hikyuu/data/sqlite_upgrade/0002.sql b/hikyuu/data/sqlite_upgrade/0002.sql index 510028f20..5a02c0bcc 100644 --- a/hikyuu/data/sqlite_upgrade/0002.sql +++ b/hikyuu/data/sqlite_upgrade/0002.sql @@ -1,43 +1,48 @@ +BEGIN TRANSACTION; CREATE TABLE `stkfinance` ( id INTEGER NOT NULL, stockid INTEGER, `updated_date` INTEGER, - `ipo_date` INTEGER, + `ipo_date` INTEGER, --42.上市日期 `province` INTEGER, `industry` INTEGER, - `zongguben` NUMERIC, - `liutongguben` NUMERIC, - `guojiagu` NUMERIC, - `faqirenfarengu` NUMERIC, - `farengu` NUMERIC, - `bgu` NUMERIC, - `hgu` NUMERIC, - `zhigonggu` NUMERIC, - `zongzichan` NUMERIC, - `liudongzichan` NUMERIC, - `gudingzichan` NUMERIC, - `wuxingzichan` NUMERIC, - `gudongrenshu` NUMERIC, - `liudongfuzhai` NUMERIC, - `changqifuzhai` NUMERIC, - `zibengongjijin` NUMERIC, - `jingzichan` NUMERIC, - `zhuyingshouru` NUMERIC, - `zhuyinglirun` NUMERIC, - `yingshouzhangkuan` NUMERIC, - `yingyelirun` NUMERIC, - `touzishouru` NUMERIC, - `jingyingxianjinliu` NUMERIC, - `zongxianjinliu` NUMERIC, - `cunhuo` NUMERIC, - `lirunzonghe` NUMERIC, - `shuihoulirun` NUMERIC, - `jinglirun` NUMERIC, - `weifenpeilirun` NUMERIC, - `meigujingzichan` NUMERIC, + `zongguben` NUMERIC, --1.总股本(股) + `liutongguben` NUMERIC, --7.流通A股(股) + `guojiagu` NUMERIC, --2.国家股(股) + `faqirenfarengu` NUMERIC, --3.发起人法人股(股) + `farengu` NUMERIC, --4.法人股(股) + `bgu` NUMERIC, --5.B股(股) + `hgu` NUMERIC, --6.H股(股) + `zhigonggu` NUMERIC, --8.职工股(股) + `zongzichan` NUMERIC, --10.总资产(元) + `liudongzichan` NUMERIC, --11.流动资产(元) + `gudingzichan` NUMERIC, --12.固定资产(元) + `wuxingzichan` NUMERIC, --13.无形资产(元) + `gudongrenshu` NUMERIC, --股东人数 + `liudongfuzhai` NUMERIC, --15.流动负债 + `changqifuzhai` NUMERIC, --16.长期负债 + `zibengongjijin` NUMERIC, --17.资本公积金 + `jingzichan` NUMERIC, --净资产(元) + `zhuyingshouru` NUMERIC, --20.主营收入 + `zhuyinglirun` NUMERIC, --21.主营利润 + `yingshouzhangkuan` NUMERIC, --应收账款 + `yingyelirun` NUMERIC, --23.营业利润 + `touzishouyu` NUMERIC, --投资收益 + `jingyingxianjinliu` NUMERIC, --经营现金流 + `zongxianjinliu` NUMERIC, --总现金流 + `cunhuo` NUMERIC, --存货 + `lirunzonghe` NUMERIC, --28.利润总额 + `shuihoulirun` NUMERIC, --29.税后利润 + `jinglirun` NUMERIC, --30.净利润 + `weifenpeilirun` NUMERIC, --31.未分配利润 + `meigujingzichan` NUMERIC, --34.每股净资产 `baoliu2` NUMERIC, PRIMARY KEY (id), FOREIGN KEY(stockid) REFERENCES "Stock" (stockid) ); +CREATE INDEX "ix_stkfinance_date" ON "stkfinance" (updated_date); +CREATE INDEX "ix_stkfinance_stock_date" ON "stkfinance" (stockid, updated_date); +COMMIT; + UPDATE `version` set `version` = 2; \ No newline at end of file diff --git a/hikyuu/gui/HikyuuTDX.py b/hikyuu/gui/HikyuuTDX.py index 9f2055348..ae3d833c7 100644 --- a/hikyuu/gui/HikyuuTDX.py +++ b/hikyuu/gui/HikyuuTDX.py @@ -272,8 +272,10 @@ def on_message_from_thread(self, msg): elif msg_task_name == 'IMPORT_WEIGHT': self.hdf5_weight_label.setText(msg[2]) - if msg[2] == '导入完成!': - self.import_detail_textEdit.append('导入权息记录数:{}'.format(msg[3])) + if msg[2] == '导入钱龙权息数据完毕!': + self.import_detail_textEdit.append('导入钱龙权息记录数:{}'.format(msg[3])) + elif msg[2] == '导入通达信权息信息完毕!': + self.import_detail_textEdit.append('导入通达信权息记录数:{}'.format(msg[3])) elif msg_task_name == 'IMPORT_FINANCE': if msg[2] != 'FINISHED': diff --git a/hikyuu/gui/data/ImportWeightToSqliteTask.py b/hikyuu/gui/data/ImportWeightToSqliteTask.py index bef3c67fe..72c57e9b4 100644 --- a/hikyuu/gui/data/ImportWeightToSqliteTask.py +++ b/hikyuu/gui/data/ImportWeightToSqliteTask.py @@ -27,7 +27,10 @@ import sqlite3 import urllib.request +from pytdx.hq import TdxHq_API +from hikyuu.data.common_pytdx import search_best_tdx from hikyuu.data.weight_to_sqlite import qianlong_import_weight +from hikyuu.data.pytdx_finance_to_sqlite import pytdx_import_finance class ImportWeightToSqliteTask: def __init__(self, queue, sqlitefile, dest_dir): @@ -51,11 +54,11 @@ def __call__(self): if not os.path.lexists(download_dir): os.makedirs(download_dir) - self.queue.put([self.msg_name, '正在下载...', 0, 0, 0]) + self.queue.put([self.msg_name, '正在下载钱龙权限信息...', 0, 0, 0]) net_file = urllib.request.urlopen('http://www.qianlong.com.cn/download/history/weight.rar', timeout=60) buffer = net_file.read() - self.queue.put([self.msg_name, '下载完成,正在校验是否存在更新...', 0, 0, 0]) + self.queue.put([self.msg_name, '钱龙权限信息下载完成,正在校验是否存在更新...', 0, 0, 0]) new_md5 = hashlib.md5(buffer).hexdigest() dest_filename = download_dir + '/weight.rar' @@ -69,18 +72,31 @@ def __call__(self): with open(dest_filename, 'wb') as file: file.write(buffer) - self.queue.put([self.msg_name, '下载完成,正在解压...', 0, 0, 0]) + self.queue.put([self.msg_name, '正在解压钱龙权限信息...', 0, 0, 0]) x = os.system('unrar x -o+ -inul {} {}'.format(dest_filename, download_dir)) if x != 0: raise Exception("无法找到unrar命令!") - self.queue.put([self.msg_name, '解压完毕,正在导入权息数据...', 0, 0, 0]) + self.queue.put([self.msg_name, '正在导入钱龙权息数据...', 0, 0, 0]) total_count = qianlong_import_weight(connect, download_dir + '/weight', 'SH') total_count += qianlong_import_weight(connect, download_dir + '/weight', 'SZ') - self.queue.put([self.msg_name, '导入完成!', 0, 0, total_count]) + self.queue.put([self.msg_name, '导入钱龙权息数据完毕!', 0, 0, total_count]) else: - self.queue.put([self.msg_name, 'INFO', '权息数据无变化', 0, 0]) + self.queue.put([self.msg_name, '钱龙权息数据无变化', 0, 0, 0]) + + hosts = search_best_tdx() + api = TdxHq_API() + api.connect(hosts[0][2], hosts[0][3]) + + self.queue.put([self.msg_name, '下载通达信权限信息(上证)...', 0, 0, 0]) + x = pytdx_import_finance(connect, api, "SH") + + self.queue.put([self.msg_name, '下载通达信权限信息(深证)...', 0, 0, 0]) + x += pytdx_import_finance(connect, api, "SZ") + self.queue.put([self.msg_name, '导入通达信权息信息完毕!', 0, 0, x]) + + api.disconnect() except Exception as e: #self.queue.put([self.msg_name, str(e), -1, 0, total_count])