Skip to content

Instantly share code, notes, and snippets.

@lamres
Created September 3, 2019 17:42
Show Gist options
  • Select an option

  • Save lamres/900f1e6cf44ab6040f5509d6bcacd50f to your computer and use it in GitHub Desktop.

Select an option

Save lamres/900f1e6cf44ab6040f5509d6bcacd50f to your computer and use it in GitHub Desktop.
# Normalized st. deviation
def std_normalized(vals):
return np.std(vals) / np.mean(vals)
# Ratio of diff between last price and mean value to last price
def ma_ratio(vals):
return (vals[-1] - np.mean(vals)) / vals[-1]
# z-score for volumes and price
def values_deviation(vals):
return (vals[-1] - np.mean(vals)) / np.std(vals)
# Feature params
future_period = 1
std_period = 10
ma_period = 10
price_deviation_period = 10
volume_deviation_period = 10
# Create features
cols_features = ['last_return', 'std_normalized', 'ma_ratio', 'price_deviation', 'volume_deviation']
dataset['last_return'] = dataset[column_price].pct_change()
dataset['std_normalized'] = dataset[column_price].rolling(std_period).apply(std_normalized)
dataset['ma_ratio'] = dataset[column_price].rolling(ma_period).apply(ma_ratio)
dataset['price_deviation'] = dataset[column_price].rolling(price_deviation_period).apply(values_deviation)
dataset['volume_deviation'] = dataset[column_volume].rolling(volume_deviation_period).apply(values_deviation)
dataset["future_return"] = dataset[column_price].pct_change(future_period).shift(-future_period)
dataset = dataset.replace([np.inf, -np.inf], np.nan)
dataset = dataset.dropna()
# Split the data on sets
train_ind = int(np.where(dataset.index == '2018-01-01 00:00:00')[0])
train_set = dataset[cols_features].values[:train_ind]
test_set = dataset[cols_features].values[train_ind:]
# Plot features
plt.figure(figsize=(20,10))
fig, axs = plt.subplots(len(cols_features), 1, figsize = (15, 15))
colours = cm.rainbow(np.linspace(0, 1, len(cols_features)))
for i in range(0, len(cols_features)):
axs[i].plot(dataset.reset_index()[cols_features[i]], color = colours[i])
axs[i].set_title(cols_features[i])
axs[i].grid(True)
plt.tight_layout()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment