def get_Stock_Price(
        self,
        code,
        end_date,
        count=10,
        start_date=None,
    ):
        if (start_date == None):
            return jq.get_price(
                code,
                count=100,
                end_date=end_date,
                fq='pre',
            )
        else:
            return jq.get_price(
                code,
                start_date=start_date,
                end_date=end_date,
                fq='pre',
            )


# query = InternationalIndice()
# data = query.get_Nasdaq_Composite_Index()
# print(data[['open','close','low','high']])
# print(data[['day','open','close','low','high']].to_json())
# print(numpy.array(data[['open','close','low','high']]))
# print(query.get_SSE_50_Index())
Esempio n. 2
0
 def get_min_data(self,
                  stock_list: list,
                  start_time: str,
                  end_time: str,
                  type_: str = "stock"):
     """
     获取分钟数据
     :param stock_list: 股票池
     :param start_time: 起始时间
     :param end_time: 结束时间
     :param type_: 股票 "stock" 或者 指数 "index" 类型选择
     :return: panel 数据格式 (聚宽取数据如果是多标的,返回的是 panel 格式)
     e.g.
     Dimensions: 6 (items) x 2 (major_axis) x 2 (minor_axis)
     Items axis: close to volume
     Major_axis axis: 2019-01-21 10:34:00 to 2019-01-21 10:35:00
     Minor_axis axis: 600000.XSHG to 000001.XSHE
     """
     if type_ not in ["stock", "index"]:
         raise ValueError("错误,目前仅支持股票与指数行情获取")
     if type_ == "stock":
         return jqdatasdk.get_price(
             security=stock_list,
             start_date=start_time,
             end_date=end_time,
             frequency="1m",
             fq="None",
         )
     else:
         return jqdatasdk.get_price(
             security="00000.XSHG",
             start_time=start_time,
             end_time=end_time,
             frequency="1m",
         )
Esempio n. 3
0
 def __saving_work(code, coll):
     QA_util_log_info("##JOB03 Now Saving STOCK_MIN ==== {}".format(code),
                      ui_log=ui_log)
     try:
         for type in ["1min"]:
             ref_ = coll.find({"code": str(code)[0:6], "type": type})
             end_time = str(now_time())[0:19]
             if ref_.count() > 0:
                 start_time = ref_[ref_.count() - 1]["datetime"]
                 QA_util_log_info(
                     "##JOB03.{} Now Saving {} from {} to {} == {}".format(
                         ["1min"].index(type),
                         str(code)[0:6],
                         start_time,
                         end_time,
                         type,
                     ),
                     ui_log=ui_log,
                 )
                 if start_time != end_time:
                     df = jqdatasdk.get_price(
                         security=code,
                         start_date=start_time,
                         end_date=end_time,
                         frequency=type[:2],
                     )
                     __data = __transform_jq_to_qa(df, code=code[:6])
                     if len(__data) > 1:
                         coll.insert_many(
                             QA_util_to_json_from_pandas(__data)[1::])
             else:
                 start_time = "2015-01-01 09:30:00"
                 QA_util_log_info(
                     "##JOB03.{} Now Saving {} from {} to {} == {}".format(
                         ["1min"].index(type),
                         str(code)[0:6],
                         start_time,
                         end_time,
                         type,
                     ),
                     ui_log=ui_log,
                 )
                 if start_time != end_time:
                     __data == __transform_jq_to_qa(
                         jqdatasdk.get_price(
                             security=code,
                             start_date=start_time,
                             end_date=end_time,
                             frequency=type[:2],
                         ),
                         code=code[:6],
                     )
                     if len(__data) > 1:
                         coll.insert_many(
                             QA_util_to_json_from_pandas(__data)[1::])
     except Exception as e:
         QA_util_log_info(e, ui_log=ui_log)
         err.append(code)
         QA_util_log_info(err, ui_log=ui_log)
Esempio n. 4
0
    def init_data(self, trade_date):
        """
        数据初始化
        """
        # 指数日线数据
        index_daily_info = jqdatasdk.get_price(
            security="000001.XSHG",
            start_date="2017-01-01",
            end_date=self.current_trade_date,
        )
        index_daily_info.index = pd.to_datetime(index_daily_info.index)
        index_daily_high = index_daily_info.[["high"]].rename(columns={"close": "000001"})
        index_daily_low = index_daily_info.[["low"]].rename(columns={"close": "000001"})
        index_daily_close = index_daily_info.[["close"]].rename(columns={"close": "000001"})
        # 股票日线数据
        stock_daily_info = jqdatasdk.get_price(
            security=self.subscribe_stocks,
            start_date="2017-01-01",
            end_date=self.current_trade_date,
        )
        df_stock_daily = stock_daily_info.to_frame().reset_index().rename(columns={"minor": "code", "major": "date"})
        df_stock_daily['code'] = df_stock_daily["code"].map(str).str.slice(0, 6)
        stock_daily_high = df_stock_daily.reset_index().pivot(index="date", columns="code", values="high")
        stock_daily_low = df_stock_daily.reset_index().pivot(index="date", columns="code", values="low")
        stock_daily_close = df_stock_daily.reset_index().pivot(index="date", columns="code", values="close")

        stock_daily_high.index = pd.to_datetime(stock_daily_high.index)
        stock_daily_low.index = pd.to_datetime(stock_daily_low.index)
        stock_daily_close.index = pd.to_datetime(stock_daily_close.index)

        # 周线数据处理
        index_weekly_high = index_daily_high.resample("w", closed="right").apply("max")
        index_weekly_low = index_daily_high.resample("w", closed="right").apply("min")
        index_weekly_close = index_daily_high.resample("w", closed="right").apply("last")
        stock_weekly_high = stock_daily_high.resample("w", closed="right").apply("max")
        stock_weekly_low = stock_daily_high.resample("w", closed="right").apply("min")
        stock_weekly_close = stock_daily_high.resample("w", closed="right").apply("last")

        # 考虑到春节等长假影响,可能会有一周以上的缺口
        index_weekly_high = index_weekly_high.dropna()
        index_weekly_low = index_weekly_low.dropna()
        index_weekly_close = index_weekly_close.dropna()


        stock_weekly_high = stock_weekly_high.loc[index_weekly_high.index]
        stock_weekly_low = stock_weekly_low.loc[index_weekly_low.index]
        stock_weekly_close = stock_weekly_close.loc[index_weekly_low.index]

        return {
            "stock_high": stock_weekly_high,
            "stock_low": stock_weekly_low,
            "stock_close": stock_weekly_close,
            "index_high": index_weekly_high,
            "index_low": index_weekly_low,
            "index_close": index_weekly_close,
        }
Esempio n. 5
0
 def analyze_profit(profit_calculate_stock_dict, start_date, end_date):
     # 构建结果df
     df = pd.DataFrame(index=profit_calculate_stock_dict.keys(), columns=[start_date, end_date, "Increment Ratio"])
     for index, row in df.iterrows():
         row[start_date] = \
             jq.get_price(profit_calculate_stock_dict[index], count=1, end_date=start_date, frequency='daily',
                          fields=['close']).iloc[0, 0]
         row[end_date] = \
             jq.get_price(profit_calculate_stock_dict[index], count=1, end_date=end_date, frequency='daily',
                          fields=['close']).iloc[0, 0]
         row["Increment Ratio"] = '{:.2f}%'.format((row[end_date] - row[start_date]) / row[start_date] * 100)
     return df
Esempio n. 6
0
def get_k_data_JQ(stk,
                  count=None,
                  start_date=None,
                  end_date=None,
                  freq='daily'):
    """
    使用JQData来下载stk的历史数据
    :param stk_code:
    :param amount:
    :return:
    """
    if 'm' in freq:
        end_date = add_date_str(get_current_date_str(), 1)

    if pd.isnull(end_date):
        end_date = get_current_date_str()
    try:

        # 增加以兼容list的情况
        if isinstance(stk, list):
            stk_code = [jqdatasdk.normalize_code(x) for x in stk]

            df = jqdatasdk.get_price(stk_code,
                                     frequency=freq,
                                     count=count,
                                     end_date=end_date,
                                     start_date=start_date)

        elif stk in ['sh', 'sz', 'cyb', 'hs300', 'sz50', 'zz500']:
            stk_code_normal = JQMethod.get_index_jq_code(stk)
            df = jqdatasdk.get_price(stk_code_normal,
                                     frequency=freq,
                                     count=count,
                                     start_date=start_date,
                                     end_date=end_date)
        else:
            df = jqdatasdk.get_price(jqdatasdk.normalize_code(stk),
                                     frequency=freq,
                                     count=count,
                                     end_date=end_date,
                                     start_date=start_date)

        if df.empty:
            return df

        df['datetime'] = df.index
        df['date'] = df.apply(lambda x: str(x['datetime'])[:10], axis=1)

        return df
    except Exception as e:
        print('函数get_k_data_JQ:出错!错误详情:\n' + str(e))
        return pd.DataFrame()
Esempio n. 7
0
def getPriceLeadingScore(
        stock_list,
        stock_index_dict,
        date_index_dict,
        inverse_date_index_dict,
        date,
        #interval_list = [1,2,3,5,7,9,11,13,17],
        #interval_weights = [1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0]):
        interval_list=[1, 20],
        interval_weights=[1.0, 1.0]):

    today_price = jq.get_price(stock_list,
                               start_date=date,
                               end_date=date,
                               frequency='daily',
                               fields=None,
                               skip_paused=False,
                               fq='pre',
                               count=None,
                               panel=False,
                               fill_paused=True)
    today_price = today_price.to_numpy()
    today_price_data = dataframe2Arr(today_price, stock_index_dict)

    return_score = []

    for i, interval in enumerate(interval_list):
        weight = interval_weights[i]
        befor_date = inverse_date_index_dict[date_index_dict[date] - interval]
        befor_price = jq.get_price(stock_list,
                                   start_date=befor_date,
                                   end_date=befor_date,
                                   frequency='daily',
                                   fields=None,
                                   skip_paused=False,
                                   fq='pre',
                                   count=None,
                                   panel=False,
                                   fill_paused=True)
        #print (befor_price)
        befor_price = befor_price.to_numpy()
        befor_price_data = dataframe2Arr(befor_price, stock_index_dict)
        score_i = getReturnScore(befor_price_data, today_price_data)

        return_score.append(score_i * interval_weights[i])

    return_score = np.array(return_score)

    return np.sum(return_score, axis=0)
Esempio n. 8
0
def downMinuteBarBySymbol(symbol, info, today, pre_trade_day):
    start = time()

    symbol_name = info['name']
    cl = db[symbol_name]
    cl.ensure_index([('datetime', ASCENDING)], unique=True)  # 添加索引

    # 在此时间段内可以获取期货夜盘数据
    #skip_paused=False get_price(security, start_date=None, end_date=None, frequency='daily',
    #fields=None, skip_paused=False, fq='pre', count=None)
    #minute_df = jqdatasdk.get_price(symbol, start_date=pre_trade_day + " 20:30:00",end_date=today + " 20:30:00", frequency='5minute')
    #minute_df = jqdatasdk.get_price(symbol, start_date=pre_trade_day + " 20:30:00",end_date=today + " 20:30:00", frequency='minute',skip_paused=True)
    minute_df = jqdatasdk.get_price(symbol,
                                    start_date='2016-01-01',
                                    end_date='2018-10-01',
                                    frequency='minute',
                                    skip_paused=True)
    #fields=['open': 'Open', 'close': 'Close', 'high': 'High', 'low': 'Low','volume': 'TotalVolume'],
    #self.df_day['index_number']=np.arange(len(self.df_day.index.date))
    # 将数据传入到数据队列当中
    for index, row in minute_df.iterrows():
        bar = generateVtBar(symbol_name, str(index), row)
        d = bar.__dict__
        flt = {'datetime': bar.datetime}
        cl.replace_one(flt, d, True)

    e = time()
    cost = (e - start) * 1000

    print(u'合约%s数据下载完成%s - %s,耗时%s毫秒' %
          (symbol_name, pre_trade_day, today, cost))
Esempio n. 9
0
    def request(self, url=None, method='get', param=None, path_fields=None):
        security_item = param['security_item']
        start_timestamp = param['start_timestamp']
        end_timestamp = param['end_timestamp']
        # 不复权
        df = get_price(
            to_jq_security_id(security_item),
            start_date=to_time_str(start_timestamp),
            end_date=end_timestamp,
            frequency=param['jq_level'],
            fields=['open', 'close', 'low', 'high', 'volume', 'money'],
            skip_paused=True,
            fq=None)
        df.index.name = 'timestamp'
        df.reset_index(inplace=True)
        df['name'] = security_item.name
        df.rename(columns={'money': 'turnover'}, inplace=True)

        df['timestamp'] = pd.to_datetime(df['timestamp'])
        df['provider'] = Provider.JOINQUANT.value
        df['level'] = param['level']

        # remove the unfinished kdata
        if is_in_trading(security_type='stock',
                         exchange='sh',
                         timestamp=df.iloc[-1, :]['timestamp']):
            df = df.iloc[:-1, :]

        return df.to_dict(orient='records')
 def get_internal_Index(self, code, count=100):
     return jq.get_price(
         code,
         count=count,
         end_date=datetime.date.today(),
         fq='pre',
     )
 def get_missing_data(self):
     global data_exist
     global path
     f = open("futures_list.txt", "r+")
     lis = f.read()
     data_exist = lis.split(" ")
     futures_dict = {}
     substring = self.f_kind
     for item in data_exist:
         if item == "":
             continue
         if substring in item:
             str = item + '.csv'
             future_info = pd.read_csv(self.path + str)
             # groupby datetime and get the size of futures of that day
             date_with_data = future_info.groupby(['time']).size()
             all_date = pd.date_range(
                 start=datetime.strptime(self.f_start, '%Y-%m-%d'),
                 end=datetime.strptime(self.f_end, '%Y-%m-%d'))
             date_with_data.index = pd.DatetimeIndex(date_with_data.index)
             # set new index to data series, if its NaN then 0
             date_with_data = date_with_data.reindex(all_date, fill_value=0)
             # get the date of missing data
             date_with_data = date_with_data[
                 date_with_data.index.dayofweek < 5]
             miss_date_list = []
             for index, value in date_with_data.items():
                 if value == 0:
                     miss_date_list.append(index.strftime('%Y-%m-%d'))
                     futures_dict[item] = miss_date_list
     for key, value in futures_dict.items():
         print(key, end=":")
         print(value)
     jq.auth('uraccount', 'urpassword')
     count = 0
     for item in futures_dict:
         date_list = []
         for time in futures_dict[item]:
             date_list.append(datetime.strptime(time, '%Y-%m-%d'))
         max_date = max(date_list)
         min_date = min(date_list)
         future_info = jq.get_price(security=item,
                                    start_date=min_date,
                                    end_date=max_date,
                                    frequency='1m')
         str = item + '.csv'
         future_info.to_csv(os.path.join(self.path, str), mode='a')
         count += 1
         print(count)
     for item in data_exist:
         if item == "":
             continue
         str = item + '.csv'
         df = pd.read_csv(self.path + str)
         df.drop_duplicates(subset=['time'], inplace=True)
         df.sort_values(by='time', inplace=True)
         df.dropna(inplace=True)
         df.to_csv(os.path.join(self.path, str),
                   index=False,
                   index_label='time')
Esempio n. 12
0
    def _get_stock_minute_data_first_minute(self, date: dt.datetime):
        renaming_dict = self._factor_param['行情数据']
        diff_columns = ['成交量', '成交额']
        tickers = self.stock_tickers.ticker(date)
        tickers = [self.windcode2jqcode(it) for it in tickers]

        auction_time = date + dt.timedelta(hours=9, minutes=25)
        auction_data = self.db_interface.read_table(
            '股票集合竞价数据', columns=['成交价', '成交量', '成交额'], dates=auction_time)
        auction_db_data = self._auction_data_to_price_data(auction_data)

        real_first_minute = date + dt.timedelta(hours=9, minutes=30)
        first_minute = date + dt.timedelta(hours=9, minutes=31)
        first_minute_raw = jq.get_price(tickers,
                                        start_date=first_minute,
                                        end_date=first_minute,
                                        frequency='1m',
                                        fq=None,
                                        fill_paused=True)
        first_minute_raw.time = real_first_minute
        first_minute_data = self._standardize_df(first_minute_raw,
                                                 renaming_dict)
        tmp = first_minute_data.loc[:, diff_columns].droplevel('DateTime').fillna(0) - \
              auction_db_data.loc[:, diff_columns].droplevel('DateTime').fillna(0)
        tmp['DateTime'] = real_first_minute
        tmp.set_index('DateTime', append=True, inplace=True)
        tmp.index = tmp.index.swaplevel()
        first_minute_db_data = pd.concat(
            [first_minute_data.drop(diff_columns, axis=1), tmp],
            sort=True,
            axis=1)
        db_data = pd.concat([auction_db_data, first_minute_db_data], sort=True)
        self.db_interface.insert_df(db_data, '股票分钟行情')
Esempio n. 13
0
def get_daily_line_n(sec_code, t_start, howmany):

    dt_delta = timedelta(days=howmany * 2 + 10)
    dt_end = t_start + dt_delta

    #print "    fetch daily line of %s, %s ~ %s" % ( sec_code, t_start, t_end  )

    try:

        df = jq.get_price(
            sec_code,
            start_date=t_start,
            end_date=dt_end,
            frequency='daily'
            #  默认是None(表示[‘open’, ‘close’, ‘high’, ‘low’, ‘volume’, ‘money’]这几个标准字段)
            ,
            fields=[
                'open', 'close', 'high', 'low', 'volume', 'money',
                'high_limit', 'low_limit', 'pre_close', 'paused', 'factor'
            ],
            skip_paused=True,
            fq='pre')
    except Exception as e:
        (t, v, bt) = sys.exc_info()
        traceback.print_exception(t, v, bt)
        print

        emsg = str(e)
        if '找不到标的' in emsg:
            return None
        else:
            raise e

    # 只要前 howmany 行
    return df.iloc[0:howmany - 1]
