Esempio n. 1
0
def extract_stock_records(code='sh600000', start=None, end =None,adjust = 'qfq', identifier = 'a'):
    #qfq:前复权, hfq:后复权
    if identifier == 'a':
        result = ak.stock_zh_a_daily(symbol=code, adjust=adjust)
    elif identifier == 'hk':
        result = ak.stock_hk_daily(symbol=code, adjust=adjust)
    elif identifier == 'us':
        result = ak.stock_us_daily(symbol=code, adjust=adjust)
    result.index = result.index.tz_localize(None)
    result['date'] = result.index
    result = result[['date','open','close','high','low','volume']]
    result['total'] = 'NaN'
    result['amp'] = 'NaN'
    result.columns = ['date','open_price','close_price','high_price','low_price','trancaction','total','amp']
    result['rate'] = pd.Series([0]).append(pd.Series((result['close_price'].iloc[1:].values-result['close_price'].iloc[0:-1].values)/result['close_price'].iloc[0:-1].values)).values*100
    if start==None and end==None:
        pass
    else:
        result = result[(result['date']<=pd.Timestamp(end)) & (result['date']>=pd.Timestamp(start))]
    result['date'] = result['date'].apply(lambda x: (x.date()-datetime.date(1, 1, 1)).days)
    result.index = result.index.rename('')
    result = result.reset_index()
    return result[['date','open_price','close_price','low_price','high_price','trancaction','total','rate','amp']]
Esempio n. 2
0
 def getHK_stockQuotes(self, scode):
     data = ak.stock_hk_daily(symbol=scode, adjust="qfq")  #qfq: 前复权 hfq:后复权
     data = pd.DataFrame(data)
     return data
Esempio n. 3
0
 def get_stock_hk_daily(self, stockcode, adjust):
     stock_hk_daily_hfq_df = ak.stock_hk_daily(symbol=stockcode,
                                               adjust=adjust)
     return stock_hk_daily_hfq_df
Esempio n. 4
0
def get_hongkong_ma10(symbol1):
    stock_hk_daily_hfq_df = ak.stock_hk_daily(symbol=symbol1, adjust="")
    return stock_hk_daily_hfq_df.sort_index(ascending=False).head(10)[[
        'close'
    ]].mean()
Esempio n. 5
0
    def update_hk_all_stock_point_day(self, oneday_str):
        '''
        更新每一只港股指定的某一天历史行情
        :return:
        '''
        start_time = datetime.now()
        print("update_hk_all_stock_point_day 更新每一只港股指定的某一天历史行情 start_time:", start_time)

        # 获取所有港股代码字符串
        stock_exist = self.get_hk_stock_exist()
        stock_exist_list = stock_exist.split(',')

        df_list_tuple = []
        i = 0
        for symbol_name in stock_exist_list:
            #if symbol_name == '02936_朗廷-SS股权':
                try:
                    symbol_name_array = symbol_name.split("_")
                    if len(symbol_name_array) >= 2:
                        symbol = symbol_name_array[0]
                        name = symbol_name_array[1]
                        stock_hk_daily_df = ak.stock_hk_daily(symbol=symbol)
                        #print(stock_hk_daily_df, type(stock_hk_daily_df))
                except Exception as e:
                    stock_hk_daily_df = None
                    print(" stock_us_daily_df exception, reason:", e)

                df_list_list = stock_hk_daily_df.values.__array__() if stock_hk_daily_df is not None else []
                df_date_list = stock_hk_daily_df.axes[0].array if stock_hk_daily_df is not None else []

                count = len(df_date_list)

                if count > 0:
                    oneday_list = oneday_str.split(',')
                    for oneday in oneday_list:
                        #print('df_date_list', tuple(df_date_list))
                        #print('count', count)
                        last_date = datetime.date(df_date_list[count - 1])
                        #print('last_date', last_date)
                        onedate = self.exchange_oneday_to_date(oneday)
                        #print('onedate', onedate)
                        days = self.days_reduce(last_date, onedate)
                        #print('days', days)
                        onedate_index = count - 1 - days
                        #print('onedate_index', onedate_index)
                        if days >= 0 and onedate_index >= 0:
                            ondedate_lt = df_list_list[onedate_index]
                            # print('ondedate_lt', tuple(ondedate_lt))

                            df_list_tuple.append(
                                (symbol, onedate, float(ondedate_lt[0]), float(ondedate_lt[1]), float(ondedate_lt[2]),
                                 float(ondedate_lt[3]), float(ondedate_lt[4])))
                            print(name, symbol, onedate, float(ondedate_lt[0]), float(ondedate_lt[1]), float(ondedate_lt[2]),
                                  float(ondedate_lt[3]), float(ondedate_lt[4]))
                            i += 1
                            if i % 500 == 0:
                                df_tuple_tuple = tuple(df_list_tuple)
                                flag = False
                                try:
                                    flag = self.hk_stock_service.insert_all_stock_daily_batch(df_tuple_tuple)
                                    df_list_tuple = []
                                    print(oneday_str, flag, i)
                                except Exception as ex:
                                    print("update_hk_all_stock_point_day exception, reason:", ex)

        df_tuple_tuple = tuple(df_list_tuple)
        flag = False
        try:
            flag = self.hk_stock_service.insert_all_stock_daily_batch(df_tuple_tuple)
            print(oneday_str, flag, i)
        except Exception as ex:
            print("update_hk_all_stock_point_day exception, reason:", ex)

        end_time = datetime.now()
        time = (end_time - start_time)
        print("update_hk_all_stock_point_day 更新每一只港股指定的某一天历史行情 end_time:", end_time, "耗时:", time)
Esempio n. 6
0
    def get_hk_all_stock_daily(self):
        '''
        获取所有港股的历史行情,不包含今天
        :return:
        '''
        start_time = datetime.now()
        print("get_hk_all_stock_daily 获取所有港股的历史行情,不包含今天 start_time:", start_time)

        # 获取所有港股代码字符串
        stock_exist = self.hk_stock_service.get_hk_stock_exist(id=1)
        stock_exist_list = stock_exist.split(',')

        # 获取港股历史行情所有港股代码字符串
        all_stock_daily_exist = self.get_hk_all_stock_daily_exist()
        hk_all_stock_daily_str = all_stock_daily_exist
        hk_all_stock_daily_count = 0

        for symbol in stock_exist_list:
            # if symbol > '01858' and symbol < '02226':
            #     flag = hk_stock_service.test(symbol)
            #     print(symbol, flag)

            # if symbol == '00001':
            if all_stock_daily_exist.find(symbol) < 0:
                # print(symbol)
                # 根据港股代码获取某一只港股的所有历史行情
                try:
                    stock_hk_daily_df = ak.stock_hk_daily(symbol=symbol)
                    # print(stock_hk_daily_hfq_df, type(stock_hk_daily_df))
                except Exception as e:
                    stock_hk_daily_df = None
                    print(" stock_hk_daily_hfq_df exception, reason:", e)

                df_list_list = stock_hk_daily_df.values.__array__() if stock_hk_daily_df is not None else []
                df_date_list = stock_hk_daily_df.axes[0].array if stock_hk_daily_df is not None else []

                df_list_tuple = []
                for i in range(len(df_list_list)):
                    lt = df_list_list[i]
                    date = datetime.date(df_date_list[i])
                    # print(bool(np.isnan(lt[0])))
                    # print(type(np.isnan(lt[0])))
                    if np.isnan(lt[0]) == False:
                        df_list_tuple.append(
                            (date, float(lt[0]), float(lt[1]), float(lt[2]), float(lt[3]), float(lt[4])))
                    #     print(df_list_tuple)

                df_tuple_tuple = tuple(df_list_tuple)
                # print(df_tuple_tuple)
                flag = False
                try:
                    flag = self.hk_stock_service.insert_one_stock_all_daily_batch(symbol, df_tuple_tuple)
                    print(symbol, flag)
                except Exception as ex:
                    print("insert_stock_daily_batch exception, reason:", ex)
                if flag is True:
                    hk_all_stock_daily_count += 1
                    hk_all_stock_daily_str += symbol + ","

        # 更新港股历史行情所有港股代码字符串
        self.update_hk_stock_daily_exist(hk_all_stock_daily_str, 'hk_stock_daily', hk_all_stock_daily_count)

        end_time = datetime.now()
        time = (end_time - start_time)
        print("get_hk_all_stock_daily 获取所有港股的历史行情,不包含今天 end_time:", end_time, "耗时:", time)
