Beispiel #1
0
def get_bars():
    stocks = request.args.get('stocks').split(',')
    date = request.args.get('date')
    data = {}
    query_date = datetime.strptime(date, '%Y-%m-%d')
    total_count = 240
    _list = [[] for _ in range(total_count)]
    _stocks = []
    reply_close = {}
    current_open = {}

    for stock in stocks:
        stock = stock.split('.')[0]
        print('stock => ', stock)
        _stocks.append(stock)
        reply_close[stock] = list(
            jqdatasdk.get_bars(parseStockCode(stock),
                               2,
                               unit='1d',
                               fields=['close'],
                               include_now=False,
                               end_dt=query_date,
                               fq_ref_date=None)['close'])[0]
        current_open[stock] = list(
            jqdatasdk.get_bars(parseStockCode(stock),
                               1,
                               unit='1d',
                               fields=['open'],
                               include_now=False,
                               end_dt=query_date,
                               fq_ref_date=None)['open'])

        ## 获取标的分时图每个点
        stock_bars = list(
            jqdatasdk.get_bars(parseStockCode(stock),
                               total_count,
                               unit='1m',
                               fields=['close'],
                               include_now=False,
                               end_dt=query_date,
                               fq_ref_date=None)['close'])
        data[stock] = current_open[stock] + stock_bars

    ## 统计当日涨幅
    for index in range(0, total_count):
        _list[index] = {
            'index': index,
        }

        for stock in data:
            _list[index][stock] = round(
                (data[stock][index] - reply_close[stock]) /
                reply_close[stock] * 100, 2)

    # print('昨日收盘价')
    # print(reply_close)

    # print('涨幅')
    # print(result)
    return jsonify({'stocks': _stocks, 'list': _list})
    def record(self, entity, start, end, size, timestamps):
        if self.adjust_type == AdjustType.hfq:
            fq_ref_date = '2000-01-01'
        else:
            fq_ref_date = to_time_str(now_pd_timestamp())

        if not self.end_timestamp:
            df = get_bars(to_jq_entity_id(entity),
                          count=size,
                          unit=self.jq_trading_level,
                          fields=['date', 'open', 'close', 'low', 'high', 'volume', 'money'],
                          fq_ref_date=fq_ref_date,
                          include_now=self.real_time)
        else:
            end_timestamp = to_time_str(self.end_timestamp)
            df = get_bars(to_jq_entity_id(entity),
                          count=size,
                          unit=self.jq_trading_level,
                          fields=['date', 'open', 'close', 'low', 'high', 'volume', 'money'],
                          end_dt=end_timestamp,
                          fq_ref_date=fq_ref_date,
                          include_now=False)
        if pd_is_not_null(df):
            df['name'] = entity.name
            df.rename(columns={'money': 'turnover', 'date': 'timestamp'}, inplace=True)

            df['entity_id'] = entity.id
            df['timestamp'] = pd.to_datetime(df['timestamp'])
            df['provider'] = 'joinquant'
            df['level'] = self.level.value
            df['code'] = entity.code

            # 判断是否需要重新计算之前保存的前复权数据
            if self.adjust_type == AdjustType.qfq:
                check_df = df.head(1)
                check_date = check_df['timestamp'][0]
                current_df = get_kdata(entity_id=entity.id, provider=self.provider, start_timestamp=check_date,
                                       end_timestamp=check_date, limit=1, level=self.level,
                                       adjust_type=self.adjust_type)
                if pd_is_not_null(current_df):
                    old = current_df.iloc[0, :]['close']
                    new = check_df['close'][0]
                    # 相同时间的close不同,表明前复权需要重新计算
                    if round(old, 2) != round(new, 2):
                        qfq_factor = new / old
                        last_timestamp = pd.Timestamp(check_date)
                        self.recompute_qfq(entity, qfq_factor=qfq_factor, last_timestamp=last_timestamp)

            def generate_kdata_id(se):
                if self.level >= IntervalLevel.LEVEL_1DAY:
                    return "{}_{}".format(se['entity_id'], to_time_str(se['timestamp'], fmt=TIME_FORMAT_DAY))
                else:
                    return "{}_{}".format(se['entity_id'], to_time_str(se['timestamp'], fmt=TIME_FORMAT_ISO8601))

            df['id'] = df[['entity_id', 'timestamp']].apply(generate_kdata_id, axis=1)

            df_to_db(df=df, data_schema=self.data_schema, provider=self.provider, force_update=self.force_update)

        return None
Beispiel #3
0
    def record(self, entity, start, end, size, timestamps):
        # 不复权
        try:
            df = get_bars(to_jq_entity_id(entity),
                          count=size,
                          unit=self.jq_trading_level,
                          fields=[
                              'date', 'open', 'close', 'low', 'high', 'volume',
                              'money'
                          ],
                          include_now=False)
        except Exception as e:
            # just ignore the error,for some new stocks not in the index
            self.logger.exception(e)
            return None
        df['name'] = entity.name
        df.rename(columns={'money': 'turnover'}, inplace=True)

        df['timestamp'] = pd.to_datetime(df['date'])
        df['provider'] = 'joinquant'
        df['level'] = self.level.value

        # 前复权
        end_timestamp = to_time_str(now_pd_timestamp())
        qfq_df = get_bars(to_jq_entity_id(entity),
                          count=size,
                          unit=self.jq_trading_level,
                          fields=['date', 'open', 'close', 'low', 'high'],
                          fq_ref_date=end_timestamp,
                          include_now=False)
        # not need to update past
        df['qfq_close'] = qfq_df['close']
        df['qfq_open'] = qfq_df['open']
        df['qfq_high'] = qfq_df['high']
        df['qfq_low'] = qfq_df['low']

        check_df = qfq_df.head(1)
        check_date = check_df['date'][0]

        current_df = get_kdata(entity_id=entity.id,
                               provider=self.provider,
                               start_timestamp=check_date,
                               end_timestamp=check_date,
                               limit=1,
                               level=self.level)

        if df_is_not_null(current_df):
            old = current_df.iloc[0, :]['qfq_close']
            new = check_df['close'][0]
            # 相同时间的close不同,表明前复权需要重新计算
            if old != new:
                self.factor = new / old
                self.last_timestamp = pd.Timestamp(check_date)

        return df.to_dict(orient='records')
def TestGetMinDataFromJQdata():
    df = get_bars(
        '000002.XSHE',
        1000000,
        unit='5m',
        fields=['date', 'open', 'high', 'low', 'close', 'volume', 'factor'])
    df.to_csv("格力5min")
Beispiel #5
0
def history_tradings(order_book_ids,
                     bar_count,
                     frequency,
                     dt,
                     fields=['date', 'open', 'high', 'low', 'close'],
                     skip_suspended=True,
                     include_now=True,
                     adjust_type="pre",
                     adjust_orig=None):
    #get_bars(security, count, unit='1d', fields=['date','open','high','low','close'], include_now=False, end_dt=None, fq_ref_date=None)
    if "date" not in fields:
        fields.append("date")

    dt = DataProxy.get_instance().get_next_trading_date(dt, n=1)
    data = get_bars(security=order_book_ids,
                    count=bar_count,
                    unit=frequency,
                    end_dt=dt,
                    fields=fields,
                    include_now=include_now,
                    fq_ref_date=adjust_orig)
    data["datetime"] = data["date"].apply(convert_to_timestamp)
    data = data.reset_index().rename(columns={"level_0": "order_book_id"})
    data = data.set_index(["order_book_id", "datetime"])
    data = data.drop(columns=["level_1", "date"])
    return data
Beispiel #6
0
def load_price(index_name, reload=False):
    save_dir = Path('data') / 'buffer' / f"{index_name}_return.csv"
    if not reload:
        if os.path.exists(save_dir):
            price = pd.read_csv(save_dir,
                                index_col=0,
                                parse_dates=[0],
                                infer_datetime_format=True)
            return price
        else:
            return load_price(index_name, reload=True)
    else:
        weight_dict = load_weight(index_name)
        price = pd.DataFrame()
        for dt, weight in tqdm(weight_dict.items()):
            end_dt = datetime.datetime.strptime(
                dt,
                '%Y-%m-%d') + relativedelta(months=+1) + pd.offsets.MonthEnd(0)
            price_df = jq.get_bars(weight.index.to_list(),
                                   2,
                                   unit='1M',
                                   fields=['date', 'close'],
                                   end_dt=end_dt,
                                   include_now=True)
            change_df = pd.DataFrame(
                price_df.groupby(level=0)['close'].pct_change()).loc[
                    pd.IndexSlice[:, 1], pd.IndexSlice[:]].droplevel(1)
            change_df = change_df.rename({'close': 'pct_change'}, axis=1)

            df = weight.merge(change_df, left_index=True, right_index=True)
            assert df.shape[0] == 300
            price.loc[end_dt, 'predict_return'] = (df['weight'] *
                                                   df['pct_change']).sum()
        price.to_csv(save_dir)
        return price
Beispiel #7
0
 def get_1min_bars(self, stock_id: str, count: int, end_date: str):
     return jq.get_bars(
         jq.normalize_code(stock_id),
         count,
         unit='1m',
         fields=['date', 'open', 'close', 'high', 'low', 'volume', 'money'],
         include_now=False,
         end_dt=end_date)
Beispiel #8
0
 def data_fetch(self, stock_list, num, unit, end_dt):
     df = jq.get_bars(
         security=stock_list,
         count=num,
         unit=unit,
         fields=["date", "open", "high", "low", "close", "volume"],
         end_dt=end_dt,
     )
     return df
Beispiel #9
0
def readdata(code,enddate,days:int):
    df=pd.DataFrame()
    getAuthJQ()
    df=jq.get_bars(code, days, unit='1d',
             fields=['date', 'open', 'high', 'low', 'close','volume'],
             end_dt=enddate, df=True,include_now=True)
    df = df.sort_values('date')
    #print(df)
    return df
Beispiel #10
0
 def week(self, stock_code, count=380, end=default_end):
     fields = ['date', 'open', 'high', 'low', 'close', 'volume']
     df = jq.get_bars(stock_code,
                      count,
                      end_dt=end,
                      unit='1w',
                      fields=fields)
     df.index = pd.to_datetime(df.date)
     df['openinterest'] = 0
     df = df[['open', 'high', 'low', 'close', 'volume', 'openinterest']]
     return df
Beispiel #11
0
def crawle_index(code, index):
    days = get_days(index.start, date_utils.today())

    # https://www.joinquant.com/help/api/help#JQData:%E6%8C%87%E6%95%B0%E6%A6%82%E5%86%B5
    data = get_bars(security=code,
                    count=days,
                    unit='1d',
                    fields=['date', 'close', 'factor'],
                    end_dt=date_utils.today(),
                    df=True)
    logger.debug("爬取'%s'[%s]数据,从%s至今,一共%d条", index.name, code, index.start,
                 len(data))
    return data
Beispiel #12
0
def exact_index(index_name):
    price = load_price(index_name)
    bm = jq.get_bars(index_name,
                     82,
                     unit='1M',
                     fields=['date', 'close'],
                     end_dt='2020-09-30',
                     include_now=True).set_index('date')
    bm = bm.pct_change().iloc[1:, :].rename({'close': 'index_return'}, axis=1)
    bm.index = bm.index + pd.offsets.MonthEnd(0)
    combine = price.merge(bm, left_index=True, right_index=True)
    combine['predict_return'] = combine['predict_return'] / 100
    combine.plot()
    plt.show()
    return price
Beispiel #13
0
    def test_000_something(self):
        """Test something."""
        bars = jq.get_bars('002082.XSHE',
                           30,
                           df=False,
                           fq_ref_date='2020-7-29',
                           end_dt='2020-7-30')
        ma5 = moving_average(bars['close'], 5)
        ma10 = moving_average(bars['close'], 10)
        ma20 = moving_average(bars['close'], 20)

        print(ma5[-5:])
        print(bars['date'][-1], len(ma5), len(ma10), len(ma20))
        for i in range(16, 21):
            err, curve, coef = polyfit(ma5[i:i + 5])
            a, b, c = coef
            axis_x = -b / (2 * a)
            axis_y = (4 * a * c - b * b) / (4 * a)

            print(f"{bars['date'][i+9]} err:{err:.4f},coef:{a:.3f},{b:.3f}"
                  f",{c:.3f}, "
                  f"(x,"
                  f"y):{axis_x:.1f}"
                  f",{axis_y:.2f}")

        print("=" * 5 + "ma10")
        for i in range(11, 16):
            err, curve, coef = polyfit(ma10[i:i + 5])
            a, b, c = coef
            axis_x = -b / (2 * a)
            axis_y = (4 * a * c - b * b) / (4 * a)

            print(
                f"{bars[i+14]['date']} err:{err:.4f}, coef:{a:.3f},{b:.3f},{c:.3f}, "
                f"(x,y):{axis_x:.1f}"
                f",{axis_y:.2f}")

        print("=" * 5 + "ma20")
        for i in range(1, 6):
            err, curve, coef = polyfit(ma20[i:i + 5])
            a, b, c = coef
            axis_x = -b / (2 * a)
            axis_y = (4 * a * c - b * b) / (4 * a)

            print(
                f"{bars[i+24]['date']} err:{err:.4f}, coef:{a:.3f},{b:.3f},{c:.3f}, "
                f"(x,y):{axis_x:.1f}"
                f",{axis_y:.2f}")
Beispiel #14
0
 def get_bars(self, code, count, frame, end_dt):
     if not (code.upper().endswith('.XSHE')
             or code.upper().endswith('.XSHG')):
         if code.startswith('60'):
             code += '.XSHG'
         else:
             code += '.XSHE'
     fields = [
         'date', 'open', 'high', 'low', 'close', 'volume', 'money', 'factor'
     ]
     return jq.get_bars(code,
                        count,
                        frame,
                        fields,
                        include_now=True,
                        end_dt=end_dt,
                        fq_ref_date=end_dt)
def jq_get_bars(security,
                count,
                unit="1d",
                fields=("date", "open", "high", "low", "close"),
                include_now=False,
                end_dt=None,
                fq_ref_date=None,
                df=True):
    # logger.info("HTTP GET: bars, with unit={}, fq_ref_date={}".format(unit, fq_ref_date))
    return get_bars(security,
                    count,
                    unit=unit,
                    fields=fields,
                    include_now=include_now,
                    end_dt=end_dt,
                    fq_ref_date=fq_ref_date,
                    df=df)
Beispiel #16
0
def monitor():
    window = 20

    for i in dom_futures:
        if g_report[i] == 1 and g_report_interval[i] == 0:
            g_report[i] = 0
        if g_report[i] == 1 and g_report_interval[i] > 0:
            g_report_interval[i] -= 1
            print("time wait")
            continue

        df = jqdatasdk.get_bars(
            i,
            window + 1,
            unit='1d',
            fields=['date', 'open', 'high', 'low', 'close'],
            include_now=True,
            end_dt=None)

        if len(df) < 21:
            continue
        if str(df['date'][20]).find("2019") == -1:
            continue
        price = df['close'][20]
        print(price)

        if price == g_lastprice[i]:
            print("price not renew return")
            continue
        N = ATR(df['high'][1:21], df['low'][1:21], df['close'][1:21])
        if N == 0:
            continue
        highest = df['high'][0:20].max()
        lowest = df['low'][0:20].min()
        print(i, df['date'][20], price, highest, lowest, N)
        positionSizePercent = price / N
        positionSize = 10000 * 0.01 / N
        if price > highest:
            indicator = "LONG"
            send_email(i, indicator, "1d", price, highest, lowest, N,
                       positionSizePercent, positionSize)
        elif price < lowest:
            indicator = "SHORT"
            send_email(i, indicator, "1d", price, highest, lowest, N,
                       positionSizePercent, positionSize)
Beispiel #17
0
 def get_bars(self, code, count, frame, end_dt=None):
     if end_dt is None:
         end_dt = arrow.now().datetime
     if isinstance(end_dt, arrow.Arrow):
         end_dt = end_dt.datetime
     if not (code.upper().endswith('.XSHE')
             or code.upper().endswith('.XSHG')):
         if code.startswith('60'):
             code += '.XSHG'
         else:
             code += '.XSHE'
     fields = [
         'date', 'open', 'high', 'low', 'close', 'volume', 'money', 'factor'
     ]
     return jq.get_bars(code,
                        count,
                        frame,
                        fields,
                        include_now=True,
                        end_dt=end_dt,
                        fq_ref_date=end_dt)
Beispiel #18
0
 def getPrice_bars(self,
                   security,
                   count,
                   unit='1d',
                   include_now=False,
                   fields=None,
                   end_date=None):
     # 查询数据
     try:
         if (fields == None):
             fields = [
                 'date', 'open', 'close', 'high', 'low', 'volume', 'money'
             ]
         values = jqdatasdk.get_bars(security=security,
                                     count=count,
                                     unit=unit,
                                     fields=fields,
                                     include_now=include_now,
                                     end_dt=end_date)
         return values
     except:
         return []
Beispiel #19
0
 def get_bars(
         self,
         security: str,  # Code. Can be `str` or `tuple` of `str`s.
         count:
     int = 1,  # PosInt. Number of bars. Make no sense if too large.
         unit:
     str = '15m',  # Time period of a bar. Can be one of '1/5/15/30/60/120m', '1d/w/M' &etc.
         fields: tuple = (
             'date',  # Fields to got.
             'open',
             'high',
             'low',
             'close',
             'volume',  # Deal Volume
             'money',  # Deal Money
             'open_interest',  # Unclosed Amount
             'factor'  # FQ factor
         ),
         include_now=False,  # Whether including `last` to `now` as an incomplete bar or not.
         end_dt=None,  # `datetime.datetime` or `None`. `datetime.now()` by default.
         fq_ref_date=None,  # Reference date for fq.
         df=True,  # Whether return `dataframe` or not.
 ):
     if isinstance(security, tuple):
         return {sec: self.get_bars(sec) for sec in security}
     path = f'./data/securities/{security}.{unit}.pkl'
     if hasattr(self, security):
         return object.__getattribute__(self, security)
     if os.path.exists(path):
         object.__setattr__(self, security, pd.read_pickle(path))
         return object.__getattribute__(self, security)
     if self.confirm_get(f'bars of {security}'):
         res = jq.get_bars(security, count, unit, fields, include_now,
                           end_dt, fq_ref_date, df)
         object.__setattr__(self, security, res)
         res.to_pickle(_exist_path(path))
         return res
     print('Got Nothing')
Beispiel #20
0
def _get_bar_range(jq: jq, code: str, season_date: datetime):
    """
    返回前3个月的涨幅,后两个月的最大涨幅
    """
    query_end_date = season_date + timedelta(days=63)

    if datetime.now() < query_end_date:
        return None

    df = jq.get_bars(jq.normalize_code(code),
                     6,
                     unit='1M',
                     fields=['date', 'open', 'high', 'low', 'close', 'volume'],
                     include_now=True,
                     end_dt=query_end_date)
    if df is None:
        return None
    size = df.shape[0]
    if size != 6:
        return None

    ##前3个月的涨幅
    ranges = []
    for i in range(1, 4):
        _close = float(df.at[i, 'close'])
        _close_pre = float(df.at[i - 1, 'close'])
        v = (_close - _close_pre) / _close_pre
        ranges.append(v)

    ###后两个月之内的最大涨幅
    close = float(df.at[3, 'close'])
    high = close
    for i in range(4, 6):
        _high = float(df.at[i, 'high'])
        if _high > high:
            high = _high
    max_range = (high - close) / close
    return ranges[0], ranges[1], ranges[2], max_range
Beispiel #21
0
 def get_data_once(self, stock_code, period, end_time=None, count=1):
     ''' 获取单个股票的历史数据,限制长度5000条记录 '''
     if end_time is None:
         end_time = datetime.datetime.now()
     if not (isinstance(count, int) and 0 < count <= 5000):
         count = 5000
     if not self.is_auth():
         self.connect_server()
     df = jqdatasdk.get_bars(
         security=stock_code,
         count=count,
         unit=period,
         fields=['date', 'open', 'high', 'low', 'close'],
         include_now=False,
         end_dt=end_time,
         fq_ref_date=None,
         df=True)
     if df.empty:
         raise ValueError(f'未能获取数据; {stock_code}, {period}')
     index_name = 'date'
     df.set_index(index_name, inplace=True)
     self.set_time_left(df, Minute(1))
     return df
Beispiel #22
0
def rep_index(index_name, factor_df, model):
    weight_dict = load_weight(index_name)

    res = pd.DataFrame()
    for dt, weight in tqdm(weight_dict.items()):
        weight = weight_dict[dt]
        end_dt = datetime.datetime.strptime(
            dt, '%Y-%m-%d') + relativedelta(months=+1) + pd.offsets.MonthEnd(0)
        if end_dt < factor_df.index.get_level_values('date').min():
            continue
        factor = factor_df.loc[pd.IndexSlice[:, end_dt.strftime('%Y-%m-%d')],
                               pd.IndexSlice[:]].reset_index(level=1)
        factor.index = renamer(factor.index.to_list())
        df = weight.merge(factor,
                          how="left",
                          left_index=True,
                          right_index=True,
                          suffixes=("_w", "_f"))
        # print("missing value in prediciton:", df.shape[0] - df.predict.count())
        res.loc[end_dt,
                'predict_return'] = (df['weight'] * df['predict']).sum()

    bm = jq.get_bars(index_name,
                     82,
                     unit='1M',
                     fields=['date', 'close'],
                     end_dt='2020-09-30',
                     include_now=True).set_index('date')
    bm = bm.pct_change().iloc[1:, :].rename({'close': 'true_return'}, axis=1)
    bm.index = bm.index + pd.offsets.MonthEnd(0)
    combine = res.merge(bm, left_index=True, right_index=True)
    combine['predict_return'] = combine['predict_return'] / 100
    combine.plot()
    plt.title(f"{model}")
    plt.show()
    stat, stat_df = cal_index_stat(combine, model)
    return combine, stat, stat_df
Beispiel #23
0
    async def get_bars(
        self,
        sec: str,
        end_at: Union[datetime.date, datetime.datetime],
        n_bars: int,
        frame_type: str,
        include_unclosed=True,
    ) -> np.array:
        """
        fetch quotes for security (code), and convert it to a numpy array
        consists of:
        index   date    open    high    low    close  volume money factor

        :param sec: security code in format "\\d{6}.{exchange server code}"
        :param end_at: the end_date of fetched quotes.
        :param n_bars: how many n_bars need to be fetched
        :param frame_type:
        :param include_unclosed: if True, then frame at end_at is included, even if
        it's not closed. In such case, the frame time will not aligned.
        :return:
        """
        if not self.connected:
            logger.warning("not connected")
            return None

        logger.debug("fetching %s bars for %s until %s", n_bars, sec, end_at)

        if type(end_at) not in [datetime.date, datetime.datetime]:
            raise TypeError(
                "end_at must by type of datetime.date or datetime.datetime")

        if type(end_at) is datetime.date:  # noqa
            end_at = datetime.datetime(end_at.year, end_at.month, end_at.day,
                                       15)
        try:
            bars = jq.get_bars(
                sec,
                n_bars,
                unit=frame_type,
                end_dt=end_at,
                fq_ref_date=None,
                df=False,
                fields=[
                    "date",
                    "open",
                    "high",
                    "low",
                    "close",
                    "volume",
                    "money",
                    "factor",
                ],
                include_now=include_unclosed,
            )
            # convert to omega supported format
            bars = np.array(
                bars,
                dtype=[
                    ("frame", "O"),
                    ("open", "f4"),
                    ("high", "f4"),
                    ("low", "f4"),
                    ("close", "f4"),
                    ("volume", "f8"),
                    ("amount", "f8"),
                    ("factor", "f4"),
                ],
            )
            if len(bars) == 0:
                logger.warning("fetching %s(%s,%s) returns empty result", sec,
                               n_bars, end_at)
                return bars

            if frame_type in minute_level_frames:
                bars["frame"] = [
                    frame.replace(tzinfo=self.tz) for frame in bars["frame"]
                ]

            return bars
        except Exception as e:  # pylint: disable=broad-except
            logger.exception(e)
            if str(e).find("最大查询限制") != -1:
                raise FetcherQuotaError("Exceeded JQDataSDK Quota") from e
            else:
                raise e
Beispiel #24
0
    async def get_bars_batch(
        self,
        secs: List[str],
        end_at: datetime.datetime,
        n_bars: int,
        frame_type: str,
        include_unclosed=True,
    ) -> np.array:
        if not self.connected:
            logger.warning("not connected.")
            return None

        if type(end_at) not in [datetime.date, datetime.datetime]:
            raise TypeError(
                "end_at must by type of datetime.date or datetime.datetime")

        # has to use type rather than isinstance, since the latter always return true
        # when check if isinstance(datetime.datetime, datetime.date)
        if type(end_at) is datetime.date:  # pylint: disable=unidiomatic-typecheck
            end_at = datetime.datetime(end_at.year, end_at.month, end_at.day,
                                       15)
        resp = jq.get_bars(
            secs,
            n_bars,
            frame_type,
            fields=[
                "date",
                "open",
                "high",
                "low",
                "close",
                "volume",
                "money",
                "factor",
            ],
            end_dt=end_at,
            include_now=include_unclosed,
            fq_ref_date=end_at,
            df=False,
        )
        results = {}
        for code, bars in resp.items():
            bars = np.array(
                bars,
                dtype=[
                    ("frame", "O"),
                    ("open", "f4"),
                    ("high", "f4"),
                    ("low", "f4"),
                    ("close", "f4"),
                    ("volume", "f8"),
                    ("amount", "f8"),
                    ("factor", "f4"),
                ],
            )

            if frame_type in minute_level_frames:
                bars["frame"] = [
                    frame.replace(tzinfo=self.tz) for frame in bars["frame"]
                ]

            results[code] = bars

        return results
Beispiel #25
0
a = pd.read_csv("./data/csv/all_securities.csv")
jqdatasdk.get_query_count()
# In[]

all_index = a[a["type"] == "index"]

for security in all_index.iloc[:, 0]:

    if os.path.exists(
            "./data/csv/daily/{security}.csv".format(security=security)):
        continue

    security_bar = jqdatasdk.get_bars(
        "000016.XSHG",
        3650,
        unit='1d',
        fields=['date', 'open', 'close', 'high', 'low', 'volume', 'money'],
        include_now=False,
        end_dt=None,
        fq_ref_date=None)

    security_bar.to_csv(
        "./data/csv/daily/{security}.csv".format(security=security),
        index=False)

#In[]

# {
#     "code": 0,
#     "msg": null,
#     "data": {
#         "fields": ["ts_code", "trade_date", "pre_close", "open", "high", "low", "close", "change", "pct_chg", "vol", "amount"],
Beispiel #26
0
import utils_jq
import jqdatasdk
bar_path = root_path + '/../mnt/data/bar/'

if len(sys.argv) >= 2: d = sys.argv[1]
else: d = datetime.datetime.now().strftime('%Y%m%d')
country = 'cn'

if not utils.is_bday(d, country):
    print('{} is {} holiday'.format(d, country))
    quit()
stocks = utils_jq.get_universe(utils.date_str(d))
e = jqdatasdk.get_bars(
    stocks,
    48,
    unit='5m',
    fields=['date', 'open', 'high', 'low', 'close', 'volume', 'money'],
    include_now=False,
    end_dt=utils.next_bday_from_str(d).strftime('%Y-%m-%d'),
    df=True,
    fq_ref_date=datetime.date.today())
if 0 == e.size:
    quit()
if e['date'][0].date() != datetime.datetime.strptime(d, '%Y%m%d').date():
    quit()
e = e.reset_index()
e = e.drop(columns=['level_1'])
e = e.rename(columns={'level_0': 'ric'})
print(bar_path + '{}.txt'.format(d))
e.to_csv(bar_path + '{}.txt'.format(d), sep='\t', index=False)
Beispiel #27
0
def get_stock_data(stock, count, end_date, unit):
    return jq.get_bars(security=stock, fields=['date','open','high','low','close'], count=count, unit=unit, include_now=False, end_dt=end_date, fq_ref_date=None)
Beispiel #28
0
def get_stock_data_multicycle(stock, count, end_date, units):
    map_unit = {}
    for u in units:
        data = jq.get_bars(security=stock, fields=['date','open','high','low','close'], count=count, unit=u, include_now=False, end_dt=end_date, fq_ref_date=None)
        map_unit[u] = data
    return map_unit
Beispiel #29
0
def history_bars_fundamentals_jq(order_book_ids,
                                 bar_count,
                                 frequency,
                                 fields=None,
                                 dt=None):
    """
    return: [pd.Panel] 
           Items: date
           Major: order_boook_id
           Minor: fields
    """
    if (frequency[-1] not in ['d']) and (frequency not in ['1w', '1M']):
        raise Exception("only support xd, 1w and 1M data")

    if frequency == '1d':
        data_df = _history_bars_fundamentals_jq(order_book_ids=order_book_ids,
                                                bar_count=bar_count,
                                                fields=fields,
                                                dt=dt)
        return data_df

    dt = DataProxy.get_instance().get_next_trading_date(dt, n=1)
    trading_data = get_bars(security='000001.XSHG',
                            count=bar_count,
                            unit=frequency,
                            fields=['date'],
                            include_now=True,
                            end_dt=dt,
                            fq_ref_date=None)
    date_list = trading_data['date'].tolist()
    date_str_list = [date.strftime("%Y-%m-%d") for date in date_list]
    #pdb.set_trace()
    if 'turnover_ratio' not in fields:
        data_df = get_fundamental_data(order_book_ids=order_book_ids,
                                       fields=fields,
                                       dt_list=date_str_list)
        return data_df
    else:
        fields.remove('turnover_ratio')
        data_df = get_fundamental_data(
            order_book_ids=order_book_ids,
            fields=fields,
            dt_list=date_str_list)  # difference frequency
        # scale
        scale = 1
        if frequency[-1] == 'd':
            scale *= int(frequency[:-1])
        elif frequency == "1w":
            scale *= 5
        elif frequency == '1M':
            scale *= 20
        bar_count_ = bar_count * scale

        # get turnover_ratio data
        turnover_df = _history_bars_fundamentals_jq(
            order_book_ids=order_book_ids,
            bar_count=bar_count_,
            fields=['turnover_ratio'],
            dt=dt)['turnover_ratio']

        timestamp_list = [
            convert_to_timestamp(date_str) for date_str in date_str_list
        ]
        frequency_date_df = pd.DataFrame(timestamp_list,
                                         index=timestamp_list,
                                         columns=['datetime'])
        frequency_date_df.index.name = "datetime"
        #pdb.set_trace()
        turnover = frequency_date_df.join(turnover_df, how='outer')

        turnover['datetime1'] = turnover.groupby(
            level="order_book_id")['datetime'].fillna(method="bfill")
        turnover = turnover.reset_index(level="order_book_id").groupby(
            by=['order_book_id', 'datetime1'])["turnover_ratio"].sum()
        turnover.rename_axis(index={"datetime1": "datetime"}, inplace=True)

        data = turnover.to_frame().join(data_df, how='outer')
        return data
    # jq.auth('15869688376', '688376')
    count = 12
    unit = '1M'
    stock_df = jq.get_all_securities(['stock'], '22000101')
    stock_df_exc_ST_DELIST = stock_df[
        (~stock_df.display_name.str.contains('ST'))
        & (~stock_df.display_name.str.contains('退市'))]
    price_df = jq.get_price(list(stock_df_exc_ST_DELIST.index),
                            end_date=datetime.date.today(),
                            count=1,
                            fill_paused=False)
    price_df.sort_values('code', inplace=True)
    avai_stock_list = list(price_df[~pd.isna(price_df['volume'])].code)
    # margin_list = jq.get_marginsec_stocks() 获取可融券列表
    market_df = jq.get_bars(avai_stock_list,
                            count,
                            unit, ['date', 'close'],
                            fq_ref_date=datetime.date.today())
    market_df.index.names = ['code', 'idx']
    market_df_copy = market_df.copy()
    market_df_copy['year'] = market_df_copy['date'].apply(lambda x: x.year)
    market_df_copy['month'] = market_df_copy['date'].apply(lambda x: x.month)
    market_df_copy.index = market_df_copy.index.droplevel(1)
    market_df_copy['pd_datetime'] = pd.to_datetime(market_df_copy.date)
    market_df_copy.reset_index(inplace=True)

    #---------------------------------get data--------------------------
    date_list = market_df_copy.date.unique()

    char_df = pd.DataFrame()
    for date in date_list:
        q = jq.query(jq.valuation.day, jq.valuation.code,