Skip to content

Instantly share code, notes, and snippets.

@StuartFarmer
Created October 24, 2016 02:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StuartFarmer/1d1801a3160c2a8be2901bb0978a23c4 to your computer and use it in GitHub Desktop.
Save StuartFarmer/1d1801a3160c2a8be2901bb0978a23c4 to your computer and use it in GitHub Desktop.
Classification Forex Prediction Model
import csv
import random
import numpy as np
import matplotlib.pyplot as plt
import math
import os
os.environ["THEANO_FLAGS"] = "mode=FAST_RUN,device=gpu,floatX=float32,allow_gc=False"
from keras.models import Sequential
from keras.layers import Dense, LSTM, Flatten, TimeDistributed, Reshape
from keras.callbacks import ReduceLROnPlateau, ModelCheckpoint
from keras.layers.convolutional import Convolution2D
LINES_OF_DATA = 100000
PERIODS_PER_X = 180
PERIODS_FORWARD = 60
PIP_LEVEL = 10
NEG_PIP = [1, 0, 0]
NIL_PIP = [0, 1, 0]
POS_PIP = [0, 0, 1]
FILE_NAME = 'with_y_simple_all.csv'
# set up lists to contain data
raw_data = []
X = []
Y = []
print 'loading data'
f = open(FILE_NAME)
for line in f:
raw_data.append([float(x) for x in line.split(',')])
if len(raw_data) % 10000 == 0 and len(raw_data) > 0:
print 'loaded ' + str(len(raw_data))
if len(raw_data) >= LINES_OF_DATA:
break
f.close()
FEATURES = len(raw_data[0])
print raw_data[0]
# break raw_data into working array
print 'processing and shaping data'
for x in xrange(len(raw_data)-PERIODS_PER_X-PERIODS_FORWARD-1):
X.append(raw_data[x:x+PERIODS_PER_X])
for x in xrange(len(X)):
classifier_value = NIL_PIP
for y in xrange(PERIODS_FORWARD):
if X[x][-1][3] - raw_data[PERIODS_PER_X+x+y-1][3] >= PIP_LEVEL*0.0001:
classifier_value = POS_PIP
break
elif X[x][-1][3] - raw_data[PERIODS_PER_X+x+y-1][3] <= -PIP_LEVEL*0.0001:
classifier_value = NEG_PIP
break
Y.append(classifier_value)
print np.array(X).shape
print FEATURES
print 'setting up model'
model = Sequential()
# model.add(Reshape((1, PERIODS_PER_X, FEATURES), input_shape=(PERIODS_PER_X, FEATURES)))
# model.add(Convolution2D(64, 3, 3, border_mode='same'))
# model.add(Convolution2D(1, 1, 1, border_mode='same'))
# model.add(Reshape((PERIODS_PER_X, FEATURES)))
model.add(LSTM(PERIODS_PER_X, input_dim=FEATURES, input_length=PERIODS_PER_X, return_sequences=True))
model.add(LSTM(PERIODS_PER_X, input_dim=FEATURES, input_length=PERIODS_PER_X, return_sequences=True))
model.add(TimeDistributed(Dense(FEATURES)))
model.add(Flatten())
# if doesn't fit as well as other one, try adding more layers, or try using the old model with only price information (KISS)
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
filename = 'classification-model.h5'
save_best_model = ModelCheckpoint(filename, monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=False, mode='auto')
model.fit(X, Y, batch_size=100, nb_epoch=2500, verbose=1, callbacks=[save_best_model], validation_split=.3, validation_data=None, shuffle=True, class_weight=None, sample_weight=None)
print 'saving model'
model.save(filename)
print 'model saved'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment