コード例 #1
0
ファイル: engine.py プロジェクト: caizhanjin/vnpy_extend
    def load_bar(self, vt_symbol: str, days: int, interval: Interval,
                 callback: Callable[[BarData], None]):
        """"""
        symbol, exchange = extract_vt_symbol(vt_symbol)
        end = datetime.now()
        start = end - timedelta(days)

        # Query bars from gateway if available
        contract = self.main_engine.get_contract(vt_symbol)

        if contract and contract.history_data:
            req = HistoryRequest(symbol=symbol,
                                 exchange=exchange,
                                 interval=interval,
                                 start=start,
                                 end=end)
            bars = self.main_engine.query_history(req, contract.gateway_name)

        # Try to query bars from RQData, if not found, load from database.
        else:
            bars = self.query_bar_from_rq(symbol, exchange, interval, start,
                                          end)

        if not bars:
            bars = database_manager.load_bar_data(
                symbol=symbol,
                exchange=exchange,
                interval=interval,
                start=start,
                end=end,
            )

        for bar in bars:
            callback(bar)
コード例 #2
0
 def data_step(self, index_info: IndexInfo):
     """
     数据准备阶段
     :param index_info:
     :return:
     """
     newest_bar = database_manager.get_newest_bar_data(
         index_info.symbol, index_info.exchange, Interval.DAILY)
     if newest_bar is None:
         print('当前数据未初始化')
     filling_days = (datetime.now() -
                     newest_bar.datetime).days + 3  # 需要补齐数据的天数
     ds = DataSource()
     if index_info.cal_close:
         # 保存最近收盘数据
         ds.save_bar_data(index_info.symbol, index_info.alias, filling_days)
     if index_info.cal_finance:
         # 保存最近金融相关数据
         trade_dates = [
             bar.datetime for bar in database_manager.load_bar_data(
                 index_info.symbol, index_info.exchange, Interval.DAILY,
                 datetime.now() -
                 timedelta(days=filling_days), datetime.now())
         ]
         ds.save_index_finance(trade_dates=trade_dates,
                               index_code=index_info.index_code)
コード例 #3
0
ファイル: engine.py プロジェクト: zhouchengming1/vnpy
    def load_bar(
        self, 
        vt_symbol: str, 
        days: int, 
        interval: Interval,
        callback: Callable[[BarData], None]
    ):
        """"""
        symbol, exchange = extract_vt_symbol(vt_symbol)
        end = datetime.now()
        start = end - timedelta(days)

        # Query bars from RQData by default, if not found, load from database.
        bars = self.query_bar_from_rq(symbol, exchange, interval, start, end)
        if not bars:
            bars = database_manager.load_bar_data(
                symbol=symbol,
                exchange=exchange,
                interval=interval,
                start=start,
                end=end,
            )

        for bar in bars:
            callback(bar)
コード例 #4
0
def train():

  optimizer = tf.keras.optimizers.Adam(tf.keras.optimizers.schedules.ExponentialDecay(1e-3, decay_steps = 60000, decay_rate = 0.5));
  predictor = PositionPredictor(10);
  checkpoint = tf.train.Checkpoint(model = predictor, optimizer = optimizer);
  checkpoint.restore(tf.train.latest_checkpoint('checkpoints'));
  bars = database_manager.load_bar_data('IF88', Exchange.CFFEX, Interval.MINUTE, datetime(2009,1,1), datetime(2020,8,8));
  mu = 1000000 / 300; # how many possible positions
  delta = 0.5 / 10000; # rate
  F_tm1 = tf.zeros((1,1), dtype = tf.float32);
  history_length = len(bars);
  r = [bars[i].close_price - bars[i-1].close_price for i in range(1, len(bars))]; # r.shape = (history_length - 1,)
  for i in range(100):
    R = list();
    with tf.GradientTape() as tape:
      for i in range(len(r) - 11 + 1):
        xt = tf.concat([tf.ones((1,1), dtype = tf.float32), tf.expand_dims(r[i:i+11], axis = 0), F_tm1], axis = -1); # xt.shape = (1, 13)
        F_t = predictor(xt); # F_t.shape = (1, 1)
        R.append(mu * (F_tm1 * r[i] - delta * tf.math.abs(F_t - F_tm1)));
        F_tm1 = F_t;
      sharpe_rate = tf.math.reduce_mean(R) / tf.math.sqrt(tf.math.reduce_mean(tf.math.square(R)) - tf.math.square(tf.math.reduce_mean(R)));
      loss = -sharpe_rate;
    grads = tape.gradient(loss, predictor.trainable_variables);
    optimizer.apply_gradients(zip(grads, predictor.trainable_variables));
    print('#%d sharpe rate: %f' % (optimizer.iterations, sharpe_rate));
    checkpoint.save(join('checkpoints', 'ckpt'));
  predictor.save_weights('weights.h5');
コード例 #5
0
    def update_all(self, symbol, exchange, trade_datas, start, end=None):
        self._symbol = symbol
        self._exchange = exchange
        self._start = start
        interval = self._interval
        tp = self.interval2timdelta(self._interval)

        backward_n = max(60 * tp, dt.timedelta(hours=25))
        end = start + backward_n if end is None else end
        history_data = database_manager.load_bar_data(symbol,
                                                      exchange,
                                                      interval,
                                                      start=start,
                                                      end=end)
        self.trade_datas = trade_datas

        if len(history_data) > 0 and len(history_data) / (
            (end - start).total_seconds() / 60) > 0.7:
            self.chart.update_all(history_data, trade_datas, [])
        else:
            req = HistoryRequest(symbol, exchange, start, end, interval)
            gateway = self.main_engine.get_gateway('IB')
            if gateway and gateway.api.status:
                self.history_data = history_data = gateway.query_history(req)
                self.chart.update_all(history_data, trade_datas, [])
            database_manager.save_bar_data(history_data)

        if len(getattr(self, 'history_data', [])) > 0:
            self._end = self.history_data[-1].datetime
コード例 #6
0
ファイル: engine.py プロジェクト: zpf4934/vnpy
    def load_bar(self, vt_symbol: str, days: int,
                 interval: Interval) -> List[BarData]:
        """"""
        symbol, exchange = extract_vt_symbol(vt_symbol)
        end = datetime.now(get_localzone())
        start = end - timedelta(days)
        contract: ContractData = self.main_engine.get_contract(vt_symbol)
        data = []

        # Query bars from gateway if available
        if contract and contract.history_data:
            req = HistoryRequest(symbol=symbol,
                                 exchange=exchange,
                                 interval=interval,
                                 start=start,
                                 end=end)
            data = self.main_engine.query_history(req, contract.gateway_name)
        # Try to query bars from RQData, if not found, load from database.
        else:
            data = self.query_bar_from_rq(symbol, exchange, interval, start,
                                          end)

        if not data:
            data = database_manager.load_bar_data(
                symbol=symbol,
                exchange=exchange,
                interval=interval,
                start=start,
                end=end,
            )

        return data
コード例 #7
0
ファイル: backtesting.py プロジェクト: edword01/vnpy
def load_bar_data(vt_symbol: str, interval: Interval, start: datetime,
                  end: datetime):
    """"""
    symbol, exchange = extract_vt_symbol(vt_symbol)

    return database_manager.load_bar_data(symbol, exchange, interval, start,
                                          end)
