Ejemplo n.º 1
0
 def market_data_panel(self, freq="D"):
     start, end = self.date_range
     df = DataAPI.candle(self.universe, start=start, end=end,
                         freq=freq).transpose(2, 1, 0)
     if freq == "D":
         df.major_axis = df.major_axis.normalize()
     return df
Ejemplo n.º 2
0
def read_benchmark(start, end, index_code="000300.XSHG", freq="D"):
    """
    获取指数行情数据
    :param start:起始时间(datetime)
    :param end:结束时间(datetime)
    :param index_code:指数代码(str)
    :param freq:数据频率(str)
    :return:指数行情。返回一个benchmark对象
                     包含五个属性。"index", "close", "open", "high", "low"。
                     "index":一个MultiIndex Series的序列。索引为date (level 0) 和 asset (level 1),包含一列factor值。其中asset的值固定是index_code,
                      factor值固定是1。用于fxdayu_alphaman.selector.selector_analysis.get_stocklist_mean_return中
                      作为stock_list参数来计算指数收益。
                     "close":收盘价。(pandas.Dateframe ),index为datetime,column.name 为index_code,值为对应指数的收盘价。
                     "open":开盘价。(pandas.Dateframe ),index为datetime,column.name 为index_code,值为对应指数的开盘价。
                     "high":最高价。(pandas.Dateframe ),index为datetime,column.name 为index_code,值为对应指数的最高价。
                     "low":最低价。(pandas.Dateframe ),index为datetime,column.name 为index_code,值为对应指数的最低价。
    """
    benchmark = Benchmark()
    benchmark_value = DataAPI.candle((index_code,), freq=freq, start=start, end=end)
    benchmark.open = benchmark_value.minor_xs("open")
    if len(benchmark.open)==0:
        raise ValueError
    benchmark.high = benchmark_value.minor_xs("high")
    benchmark.low = benchmark_value.minor_xs("low")
    benchmark.close = benchmark_value.minor_xs("close")
    benchmark.open.index.name = "date"
    benchmark.high.index.name = "date"
    benchmark.low.index.name = "date"
    benchmark.close.index.name = "date"

    benchmark.index = pd.DataFrame(data=benchmark.open.index)
    benchmark.index["asset"] = index_code
    benchmark.index["factor"] = 1
    benchmark.index = benchmark.index.set_index(["date", "asset"])
    return benchmark
Ejemplo n.º 3
0
 def get_price_data(pool, start, end, max_window=10):
     data = DataAPI.candle(
         tuple(pool),
         "D",
         start=start - datetime.timedelta(days=max_window),
         end=end + datetime.timedelta(days=max_window))
     data = data.replace(to_replace=0, value=np.NaN)
     return data
Ejemplo n.º 4
0
    def market_data(self):
        start, end = self.date_range
        df = DataAPI.candle(self.universe,
                            fields=["close"],
                            start=start,
                            end=end,
                            freq=self._freq).loc[:, :, "close"].T

        df.index.name = "order_book_id"
        series = df.stack()
        series.name = "close"
        return series
Ejemplo n.º 5
0
 def benchmark_rets(self, code="000300.XSHG", freq="D", isIndex=True):
     start, end = self.date_range
     adjust = None
     if not isIndex:
         adjust = "before"
     benchmark = DataAPI.candle(code,
                                freq=freq,
                                fields="close",
                                start=start,
                                end=end,
                                adjust=adjust)
     benchmark_rets = benchmark["close"].pct_change()
     benchmark_rets.name = "benchmark_rets"
     if freq == "D":
         benchmark_rets.index = benchmark_rets.index.normalize()
     return benchmark_rets
Ejemplo n.º 6
0
def read_benchmark(start, end, index_code="000300.XSHG", freq="D"):
    """
    获取指数行情数据
    :param start:起始时间(datetime)
    :param end:结束时间(datetime)
    :param index_code:指数代码(str)
    :param freq:数据频率(str)
    :return:指数行情。返回一个benchmark对象
                     包含五个属性。"index", "close", "open", "high", "low"。
                     "index":一个MultiIndex Series的序列。索引为date (level 0) 和 asset (level 1),包含一列factor值。其中asset的值固定是index_code,
                      factor值固定是1。用于fxdayu_alphaman.selector.selector_analysis.get_stocklist_mean_return中
                      作为stock_list参数来计算指数收益。
                     "close":收盘价。(pandas.Dateframe ),index为datetime,column.name 为index_code,值为对应指数的收盘价。
                     "open":开盘价。(pandas.Dateframe ),index为datetime,column.name 为index_code,值为对应指数的开盘价。
                     "high":最高价。(pandas.Dateframe ),index为datetime,column.name 为index_code,值为对应指数的最高价。
                     "low":最低价。(pandas.Dateframe ),index为datetime,column.name 为index_code,值为对应指数的最低价。
    """
    benchmark = Benchmark()
    try:
        benchmark_value = DataAPI.candle((index_code, ),
                                         freq=freq,
                                         start=start,
                                         end=end)
        benchmark.open = benchmark_value.minor_xs("open")
        if len(benchmark.open) == 0:
            raise ValueError
        benchmark.high = benchmark_value.minor_xs("high")
        benchmark.low = benchmark_value.minor_xs("low")
        benchmark.close = benchmark_value.minor_xs("close")
        benchmark.open.index.name = "date"
        benchmark.high.index.name = "date"
        benchmark.low.index.name = "date"
        benchmark.close.index.name = "date"
    except:
        index_value = ts.get_k_data(code=index_code[0:6],
                                    start=start.strftime("%Y-%m-%d"),
                                    end=end.strftime("%Y-%m-%d"),
                                    ktype=freq,
                                    index=True)
        date = index_value.pop('date')
        index_value["date"] = pd.to_datetime(date + " 15:00:00",
                                             format='%Y-%m-%d %H:%M:%S')
        benchmark.close = index_value[["date", "close"]]
        benchmark.open = index_value[["date", "open"]]
        benchmark.high = index_value[["date", "high"]]
        benchmark.low = index_value[["date", "low"]]
        benchmark.close.columns = ["date", index_code]
        benchmark.open.columns = ["date", index_code]
        benchmark.high.columns = ["date", index_code]
        benchmark.low.columns = ["date", index_code]
        benchmark.close = benchmark.close.set_index("date")
        benchmark.open = benchmark.open.set_index("date")
        benchmark.high = benchmark.high.set_index("date")
        benchmark.low = benchmark.low.set_index("date")

    benchmark.index = pd.DataFrame(data=benchmark.open.index)
    benchmark.index["asset"] = index_code
    benchmark.index["factor"] = 1
    benchmark.index = benchmark.index.set_index(["date", "asset"])

    return benchmark
Ejemplo n.º 7
0
from fxdayu_data import DataAPI

print(DataAPI.candle("000001.XSHE"))