-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtradebot.js
100 lines (86 loc) · 2.27 KB
/
tradebot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"use strict";
global.__rootdir = __dirname;
const path = require("path");
const {state,wallet,setWeb,setGetter} = require("./lib/globals.js");
const {Log} = require("./lib/logging.js");
const recorder = require("./recorder/record.js");
const Bitstamp = require("./lib/data.js");
const Calc = require("./lib/calc.js");
state.loglevel = 0;
let predict,getter;
const loadingBuffer = [];
function processBatch(batch){
while (batch.length){
const item = batch.shift();
Calc.order({
timestamp:item.t.timestamp,
bids:item.b,
asks:item.a
},true);
predict.addTrade(item.t);
}
getter.send({batch:true});
}
Bitstamp.channels.trades.bind("trade", function (data) {
if(state.loading && !state.record){
data._T = "trades";
loadingBuffer.push(data);
}else{
if(!state.dev) recorder.input(data);
if(!state.record) predict.addTrade(data);
}
});
Bitstamp.channels.orders.bind("data", function (data) {
if(state.loading && !state.record){
data._T = "orders";
loadingBuffer.push(data);
}else{
Calc.order(data);
}
});
if(!state.record){
state.loading = true;
Calc.setWallet((err)=>{
if(!state.dev && err){
Log.info("Ending the process");
process.exit();
return;
}
const {fork} = require("child_process");
const web = fork(path.join(__rootdir,"lib/graphserver"),process.argv.slice(2),{
cwd:__rootdir
});
setWeb(web);
getter = fork(path.join(__rootdir,"recorder/getter"),process.argv.slice(2),{
cwd:__rootdir
});
setGetter(getter);
predict = require("./lib/predict.js");
getter.on("message",function(m){
if(m.batch) processBatch(m.batch);
if(m.done){
while (loadingBuffer.length) {
let item = loadingBuffer.shift();
item._T === "trades"?
predict.addTrade(item):
Calc.order(item);
}
state.loading = false;
web.send({loading:false});
getter.send({exit:true});
Log.info("_______________FINISHED RESTORATION_________________________________");
}
});
if(!state.dev){
state.trade_dir = wallet.coin && wallet.fiat < 100?"sell":"buy";
state.opp_trade_dir = state.trade_dir === "sell"?"buy":"sell";
}else{
wallet.coin = 1;
wallet.fiat = 0;
}
Log.info(wallet);
Log.info("Trading direction: "+state.trade_dir);
//getter.send({start:"20181028"});
getter.send({start:""});
});
}