Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b48bdbd
commit 6b9db6e
Showing
6 changed files
with
244 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import gzip | ||
import os | ||
|
||
import numpy as np | ||
import six | ||
from six.moves.urllib import request | ||
|
||
from numpy import genfromtxt | ||
from sklearn.cross_validation import train_test_split | ||
from sklearn import preprocessing | ||
import numpy as np | ||
import dateutil.parser | ||
#ticket data wrting to some csv or temp array | ||
|
||
def prepare_data(scrip_id): | ||
stock_data = genfromtxt('/home/deep/development/deep_trading/ib/csv_data/' + scrip_id + '.csv', delimiter=',', dtype=None, names=True) | ||
output = None | ||
daily_gain = None | ||
supervised_data = {} | ||
output_data = [] | ||
new_data_list = [] | ||
intial_data = stock_data[0] | ||
for data in stock_data[1:]: | ||
final_value = data['Low'] | ||
intial_value = intial_data['High'] | ||
#can also add transaction fee | ||
if (final_value > intial_value): | ||
output = 1 #should have bought | ||
elif (final_value < intial_value): | ||
output = 2 #should have sold | ||
else: | ||
output = 0 #should have done nothing | ||
final_date = dateutil.parser.parse(data["DateTime"]).date() | ||
intial_date = dateutil.parser.parse(intial_data["DateTime"]).date() | ||
if final_date != intial_date: | ||
opening_price = data["Open"] | ||
closing_price = intial_data["Close"] | ||
gain = (opening_price - closing_price) / closing_price | ||
#converting into 0 to 1 from -1 to 1 | ||
#new_value = ( (old_value - old_min) / (old_max - old_min) ) * (new_max - new_min) + new_min | ||
daily_gain = ((gain - (-1)) / (1 - (-1)) ) * (1 - 0) + 0 | ||
if daily_gain is not None: | ||
#TODO can use this place for standardization also | ||
list_data = [daily_gain,intial_data["Low"], intial_data["High"], intial_data["Close"], intial_data["Open"], intial_data["Volume"]] | ||
output_data.append(output) | ||
new_data_list.append(list_data) | ||
stock_data = np.asarray(new_data_list) | ||
output_data = np.asarray(output_data) | ||
supervised_data["data"] = stock_data | ||
supervised_data["target"] = output_data | ||
return supervised_data | ||
|
||
def standardization(data): | ||
#Standard standardization with mean = 0 | ||
return preprocessing.scale(data) | ||
|
||
|
||
def find_average(data): | ||
return np.mean(data, axis=0) | ||
|
||
|
||
def load_stock_data(): | ||
return prepare_data("CANFINHOM") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import chainer | ||
import chainer.functions as F | ||
import chainer.links as L | ||
|
||
|
||
class MnistMLP(chainer.Chain): | ||
|
||
"""An example of multi-layer perceptron for MNIST dataset. | ||
This is a very simple implementation of an MLP. You can modify this code to | ||
build your own neural net. | ||
""" | ||
def __init__(self, n_in, n_units, n_out): | ||
super(MnistMLP, self).__init__( | ||
l1=L.Linear(n_in, n_units), | ||
l2=L.Linear(n_units, n_units), | ||
l3=L.Linear(n_units, n_out), | ||
) | ||
|
||
def __call__(self, x): | ||
h1 = F.relu(self.l1(x)) | ||
h2 = F.relu(self.l2(h1)) | ||
return self.l3(h2) | ||
|
||
|
||
class MnistMLPParallel(chainer.Chain): | ||
|
||
"""An example of model-parallel MLP. | ||
This chain combines four small MLPs on two different devices. | ||
""" | ||
def __init__(self, n_in, n_units, n_out): | ||
super(MnistMLPParallel, self).__init__( | ||
first0=MnistMLP(n_in, n_units // 2, n_units).to_gpu(0), | ||
first1=MnistMLP(n_in, n_units // 2, n_units).to_gpu(1), | ||
second0=MnistMLP(n_units, n_units // 2, n_out).to_gpu(0), | ||
second1=MnistMLP(n_units, n_units // 2, n_out).to_gpu(1), | ||
) | ||
|
||
def __call__(self, x): | ||
# assume x is on GPU 0 | ||
x1 = F.copy(x, 1) | ||
|
||
z0 = self.first0(x) | ||
z1 = self.first1(x1) | ||
|
||
# sync | ||
h0 = z0 + F.copy(z1, 0) | ||
h1 = z1 + F.copy(z0, 1) | ||
|
||
y0 = self.second0(F.relu(h0)) | ||
y1 = self.second1(F.relu(h1)) | ||
|
||
# sync | ||
y = y0 + F.copy(y1, 0) | ||
return y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#!/usr/bin/env python | ||
""" | ||
This is a supervised Feef forward network | ||
""" | ||
from __future__ import print_function | ||
import argparse | ||
|
||
import numpy as np | ||
import six | ||
|
||
import chainer | ||
from chainer import computational_graph | ||
from chainer import cuda | ||
import chainer.links as L | ||
from chainer import optimizers | ||
from chainer import serializers | ||
|
||
import data | ||
import net | ||
import pdb | ||
|
||
parser = argparse.ArgumentParser(description='Chainer example: MNIST') | ||
parser.add_argument('--initmodel', '-m', default='', | ||
help='Initialize the model from given file') | ||
parser.add_argument('--resume', '-r', default='', | ||
help='Resume the optimization from snapshot') | ||
parser.add_argument('--net', '-n', choices=('simple', 'parallel'), | ||
default='simple', help='Network type') | ||
parser.add_argument('--gpu', '-g', default=-1, type=int, | ||
help='GPU ID (negative value indicates CPU)') | ||
args = parser.parse_args() | ||
|
||
batchsize = 500 | ||
n_epoch = 20 | ||
n_units = 1000 | ||
|
||
# Prepare dataset | ||
print('load STOCK dataset') | ||
mnist = data.load_stock_data() | ||
mnist['data'] = mnist['data'].astype(np.float32) | ||
#mnist['data'] /= 255 | ||
mnist['target'] = mnist['target'].astype(np.int32) | ||
|
||
N = 300000 | ||
x_train, x_test = np.split(mnist['data'], [N]) | ||
y_train, y_test = np.split(mnist['target'], [N]) | ||
N_test = y_test.size | ||
|
||
# Prepare multi-layer perceptron model, defined in net.py | ||
if args.net == 'simple': | ||
model = L.Classifier(net.MnistMLP(6, n_units, 3)) | ||
if args.gpu >= 0: | ||
cuda.get_device(args.gpu).use() | ||
model.to_gpu() | ||
xp = np if args.gpu < 0 else cuda.cupy | ||
elif args.net == 'parallel': | ||
cuda.check_cuda_available() | ||
model = L.Classifier(net.MnistMLPParallel(6, n_units, 3)) | ||
xp = cuda.cupy | ||
|
||
# Setup optimizer | ||
optimizer = optimizers.Adam() | ||
optimizer.setup(model) | ||
|
||
# Init/Resume | ||
if args.initmodel: | ||
print('Load model from', args.initmodel) | ||
serializers.load_npz(args.initmodel, model) | ||
if args.resume: | ||
print('Load optimizer state from', args.resume) | ||
serializers.load_npz(args.resume, optimizer) | ||
|
||
# Learning loop | ||
for epoch in six.moves.range(1, n_epoch + 1): | ||
print('epoch', epoch) | ||
|
||
# training | ||
perm = np.random.permutation(N) | ||
sum_accuracy = 0 | ||
sum_loss = 0 | ||
for i in six.moves.range(0, N, batchsize): | ||
x = chainer.Variable(xp.asarray(x_train[perm[i:i + batchsize]])) | ||
t = chainer.Variable(xp.asarray(y_train[perm[i:i + batchsize]])) | ||
|
||
# Pass the loss function (Classifier defines it) and its arguments | ||
optimizer.update(model, x, t) | ||
|
||
if epoch == 1 and i == 0: | ||
with open('graph.dot', 'w') as o: | ||
g = computational_graph.build_computational_graph( | ||
(model.loss, ), remove_split=True) | ||
o.write(g.dump()) | ||
print('graph generated') | ||
|
||
sum_loss += float(model.loss.data) * len(t.data) | ||
sum_accuracy += float(model.accuracy.data) * len(t.data) | ||
|
||
print('train mean loss={}, accuracy={}'.format( | ||
sum_loss / N, sum_accuracy / N)) | ||
|
||
# evaluation | ||
sum_accuracy = 0 | ||
sum_loss = 0 | ||
for i in six.moves.range(0, N_test, batchsize): | ||
x = chainer.Variable(xp.asarray(x_test[i:i + batchsize]), | ||
volatile='on') | ||
t = chainer.Variable(xp.asarray(y_test[i:i + batchsize]), | ||
volatile='on') | ||
loss = model(x, t) | ||
sum_loss += float(loss.data) * len(t.data) | ||
sum_accuracy += float(model.accuracy.data) * len(t.data) | ||
|
||
print('test mean loss={}, accuracy={}'.format( | ||
sum_loss / N_test, sum_accuracy / N_test)) | ||
|
||
# Save the model and the optimizer | ||
print('save the model') | ||
serializers.save_npz('mlp.model', model) | ||
print('save the optimizer') | ||
serializers.save_npz('mlp.state', optimizer) |