Created
September 25, 2015 18:30
-
-
Save Anderson-Juhasc/5e48dfc53de126995f35 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
var OKCoin = require("okcoin-ws"); | |
var talib = require("talib"); | |
var fs = require("fs"); | |
var jsonfile = require("jsonfile"); | |
var _ = require("lodash"); | |
var Q = require("q"); | |
var buy_treshold = 0.25; | |
var sell_treshold = 0.25; | |
var file = 'okcoin.json'; | |
var obj = {}; | |
// execute SMA indicator function with time period 180 | |
function ta(name, inReal, period, callback) { | |
talib.execute({ | |
name: name, | |
startIdx: 0, | |
endIdx: inReal.close.length - 1, | |
inReal: inReal.close, | |
optInTimePeriod: period | |
}, function (data) { | |
if (typeof(data.result) == 'undefined') { | |
callback([]); | |
} else { | |
callback(data.result.outReal); | |
} | |
}); | |
} | |
var okcoin = new OKCoin('cn'); | |
var marketData = {}; | |
okcoin.subscribe({ | |
'ok_btccny_kline_1min': function cb(data, err) { | |
jsonfile.readFile(file, function (err, obj) { | |
marketData = obj; | |
console.log(marketData.close.length); | |
if (!isNaN(data[0])) { | |
obj.timestamp.push(data[0]); | |
obj.open.push(data[1]); | |
obj.high.push(data[2]); | |
obj.low.push(data[3]); | |
obj.close.push(data[4]); | |
obj.volume.push(data[5]); | |
} else { | |
data.forEach(function(e, index, array) { | |
obj.timestamp.push(array[index][0]); | |
obj.open.push(array[index][1]); | |
obj.high.push(array[index][2]); | |
obj.low.push(array[index][3]); | |
obj.close.push(array[index][4]); | |
obj.volume.push(array[index][5]); | |
}); | |
} | |
var demaData14; | |
var dema14 = function() { | |
var d = Q.defer(); | |
ta('DEMA', marketData, 14, function(result) { | |
demaData14 = result; | |
d.resolve(); | |
}); | |
return d.promise; | |
}; | |
var demaData21; | |
var dema21 = function() { | |
var d = Q.defer(); | |
ta('DEMA', marketData, 21, function(result) { | |
demaData21 = result; | |
d.resolve(); | |
}); | |
return d.promise; | |
}; | |
Q.all([ dema14(), dema21() ]).done(function() { | |
var short = _.last(demaData14); | |
var long = _.last(demaData21); | |
var diff = 100 * (short - long) / ((short + long) / 2) | |
console.log(short); | |
console.log(long); | |
console.log(diff); | |
if (diff > buy_treshold) { | |
console.log("Buy"); | |
} else { | |
if (diff < -sell_treshold) { | |
console.log("Sell"); | |
} | |
} | |
}); | |
jsonfile.writeFile(file, obj, {spaces: 2}, function (err) { | |
//console.error(err) | |
}); | |
}); | |
}, | |
'ok_btccny_depth': function cb(data, err) { | |
//console.log(data); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment