Пример #1
0
    def __init__(self,
                 code=None,
                 start_date=None,
                 end_date=None,
                 type=None,
                 **kwargs):
        assert code and start_date and type
        if type == 'stock':
            type = SecurityType.STOCK
        elif type == 'index':
            type = SecurityType.INDEX
        elif type == 'futures':
            type = SecurityType.FUTURES
        elif type == 'fund':
            type = SecurityType.FUND
        elif type == 'open_fund':
            type = SecurityType.OPEN_FUND
        else:
            type = SecurityType.UNKNOWN

        self._code = code
        self._start_date = datetime_utils.parse_date(start_date)
        if end_date:
            self._end_date = datetime_utils.parse_date(end_date)
        else:
            self._end_date = datetime.date(2200, 1, 1)
        self._type = type
        self._subtype = kwargs.pop('subtype', None)
        if self._type == SecurityType.FUTURES and self._subtype == 'commodity_futures':
            self._trade_times = self._parse_trade_times(
                kwargs.pop('trade_time'))
        self._parent = kwargs.pop('parent', None)
        self._name = kwargs.pop('name', '')
        self._display_name = kwargs.pop('display_name', '')
        self._extra = kwargs
Пример #2
0
 def _parse_trade_times(self, trade_time):
     ret = []
     for period in trade_time:
         start_date = parse_date(period[0])
         end_date = parse_date(period[1])
         minute_periods = [
             commodity_trade_time_point[i] for i in period[2:]
         ]
         ret.append(TradeTime(start_date, end_date, minute_periods))
     return ret
Пример #3
0
 def get_merged_info(self, code):
     ret = self._dic.get(code, None)
     if ret is None:
         return None
     return {
         'merge_date': parse_date(ret[2]),
         'target_code': ret[0],
         'scale_factor': ret[1]
     }
Пример #4
0
def get_trade_days(start_date=None, end_date=None, count=None):
    if start_date and count:
        raise ParamsError("start_date 参数与 count 参数只能二选一")
    if not (count is None or count > 0):
        raise ParamsError("count 参数需要大于 0 或者为 None")

    if not end_date:
        end_date = datetime.date.today()
    else:
        end_date = parse_date(end_date)

    store = get_calendar_store()
    if start_date:
        start_date = parse_date(start_date)
        return store.get_trade_days_between(start_date, end_date)
    elif count is not None:
        return store.get_trade_days_by_count(end_date, count)
    else:
        raise ParamsError("start_date 参数与 count 参数必须输入一个")
Пример #5
0
 def get_all_securities(self, types=[], date=None):
     '''
     所有证劵(不包括场外基金)
     :param types:
     :param date:
     :return:
     '''
     if not types:
         r = self.get_all_stocks()
     else:
         maps = {
             'stock': self.get_all_stocks,
             'index': self.get_all_indexs,
             'fund': self.get_all_funds,
             'futures': self.get_all_futures,
             'etf': self.get_all_etf,
             'lof': self.get_all_lof,
             'fja': self.get_all_fja,
             'fjb': self.get_all_fjb,
             'open_fund': self.get_all_otcfunds,
             'bond_fund': self.get_all_bondfunds,
             'stock_fund': self.get_all_stockfunds,
             'QDII_fund': self.get_all_QDIIfunds,
             'money_market_fund': self.get_all_money_market_funds,
             'mixture_fund': self.get_all_mixturefunds
         }
         r = {}
         for t in types:
             assert (t in ('stock', 'index', 'fund', 'futures', 'etf',
                           'lof', 'fja', 'fjb', 'open_fund', 'bond_fund',
                           'stock_fund', 'QDII_fund', 'money_market_fund',
                           'mixture_fund'))
             r.update(maps[t]())
     if date:
         from jqdata.utils.datetime_utils import parse_date
         if isinstance(date, six.string_types) and ":" in date:
             date = date[:10]
         date = parse_date(date)
         ss = [
             v for v in r.values()
             if (v.start_date <= date and date <= v.end_date)
         ]
     else:
         ss = r.values()
     ss = list(ss)
     ss.sort(key=lambda s: s.code)
     columns = ('display_name', 'name', 'start_date', 'end_date', 'type')
     res = dict(index=[s.code for s in ss],
                columns=columns,
                data=[[(getattr(s, c)) for c in columns] for s in ss])
     df = pd.DataFrame(**res)
     return df
Пример #6
0
def get_fundamentals(query_object, date=None, statDate=None):  # noqa
    if date and statDate:
        raise Exception('date和statDate参数只能输入一个')
    if date is None and statDate is None:
        cal_store = get_calendar_store()
        stock = SecurityStore.instance().get_security('000001.XSHE')
        date = cal_store.get_previous_trade_date(stock, _get_today())
    if date:
        yestoday = datetime.date.today() - datetime.timedelta(days=1)
        date = min(parse_date(date), yestoday)
    return jqdata.apis.get_fundamentals(query_object,
                                        date=date,
                                        statDate=statDate)
Пример #7
0
 def get_split_dividend(self, code, start_date, end_date):
     ret = []
     # 普通股票分红
     infos = self._dic.get(code)
     if infos:
         start_date_s = parse_date(start_date).strftime("%Y-%m-%d")
         end_date_s = parse_date(end_date).strftime("%Y-%m-%d")
         for info in infos:
             if start_date_s <= info[0] <= end_date_s:
                 # 每十股: 送股,转增股,派现税前,派现税后
                 date_s, stock_paid, into_shares, bonus_pre_tax = info[:4]
                 ret.append({
                     'date':
                     parse_date(date_s),
                     'bonus_pre_tax':
                     bonus_pre_tax / 10.0,
                     'scale_factor': (stock_paid + into_shares) / 10.0 + 1.,
                 })
         return ret
     # 场外基金分红
     if code.endswith('.OF'):
         if len(self._otcdic) == 0:
             self.load_open_fund()
     else:
         return ret
     otc_infos = self._otcdic.get(code)
     if otc_infos:
         start_date_s = parse_date(start_date).strftime("%Y-%m-%d")
         end_date_s = parse_date(end_date).strftime("%Y-%m-%d")
         for info in otc_infos:
             ex_date = info['ex_date']
             otc_ex_date = info['otc_ex_date']
             date = None
             if ex_date < '2200-01-01':
                 date = ex_date
             else:
                 date = otc_ex_date
             if start_date_s <= date <= end_date_s:
                 proportion = info['proportion']
                 if proportion:
                     proportion = float(proportion)
                 else:
                     proportion = 0
                 split_ratio = info['split_ratio']
                 if split_ratio:
                     split_ratio = float(split_ratio)
                 else:
                     split_ratio = 0
                 ret.append({
                     'date': parse_date(date),
                     'proportion': proportion,
                     'split_ratio': split_ratio,
                 })
         return ret
     return ret
Пример #8
0
 def __init__(self, f):
     with open(f, "rb") as store:
         self._dic = pickle.load(store)
         for code in self._dic.keys():
             self._dic[code] = [parse_date(s) for s in self._dic[code]]