コード例 #8
0
def plot_bar(symbol="AG2007",
             exchange=Exchange.SHFE,
             interval=Interval.MINUTE,
             start=datetime(2019, 4, 1),
             end=datetime(2020, 10, 30),
             label_count=15,
             fig_size=(16, 6),
             label_rotation=15,
             time_format='%Y-%m-%d %H:%M:%S'):
    # Load history data
    bars = database_manager.load_bar_data(symbol=symbol,
                                          exchange=exchange,
                                          interval=interval,
                                          start=start,
                                          end=end)

    # Generate x, y
    x = [bar.datetime for bar in bars]
    y = [bar.close_price for bar in bars]

    # Show plot
    y_len = len(y)
    xticks = list(range(0, y_len, y_len // label_count))
    xlabels = [x[_].strftime(time_format) for _ in xticks]
    fig, ax = plt.subplots(figsize=fig_size)
    ax.set_xticks(xticks)
    ax.set_xticklabels(xlabels, rotation=label_rotation)
    plt.plot(y)
    plt.title(
        f"{symbol} {interval.value} {min(x).strftime(time_format)}~{max(x).strftime(time_format)}"
    )
    plt.legend([symbol])
    plt.show()
コード例 #9
0
    def check_update_all(self):
        """
        这个方法太慢了,不建议调用。
        这个方法用于本地数据库已经建立,但可能有部分数据缺失时使用
        使用tushare检查更新所有的A股股票全市场日线数据
        检查哪一个交易日的数据是缺失的,补全它
        检查上市后是否每个交易日都有数据,若存在某一交易日无数据,尝试从tushare查询该日数据,若仍无,则说明当天停盘
        :return:
        """
        log.info("开始检查更新所有的A股股票全市场日线数据")

        if self.symbols is not None:
            with tqdm(total=len(self.symbols)) as pbar:
                for tscode, list_date in zip(self.symbols['ts_code'],
                                             self.symbols['list_date']):
                    pbar.set_description_str("正在检查A股日线数据,股票代码:" + tscode)

                    symbol, exchange = to_split_ts_codes(tscode)

                    local_bar = database_manager.load_bar_data(
                        symbol=symbol,
                        exchange=exchange,
                        interval=Interval.DAILY,
                        start=datetime.strptime(list_date, TS_DATE_FORMATE),
                        end=datetime.now())
                    local_bar_dates = [
                        bar.datetime.strftime(TS_DATE_FORMATE)
                        for bar in local_bar
                    ]

                    index = (self.trade_cal[exchange.value][(
                        self.trade_cal[exchange.value].cal_date == list_date)])
                    trade_cal = self.trade_cal[
                        exchange.value].iloc[index.index[0]:]
                    for trade_date in trade_cal['cal_date']:
                        if trade_date not in local_bar_dates:
                            req = HistoryRequest(symbol=symbol,
                                                 exchange=exchange,
                                                 start=datetime.strptime(
                                                     trade_date,
                                                     TS_DATE_FORMATE),
                                                 end=datetime.strptime(
                                                     trade_date,
                                                     TS_DATE_FORMATE),
                                                 interval=Interval.DAILY)
                            bardata = self.tushare_client.query_history(
                                req=req)
                            if bardata:
                                log.info(tscode + "本地数据库缺失:" + trade_date)
                                try:
                                    database_manager.save_bar_data(bardata)
                                except Exception as ex:
                                    log.error(tscode + "数据存入数据库异常")
                                    log.error(ex)
                                    traceback.print_exc()
                    pbar.update(1)
                    log.info(pbar.desc)

        log.info("A股股票全市场日线数据检查更新完毕")
コード例 #10
0
ファイル: engine.py プロジェクト: TradingHacker/vnpy_charting
    def load_bar_data(self, symbol: str, exchange: Exchange,
                      interval: Interval, start: datetime,
                      end: datetime) -> List[BarData]:
        """"""
        bars = database_manager.load_bar_data(symbol, exchange, interval,
                                              start, end)

        return bars
コード例 #11
0
def load_bar_data(
    spread: SpreadData,
    interval: Interval,
    start: datetime,
    end: datetime,
    pricetick: float = 0
):
    """"""
    # Load bar data of each spread leg
    leg_bars: Dict[str, Dict] = {}

    for vt_symbol in spread.legs.keys():
        symbol, exchange = extract_vt_symbol(vt_symbol)

        bar_data: List[BarData] = database_manager.load_bar_data(
            symbol, exchange, interval, start, end
        )

        bars: Dict[datetime, BarData] = {bar.datetime: bar for bar in bar_data}
        leg_bars[vt_symbol] = bars

    # Calculate spread bar data
    spread_bars: List[BarData] = []

    for dt in bars.keys():
        spread_price = 0
        spread_value = 0
        spread_available = True

        for leg in spread.legs.values():
            leg_bar = leg_bars[leg.vt_symbol].get(dt, None)

            if leg_bar:
                price_multiplier = spread.price_multipliers[leg.vt_symbol]
                spread_price += price_multiplier * leg_bar.close_price
                spread_value += abs(price_multiplier) * leg_bar.close_price
            else:
                spread_available = False

        if spread_available:
            if pricetick:
                spread_price = round_to(spread_price, pricetick)

            spread_bar = BarData(
                symbol=spread.name,
                exchange=exchange.LOCAL,
                datetime=dt,
                interval=interval,
                open_price=spread_price,
                high_price=spread_price,
                low_price=spread_price,
                close_price=spread_price,
                gateway_name="SPREAD",
            )
            spread_bar.value = spread_value
            spread_bars.append(spread_bar)

    return spread_bars
コード例 #12
0
def main(symbol, exchange, start, end):

  # 1) prepare observation X
  data = database_manager.load_bar_data(symbol, exchange, Interval.MINUTE, start, end);
  if len(data) == 0:
    # download data if not presented
    if not tsdata_client.inited:
      print('登录tushare');
      succeed = tsdata_client.init();
      if False == succeed:
        print('tushare登录失败');
        return;
    req = HistoryRequest(symbol = symbol, exchange = exchange, interval = Interval.MINUTE, start = start, end = end);
    data = tsdata_client.query_history(req);
    database_manager.save_bar_data(data);
    data = database_manager.load_bar_data(symbol, exchange, Interval.MINUTE, start, end);
  X = [[log(data[i].close_price) - log(data[i-1].close_price),
        log(data[i].close_price) - log(data[i-5].close_price),
        log(data[i].high_price) - log(data[i].low_price)] for i in range(5, len(data))]; # X.shape = (len(data) - 5, 3)
  # 2) learn the HMM model
  hmm = GaussianHMM(n_components = 6, covariance_type = 'diag', n_iter = 5000).fit(X);
  print('hidden markov model %s converged' % ('is' if hmm.monitor_.converged else 'is not'));
  with open('hmm.pkl', 'wb') as f:
    pickle.dump(hmm, f);
  # 3) visualize
  latent_states_sequence = hmm.predict(X);
  plt.figure(figsize = (15,8));
  dates = [data[i].datetime.strftime('%Y-%m-%d') for i in range(5, len(data))];
  close_prices = [data[i].close_price for i in range(5, len(data))];
  for i in range(hmm.n_components):
    idx = (latent_states_sequence == i); # index of day labeled with i
    plt.plot(np.array(dates)[idx], np.array(close_prices)[idx], '.', label = 'latent idx %d' % i, lw = 1);
    plt.legend();
    plt.grid(1);
  plt.axis([0, len(close_prices), min(close_prices), max(close_prices)]);
  plt.savefig('colored_k_bar.png');
  plt.show();
  for i in range(hmm.n_components):
    idx = (latent_states_sequence == i); # index of day labeled with i
    idx = np.append(False, idx[:-1]); # index of the next day of the day labeled with i, because if you trade on day with label i the reward comes one day after
    plt.plot(np.exp(np.array(X)[idx, 0].cumsum()), label = 'latent_state %d' % i);
    plt.legend();
    plt.grid(1);
  plt.savefig('return_curve.png');
  plt.show();
コード例 #13
0
def load_history_data():
    symbol = 'rb1910'
    exchange = Exchange.SHFE
    interval = Interval.MINUTE
    start = datetime(2019, 7, 10)
    end = datetime.now()

    return database_manager.load_bar_data(symbol, exchange, interval, start,
                                          end)
コード例 #14
0
ファイル: utility.py プロジェクト: linbozhong/research_tools
def load_data(vt_symbol: str, interval: str, start: datetime,
              end: datetime) -> pd.DataFrame:
    symbol, exchange = extract_vt_symbol(vt_symbol)
    data = database_manager.load_bar_data(
        symbol,
        exchange,
        Interval(interval),
        start=start,
        end=end,
    )
    return vt_bar_to_df(data)
コード例 #15
0
def load_bar_data(
        symbol: str,
        exchange: Exchange,
        interval: Interval,
        start: datetime,
        end: datetime
):
    """"""
    return database_manager.load_bar_data(
        symbol, exchange, interval, start, end
    )
コード例 #16
0
def load_bar_data(
    symbol: str,
    exchange: Exchange,
    interval: Interval,
    start: datetime,
    end: datetime,
    collection_name:str = None
):
    """"""
    return database_manager.load_bar_data(
        symbol, exchange, interval, start, end, collection_name = collection_name
    )
コード例 #17
0
 def get_newest_bar_data(self, symbol: str, exchange: Exchange,
                         interval: Interval) -> BarData or None:
     """"""
     for overview in self.bar_overviews:
         if exchange == overview.exchange and interval == overview.interval and symbol == overview.symbol:
             bars = database_manager.load_bar_data(symbol=symbol,
                                                   exchange=exchange,
                                                   interval=interval,
                                                   start=overview.start,
                                                   end=overview.start)
             return bars[0] if bars is not None else None
     return None
コード例 #18
0
    def query_history(self, req: HistoryRequest):
        """
        Query history bar data from RQData.
        """
        symbol = req.symbol
        exchange = req.exchange
        interval = req.interval
        start = req.start
        end = req.end

        bars = database_manager.load_bar_data(
            symbol=symbol,
            exchange=exchange,
            interval=interval,
            start=start,
            end=end,
        )

        if not bars:
            self.download_bar(symbol=symbol,
                              exchange=exchange,
                              interval=interval,
                              start_dt=start,
                              end_dt=end)
            self.data_csv2db(symbol=symbol,
                             exchange=exchange,
                             interval=interval,
                             start_dt=start,
                             end_dt=end)
            bars = database_manager.load_bar_data(
                symbol=symbol,
                exchange=exchange,
                interval=interval,
                start=start,
                end=end,
            )

        return bars
コード例 #19
0
 def test_init(self):
     symbol, alias = '000042', 'XSHG'
     ds = DataSource(mode='remote')
     ds.save_bar_data(symbol, alias)
     bar_datas = database_manager.load_bar_data(
         symbol, const.Exchange.get_exchange_by_alias(alias),
         const.Interval.DAILY, datetime(2010, 1, 1), datetime.now())
     finance_datas = []
     for bar in bar_datas:
         finance_data = ds.get_index_finance(bar.datetime,
                                             symbol + '.' + alias)
         finance_datas.append(finance_data)
         if len(finance_datas) == 20:
             database_manager.save_finance_data(finance_datas)
             finance_datas.clear()
コード例 #20
0
    def load_bar_data(self,
                      symbol,
                      alias,
                      start_date: datetime = None,
                      end_data: datetime = None):
        """取出bar数据"""
        exchange = const.Exchange.get_exchange_by_alias(alias)
        interval = const.Interval.DAILY
        progress_delta = timedelta(days=30)
        total_delta = end_data - start_date
        interval_delta = INTERVAL_DELTA_MAP[interval]

        start = start_date
        end = start_date + progress_delta
        progress = 0

        df = pd.DataFrame(columns=('date', 'open', 'high', 'low', 'close',
                                   'volume'))
        while start < end_data:
            end = min(end,
                      end_data)  # Make sure end time stays within set range

            datas = database_manager.load_bar_data(symbol, exchange, interval,
                                                   start, end)
            # data转为dataframe数据
            for i, data in enumerate(datas):
                df = df.append(
                    {
                        'date': data.datetime,
                        'open': data.open_price,
                        'high': data.high_price,
                        'low': data.low_price,
                        'close': data.close_price,
                        'volume': data.volume
                    },
                    ignore_index=True)

            progress += progress_delta / total_delta
            progress = min(progress, 1)
            progress_bar = "#" * int(progress * 10)
            print(f"加载进度:{progress_bar} [{progress:.0%}]")

            start = end + interval_delta
            end += (progress_delta + interval_delta)

        print(f"历史数据加载完成,数据量:{df.__len__()}")
        return df
コード例 #21
0
    def update_backward_bars(self, n):
        chart = self.chart
        last_bar = chart._manager.get_bar(chart.last_ix)
        if last_bar:
            symbol = last_bar.symbol
            exchange = last_bar.exchange
            if self._end:
                start = max(last_bar.datetime, self._end)
            else:
                start = last_bar.datetime

            if start >= dt.datetime.now(get_localzone()):
                return

            tp = self.interval2timdelta(self._interval)
            backward_n = max(tp * n, dt.timedelta(minutes=60))
            end = start + backward_n
            if not self.checkTradeTime(end.time()):
                history_data = database_manager.load_bar_data(symbol,
                                                              exchange,
                                                              self._interval,
                                                              start=start,
                                                              end=end)

                if len(history_data) == 0 or len(history_data) / (
                    (end - start).total_seconds() / 60) < 0.7:
                    req = HistoryRequest(symbol, exchange, start, end,
                                         self._interval)
                    gateway = self.main_engine.get_gateway('IB')
                    if gateway and gateway.api.status:
                        history_data = gateway.query_history(req)
                    database_manager.save_bar_data(history_data)

                for bar in history_data:
                    self.chart.update_bar(bar)

                last_bar_after_update = chart._manager.get_bar(chart.last_ix)
                self.chart.clear_trades()
                self.chart.update_trades([
                    t for t in self.trade_datas
                    if t.datetime <= last_bar_after_update.datetime
                ])
                self.chart.update_pos()
                self.chart.update_pnl()

            self._end = end
コード例 #22
0
ファイル: engine.py プロジェクト: laoshu198838/vnpy_learning
    def load_bar(
        self,
        vt_symbol: str,
        days: int,
        interval: Interval,
        callback: Callable[[BarData], None],
        use_database: bool
    ):
        """"""
        symbol, exchange = extract_vt_symbol(vt_symbol)
        end = datetime.now()
        start = end - timedelta(days)
        bars = []

        # Pass gateway and RQData if use_database set to True
        #如果database设置为foalse,就先从rqdata里面取数,如果里面没有再从数据库里面查找有
        #没有相关数据
        if not use_database:
            # Query bars from gateway if available
            contract = self.main_engine.get_contract(vt_symbol)##什么意思

            if contract and contract.history_data:
                req = HistoryRequest(
                    symbol=symbol,
                    exchange=exchange,
                    interval=interval,
                    start=start,
                    end=end
                )
                bars = self.main_engine.query_history(req, contract.gateway_name)

            # Try to query bars from RQData, if not found, load from database.
            else:
                bars = self.query_bar_from_rq(symbol, exchange, interval, start, end)
        #从数据库中提取数据
        if not bars:
            bars = database_manager.load_bar_data(
                symbol=symbol,
                exchange=exchange,
                interval=interval,
                start=start,
                end=end,
            )

        for bar in bars:
            callback(bar)
コード例 #23
0
    def load_bar(
        self,
        vt_symbol: str,
        days: int,
        interval: Interval,
        callback: Callable[[BarData], None],
        use_database: bool
    ):
        """"""
        symbol, exchange = extract_vt_symbol(vt_symbol)
        end = datetime.now(get_localzone())
        end = datetime(year=end.year, month=end.month, day=end.day, hour=end.hour, minute=0)
        start = end - timedelta(days)
        bars = []

        # Pass gateway and RQData if use_database set to True
        if not use_database:
            # Query bars from gateway if available
            contract = self.main_engine.get_contract(vt_symbol)

            if contract and contract.history_data:
                req = HistoryRequest(
                    symbol=symbol,
                    exchange=exchange,
                    interval=interval,
                    start=start,
                    end=end
                )
                bars = self.main_engine.query_history(req, contract.gateway_name)

            # Try to query bars from RQData, if not found, load from database.
            else:
                bars = self.query_bar_from_rq(symbol, exchange, interval, start, end)

        if not bars:
            bars = database_manager.load_bar_data(
                symbol=symbol,
                exchange=exchange,
                interval=interval,
                start=start,
                end=end,
            )

        for bar in bars:
            callback(bar)
コード例 #24
0
ファイル: engine.py プロジェクト: zhangjf76/vnpy
    def load_bar(self, vt_symbol: str, days: int, interval: Interval,
                 callback: Callable[[BarData], None], use_database: bool):
        """"""
        symbol, exchange = extract_vt_symbol(vt_symbol)
        end = datetime.now()
        start = end - timedelta(days)
        bars = []

        data_source = ""
        # Pass gateway and RQData if use_database set to True
        if not use_database:
            # Query bars from gateway if available
            contract = self.main_engine.get_contract(vt_symbol)

            if contract and contract.history_data:
                data_source = "行情服务器"
                req = HistoryRequest(symbol=symbol,
                                     exchange=exchange,
                                     interval=interval,
                                     start=start,
                                     end=end)
                bars = self.main_engine.query_history(req,
                                                      contract.gateway_name)

            # Try to query bars from RQData, if not found, load from database.
            else:
                data_source = "tdx数据源"
                bars = self.query_bar_from_rq(symbol, exchange, interval,
                                              start, end)

        if not bars:
            data_source = "本地数据库"
            bars = database_manager.load_bar_data(
                symbol=symbol,
                exchange=exchange,
                interval=interval,
                start=start,
                end=end,
            )

        # JinAdd : 添加日志
        self.write_log(f"从{data_source}加载{len(bars)}条数据")
        for bar in bars:
            callback(bar)
コード例 #25
0
    def visulize(self, QModelIndex):
        from vnpy.trader.database import database_manager
        symbol = self.trade_values[QModelIndex.row()].symbol
        exchange = self.trade_values[QModelIndex.row()].exchange
        trade_date: dt.date = self.daily_trade_result.name
        start = dt.datetime(trade_date.year, trade_date.month, trade_date.day)
        end = dt.datetime(trade_date.year, trade_date.month, trade_date.day,
                          23, 59)

        history = database_manager.load_bar_data(symbol, exchange,
                                                 Interval.MINUTE, start, end)

        tradeData = [t for t in self.trade_values if t.symbol == symbol]

        chart = TradeResultVisulizationChart(
            history,
            tradeData,
            title=f"{self.daily_trade_result.name}交易行情",
            parent=self.parent())
        chart.exec_()
コード例 #26
0
ファイル: 协整分析.py プロジェクト: zf6413/vnpygithub
def load_symbol_data(vt_symbol, start, end):
    """
	从数据库读取数据
	:param vt_symbol:
	:param start:
	:param end:
	:return:
	"""
    symbol, exchange = extract_vt_symbol(vt_symbol)  # XBTUSD.BITMEX 分离
    start = datetime.strptime(start, "%Y%m%d")
    end = datetime.strptime(end, "%Y%m%d")
    interval = Interval.MINUTE
    data = database_manager.load_bar_data(symbol, exchange, interval, start,
                                          end)  # 数据库读取数据

    dt_list = []
    close_list = []
    for bar in data:
        dt_list.append(bar.datetime)
        close_list.append(bar.close_price)

    s = pd.Series(close_list, index=dt_list)
    return s
コード例 #27
0
    def load_bar(self, vt_symbol: str, days: int, interval: Interval,
                 callback: Callable[[BarData], None]):
        """载入历史bar"""
        symbol, exchange = extract_vt_symbol(vt_symbol)
        end = datetime.now()
        start = end - timedelta(days)

        # Query bars from RQData by default, if not found, load from database.
        # TODO
        # 这里CTA的载入历史数据是从米匡那里拿,这个必须修改,没有米匡账号!
        # 初步建议修改为在主引擎中发送query_history拿数据,由gateway回调数据
        # OKEX的历史数据由OKEX提供,FUTURES的历史数据由数据库提供,每个都不一样,因此,不能在这里统一,要改成在gaetway中分发
        bars = self.query_bar_from_rq(symbol, exchange, interval, start, end)
        if not bars:
            bars = database_manager.load_bar_data(
                symbol=symbol,
                exchange=exchange,
                interval=interval,
                start=start,
                end=end,
            )

        for bar in bars:
            callback(bar)
コード例 #28
0
ファイル: engine.py プロジェクト: h1222443/vnpy
    def _query_history(self, vt_symbol: str, interval: Interval,
                       start: datetime, end: datetime) -> None:
        """"""
        contract: ContractData = self.main_engine.get_contract(vt_symbol)

        req = HistoryRequest(symbol=contract.symbol,
                             exchange=contract.exchange,
                             interval=interval,
                             start=start,
                             end=end)

        if contract.history_data:
            data = self.main_engine.query_history(req, contract.gateway_name)
        else:
            data = rqdata_client.query_history(req)
        if not data:
            data = database_manager.load_bar_data(symbol=contract.symbol,
                                                  exchange=contract.exchange,
                                                  interval=interval,
                                                  start=start,
                                                  end=end)

        event = Event(EVENT_CHART_HISTORY, data)
        self.event_engine.put(event)
コード例 #29
0
    def market_open(self):
        # 读一下前几天的日内收益率数据,算峰度
        bars = []
        # 回测时自带
        if self.cta_engine.engine_type == EngineType.BACKTESTING:
            symbol = self.cta_engine.symbol
            exchange = self.cta_engine.exchange
            end = self.cta_engine.datetime - timedelta(1)
            start = end - timedelta(30)
        # 实盘时
        else:
            symbol, exchange = self.cta_engine.extract_vt_symbol(self.vt_symbol)
            end = datetime.now()
            start = end - timedelta(30)
            # 在线数据源获取
            bars = self.cta_engine.query_bar_from_rq(symbol, exchange, Interval.DAILY, start, end)
        
        # 没有的话问本地数据库要
        if not bars:
            bars = database_manager.load_bar_data(
                symbol=symbol,
                exchange=exchange,
                interval=Interval.DAILY,
                start=start,
                end=end,
            )

        # Generate history data in DataFrame
        df = pd.DataFrame()
        t = []
        o = []
        h = []
        l = []  
        c = []
        v = []
        for i in range(0,len(bars)):
            time = bars[i].datetime
            open_price = bars[i].open_price
            high_price = bars[i].high_price
            low_price = bars[i].low_price
            close_price = bars[i].close_price
            volume = bars[i].volume
            t.append(time)
            o.append(open_price)
            h.append(high_price)
            l.append(low_price)
            c.append(close_price)
            v.append(volume)

        df["open"] = o
        df["high"] = h
        df["low"] = l
        df["close"] = c
        df["volume"] = v
        df["time"] = t
        df["intraday_return"] = np.log(df["close"]/df["open"])
        kurt_array = df["intraday_return"].rolling(self.kurt_len).kurt().tolist()
        
        kurt_value = 0
        if len(kurt_array) >= 1:
            kurt_value = kurt_array[-1]

        self.write_log(f"近{self.kurt_len}日日内峰度{kurt_value},阈值{self.kurt_filter}")
        if kurt_value < self.kurt_filter:
            self.cannot_kurt = 1
        else:
            self.cannot_kurt = 0
コード例 #30
0
ファイル: test_vnpychart.py プロジェクト: ypochien/5element
from vnpy.trader.ui import create_qapp, QtCore
from vnpy.trader.database import database_manager
from vnpy.trader.constant import Exchange, Interval
from vnpy.chart import ChartWidget, VolumeItem, CandleItem
from vnpy.trader.object import BarData
from random import randint
import pandas as pd
"""
"""

if __name__ == "__main__":
    app = create_qapp()

    bars = database_manager.load_bar_data("IF88",
                                          Exchange.CFFEX,
                                          interval=Interval.MINUTE,
                                          start=datetime(2019, 7, 1),
                                          end=datetime(2019, 7, 17))
    """
    symbol: str
    exchange: Exchange
    datetime: datetime

    interval: Interval = None
    volume: float = 0
    open_interest: float = 0
    open_price: float = 0
    high_price: float = 0
    low_price: float = 0
    close_price: float = 0
    """