def start_downloading(self): """""" vt_symbol = self.symbol_line.text() interval = self.interval_combo.currentText() start_date = self.start_date_edit.date() end_date = self.end_date_edit.date() start = datetime( start_date.year(), start_date.month(), start_date.day(), ) start = DB_TZ.localize(start) end = datetime( end_date.year(), end_date.month(), end_date.day(), 23, 59, 59, ) end = DB_TZ.localize(end) self.backtester_engine.start_downloading( vt_symbol, interval, start, end )
def load_tick_data( self, symbol: str, exchange: Exchange, start: datetime, end: datetime ) -> List[TickData]: """""" s: ModelSelect = ( DbTickData.select().where( (DbTickData.symbol == symbol) & (DbTickData.exchange == exchange.value) & (DbTickData.datetime >= start) & (DbTickData.datetime <= end) ).order_by(DbTickData.datetime) ) vt_symbol = f"{symbol}.{exchange.value}" ticks: List[TickData] = [] for db_tick in s: db_tick.datetime = DB_TZ.localize(db_tick.datetime) db_tick.exchange = Exchange(db_tick.exchange) db_tick.gateway_name = "DB" db_tick.vt_symbol = vt_symbol ticks.append(db_tick) return ticks
def load_tick_data( self, symbol: str, exchange: Exchange, start: datetime, end: datetime ) -> List[TickData]: """""" s: QuerySet = DbTickData.objects( symbol=symbol, exchange=exchange.value, datetime__gte=convert_tz(start), datetime__lte=convert_tz(end), ) vt_symbol = f"{symbol}.{exchange.value}" ticks: List[TickData] = [] for db_tick in s: db_tick.datetime = DB_TZ.localize(db_tick.datetime) db_tick.exchange = Exchange(db_tick.exchange) db_tick.gateway_name = "DB" db_tick.vt_symbol = vt_symbol ticks.append(db_tick) return ticks
def load_bar_data( self, symbol: str, exchange: Exchange, interval: Interval, start: datetime, end: datetime ) -> List[BarData]: """""" s: ModelSelect = ( DbBarData.select().where( (DbBarData.symbol == symbol) & (DbBarData.exchange == exchange.value) & (DbBarData.interval == interval.value) & (DbBarData.datetime >= start) & (DbBarData.datetime <= end) ).order_by(DbBarData.datetime) ) vt_symbol = f"{symbol}.{exchange.value}" bars: List[BarData] = [] for db_bar in s: db_bar.datetime = DB_TZ.localize(db_bar.datetime) db_bar.exchange = Exchange(db_bar.exchange) db_bar.interval = Interval(db_bar.interval) db_bar.gateway_name = "DB" db_bar.vt_symbol = vt_symbol bars.append(db_bar) return bars
def load_bar_data( self, symbol: str, exchange: Exchange, interval: Interval, start: datetime, end: datetime ) -> List[BarData]: """""" s: QuerySet = DbBarData.objects( symbol=symbol, exchange=exchange.value, interval=interval.value, datetime__gte=convert_tz(start), datetime__lte=convert_tz(end), ) vt_symbol = f"{symbol}.{exchange.value}" bars: List[BarData] = [] for db_bar in s: db_bar.datetime = DB_TZ.localize(db_bar.datetime) db_bar.exchange = Exchange(db_bar.exchange) db_bar.interval = Interval(db_bar.interval) db_bar.gateway_name = "DB" db_bar.vt_symbol = vt_symbol bars.append(db_bar) return bars
def load_tick_data( self, symbol: str, exchange: Exchange, start: datetime, end: datetime ) -> List[TickData]: """加载TICK数据""" s = self.db.query(DbTickData).filter( DbTickData.symbol == symbol, DbTickData.exchange == exchange.value, DbTickData.datetime >= start, DbTickData.datetime <= end ).order_by(DbTickData.datetime).all() ticks: List[TickData] = [] for db_tick in s: data = TickData( symbol=db_tick.symbol, exchange=Exchange(db_tick.exchange), datetime=DB_TZ.localize(db_tick.datetime), volume=db_tick.volume, open_interest=db_tick.open_interest, last_price=db_tick.last_price, last_volume=db_tick.last_volume, limit_up=db_tick.limit_up, limit_down=db_tick.limit_down, open_price=db_tick.open_price, high_price=db_tick.high_price, low_price=db_tick.low_price, pre_close=db_tick.pre_close, bid_price_1=db_tick.bid_price_1, bid_price_2=db_tick.bid_price_2, bid_price_3=db_tick.bid_price_3, bid_price_4=db_tick.bid_price_4, bid_price_5=db_tick.bid_price_5, ask_price_1=db_tick.ask_price_1, ask_price_2=db_tick.ask_price_2, ask_price_3=db_tick.ask_price_3, ask_price_4=db_tick.ask_price_4, ask_price_5=db_tick.ask_price_5, bid_volume_1=db_tick.bid_volume_1, bid_volume_2=db_tick.bid_volume_2, bid_volume_3=db_tick.bid_volume_3, bid_volume_4=db_tick.bid_volume_4, bid_volume_5=db_tick.bid_volume_5, ask_volume_1=db_tick.ask_volume_1, ask_volume_2=db_tick.ask_volume_2, ask_volume_3=db_tick.ask_volume_3, ask_volume_4=db_tick.ask_volume_4, ask_volume_5=db_tick.ask_volume_5, gateway_name="DB" ) ticks.append(data) return ticks
def download(self): """""" symbol = self.symbol_edit.text() exchange = Exchange(self.exchange_combo.currentData()) interval = Interval(self.interval_combo.currentData()) start_date = self.start_date_edit.date() start = datetime(start_date.year(), start_date.month(), start_date.day()) start = DB_TZ.localize(start) if interval == Interval.TICK: count = self.engine.download_tick_data(symbol, exchange, start) else: count = self.engine.download_bar_data(symbol, exchange, interval, start) QtWidgets.QMessageBox.information(self, "下载结束", f"下载总数据量:{count}条")
def load_bar_data( self, symbol: str, exchange: Exchange, interval: Interval, start: datetime, end: datetime ) -> List[BarData]: """""" query = ( "select * from bar_data" " where vt_symbol=$vt_symbol" " and interval=$interval" f" and time >= '{start.date().isoformat()}'" f" and time <= '{end.date().isoformat()}';" ) bind_params = { "vt_symbol": generate_vt_symbol(symbol, exchange), "interval": interval.value } result = self.client.query(query, bind_params=bind_params) points = result.get_points() bars: List[BarData] = [] for d in points: dt = datetime.strptime(d["time"], "%Y-%m-%dT%H:%M:%SZ") bar = BarData( symbol=symbol, exchange=exchange, interval=interval, datetime=DB_TZ.localize(dt), open_price=d["open_price"], high_price=d["high_price"], low_price=d["low_price"], close_price=d["close_price"], volume=d["volume"], open_interest=d["open_interest"], gateway_name="DB" ) bars.append(bar) return bars
def load_bar_data( self, symbol: str, exchange: Exchange, interval: Interval, start: datetime, end: datetime ) -> List[BarData]: """""" s = self.db.query(DbBarData).filter( DbBarData.symbol == symbol, DbBarData.exchange == exchange.value, DbBarData.interval == interval.value, DbBarData.datetime >= start, DbBarData.datetime <= end ).order_by(DbBarData.datetime).all() bars: List[BarData] = [] for db_bar in s: data = BarData( symbol=db_bar.symbol, exchange=Exchange(db_bar.exchange), datetime=DB_TZ.localize(db_bar.datetime), interval=Interval(db_bar.interval), volume=db_bar.volume, open_interest=db_bar.open_interest, open_price=db_bar.open_price, high_price=db_bar.high_price, low_price=db_bar.low_price, close_price=db_bar.close_price, gateway_name="DB" ) bars.append(data) return bars
def load_tick_data( self, symbol: str, exchange: Exchange, start: datetime, end: datetime ) -> List[TickData]: """""" query = ( "select * from tick_data" " where vt_symbol=$vt_symbol" f" and time >= '{start.date().isoformat()}'" f" and time <= '{end.date().isoformat()}';" ) bind_params = { "vt_symbol": generate_vt_symbol(symbol, exchange), } result = self.client.query(query, bind_params=bind_params) points = result.get_points() ticks: List[TickData] = [] for d in points: dt = datetime.strptime(d["time"], "%Y-%m-%dT%H:%M:%SZ") tick = TickData( symbol=symbol, exchange=exchange, datetime=DB_TZ.localize(dt), name=d["name"], volume=d["volume"], open_interest=d["open_interest"], last_price=d["last_price"], last_volume=d["last_volume"], limit_up=d["limit_up"], limit_down=d["limit_down"], open_price=d["open_price"], high_price=d["high_price"], low_price=d["low_price"], pre_close=d["pre_close"], bid_price_1=d["bid_price_1"], bid_price_2=d["bid_price_2"], bid_price_3=d["bid_price_3"], bid_price_4=d["bid_price_4"], bid_price_5=d["bid_price_5"], ask_price_1=d["ask_price_1"], ask_price_2=d["ask_price_2"], ask_price_3=d["ask_price_3"], ask_price_4=d["ask_price_4"], ask_price_5=d["ask_price_5"], bid_volume_1=d["bid_volume_1"], bid_volume_2=d["bid_volume_2"], bid_volume_3=d["bid_volume_3"], bid_volume_4=d["bid_volume_4"], bid_volume_5=d["bid_volume_5"], ask_volume_1=d["ask_volume_1"], ask_volume_2=d["ask_volume_2"], ask_volume_3=d["ask_volume_3"], ask_volume_4=d["ask_volume_4"], ask_volume_5=d["ask_volume_5"], gateway_name="DB" ) ticks.append(tick) return ticks
from vnpy.trader.database import BarOverview, DB_TZ from vnpy.trader.database import database_manager from vnpy.trader.engine import BaseEngine, MainEngine, EventEngine from vnpy.trader.object import BarData, HistoryRequest from vnpy.trader.rqdata import rqdata_client from vnpy.trader.iexdata import iexdata_client from vnpy.trader.tiingodata import tiingo_client from vnpy.trader.setting import SETTINGS import sys from vnpy.trader.rqdata import rqdata_client username = SETTINGS['rqdata.username'] password = SETTINGS['rqdata.password'] rqdata_client.init(username, password) req = HistoryRequest(symbol='TSLA', exchange=Exchange.NASDAQ, start=DB_TZ.localize( datetime.strptime('2020-01-01', '%Y-%m-%d')), interval=Interval.DAILY, end=datetime.now(DB_TZ)) data = rqdata_client.query_history(req) print(data) # # start = datetime(2016, 1, 1) # end = datetime(2019, 7, 30) # # single_stock_history = client.getHistoricalPrices(['AAPL']) # single_company_history = client.getCompanyInfo(['AAPL']) # print(single_company_history)