Created
September 13, 2020 09:48
-
-
Save lakshaygupta21/28afbc01e305bc7c5fca160797a9fa7e to your computer and use it in GitHub Desktop.
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
const brain = require('brain.js') | |
const data = require('./data') | |
const express = require('express') | |
var app = express() | |
const cors = require('cors') | |
app.use(cors()) | |
var trainingData = [] | |
var maxClose = Math.max.apply(Math, data.map(function(o) { | |
return o.Close | |
})); | |
var maxHigh = Math.max.apply(Math, data.map(function(o) { | |
return o.High | |
})); | |
var maxLow = Math.max.apply(Math, data.map(function(o) { | |
return o.Low | |
})); | |
var maxOpen = Math.max.apply(Math, data.map(function(o) { | |
return o.Open | |
})); | |
for (var i = 0; i < data.length; i++) { | |
var input = [new Date(data[i].Date).getTime() / new Date().getTime(), data[i].High / maxHigh, data[i].Low / maxLow, data[i].Open / maxOpen] | |
var output = [data[i].Close / maxClose] | |
trainingData.push({ | |
'input': input, | |
'output': output | |
}) | |
} | |
const net = new brain.NeuralNetwork() | |
app.listen(process.env.PORT || 3001, async () => { | |
net.train(trainingData) | |
console.log('Server started') | |
}) | |
app.get('/:days', async (req, res) => { | |
var high = trainingData[trainingData.length - 1].input[1] / maxHigh; | |
var low = trainingData[trainingData.length - 1].input[2] / maxLow | |
var open = trainingData[trainingData.length - 1].input[3] / maxOpen | |
for (var i = 0; i < trainingData.length; i++) { | |
if (parseFloat(new Date().getTime() / (req.params.days * 86400000 + new Date().getTime())).toFixed(4) == trainingData[i].input[0].toFixed(4)) { | |
high = trainingData[i].input[1]; | |
low = trainingData[i].input[2]; | |
open = trainingData[i].input[3]; | |
break; | |
} | |
} | |
var output = net.run([parseFloat(new Date().getTime() / (req.params.days * 86400000 + new Date().getTime())), high, low, open]) | |
console.log(output[0] * maxClose) | |
res.json({ | |
'result': output[0] * maxClose | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment