Esempio n. 1
0
File: ffnn.py Progetto: zoding/ML4T
def bulid_TIs_dataset(stock_symbol,
                      start_date,
                      end_date,
                      window,
                      normalize=True):
    cols = ["Date", "Adj Close"]
    df = get_stock_data(stock_symbol, start_date, end_date, cols)
    df.rename(columns={"Adj Close": 'price'}, inplace=True)
    df['momentum'] = dpp.compute_momentum_ratio(df['price'], window)
    df['sma'] = dpp.compute_sma_ratio(df['price'], window)
    df['bolinger_band'] = dpp.compute_bollinger_bands_ratio(
        df['price'], window)
    df['actual_price'] = df['price']
    df = df[window:]
    scaler = None

    if normalize:
        scaler = MinMaxScaler()
        df['price'] = scaler.fit_transform(df['price'].values.reshape(-1, 1))
        df['momentum'] = scaler.fit_transform(df['momentum'].values.reshape(
            -1, 1))
        df['sma'] = scaler.fit_transform(df['sma'].values.reshape(-1, 1))
        df['bolinger_band'] = scaler.fit_transform(
            df['bolinger_band'].values.reshape(-1, 1))
        df['actual_price'] = scaler.fit_transform(
            df['actual_price'].values.reshape(-1, 1))

    print(df.head())
    print(df.tail())
    return df, scaler
Esempio n. 2
0
File: ffnn.py Progetto: zoding/ML4T
def bulid_new_TIs_dataset(stock_symbol,
                          start_date,
                          end_date,
                          window,
                          normalize=True):
    cols = ["Date", "Adj Close", "Volume"]
    df = get_stock_data(stock_symbol, start_date, end_date, cols)
    df.rename(columns={"Adj Close": 'price'}, inplace=True)
    df['momentum'] = dpp.compute_momentum_ratio(df['price'], window)
    df['sma'] = dpp.compute_sma_ratio(df['price'], window)
    df['bolinger_band'] = dpp.compute_bollinger_bands_ratio(
        df['price'], window)
    df['volatility'] = dpp.compute_volatility_ratio(df['price'], window)
    df['vroc'] = dpp.compute_vroc_ratio(df['Volume'], window)
    df['actual_price'] = df['price']
    df.drop(columns=["Volume"], inplace=True)
    df = df[window:]
    df.replace([np.inf, -np.inf], np.nan, inplace=True)
    df.fillna(method='ffill', inplace=True)
    df.fillna(method='bfill', inplace=True)
    scaler = None

    if normalize:
        scaler = MinMaxScaler()
        df['price'] = scaler.fit_transform(df['price'].values.reshape(-1, 1))
        df['momentum'] = scaler.fit_transform(df['momentum'].values.reshape(
            -1, 1))
        df['sma'] = scaler.fit_transform(df['sma'].values.reshape(-1, 1))
        df['bolinger_band'] = scaler.fit_transform(
            df['bolinger_band'].values.reshape(-1, 1))
        df['volatility'] = scaler.fit_transform(
            df['volatility'].values.reshape(-1, 1))
        df['vroc'] = scaler.fit_transform(df['vroc'].values.reshape(-1, 1))
        df['actual_price'] = scaler.fit_transform(
            df['actual_price'].values.reshape(-1, 1))

    print(df.head())
    print(df.tail())
    return df, scaler