Esempio n. 7
0
def update(config=None):
    import akshare as ak
    import pandas as pd
    import traceback
    import time

    today = dt.date.today()
    today_weekday = today.isoweekday()
    if today_weekday in [6, 7]:
        print(f'{today} has no update!')
        return

    config = config or load_json_settings('mongodb_settings.json')
    if not config:
        raise Exception('请先配置mongodb')

    print(f'connect to {config["host"]}:{config["port"]}')
    client = connect(alias='HKStock',
                     db='HKStock',
                     host=config['host'],
                     port=config['port'],
                     username=config['user'],
                     password=config['password'],
                     authentication_source='admin')

    client.get_database('admin').authenticate(name=config['user'],
                                              password=config['password'])

    db = client.get_database('HKStock')
    col_ohlcv = db.get_collection('stock_ohlcv')
    col_hfq = db.get_collection('stock_hfq_factor')
    col_qfq = db.get_collection('stock_qfq_factor')

    col_ohlcv.create_index([("code", 1)])
    col_hfq.create_index([("code", 1)])
    col_qfq.create_index([("code", 1)])
    col_ohlcv.create_index([("code", 1), ("datetime", 1)], unique=True)
    col_hfq.create_index([("code", 1), ("datetime", 1)], unique=True)
    col_qfq.create_index([("code", 1), ("datetime", 1)], unique=True)

    print(f'update code info!')
    big_df = ak.stock_hk_spot()[["symbol", "name", "engname"]]
    big_df.columns = ["code", "name", "engname"]
    big_df["market"] = 'hk'

    HKStock_Info_Code_Name.drop_collection()
    for _, data in big_df.iterrows():
        qs = HKStock_Info_Code_Name(code=data['code'],
                                    name=data['name'],
                                    engname=data['engname'],
                                    market=data['market'])
        qs.save()

    all_info = HKStock_Info_Code_Name.objects()

    print('start update stock data')
    for info in all_info:
        try:
            ohlcv_list = []
            hfq_list = []
            qfq_list = []

            last_day = (col_ohlcv.find_one(
                {'code': info.code}, sort=[('datetime', -1)]) or {}).get(
                    'datetime', dt.datetime(1970, 1, 1)) + dt.timedelta(days=1)
            if last_day.date() >= today:
                continue

            data = ak.stock_hk_daily(info.code)
            for d, v in data[last_day:].iterrows():
                ohlcv_list.append({
                    'datetime': d.to_pydatetime(),
                    'code': info.code,
                    **v.to_dict()
                })

            if ohlcv_list:
                col_ohlcv.insert_many(ohlcv_list, ordered=False)

            hfq_factor = ak.stock_hk_daily(info.code, adjust='hfq-factor')
            for d, r in hfq_factor.iterrows():
                hfq_list.append({
                    'datetime': d.to_pydatetime(),
                    'code': info.code,
                    'factor': float(r.hfq_factor),
                    'cash': float(r.cash)
                })

            col_hfq.delete_many({'code': info.code})
            col_hfq.insert_many(hfq_list)

            qfq_factor = ak.stock_hk_daily(info.code, adjust='qfq-factor')
            for d, r in qfq_factor.iterrows():
                qfq_list.append({
                    'datetime': d.to_pydatetime(),
                    'code': info.code,
                    'factor': float(r.qfq_factor)
                })

            col_qfq.delete_many({'code': info.code})
            col_qfq.insert_many(qfq_list)

            time.sleep(1)
        # except KeyError:
        #     continue
        except Exception:
            print(
                f'update {info.code} failed with error:\n {traceback.format_exc()})'
            )