Esempio n. 14
0
    def load_data(self,start_date,end_date):

        jq.auth("15825675534",'Programming123')

        self.stock_list = ['000300.XSHG', '000001.XSHE']

        return jq.get_price(security=self.stock_list,start_date=start_date,end_date=end_date)
Esempio n. 15
0
    def record(self, entity, start, end, size, timestamps):
        if self.start_timestamp:
            start = max(self.start_timestamp, to_pd_timestamp(start))

        end = now_pd_timestamp() + timedelta(days=1)

        start_timestamp = to_time_str(start)
        end_timestamp = to_time_str(end)
        # 不复权
        df = get_price(to_jq_entity_id(entity), start_date=to_time_str(start_timestamp),
                       end_date=end_timestamp,
                       frequency=self.jq_trading_level,
                       fields=['open', 'close', 'low', 'high', 'volume', 'money'],
                       skip_paused=True, fq=None)
        df.index.name = 'timestamp'
        df.reset_index(inplace=True)
        df['name'] = entity.name
        df.rename(columns={'money': 'turnover'}, inplace=True)

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

        # remove the unfinished kdata
        if is_in_trading(entity_type='stock', exchange='sh', timestamp=df.iloc[-1, :]['timestamp']):
            df = df.iloc[:-1, :]

        return df.to_dict(orient='records')
Esempio n. 16
0
def indus300_compare(stk_list):
    """
    函数功能:处理沪深300与行业龙头股的数据
    :param stk_list: 例子['000002.XSHE', '600048.XSHG', '000069.XSHE','000300.XSHG']
    :return:
    """

    stk_all = stk_list + ['000300.XSHG']

    # 获取房地产的三大龙头与沪深300指数的close数据

    df = jqdatasdk.get_price(stk_all,
                             start_date='2010-01-01',
                             end_date='2018-10-01',
                             frequency='daily')
    df_close = df['close']

    # 沪深300年初10年年初买进3000块,其收益走势如下
    df_close['000300.XSHG_std'] = df_close.apply(
        lambda x: x['000300.XSHG'] *
        (len(stk_list) * 1000 / df_close.head(1)['000300.XSHG'].values[0]),
        axis=1)

    # 每支stk起始投资1000块
    for stk in stk_list:
        df_close[stk + '_std'] = df_close.apply(
            lambda x: x[stk] * (1000 / df_close.head(1)[stk].values[0]),
            axis=1)

    # 求取个股收益情况之和
    df_close['stk_sum'] = df_close.apply(
        lambda x: np.sum([x[stk_code + '_std'] for stk_code in stk_list]),
        axis=1)

    return df_close
Esempio n. 17
0
    def startJudgeAndRefreshStatus(self, currentTimeForTesting=None):
        self.pricePositions = []
        if self._shouldStartJudge(currentTimeForTesting) is False:
            return False
        if self.enableTrade:
            time.sleep(19)
        jqdatasdk.auth(self.jqDataAccount, self.jqDataPassword)
        nowTimeString = time.strftime('%Y-%m-%d %H:%M:%S',
                                      time.localtime(time.time()))
        if currentTimeForTesting is not None:
            nowTimeString = currentTimeForTesting
        self.df = jqdatasdk.get_price(security=self.security,
                                      count=self.dataRow,
                                      end_date=nowTimeString,
                                      frequency=self.frequency,
                                      fields=['close'])
        close = [float(x) for x in self.df['close']]
        self.df['EMA5'] = talib.EMA(np.array(close), timeperiod=5)
        self.df['EMA10'] = talib.EMA(np.array(close), timeperiod=10)
        self.df['EMA20'] = talib.EMA(np.array(close), timeperiod=20)
        self.indexList = self.df[self.df.EMA10 == self.df.EMA10].index.tolist()
        for index in self.indexList:
            ema5 = self.df.loc[index, 'EMA5']
            ema10 = self.df.loc[index, 'EMA10']
            emas = sorted([ema5, ema10], reverse=True)
            pricePosi = 0
            for ema in emas:
                if ema == ema5:
                    break
                pricePosi = pricePosi + 1

            self.pricePositions.append(pricePosi)

        self._refreshStatus(nowTimeString)
        return True
Esempio n. 18
0
 def data_fetch_for_stocks(self, stocknames, start_day, end_day,
                           read_data_from_local, path_of_data):
     assert read_data_from_local in [0, 1]
     if read_data_from_local == 1:
         remove = 0
     else:
         remove = 1
     remove_all_files(remove, path_of_data)
     dfs = []
     if read_data_from_local == 1:
         dfs = self.read_data_from_csv(path_of_data, start_day, end_day)
     else:
         if os.path.exists(path_of_data) is False:
             os.makedirs(path_of_data)
         for stockname in stocknames:
             df = jq.get_price(
                 stockname,
                 start_date=start_day,
                 end_date=end_day,
                 frequency="daily",
                 fields=["open", "close", "high", "low", "volume"],
             )
             dfs.append(df)
             df.to_csv(path_of_data + "/" + stockname + ".csv",
                       float_format="%.4f")
     return dfs
Esempio n. 19
0
def smart_get_md_close(t_day, code, md_that_day):
    if code in md_that_day:
        # ‘行情’ 是  [收盘价,前日收盘,涨幅, 涨停标志,停牌标志]
        return md_that_day[code][0]

    df = jq.get_price(
        code,
        start_date=t_day,
        end_date=t_day,
        frequency='daily'
        #  默认是None(表示[‘open’, ‘close’, ‘high’, ‘low’, ‘volume’, ‘money’]这几个标准字段)
        ,
        fields=[
            'open', 'close', 'high', 'low', 'volume', 'money', 'high_limit',
            'low_limit', 'pre_close', 'paused'
        ],
        skip_paused=False,
        fq='pre')

    row_count = len(df.index)

    if row_count < 1:
        raise Exception("无法获得%s于%s的行情" % (code, t_day))

    p = df['close'].iloc[0]

    #if math.isnan(p):
    #    print df
    #    raise  Exception ("无法获得%s于%s的收盘是NaN" %(code, t_day) )

    return p
Esempio n. 20
0
def get_t_day_in_mon(y, m, tday_no):
    t_start = "%d-%02d-01" % (y, m)

    if m == 12:
        y = y + 1
        m = 1
    else:
        m = m + 1

    t_end = "%d-%02d-01" % (y, m)

    df = jq.get_price('000300.XSHG',
                      start_date=t_start,
                      end_date=t_end,
                      frequency='daily',
                      fields=None,
                      skip_paused=True,
                      fq='pre')

    if len(df.index) < tday_no:
        return None

    #jqdata 返回的行的索引类型是  numpy.datetime64[ns]  (相当于time_t,以ns为单位)
    tt = df[tday_no - 1:tday_no].index.values[0]

    #print tt
    #print tt.dtype

    ts = (tt - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's')

    d = datetime.utcfromtimestamp(ts)

    #print d

    return d.date()
Esempio n. 21
0
def jqdownloadMinuteBarBySymbol(symbol, startDate, endDate):
    """下载某一合约的分钟线数据"""
    start = time()

    cl = dbMinute[symbol]
    cl.ensure_index([('datetime', ASCENDING)], unique=True)  # 添加索引

    df = jq.get_price(setting[symbol],
                      start_date=startDate,
                      end_date=endDate,
                      frequency='1m',
                      fields=FIELDS,
                      skip_paused=True)
    for ix, row in df.iterrows():
        bar = generateVtBar(row, symbol)
        d = bar.__dict__
        flt = {'datetime': bar.datetime}
        cl.replace_one(flt, d, True)

    end = time()
    cost = (end - start) * 1000

    print(u'合约%s的分钟K线数据下载完成%s - %s,耗时%s毫秒' %
          (symbol, df.index[0], df.index[-1], cost))
    print(jq.get_query_count())
def get_wgt_return(stockPool,date_start, date_end):
    
    def get_stock(stockPool, date):
        if stockPool == 'HS300':#用于获取沪深300股票池
            stockList = jd.get_index_stocks('000300.XSHG', date)
        elif stockPool == 'ZZ500':#用于获取中证500股票池
            stockList = jd.get_index_stocks('399905.XSHE', date)
        elif stockPool == 'ZZ800':#用于获取中证800股票池
            stockList = jd.get_index_stocks('399906.XSHE', date)   
        elif stockPool == 'A':#用于获取全部A股股票池
            stockList = jd.get_index_stocks('000002.XSHG', date) + jd.get_index_stocks('399107.XSHE', date)
        else:#自定义输入股票池
            stockList = stockPool
        return stockList 
    
    turnover_ratio=get_factor_data(stockPool, 'turnover_ratio',date_start, date_end)
    
    all_stocks = get_stock('ZZ500',date_start)
    close = jd.get_price(all_stocks,start_date=date_start, end_date=date_end,\
                frequency='daily', fields=['close'], skip_paused=False, fq='pre').loc['close']
    
    earnings_ratio = (close - close.shift(-1))/close * 100

    aa = earnings_ratio * turnover_ratio
    bb = aa.rolling(10,min_periods=10,axis=0).sum()
    cc = turnover_ratio.rolling(10,min_periods=10,axis=0).sum()
    
    wgt_return = bb/cc
    return wgt_return.dropna(axis=0,how='all')
Esempio n. 23
0
def download_minute_bar(vt_symbol):
    """下载某一合约的分钟线数据"""
    print(f"开始下载合约数据{vt_symbol}")
    symbol, exchange = vt_symbol.split(".")
    exchange = JQEX[exchange]

    start = time()

    df = rq.get_price(vt_symbol,
                      frequency="1m",
                      fields=FIELDS,
                      start_date='2019-04-16',
                      end_date='2019-05-08')

    bars = []
    for ix, row in df.iterrows():
        bar = generate_bar_from_row(row, symbol, exchange)
        bars.append(bar)

    database_manager.save_bar_data(bars)

    end = time()
    cost = (end - start) * 1000

    print("合约%s的分钟K线数据下载完成%s - %s,耗时%s毫秒" %
          (symbol, df.index[0], df.index[-1], cost))
Esempio n. 24
0
def get_data_yearly(f_list, f_start, f_end):
    future_info_yearly = jq.get_price(
        security=f_list,
        start_date=datetime.strptime(f_start, '%Y-%m-%d'),
        end_date=datetime.strptime(f_end, '%Y-%m-%d'),
        frequency='1d')
    return future_info_yearly
Esempio n. 25
0
    def get_future_daily(self, date: DateUtils.DateType):
        renaming_dict = self._factor_param['行情数据']
        tickers = self.future_tickers.ticker(date)
        tickers = [self.windcode2jqcode(it) for it in tickers]

        data = jq.get_price(tickers,
                            start_date=date,
                            end_date=date,
                            frequency='daily',
                            fq=None,
                            fill_paused=True,
                            fields=[
                                'open', 'high', 'low', 'close', 'volume',
                                'money', 'open_interest'
                            ])
        settle_data = jq.get_extras('futures_sett_price',
                                    tickers,
                                    start_date=date,
                                    end_date=date)
        settle = settle_data.stack().reset_index()
        settle.columns = ['time', 'code', 'settle']
        combined_data = pd.merge(data, settle)
        db_data = self._standardize_df(combined_data,
                                       renaming_dict).sort_index()
        self.db_interface.insert_df(db_data, '期货日行情')
def get_day_price(security, date):
    date_str = date
    date = datetime.strptime(date, "%Y-%m-%d")
    start_date = date.strftime("%Y-%m-%d")
    end_date = (date + timedelta(days=1)).strftime("%Y-%m-%d")
    fields = [
        "open", "close", "low", "high", "avg", "factor", "volume", "money",
        "paused"
    ]
    # start_date 能取到,end_date 取不到
    data_df = jq.get_price([security],
                           start_date=start_date,
                           end_date=end_date,
                           frequency='1m',
                           fields=fields,
                           skip_paused=False,
                           fq='post',
                           panel=False)
    if data_df.empty:
        return pd.DataFrame(
            {field: []
             for field in ["security"] + fields + ["date"]})
    data_df["event_time"] = data_df.index
    data_df = data_df.sort_values(by=["event_time"],
                                  ascending=[True]).reset_index(drop=True)
    day_data_dict = {
        col: "|".join(list(map(lambda x: str(x),
                               data_df[col].values.tolist())))
        for col in fields
    }
    day_data_df = pd.DataFrame(day_data_dict, index=[0])
    day_data_df["security"] = security
    day_data_df["date"] = date_str
    result_df = day_data_df[["security"] + fields + ["date"]]
    return result_df
Esempio n. 27
0
def get_RT_price(stk_code, source='jq'):

    if source == 'jq':
        # 使用聚宽数据接口替代
        if stk_code in ['sh', 'sz', 'cyb']:
            stk_code_normal = {
                'sh': '000001.XSHG',
                'sz': '399001.XSHE',
                'cyb': '399006.XSHE'
            }[stk_code]

        else:
            stk_code_normal = jq.normalize_code(stk_code)

        current_price = float(
            jq.get_price(stk_code_normal,
                         count=1,
                         end_date=get_current_date_str())['close'].values[0])

    elif source == 'ts':
        # 获取实时价格
        current_price = float(
            ts.get_realtime_quotes(stk_code)['price'].values[0])

    return current_price
Esempio n. 28
0
def get_daily_line(sec_code, t_start, t_end):

    k = (sec_code, t_start, t_end)

    if k in daily_line_fetched:
        #print "    skip fetching daily line of %s, %s ~ %s" % ( sec_code, t_start, t_end  )
        return None

    daily_line_fetched[k] = 1

    #print "    fetch daily line of %s, %s ~ %s" % ( sec_code, t_start, t_end  )
    df = jq.get_price(
        sec_code,
        start_date=t_start,
        end_date=t_end,
        frequency='daily'
        #  默认是None(表示[‘open’, ‘close’, ‘high’, ‘low’, ‘volume’, ‘money’]这几个标准字段)
        ,
        fields=[
            'open', 'close', 'high', 'low', 'volume', 'money', 'high_limit',
            'low_limit', 'pre_close', 'paused', 'factor'
        ],
        skip_paused=False,
        fq='pre')

    return df
Esempio n. 29
0
def downMinuteBarBySymbol(symbol, info, today, pre_trade_day):
    start = time()

    symbol_name = info['name']
    cl = db[symbol_name]
    cl.ensure_index([('datetime', ASCENDING)], unique=True)  # 添加索引

    # 在此时间段内可以获取期货夜盘数据
    minute_df = jqdatasdk.get_price(symbol,
                                    start_date=pre_trade_day + " 20:30:00",
                                    end_date=today + " 20:30:00",
                                    frequency='minute')

    # 将数据传入到数据队列当中
    for index, row in minute_df.iterrows():
        bar = generateVtBar(symbol_name, str(index), row)
        d = bar.__dict__
        flt = {'datetime': bar.datetime}
        cl.replace_one(flt, d, True)

    e = time()
    cost = (e - start) * 1000

    print(u'合约%s数据下载完成%s - %s,耗时%s毫秒' %
          (symbol_name, pre_trade_day, today, cost))
 def cc():
     close = jd.get_price(benchmark, start_date=start_date, end_date=end_date,\
               frequency='daily', fields=['close'], skip_paused=False, fq='pre')
     
     benchmark_return = ((close-close.shift(-1))/close)['close'].fillna(0)
     benchmark_var = np.var((close-close.shift(-1))/close)
     return (benchmark_return,benchmark_var)
Esempio n. 31
0
def get_price(code="600000.XSHG"):
    return jqdatasdk.get_price(code,end_date='2018-05-14')
Esempio n. 32
0
 def __saving_work(code, coll):
     QA_util_log_info(
         "##JOB03 Now Saving STOCK_MIN ==== {}".format(code), ui_log=ui_log)
     try:
         for type_ in ["1min", "5min", "15min", "30min", "60min"]:
             col_filter = {"code": str(code)[0:6], "type": type_}
             ref_ = coll.find(col_filter)
             end_time = str(now_time())[0:19]
             if coll.count_documents(col_filter) > 0:
                 start_time = ref_[coll.count_documents(
                     col_filter) - 1]["datetime"]
                 QA_util_log_info(
                     "##JOB03.{} Now Saving {} from {} to {} == {}".format(
                         ["1min",
                          "5min",
                          "15min",
                          "30min",
                          "60min"].index(type_),
                         str(code)[0:6],
                         start_time,
                         end_time,
                         type_,
                     ),
                     ui_log=ui_log,
                 )
                 if start_time != end_time:
                     df = jqdatasdk.get_price(
                         security=code,
                         start_date=start_time,
                         end_date=end_time,
                         frequency=type_.split("min")[0]+"m",
                     )
                     __data = __transform_jq_to_qa(
                         df, code=code[:6], type_=type_)
                     if len(__data) > 1:
                         coll.insert_many(
                             QA_util_to_json_from_pandas(__data)[1::])
             else:
                 start_time = "2015-01-01 09:30:00"
                 QA_util_log_info(
                     "##JOB03.{} Now Saving {} from {} to {} == {}".format(
                         ["1min",
                          "5min",
                          "15min",
                          "30min",
                          "60min"].index(type_),
                         str(code)[0:6],
                         start_time,
                         end_time,
                         type_,
                     ),
                     ui_log=ui_log,
                 )
                 if start_time != end_time:
                     __data == __transform_jq_to_qa(
                         jqdatasdk.get_price(
                             security=code,
                             start_date=start_time,
                             end_date=end_time,
                             frequency=type_.split("min")[0]+"m",
                         ),
                         code=code[:6],
                         type_=type_
                     )
                     if len(__data) > 1:
                         coll.insert_many(
                             QA_util_to_json_from_pandas(__data)[1::])
     except Exception as e:
         QA_util_log_info(e, ui_log=ui_log)
         err.append(code)
         QA_util_log_info(err, ui_log=ui_log)