Exemple #1
0
def data_to_indicators(data, o, h, l, c, v, window) -> pd.DataFrame:
    """
    Given a DataFrame of stock prices, return the data along with the corresponding indicators

    :param data: Data in C-OHLV format
    :param window: default value of window for indicators (only for indicators that support this)
    :param o: name of column containing 'OPEN' values
    :param h: name of column containing 'HIGH' values
    :param l: name of column containing 'LOW' values
    :param c: name of column containing 'CLOSE' values
    :param v: name of column containing 'VOLUME' values
    :return: DataFrame with all indicators
    """
    df = data.reset_index(drop=True)

    # Momentum
    # TODO PMO
    df['macd_' + str(window)] = macd(df[c], n_fast=int(window / 2), n_slow=window)
    df['rsi_' + str(window)] = rsi(df[c], n=window)
    df['wr_' + str(window)] = wr(df[h], df[l], df[c], lbp=window)
    df['mfi_' + str(window)] = money_flow_index(df[h], df[l], df[c], df[v], n=window)
    df['stochk_' + str(window)] = stoch(df[h], df[l], df[c], n=window)
    df['stochd_' + str(window)] = stoch_signal(df[h], df[l], df[c], n=window, d_n=3)

    # ROC
    df['roc_' + str(window)] = [(df[c][i] - df[c][i - window]) / df[c][i - window] if i >= window else np.nan
                                for i in range(0, df[c].size)]

    # Smoothing
    df['sma_' + str(window)] = simple_moving_average.simple_moving_average(df[c], period=window)
    df['wma_' + str(window)] = weighted_moving_average.weighted_moving_average(df[c], period=window)
    df['ema_' + str(window)] = exponential_moving_average.exponential_moving_average(np.array(df[c]), period=window)
    df['hma_' + str(window)] = hull_moving_average.hull_moving_average(np.array(df[c]), period=window)
    # df['cma_' + str(window)] = [df[c][i - int(window / 2): i + int(window / 2)].mean()
    #                             if (i >= int(window / 2) and (i + int(window / 2)) < df[c].size) else np.nan
    #                             for i in range(0, df[c].size)]

    # Overbought/Oversold Signals
    df['cci_' + str(window)] = cci(df[h], df[l], df[c], n=window, c=0.015)

    # Volume
    df['adl'] = acc_dist_index(df[h], df[l], df[c], df[v])
    df['cmf_' + str(window)] = chaikin_money_flow(df[h], df[l], df[c], df[v], n=window)
    df['obv'] = on_balance_volume(df[c], df[v])
    df['emv_' + str(window)] = ease_of_movement(df[h], df[l], df[c], df[v], n=window)

    # Volatility
    df['atr_' + str(window)] = average_true_range(df[h], df[l], df[c], n=window)
    df['mass_ind_' + str(window)] = mass_index(df[h], df[l], n=window / 2, n2=window)

    # Trends
    # How will model know b/w Ichmoku A and B?
    df['ichimoku_a'] = ichimoku_a(df[h], df[l], n1=9, n2=26)
    df['ichimoku_b'] = ichimoku_b(df[h], df[l], n2=26, n3=52)

    series_aroon_up = aroon_up(df[c], n=window)
    series_aroon_down = aroon_down(df[c], n=window)
    df['aroon_ind_' + str(window)] = series_aroon_up - series_aroon_down

    df['adx_' + str(window)] = adx(df[h], df[l], df[c], n=window)

    return df
 def test_wma_invalid_period(self):
     period = 128
     with self.assertRaises(Exception) as cm:
         weighted_moving_average.weighted_moving_average(self.data, period)
     expected = "Error: data_len < period"
     self.assertEqual(str(cm.exception), expected)
Exemple #3
0
#dataset_train = dataset_test
from pyti.simple_moving_average import simple_moving_average
x = dataset_train.iloc[:, 4].values
ans = simple_moving_average(read_float_with_comma(x), 10)

open = dataset_train.iloc[:, 1].values
high = dataset_train.iloc[:, 2].values
low = dataset_train.iloc[:, 3].values
close = read_float_with_comma(dataset_train.iloc[:, 4].values)
volume = read_float_with_comma(dataset_train.iloc[:, 5].values)

from pyti.simple_moving_average import simple_moving_average
sma = simple_moving_average(close, period)

from pyti.weighted_moving_average import weighted_moving_average
wma = weighted_moving_average(close, period)

from pyti.momentum import momentum
mome = momentum(close, period)

from pyti.relative_strength_index import relative_strength_index
rsi = relative_strength_index(close, period)

from pyti.moving_average_convergence_divergence import moving_average_convergence_divergence
macd = moving_average_convergence_divergence(close,
                                             short_period=1,
                                             long_period=10)

from pyti.commodity_channel_index import commodity_channel_index
cci = commodity_channel_index(close,
                              high_data=high,
 def test_wma_period_10(self):
     period = 10
     wma = weighted_moving_average.weighted_moving_average(
         self.data, period)
     np.testing.assert_array_equal(wma, self.wma_period_10_expected)
def convert(str):
    period = 10
    dataset_train = pd.read_csv(str)

    x = dataset_train.iloc[:, 4].values

    try:
        open = dataset_train.iloc[:, 1].values
    except Exception:
        open = read_float_with_comma(dataset_train.iloc[:, 1].values)
    try:
        high = dataset_train.iloc[:, 2].values
    except Exception:
        high = read_float_with_comma(dataset_train.iloc[:, 2].values)
    try:
        low = dataset_train.iloc[:, 3].values
    except Exception:
        low = read_float_with_comma(dataset_train.iloc[:, 3].values)
    try:
        close = dataset_train.iloc[:, 4].values
    except Exception:
        close = read_float_with_comma(dataset_train.iloc[:, 4].values)
    try:
        volume = dataset_train.iloc[:, 5].values
    except Exception:
        volume = read_float_with_comma(dataset_train.iloc[:, 5].values)

    from pyti.simple_moving_average import simple_moving_average
    sma = simple_moving_average(close, period)

    from pyti.weighted_moving_average import weighted_moving_average
    wma = weighted_moving_average(close, period)

    from pyti.momentum import momentum
    mome = momentum(close, period)

    from pyti.relative_strength_index import relative_strength_index
    rsi = relative_strength_index(close, period)

    from pyti.moving_average_convergence_divergence import moving_average_convergence_divergence
    macd = moving_average_convergence_divergence(close,
                                                 short_period=1,
                                                 long_period=10)

    from pyti.commodity_channel_index import commodity_channel_index
    cci = commodity_channel_index(close,
                                  high_data=high,
                                  low_data=low,
                                  period=period)

    from pyti.williams_percent_r import williams_percent_r
    willr = williams_percent_r(close)

    from pyti.accumulation_distribution import accumulation_distribution
    acd = accumulation_distribution(close_data=close,
                                    low_data=low,
                                    high_data=high,
                                    volume=volume)

    X = []
    Y = []
    for _ in range(10, len(open)):
        tmp = []
        tmp.append(sma[_])
        tmp.append(wma[_])
        tmp.append(mome[_])
        tmp.append(rsi[_])
        tmp.append(macd[_])
        tmp.append(cci[_])
        tmp.append(willr[_])
        X.append(tmp)
        Y.append([close[_]])

    X, Y = np.array(X), np.array(Y)
    return X, Y