BATCH_SIZE = 128 BUFFER_SIZE = 10000 EPOCHS = 10 CLASSIFICATION = True step = 1 history_size = 21 target_distance = 2 features_considered = [ 'Close', 'Volume', 'MA_short', 'MA_long', 'Wilders_EMA', 'bb_bbm', 'bb_bbh', 'bb_bbl', 'bb_bbhi', 'bb_bbli' ] # features_considered = ['Close', 'MA_short', 'MA_long', 'Wilders_EMA'] # Initialize Bollinger Bands Indicator e = EquityData('data/SPY.csv', 'SPY') indicator_bb = BollingerBands(close=e.close(), n=20, ndev=2) e.data['MA_short'] = moving_average(e, window=history_size) e.data['MA_long'] = moving_average(e, window=5) e.data['Wilders_EMA'] = e.close().ewm(alpha=1 / history_size, adjust=False).mean() # Add Bollinger Bands features e.data['bb_bbm'] = indicator_bb.bollinger_mavg() e.data['bb_bbh'] = indicator_bb.bollinger_hband() e.data['bb_bbl'] = indicator_bb.bollinger_lband() # Add Bollinger Band high indicator e.data['bb_bbhi'] = indicator_bb.bollinger_hband_indicator() # Add Bollinger Band low indicator e.data['bb_bbli'] = indicator_bb.bollinger_lband_indicator() e.data = e.data[21:]
import tensorflow as tf from utils import plot_train_history, multi_step_plot from technical_analysis import moving_average BATCH_SIZE = 128 BUFFER_SIZE = 10000 tf.random.set_seed(42) step = 1 history_size = 30 target_distance = 1 features_considered = ['Close', 'Volume', 'MA_short', 'MA_long'] e = EquityData('data/SPY.csv', 'SPY') e.data['MA_short'] = moving_average(e, window=21) e.data['MA_long'] = moving_average(e, window=5) e.data = e.data[21:] EVALUATION_INTERVAL = int(e.data.shape[0]/BATCH_SIZE) * 1 features = e.data[features_considered] assert(list(features)[0] == 'Close') features.index = e.date() dataset = features.values x_train_multi, y_train_multi, x_val_multi, y_val_multi = split_multivariate(dataset, history_size, target_distance, step, single_step=False) print ('Single window of past history : {}'.format(x_train_multi[0].shape)) print ('\n Target temperature to predict : {}'.format(y_train_multi[0].shape))
from market import EquityData from models.lstm import split, show_plot import tensorflow as tf tf.random.set_seed(42) BATCH_SIZE = 256 BUFFER_SIZE = 10000 e = EquityData('data/SPY.csv', 'SPY') x_train_uni, y_train_uni, x_val_uni, y_val_uni = split(e) train_univariate = tf.data.Dataset.from_tensor_slices( (x_train_uni, y_train_uni)) train_univariate = train_univariate.cache().shuffle(BUFFER_SIZE).batch( BATCH_SIZE).repeat() val_univariate = tf.data.Dataset.from_tensor_slices((x_val_uni, y_val_uni)) val_univariate = val_univariate.batch(BATCH_SIZE).repeat() simple_lstm_model = tf.keras.models.Sequential([ tf.keras.layers.LSTM(8, input_shape=x_train_uni.shape[-2:]), tf.keras.layers.Dense(1) ]) simple_lstm_model.compile(optimizer='adam', loss='mae') for x, y in val_univariate.take(1): print(simple_lstm_model.predict(x).shape) EVALUATION_INTERVAL = 200
from market import EquityData from models.lstm import split_multivariate from utils import create_time_steps import matplotlib.pyplot as plt import tensorflow as tf import numpy as np features_considered = ['Close', 'Volume'] e = EquityData('data/SPY.csv', 'SPY') features = e.data[features_considered] features.index = e.date() step = 1 history_size = 90 target_distance = 14 dataset = features.values train_split = int(len(dataset) * 0.7) data_mean = dataset[:train_split].mean(axis=0) data_std = dataset[:train_split].std(axis=0) data_no_transform = dataset[-history_size:] data_no_transform = np.reshape( data_no_transform, (1, data_no_transform.shape[0], data_no_transform.shape[1])) dataset = (dataset - data_mean) / data_std data = dataset[-history_size:] data = np.reshape(data, (1, data.shape[0], data.shape[1])) # model = tf.keras.models.load_model('saved_models/multivariate_multi_model')
ssm.add(tf.keras.layers.LSTM(128)) ssm.add(tf.keras.layers.Dense(64)) ssm.add(tf.keras.layers.Dense(1, activation='sigmoid')) ssm.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss=tf.keras.losses.BinaryCrossentropy(), metrics=['accuracy']) return ssm if __name__ == "__main__": # construct data and get additional info e = EquityData(DATA_DIR, DATA_SYM) data = pyfinancialdata.get_multi_year(provider='histdata', instrument='SPXUSD', years=[2016, 2017, 2018], time_group='10min') e.data = data # add MACD e.data['MA_long'] = e.data['close'].rolling(window=52).mean() e.data['MA_short'] = e.data['close'].rolling(window=7).mean() # evaluation interval window = int(e.data.shape[0] / BATCH_SIZE) * 1 # pick selected features features = e.data[FEATURES]
import tensorflow as tf from utils import plot_train_history, multi_step_plot from technical_analysis import moving_average tf.random.set_seed(42) BATCH_SIZE = 128 BUFFER_SIZE = 10000 EPOCHS = 15 step = 1 history_size = 30 target_distance = 1 features_considered = ['Close', 'Volume', 'MA_short', 'MA_long', 'Wilders_EMA'] e = EquityData('data/SPY.csv', 'SPY') e.data['MA_short'] = moving_average(e, window=21) e.data['MA_long'] = moving_average(e, window=5) e.data['Wilders_EMA'] = e.close().ewm(alpha=1/15, adjust=False).mean() e.data = e.data[21:] EVALUATION_INTERVAL = int(e.data.shape[0]/BATCH_SIZE) * 1 features = e.data[features_considered] assert(list(features)[0] == 'Close') features.index = e.date() features.plot(subplots=True) plt.show() dataset = features.values x_train_multi, y_train_multi, x_val_multi, y_val_multi = split_multivariate(dataset, history_size, target_distance, step, single_step=False)
history_size = 180 target_distance = 6 features_considered = [ 'Close', 'Volume', 'MA_short', 'MA_long', 'Wilders_EMA', 'bb_bbm', 'bb_bbh', 'bb_bbl', 'bb_bbhi', 'bb_bbli' ] features_considered = [ 'Close', 'Volume', 'Wilders_EMA', 'bb_bbm', 'bb_bbh', 'bb_bbl', 'bb_bbhi', 'bb_bbli', 'trend_adx', 'momentum_stoch_signal', 'trend_trix', 'trend_ichimoku_a', 'momentum_kama', 'momentum_tsi', 'volatility_kcli' ] features_considered = ['close', 'MA_long', 'MA_short'] # features_considered = ['Close', 'MA_short', 'MA_long', 'Wilders_EMA'] # Initialize Bollinger Bands Indicator e = EquityData('data/SPY.csv', 'SPY') data = pyfinancialdata.get_multi_year(provider='histdata', instrument='SPXUSD', years=[2016, 2017, 2018], time_group='10min') e.data = data indicator_bb = BollingerBands(close=e.data['close'], n=20, ndev=2) # e.data = add_all_ta_features(e.data, open="open", high="high", low="low", close="close") e.data['MA_long'] = e.data['close'].rolling(window=52).mean() e.data['MA_short'] = e.data['close'].rolling(window=7).mean() # e.data['MA_short'] = moving_average(e, window=5) # e.data['Wilders_EMA'] = e.close().ewm(alpha=1/history_size, adjust=False).mean() # # Add Bollinger Bands features # e.data['bb_bbm'] = indicator_bb.bollinger_mavg()
def test_equity_loads_data(): e = EquityData('data/SPY.csv', 'SPY') assert (type(e.data) == dict)