(raw_data, lookback=10, horizon=10, gap=0, freq='B', included_assets=None, included_indicators=None,
              use_log=True)
return X, timestamps, y, asset_names, indicators
Convert raw data to features.

    Parameters
    ----------
    raw_data : pd.DataFrame
        Rows represents different timestamps stored in index. Note that there can be gaps. Columns are pd.MultiIndex
        with the zero level being assets and the first level indicator.

    lookback : int
        Number of timesteps to include in the features.

    horizon : int
        Number of timesteps to included in the label.

    gap : int
        Integer representing the number of time periods one cannot act after observing the features.

    freq : str
        Periodicity of the data.

    included_assets : None or list
        Assets to be included. If None then all available.

    included_indicators : None or list
        Indicators to be included. If None then all available.

    use_log : bool
        If True, then logarithmic returns are used (natural logarithm). If False, then simple returns.

    Returns
    -------
    X : np.ndarray
        Feature array of shape `(n_samples, n_indicators, lookback, n_assets)`.

    timestamps : pd.DateTimeIndex
        Per row timestamp of shape length `n_samples`.

    y : np.ndarray
        Targets arra of shape `(n_samples, n_indicators, horizon, n_assets)`.

    asset_names : list
        Names of assets.

    indicators : list
        List of indicators.
Convert raw data to features.
Convert raw data to features .
def raw_to_Xy(raw_data, lookback=10, horizon=10, gap=0, freq='B', included_assets=None, included_indicators=None,
              use_log=True):
    """Convert raw data to features.

    Parameters
    ----------
    raw_data : pd.DataFrame
        Rows represents different timestamps stored in index. Note that there can be gaps. Columns are pd.MultiIndex
        with the zero level being assets and the first level indicator.

    lookback : int
        Number of timesteps to include in the features.

    horizon : int
        Number of timesteps to included in the label.

    gap : int
        Integer representing the number of time periods one cannot act after observing the features.

    freq : str
        Periodicity of the data.

    included_assets : None or list
        Assets to be included. If None then all available.

    included_indicators : None or list
        Indicators to be included. If None then all available.

    use_log : bool
        If True, then logarithmic returns are used (natural logarithm). If False, then simple returns.

    Returns
    -------
    X : np.ndarray
        Feature array of shape `(n_samples, n_indicators, lookback, n_assets)`.

    timestamps : pd.DateTimeIndex
        Per row timestamp of shape length `n_samples`.

    y : np.ndarray
        Targets arra of shape `(n_samples, n_indicators, horizon, n_assets)`.

    asset_names : list
        Names of assets.

    indicators : list
        List of indicators.
    """
    if freq is None:
        raise ValueError('Frequency freq needs to be specified.')

    asset_names = included_assets if included_assets is not None else raw_data.columns.levels[0].to_list()
    indicators = included_indicators if included_indicators is not None else raw_data.columns.levels[1].to_list()

    index = pd.date_range(start=raw_data.index[0], end=raw_data.index[-1], freq=freq)

    new = pd.DataFrame(raw_data, index=index).ffill().bfill()

    to_exclude = []
    for a in asset_names:
        is_valid = np.all(np.isfinite(new[a])) and np.all(new[a] > 0)
        if not is_valid:
            to_exclude.append(a)

    asset_names = sorted(list(set(asset_names) - set(to_exclude)))

    absolute = new.iloc[:, new.columns.get_level_values(0).isin(asset_names)][asset_names]  # sort
    absolute = absolute.iloc[:, absolute.columns.get_level_values(1).isin(indicators)]

    returns = prices_to_returns(absolute, use_log=use_log)

    X_list = []
    y_list = []
    for ind in indicators:
        X, timestamps, y = (returns_to_Xy(returns.xs(ind, axis=1, level=1),
                                          lookback=lookback,
                                          horizon=horizon,
                                          gap=gap))
        X_list.append(X)
        y_list.append(y)

    X = np.concatenate(X_list, axis=1)
    y = np.concatenate(y_list, axis=1)

    return X, timestamps, y, asset_names, indicators
def raw_to_Xy ( raw_data , lookback = 10 , horizon = 10 , gap = 0 , freq = 'B' , included_assets = None , included_indicators = None , use_log = True ) : if freq is None : raise ValueError ( 'Frequency freq needs to be specified.' ) asset_names = included_assets if included_assets is not None else raw_data . columns . levels [ 0 ] . to_list ( ) indicators = included_indicators if included_indicators is not None else raw_data . columns . levels [ 1 ] . to_list ( ) index = pd . date_range ( start = raw_data . index [ 0 ] , end = raw_data . index [ - 1 ] , freq = freq ) new = pd . DataFrame ( raw_data , index = index ) . ffill ( ) . bfill ( ) to_exclude = [ ] for a in asset_names : is_valid = np . all ( np . isfinite ( new [ a ] ) ) and np . all ( new [ a ] > 0 ) if not is_valid : to_exclude . append ( a ) asset_names = sorted ( list ( set ( asset_names ) - set ( to_exclude ) ) ) absolute = new . iloc [ : , new . columns . get_level_values ( 0 ) . isin ( asset_names ) ] [ asset_names ] # sort absolute = absolute . iloc [ : , absolute . columns . get_level_values ( 1 ) . isin ( indicators ) ] returns = prices_to_returns ( absolute , use_log = use_log ) X_list = [ ] y_list = [ ] for ind in indicators : X , timestamps , y = ( returns_to_Xy ( returns . xs ( ind , axis = 1 , level = 1 ) , lookback = lookback , horizon = horizon , gap = gap ) ) X_list . append ( X ) y_list . append ( y ) X = np . concatenate ( X_list , axis = 1 ) y = np . concatenate ( y_list , axis = 1 ) return X , timestamps , y , asset_names , indicators