Exemplo n.º 1
0
def build_ta_feature(symbol: str, daily: Sequence[tuple], 
                     save_feature: bool = True) -> pd.DataFrame:
    '''Build features using the daily time series data. Indicators like EMA
       should be computed using the complete set of data (instead of a subset)
        
       @param: symbol: stock symbol that the daily data belongs to
       @param: daily: daily time series data for a symbol
       @param: save_feature: whether to save the features to file
    '''
    logger.info('Build ta feature for: %s. Last daily date: %s', symbol, daily[-1][0])

    col_dtype = {'date': object, 'open': 'float64', 'high': 'float64', 'low': 'float64',
                 'close': 'float64', 'adj_close': 'float64', 'volume': 'int32'}
    df = pd.DataFrame(daily, columns=col_dtype.keys(), dtype=object)
    df = df.astype(col_dtype, copy=True)

    high, low, close, volume = df['high'], df['low'], df['close'], df['volume']

    # Overlap Studies 
    upperband, middleband, lowerband = Indicator.BBANDS(close, timeperiod=5)
    df['ub_r'], df['mb_r'], df['lb_r'] = upperband / close, middleband / close, lowerband / close
    df['trima_r'] = Indicator.TRIMA(close, timeperiod=30) / close
    df['wma_r'] = Indicator.WMA(close, timeperiod=30) / close

    # Momentum Indicator
    macd, macdsignal, macdhist = Indicator.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9)
    df['macdhist'] = macdhist
    df['ppo'] = Indicator.PPO(close, fastperiod=12, slowperiod=26)
    df['rsi'] = Indicator.RSI(close, timeperiod=14)

    # Volume Indicator
    df['adosc'] = Indicator.ADOSC(high, low, close, volume * 1.0, fastperiod=3, slowperiod=10)

    # Volatility Indicator
    df['natr'] = Indicator.NATR(high, low, close, timeperiod=14)

    # Drop rows with NaN values
    df.dropna(axis=0, how='any', inplace=True)

    # Drop useless columns
    col_drop = ['open', 'high', 'low', 'close', 'volume']
    df.drop(columns=col_drop, inplace=True)

    if save_feature:
        df.to_csv(_data_folder + '{0}_ta.csv'.format(symbol), float_format='%.4f', index=False)

    return df
    temp = name.split('-')
    temp_str = temp[0] + temp[1]
    model_name = temp_str.lower().split('.')[0]
    BASELINE_MODEL_PATH = 'model/model-baseline-new/model-{}'.format(
        model_name)
    PSO_MODEL_PATH = 'model/model-pso-new/model-pso-{}'.format(model_name)

    data = pd.read_csv(FILE_PATH)
    ann = keras.models.load_model(BASELINE_MODEL_PATH)
    pso = keras.models.load_model(PSO_MODEL_PATH)

    # iteration
    print('starting for {}'.format(name.split('.')[0]))
    # create indicator
    indicator = Indicator(data)
    indicator_data = indicator.RSI()
    indicator_data = indicator.EMA()
    indicator_data = indicator.MACD()
    indicator_data.dropna(inplace=True)
    indicator_data['change_of_ema'] = (
        (indicator_data['Close'] - indicator_data['ema_5_day']) /
        indicator_data['ema_5_day']) * 100
    data_set = indicator_data[[
        'Close', 'rsi', 'Histogram', 'change_of_ema', 'change'
    ]]

    simulator = Simulator(simulate_day=N_DAY,
                          simulate_data=data_set,
                          baseline_model=ann,
                          pso_model=pso)
    simulator.start()