神经网络交易的训练部分
来源:https://uqer.io/community/share/55b8af12f9f06c91f818c607
import pybrain as brain
training_set = ("20050101", "20130101")
testing_set = ("20150101", "20150525")
universe = ['000001']
HISTORY = 10
from pybrain.datasets import SupervisedDataSet
def make_training_data():
ds = SupervisedDataSet(HISTORY, 1)
for ticker in universe:
raw_data = DataAPI.MktEqudGet(ticker=ticker, beginDate=training_set[0], endDate=training_set[1], field=[
'tradeDate', 'closePrice'
], pandas="1")
plist = list(raw_data['closePrice'])
for idx in range(1, len(plist) - HISTORY - 1):
sample = []
for i in range(HISTORY):
sample.append(plist[idx + i - 1] / plist[idx + i] - 1)
answer = plist[idx + HISTORY - 1] / plist[idx + HISTORY] - 1
ds.addSample(sample, answer)
return ds
def make_testing_data():
ds = SupervisedDataSet(HISTORY, 1)
for ticker in universe:
raw_data = DataAPI.MktEqudGet(ticker=ticker, beginDate=testing_set[0], endDate=testing_set[1], field=[
'tradeDate', 'closePrice'
], pandas="1")
plist = list(raw_data['closePrice'])
for idx in range(1, len(plist) - HISTORY - 1):
sample = []
for i in range(HISTORY):
sample.append(plist[idx + i - 1] / plist[idx + i] - 1)
answer = plist[idx + HISTORY - 1] / plist[idx + HISTORY] - 1
ds.addSample(sample, answer)
return ds
from pybrain.supervised.trainers import BackpropTrainer
def make_trainer(net, ds, momentum = 0.1, verbose = True, weightdecay = 0.01):
trainer = BackpropTrainer(net, ds, momentum = momentum, verbose = verbose, weightdecay = weightdecay)
return trainer
def start_training(trainer, epochs = 15):
trainer.trainEpochs(epochs)
def start_testing(net, dataset):
return net.activateOnDataset(dataset)
from pybrain.tools.customxml import NetworkWriter
def save_arguments(net):
NetworkWriter.writeToFile(net, 'huge_data.csv')
print 'Arguments save to file net.csv'
from pybrain.tools.shortcuts import buildNetwork
fnn = buildNetwork(HISTORY, 15, 7, 1)
training_dataset = make_training_data()
testing_dataset = make_testing_data()
trainer = make_trainer(fnn, training_dataset)
start_training(trainer, 5)
save_arguments(fnn)
print start_testing(fnn, testing_dataset)
Total error: 0.00226884924246
Total error: 0.00058242191557
Total error: 0.00058089738079
Total error: 0.000581061747831
Total error: 0.000580708420341
Arguments save to file net.csv
[[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]
[-0.00055257]]