Created
April 15, 2016 14:21
-
-
Save Nepoxx/bc446f81c248cacf44f2c26aa34f9927 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/*class Neuron { | |
Neuron(x, y) { | |
this.location = {x: x, y: y}; | |
this.connections = []; | |
this.sum = 0; | |
} | |
feedForward(input) { | |
this.sum +=input; | |
if(this.sum >1) { | |
this.fire(); | |
this.sum=0; | |
} | |
} | |
fire() { | |
for(var connection of this.connections) { | |
connection.feedForward(this.sum); | |
} | |
} | |
addConnection(connection) { | |
this.connections.push(connection); | |
} | |
} | |
class Network { | |
Network(x, y) { | |
this.neurons = []; | |
this.location = {x: x, y: y}; | |
} | |
connect(from,to) { | |
new Connection(from,to,random()); | |
from.addConnect(c); | |
} | |
} | |
class Connection { | |
Connection(from,to,w) { | |
this.a=from; | |
this.b=to; | |
this.weight = w; | |
} | |
feedForward(val) { | |
this.b.feedForward(val*this.weight); | |
} | |
} | |
class Perceptron { | |
Perceptron(n) { | |
this.weights = []; | |
this.c = 0.01; | |
for (var i = 0; i < n; i++) { | |
weights[i] = random(); | |
} | |
} | |
feedForward(inputs) { | |
var sum = 0; | |
for (var i = 0; i < this.weights.length; i++) { | |
sum += inputs[i] * this.weights[i]; | |
} | |
return activate(sum) | |
} | |
activate(sum) { | |
return sum > 0 ? 1 : -1; | |
} | |
train(inputs, desired) { | |
var guess = this.feedForward(inputs); | |
var error = desired - guess; | |
for (var i = 0; i < weights.length; i++) { | |
weights[i] += this.c * error * inputs[i]; | |
} | |
} | |
}*/ | |
//var synaptic = require('synaptic'); // this line is not needed in the browser | |
//var Architect = synaptic.Architect; | |
//var myNet = new Architect.Perceptron(3, 1, 1); | |
/*var trainingSet = [ | |
{ | |
input: [0.01,1,0.01],//1+1 | |
output: [0.02] | |
}, | |
{ | |
input: [0.02,1,0.04],//2+4 | |
output: [0.06] | |
}, | |
{ | |
input: [0.04,1,0.02],//4+2 | |
output: [0.06] | |
} | |
]*/ | |
var convnetjs = require("./convnet"); | |
var deepqlearn = require("./deepqlearn"); | |
// Count all of the links from the io.js build page | |
var readFileSync = require("fs").readFileSync; | |
var num_inputs = (6 * 5); // Date,Open High Low Close Volume | |
var num_actions = 3; // (Buy Open Exit Close), ( Sell Open Exit Close), ( Do Nothing) | |
//var actions = ['SELL']; | |
//var actions = ['BUY','NOTHING']; | |
//var actions = ['BUY', 'SELL']; | |
//var actions = ['SELL','NOTHING']; | |
var actions = ['BUY','SELL','NOTHING']; | |
var temporal_window = 1; // amount of temporal memory. 0 = agent lives in-the-moment :) | |
var network_size = num_inputs * temporal_window + num_actions * temporal_window + num_inputs; | |
// the value function network computes a value of taking any of the possible actions | |
// given an input state. Here we specify one explicitly the hard way | |
// but user could also equivalently instead use opt.hidden_layer_sizes = [20,20] | |
// to just insert simple relu hidden layers. | |
var layer_defs = []; | |
layer_defs.push({ type: 'input', out_sx: 1, out_sy: 1, out_depth: network_size }); | |
layer_defs.push({type:'fc', num_neurons:10, activation:'sigmoid'}); | |
layer_defs.push({ type: 'regression', num_neurons: num_actions }); | |
// options for the Temporal Difference learner that trains the above net | |
// by backpropping the temporal difference learning rule. | |
var batchSize = 10; | |
var tdtrainer_options = { learning_rate: 0.001, momentum: 0.0, batch_size: batchSize, l2_decay: 0.01 }; | |
var opt = {}; | |
opt.temporal_window = temporal_window; | |
opt.experience_size = 300000; | |
opt.start_learn_threshold = 100000; | |
opt.gamma = 0.7; | |
opt.learning_steps_total = 20000; | |
opt.learning_steps_burnin = 300; | |
opt.epsilon_min = 0.05; | |
opt.epsilon_test_time = 0.05; | |
opt.layer_defs = layer_defs; | |
opt.tdtrainer_options = tdtrainer_options; | |
var brain = null; | |
var numberOfTrades = 0; | |
var numberOfActualTrades = 0; | |
var totalRewards = 0; | |
var positiveTrades = 0; | |
var inputData = null | |
var averageReward = 0; | |
var Table = require('cli-table2'); | |
var csv = require("fast-csv"); | |
var fs = require("fs"); | |
var csvData = []; | |
function readMarketDataFromFile(callback) { | |
fs.createReadStream("history.csv") | |
.pipe(csv()) | |
.on("data", function (data) { | |
csvData.push(data); | |
}) | |
.on("end", function () { | |
callback(null,csvData); | |
}); | |
} | |
function Start() { | |
numberOfTrades = 0; | |
numberOfActualTrades = 0; | |
totalRewards = 0; | |
positiveTrades = 0; | |
brain = new deepqlearn.Brain(num_inputs, num_actions, opt); | |
brain.learning = true; | |
brain.epsilon_test_time = 1; // don't make any random choices, ever | |
csvData = JSON.stringify(wait.for(readMarketDataFromFile)); | |
inputData = JSON.parse(csvData); | |
while(true) { | |
ProcessData(); | |
} | |
} | |
var runningProcessData = 0; | |
var counter =0; | |
var time = new Date().getTime(); | |
function ProcessData() { | |
counter++; | |
if(counter%1000 == 0 ) { | |
console.log(new Date().getTime()-time); | |
time = new Date().getTime(); | |
} | |
runningProcessData++; | |
//console.log(1+Math.random()); | |
if ((numberOfTrades < inputData.length) && (batchSize + numberOfTrades <= inputData.length)) { | |
var arry = []; | |
for (var j = numberOfTrades; j < inputData.length && j < numberOfTrades + batchSize ; j++) { | |
arry.push(inputData[j]); | |
} | |
var averageReward = 0; | |
var reward = 0; | |
var action = brain.forward(arry); // returns index of chosen action | |
if (j < inputData.length) { | |
if (actions[action] == 'BUY') { | |
numberOfActualTrades += 1; | |
if(inputData[j] === null) { | |
console.log("wtf"); | |
console.log(inputData[j] ); | |
} | |
reward += inputData[j][5] - inputData[j][2]; | |
if(isNaN(reward)) { | |
console.log(inputData[j]); | |
console.log(j); | |
reward = 0; | |
} | |
} | |
else | |
if (actions[action] == 'SELL') { | |
numberOfActualTrades += 1; | |
reward += inputData[j][2] - inputData[j][5]; | |
if(isNaN(reward)) { | |
console.log(inputData[j]); | |
console.log(j); | |
reward = 0; | |
} | |
} | |
} | |
if (reward > 0) | |
positiveTrades += 1; | |
totalRewards += reward; | |
averageReward = (totalRewards / numberOfActualTrades); | |
brain.backward(reward); | |
inputData[numberOfTrades] = null; | |
numberOfTrades += 1; | |
if(numberOfActualTrades%250 == 0) { | |
var table = new Table({ | |
head: ['Description', 'Value'] | |
, colWidths: [100, 200] | |
}); | |
table.push( | |
['#TotalNoOfTrades', numberOfActualTrades], | |
['#TotalPipsGained', ((totalRewards) * 10000).toFixed(2)], | |
['#AverageProfitPerTrade', ((totalRewards / numberOfActualTrades) * 10000).toFixed(2)], | |
['PercentAgeCorrect',((positiveTrades / numberOfActualTrades) * 100).toFixed(2)], | |
["TotalReward",totalRewards], | |
['AverageReward',(averageReward * 100)] | |
); | |
//console.log(2+Math.random()); | |
//process.stdout.write('\033c'); | |
//console.log(totalRewards); | |
//console.log(table.toString()); | |
} | |
return; | |
} | |
else | |
{ | |
numberOfTrades = 0; | |
// if(runningProcessData === 1000) { | |
runningProcessData = 0; | |
inputData = JSON.parse(csvData); | |
//} else { | |
// TrainBrain(); | |
// } | |
return; | |
} | |
// | |
} | |
try { | |
var wait = require('wait.for'); | |
wait.launchFiber(Start); | |
} catch(exception) { | |
console.log(exception); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment