-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·274 lines (265 loc) · 8.95 KB
/
index.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
const save = true;
var websocket = null;
var users = {};
var con = null;
var currentPrice = 0;
var lastPrice = 0;
var price = 0;
var logLength = 0;
var margin = 0;
var log = null;
var mode = "";
var lastMode = "";
var key = "";
var secret = "";
var passphrase = "";
var wsURL = "";
var apiURL = "";
var product = "";
var currency = "";
var dbConnect = "";
var lm = "";
const date = new Date();
const remote = process.argv[2];
const Gdax = require('gdax');
const https = require('https');
const mysql = require('mysql');
const uuidv1 = require('uuid/v1');
const fixedArray = require("fixed-array");
const childProcess = require('child_process');
var history = fixedArray(100);
// Setup MySQL
var opt = {
host: remote,
path: '/check.php?m=database',
port: 443,
method: 'GET'
}
var request = https.request(opt, function (res) {
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
dbConnect = data.toString().split(",");
dbConnect[0] = remote;
con = mysql.createConnection({
host : dbConnect[0],
user : dbConnect[1],
password : dbConnect[2],
database : dbConnect[3]
});
con.connect(function(err) {
if (err) throw err;
});
var pid = process.pid;
con.query("UPDATE bot_last SET pid = ?", [process.pid], function (err, result) {
if (err) throw err;
});
/*
if (save) {
con.query("DELETE FROM bot", function (err, result) {
if (err) throw err;
});
}
*/
con.query("SELECT * FROM bot_config", function(err, rows, fields) {
if (err) throw err;
margin = rows[0].margin;
logLength = rows[0].points;
log = fixedArray(logLength);
if (rows[0].api == "production") {
key = rows[0].key;
secret = rows[0].secret;
passphrase = rows[0].passphrase;
wsURL = rows[0].wsURL;
apiURL = rows[0].apiURL;
product = rows[0].product;
currency = rows[0].currency;
}else{
key = rows[0].sandbox_key;
secret = rows[0].sandbox_secret;
passphrase = rows[0].sandbox_passphrase;
wsURL = rows[0].sandbox_wsURL;
apiURL = rows[0].sandbox_apiURL;
product = rows[0].sandbox_product;
currency = rows[0].sandbox_currency;
}
// Now got all config
setTimeout(startup, 500);
});
con.query("SELECT price, action FROM bot_last", function(err, rows, fields) {
if (err) throw err;
price = rows[0].price;
lastMode = rows[0].action;
if (price) {
for(var i = 0; i < logLength; i++) {
log.push(price);
}
}
});
});
});
request.end();
function startup() {
getUsers();
websocketConnect();
}
function getUsers() {
opt = {
host: remote,
path: "/traders/" + currency,
port: 443,
method: 'GET'
};
request = https.request(opt, function (res) {
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
users = JSON.parse(data);
for (var key in users) {
console.log(date.toLocaleString() + " User = " + users[key].name);
users[key].apiURI = apiURL;
users[key].product = product;
users[key].dbConnect = dbConnect;
users[key].key = key;
}
//doDeal('buy', price);
userOrders();
});
});
request.end();
}
function websocketConnect() {
websocket = new Gdax.WebsocketClient(
[product],
wsURL,
{
key: key,
secret: secret,
passphrase: passphrase
},
{
"type": "subscribe",
"product_ids": [
product
],
"channels": [
"level2",
{
"name": "ticker",
"product_ids": [
product
]
},
]
}
);
websocket.on('message', data => {
if (data.type == 'ticker') {
//console.log(data);
currentPrice = parseFloat(parseFloat(data.price).toFixed(1));
if (!price) {
price = currentPrice;
for(var i = 0; i < logLength; i++) {
log.push(price);
}
}
if (lastPrice != currentPrice) {
var action = "";
var value = 0;
var values = log.values();
history.push(currentPrice);
// Are we in a buy or sell mood
if (currentPrice > (price + margin) && (lastMode == "buy" || !lastMode)) {
// Up
mode = "sell";
}else if (currentPrice < (price - margin) && (lastMode == "sell" || !lastMode)) {
// Down
mode = "buy"
}else {
mode = "";
}
if (mode == "buy" && currentPrice > lastPrice) {
// we are in Buy mood and price has risen. See if risen for last logLength-1 times + now
var go = true;
for (var i = 1; i < logLength; i++) {
if (values[i-1] >= values[i]) {
go = false;
break;
}
}
if (go && values[logLength - 1] < currentPrice) {
action = "buy";
value = parseFloat(data.best_bid);
}
}else if (mode == "sell" && currentPrice < lastPrice) {
// we are in Sell mood and price has fallen. See if fallen for last logLength-1 times + now
var go = true;
for (var i = 1; i < logLength; i++) {
if (values[i-1] <= values[i]) {
go = false;
break;
}
}
if (go && values[logLength - 1] > currentPrice) {
action = "sell";
value = parseFloat(data.best_bid) - 0.5;
}
}
if (action) {
doDeal(action, value);
price = value;
lastMode = action;
}
if (save) {
con.query("INSERT INTO bot (price, mode, value) VALUES (?, ?, ?)", [currentPrice, action, value], function (err, result) {
if (err) throw err;
});
if (mode != lm) {
lm = mode;
con.query("UPDATE bot_last SET mode = ?", [mode], function (err, result) {
if (err) throw err;
});
}
}
lastPrice = currentPrice;
if (mode) {
// Only save to log if within sell or buy
log.push(currentPrice);
}
}
}
});
websocket.on('error', err => {
console.log(date.toLocaleString() + " " + "Websocket error = " + err);
process.exit(1); // When running under supervisor, will get restarted
});
websocket.on('close', () => {
console.log(date.toLocaleString() + " " + "Websocket closed");
process.exit(1); // When running under supervisor, will get restarted
});
}
function doDeal(action, value) {
for (var key in users) {
users[key].action = action;
users[key].value = value;
var json = JSON.stringify(users[key]);
childProcess.fork('userDeal.js', [json]);
};
if (save) {
con.query("UPDATE bot_last SET price = ?, action = ?", [value, action], function (err, result) {
if (err) throw err;
});
}
}
function userOrders() {
if (Object.keys(users).length) {
var data = {users: users, dbConnect: dbConnect}
var json = JSON.stringify(data);
childProcess.fork('userOrders.js', [json]);
}
setTimeout(userOrders, 30000);
}