Skip to content

Instantly share code, notes, and snippets.

@khuangaf
Last active January 22, 2018 03:00
Show Gist options
  • Save khuangaf/9e903beab78fae3b1d1487458f6640e1 to your computer and use it in GitHub Desktop.
Save khuangaf/9e903beab78fae3b1d1487458f6640e1 to your computer and use it in GitHub Desktop.
class DataSrc(object):
def _step(self):
# get history matrix from dataframe
data_window = self.data[:, self.step:self.step +
self.window_length].copy()
# (eq.1) prices
y1 = data_window[:, -1, 0] / data_window[:, -2, 0]
y1 = np.concatenate([[1.0], y1]) # add cash price
# (eq 18) X: prices are divided by close price
nb_pc = len(self.price_columns)
if self.scale:
last_close_price = data_window[:, -1, 0]
data_window[:, :, :nb_pc] /= last_close_price[:,np.newaxis, np.newaxis]
if self.scale_extra_cols:
# normalize non price columns
data_window[:, :, nb_pc:] -= self.stats["mean"][None, None, nb_pc:]
data_window[:, :, nb_pc:] /= self.stats["std"][None, None, nb_pc:]
data_window[:, :, nb_pc:] = np.clip(
data_window[:, :, nb_pc:],
self.stats["mean"][nb_pc:] - self.stats["std"][nb_pc:] * 10,
self.stats["mean"][nb_pc:] + self.stats["std"][nb_pc:] * 10
)
self.step += 1
history = data_window
done = bool(self.step >= self.steps)
return history, y1, done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment