Skip to content

Commit

Permalink
行情采集服务
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed Jan 29, 2021
1 parent b6f49a3 commit 94d7019
Show file tree
Hide file tree
Showing 10 changed files with 552 additions and 412 deletions.
104 changes: 49 additions & 55 deletions hikyuu/gui/HikyuuTDX.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
from hikyuu.gui.data.EscapetimeThread import EscapetimeThread
from hikyuu.gui.data.UseTdxImportToH5Thread import UseTdxImportToH5Thread
from hikyuu.gui.data.UsePytdxImportToH5Thread import UsePytdxImportToH5Thread
from hikyuu.gui.data.CollectThread import CollectThread
from hikyuu.gui.data.CollectToMemThread import CollectToMemThread
#from hikyuu.gui.data.CollectToMySQLThread import CollectToMySQLThread
#from hikyuu.gui.data.CollectToMemThread import CollectToMemThread
from hikyuu.gui.data.CollectSpotThread import CollectSpotThread

from hikyuu.data import hku_config_template
from hikyuu.util.mylog import add_class_logger_handler, class_logger, hku_logger
Expand Down Expand Up @@ -49,12 +50,14 @@ def __init__(self, parent=None, capture_output=False, use_dark_style=False):
self.initThreads()

def closeEvent(self, event):
self.stop_collect()
if self.import_running:
QMessageBox.about(self, '提示', '正在执行导入任务,请耐心等候!')
event.ignore()
return

if self.collect_spot_thread is not None and self.collect_spot_thread.isRunning():
self.collect_spot_thread.terminate()

self.saveConfig()

if self.hdf5_import_thread:
Expand Down Expand Up @@ -203,10 +206,14 @@ def initLogger(self):
)
con.setFormatter(FORMAT)
add_class_logger_handler(
con, [
MyMainWindow, CollectThread, CollectToMemThread, UsePytdxImportToH5Thread,
con,
[
MyMainWindow,
CollectSpotThread, #CollectToMySQLThread, CollectToMemThread,
UsePytdxImportToH5Thread,
UseTdxImportToH5Thread
], logging.INFO
],
logging.INFO
)
hku_logger.addHandler(con)
Expand Down Expand Up @@ -234,8 +241,6 @@ def initUI(self):
self.time_start_dateEdit.setDate(today - datetime.timedelta(7))
self.trans_start_dateEdit.setMinimumDate(today - datetime.timedelta(90))
self.time_start_dateEdit.setMinimumDate(today - datetime.timedelta(300))
self.collect_running = False
self.collect_status_Label.setText("未启动")
#读取保存的配置文件信息,如果不存在,则使用默认配置
this_dir = self.getUserConfigDir()
Expand Down Expand Up @@ -332,12 +337,22 @@ def initUI(self):
self.collect_use_zhima_checkBox.setChecked(use_zhima_proxy)
self.collect_phase1_start_timeEdit.setTime(
datetime.time.fromisoformat(
import_config.get('collect', 'phase1_start', fallback='09:05')
import_config.get('collect', 'phase1_start', fallback='09:00')
)
)
self.collect_phase1_last_timeEdit.setTime(
datetime.time.fromisoformat(
import_config.get('collect', 'phase1_end', fallback='15:15')
import_config.get('collect', 'phase1_end', fallback='12:05')
)
)
self.collect_phase2_start_timeEdit.setTime(
datetime.time.fromisoformat(
import_config.get('collect', 'phase2_start', fallback='13:00')
)
)
self.collect_phase2_last_timeEdit.setTime(
datetime.time.fromisoformat(
import_config.get('collect', 'phase2_end', fallback='15:05')
)
)
Expand Down Expand Up @@ -455,7 +470,9 @@ def getCurrentConfig(self):
'interval': self.collect_sample_spinBox.value(),
'use_zhima_proxy': self.collect_use_zhima_checkBox.isChecked(),
'phase1_start': self.collect_phase1_start_timeEdit.time().toString(),
'phase1_end': self.collect_phase1_last_timeEdit.time().toString()
'phase1_end': self.collect_phase1_last_timeEdit.time().toString(),
'phase2_start': self.collect_phase2_start_timeEdit.time().toString(),
'phase2_end': self.collect_phase2_last_timeEdit.time().toString(),
}
import_config['preload'] = {
'day': self.preload_day_checkBox.isChecked(),
Expand Down Expand Up @@ -487,8 +504,7 @@ def initThreads(self):
self.escape_time_thread = None
self.hdf5_import_thread = None
self.mysql_import_thread = None
self.collect_sh_thread = None
self.collect_sz_thread = None
self.collect_spot_thread = None
self.import_running = False
self.hdf5_import_progress_bar = {
Expand Down Expand Up @@ -705,55 +721,33 @@ def on_start_import_pushButton_clicked(self):
self.escape_time_thread.message.connect(self.on_message_from_thread)
self.escape_time_thread.start()
def start_collect(self):
self.collect_sh_thread = CollectThread(
self.getCurrentConfig(), self.getHikyuuConfigFileName(), 'SH'
)
self.collect_sh_thread.start()
self.collect_sz_thread = CollectThread(
self.getCurrentConfig(), self.getHikyuuConfigFileName(), 'SZ'
)
self.collect_sz_thread.start()
def stop_collect(self):
self.logger.info("终止采集!")
if self.collect_sh_thread is not None:
self.collect_sh_thread.stop()
if self.collect_sz_thread is not None:
self.collect_sz_thread.stop()
@pyqtSlot()
def on_collect_start_pushButton_clicked(self):
if self.collect_running:
self.collect_start_pushButton.setEnabled(False)
self.collect_status_Label.setText("正在停止...")
QApplication.processEvents()
self.stop_collect()
self.collect_start_pushButton.setText("启动定时采集")
self.collect_running = False
self.collect_status_Label.setText("已停止")
self.collect_start_pushButton.setEnabled(True)
else:
config = self.getCurrentConfig()
#if not config.getboolean("mysql", "enable", fallback=False):
# QMessageBox.critical(self, "定时采集", "仅在存储设置为 MySQL 时支持定时采集!")
# return
self.collect_status_Label.setText("正在启动...")
self.collect_start_pushButton.setEnabled(False)
QApplication.processEvents()
self.start_collect()
self.collect_start_pushButton.setText("停止采集")
self.collect_running = True
self.collect_status_Label.setText("运行中...")
self.collect_start_pushButton.setEnabled(True)
if self.collect_spot_thread is None or self.collect_spot_thread.isFinished():
self.collect_spot_thread = CollectSpotThread(
self.getCurrentConfig(),
self.getHikyuuConfigFileName(),
)
self.collect_spot_thread.start()
QMessageBox.about(self, '', '已启动,请在控制台日志查看是否正常运行')
@pyqtSlot()
def on_collect_stop_pushButton_clicked(self):
if self.collect_spot_thread is not None and self.collect_spot_thread.isRunning():
self.collect_spot_thread.terminate()
self.collect_spot_thread.wait()
QMessageBox.about(self, '', '已停止')
class_logger(MyMainWindow)
def start():
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
app = QApplication(sys.argv)
myWin = MyMainWindow(None)
use_dark_style = False # 使用暗黑主题
myWin = MyMainWindow(capture_output=True, use_dark_style=use_dark_style)
myWin.show()
sys.exit(app.exec())
Expand All @@ -775,8 +769,8 @@ def start():
logging.StreamHandler(),
]
)
#myWin = MyMainWindow(capture_output=True, use_dark_style=use_dark_style)
myWin = MyMainWindow(capture_output=False, use_dark_style=use_dark_style)
myWin = MyMainWindow(capture_output=True, use_dark_style=use_dark_style)
#myWin = MyMainWindow(capture_output=False, use_dark_style=use_dark_style)
else:
myWin = MyMainWindow(capture_output=True, use_dark_style=use_dark_style)
Expand Down
42 changes: 42 additions & 0 deletions hikyuu/gui/data/CollectSpotThread.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/python
# -*- coding: utf8 -*-
# cp936

import logging
import time
import datetime
from math import ceil
from PyQt5.QtCore import QThread, QWaitCondition, QMutex

from hikyuu import Datetime, TimeDelta, hikyuu_init, StockManager, constant
from hikyuu.util import *
from hikyuu.fetcher.stock.zh_stock_a_sina_qq import get_spot, get_spot_parallel
from hikyuu.gui.spot_server import collect


class CollectSpotThread(QThread):
def __init__(self, config, hku_config_file):
super(self.__class__, self).__init__()
self.working = True
self._config = config
self.hku_config_file = hku_config_file
self._interval = config.getint('collect', 'interval', fallback=60 * 60)
self._phase1_start_time = config.get('collect', 'phase1_start', fallback='09:05')
self._phase1_end_time = config.get('collect', 'phase1_end', fallback='09:05')
self._phase2_start_time = config.get('collect', 'phase2_start', fallback='09:05')
self._phase2_end_time = config.get('collect', 'phase2_end', fallback='09:05')
self._use_zhima_proxy = config.getboolean('collect', 'use_zhima_proxy', fallback=False)

def __del__(self):
hku_info("Quit CollectSpotThread")

@hku_catch()
def run(self):
collect(
self._use_zhima_proxy, 'sina', self._interval,
'{}-{}'.format(self._phase1_start_time, self._phase1_end_time),
'{}-{}'.format(self._phase2_start_time, self._phase2_end_time), True
)
class_logger(CollectSpotThread)
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from hikyuu.fetcher.stock.zh_stock_a_sina_qq import get_spot, get_spot_parallel


class CollectThread(QThread):
class CollectToMySQLThread(QThread):
def __init__(self, config, market='SH'):
super(self.__class__, self).__init__()
self.working = True
Expand Down Expand Up @@ -179,4 +179,4 @@ def get_stock_list(self):
return ["{}{}".format(self.market.lower(), item[2]) for item in stk_list if item[3] == 1]
class_logger(CollectThread)
class_logger(CollectToMySQLThread)
Loading

0 comments on commit 94d7019

Please sign in to comment.