def retrieve_company_info() -> DataFrame: df = pro_api().stock_basic() industry_df = unique_of( transform(df, [Pipeline('industry', Industry.industry.key)]), Industry.industry.key) insert_dataframe(industry_df, Industry) exchange_df = unique_of( transform(df, [ Pipeline('ts_code', ExchangeMarkets.exchange.key, lambda x: x.split('.')[-1]), ]), ExchangeMarkets.exchange.key) insert_dataframe(exchange_df, ExchangeMarkets) areas_df = unique_of(transform(df, [Pipeline('area', Areas.area.key)]), Areas.area.key) insert_dataframe(areas_df, Areas) stocks_df = transform(df, [ Pipeline('symbol', Stocks.code.key), Pipeline('name', Stocks.full_name.key), Pipeline('ts_code', Stocks.exchange.key, lambda x: x.split('.')[-1]), Pipeline('list_date', Stocks.list_date.key, ts_date_to_sql) ], preserve_list=['industry', 'area']) return stocks_df
def ts_daily_to_daily_price_summaries_table(df, trade_date=None): if trade_date: trade_date_pipeline = Pipeline([], DailyPriceSummaries.date.key, lambda: trade_date) else: trade_date_pipeline = Pipeline("trade_date", DailyPriceSummaries.date.key, ts_date_to_sql) return transform(df, [ Pipeline('ts_code', DailyPriceSummaries.exchange.key, lambda x: x.split('.')[-1]), Pipeline('ts_code', DailyPriceSummaries.code.key, lambda x: x.split('.')[0]), Pipeline('pre_close', DailyPriceSummaries.last_day_close.key), Pipeline('vol', DailyPriceSummaries.volume.key), Pipeline('amount', DailyPriceSummaries.turnover.key), trade_date_pipeline ], ['open', 'close', 'high', 'low'])
def retrieve_interval_price_summaries(market: str, code: str, interval_minute: int, data_num: int = 1023): params = { "symbol": market + str(code), "scale": interval_minute, "datalen": data_num, "ma": "no", } url = "http://money.finance.sina.com.cn/quotes_service/api/json_v2.php/CN_MarketData.getKLineData" with urlopen("%s?%s" % (url, urlencode(params))) as response: page = response.read() result = json.loads(page) df = DataFrame(result) price_df = transform(df, [ Pipeline([], IntervalPriceSummary.code.key, lambda: code), Pipeline([], IntervalPriceSummary.exchange.key, lambda: market), Pipeline("day", IntervalPriceSummary.time.key, lambda day_str: day_str.replace(" ", "T")), Pipeline([], IntervalPriceSummary.interval_len.key, lambda: time(minute=interval_minute)), ], ["open", "close", "high", "low", "volume"]) insert_dataframe(price_df, IntervalPriceSummary)
def ts_daily_to_stocks_transform(df): return transform(df, [ Pipeline('ts_code', Stocks.exchange.key, lambda x: x.split('.')[-1]), Pipeline('ts_code', Stocks.code.key, lambda x: x.split('.')[0]), ])
def ts_daily_to_exchange_table(df): return unique_of( transform(df, [ Pipeline('ts_code', ExchangeMarkets.exchange.key, lambda x: x.split('.')[-1]), ]), Stocks.exchange.key)