Example #1
0
    def run_downloading(self, data_source: str, full_symbol: str,
                        interval: str, start: datetime, end: datetime):
        """
        Query bar data from RQData.
        """

        self.write_log(f"{full_symbol}-{interval}开始从{data_source}下载历史数据")

        symbol, exchange = extract_full_symbol(full_symbol)

        req = HistoryRequest(symbol=symbol,
                             exchange=exchange,
                             interval=Interval(interval),
                             start=start,
                             end=end)

        if data_source == 'RQData':
            data = rqdata_client.query_history(req)

        elif data_source == 'Tushare':
            QtWidgets.QMessageBox().information(None, 'Info', '待实现!',
                                                QtWidgets.QMessageBox.Ok)
            data = None
        elif data_source == 'JoinQuant':
            QtWidgets.QMessageBox().information(None, 'Info', '待实现!',
                                                QtWidgets.QMessageBox.Ok)
            data = None
        if data:
            database_manager.save_bar_data(data)
            self.write_log(f"{full_symbol}-{interval}历史数据从{data_source}下载完成")
        else:
            self.write_log(f"无法从{data_source}获取{full_symbol}的历史数据")
        # Clear thread object handler.
        self.thread = None
Example #2
0
    def load_by_handle(self, f: TextIO):
        """
        load by text mode file handle
        """
        reader = csv.DictReader(f)

        bars = []
        start = None
        count = 0
        full_sym = generate_full_symbol(self.exchange, self.symbol)
        alreadyhas = bool(sqglobal.history_bar[full_sym])
        starttime = ttime()
        try:
            for item in reader:
                if self.datetime_format:
                    dt = datetime.strptime(item[self.datetime_head],
                                           self.datetime_format)
                else:
                    dt = datetime.fromisoformat(item[self.datetime_head])

                bar = BarData(
                    symbol=self.symbol,
                    exchange=self.exchange,
                    datetime=dt,
                    interval=self.interval,
                    volume=int(float(item[self.volume_head])),
                    open_interest=int(float(item[self.openinterest_head])),
                    open_price=float(item[self.open_head]),
                    high_price=float(item[self.high_head]),
                    low_price=float(item[self.low_head]),
                    close_price=float(item[self.close_head]),
                    gateway_name="DB",
                )

                bars.append(bar)

                # do some statistics
                count += 1
                if not start:
                    start = bar.datetime
                if count % 100000 == 0:
                    if self.saveto == 'DataBase':
                        database_manager.save_bar_data(bars)
                        bars.clear()
        except Exception as e:
            msg = "Load csv error: {0}".format(str(e.args[0]))
            self.finishmsg.emit(msg)
            return

        end = bar.datetime
        # insert into database

        if self.saveto == 'Memory' and not alreadyhas:
            sqglobal.history_bar[full_sym] = bars
        elif self.saveto == 'DataBase':
            database_manager.save_bar_data(bars)
        endtime = ttime()
        totalloadtime = int(endtime - starttime)
        if start and end and count:
            msg = f"\
                CSV载入Bar成功\n\
                代码:{self.symbol}\n\
                交易所:{self.exchange.value}\n\
                周期:{self.interval.value}\n\
                起始:{start}\n\
                结束:{end}\n\
                总数量:{count}\n\
                耗时:{totalloadtime}s\n\
                "

            self.finishmsg.emit(msg)