-
Notifications
You must be signed in to change notification settings - Fork 7
/
bot_function.js
434 lines (376 loc) · 11.4 KB
/
bot_function.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
const Binance = require("node-binance-api");
const TelegramBot = require("node-telegram-bot-api");
const moment = require("moment-timezone");
const { config } = require("./config");
const { market_precision } = require("./data/market.js");
const db = require("./db.js");
const { redisDb } = require("./redis.js");
// client init untuk binance
exports.binance = function () {
const binance = new Binance().options({
test: config.test,
APIKEY: config.test ? config.test_APIKEY : config.APIKEY,
APISECRET: config.test ? config.test_APISECRET : config.APISECRET,
});
return binance;
};
// binance init leverage and margin type
exports.init_binance = async function () {
const binance = await this.binance();
try {
await binance.futuresLeverage(config.symbol, config.leverage);
await binance.futuresMarginType(config.symbol, config.margin_type);
} catch {
this.init_binance();
}
};
// get current time with timezone
exports.current_time = function () {
return moment().tz("Asia/Jakarta").format("YYYY-MM-DD HH:mm:ss");
};
// mengambil open posisi terakhir di table trades
exports.get_last_position = async function () {
const item = await db.table("trades").orderBy("id", "desc").first();
return item;
};
// fungsi untuk send log ke console dan notifikasi ke telegram
// !! unimplemented telegram
exports.send_log = async function (message, res = false) {
console.log(message);
if (config.telegram && config.telegram_bot_token && config.telegram_chat_id) {
this.send_telegram_message(message);
}
if (res) {
return res.send(message);
}
return true;
};
// get latest market_price from redis data
exports.get_market_price = async function () {
const { price = 0 } = await redisDb.get("market", "price");
return Number.parseFloat(price);
};
// hitung qty untuk order berdasarkan persentase order dan balance di config
exports.generate_qty_order = async function (price) {
if (config.balance && config.balance > 0) {
const balance = (config.balance * config.percent_order) / 100;
const market_price = !price ? await this.get_market_price() : price;
let qty = (parseFloat(balance) / market_price) * config.leverage;
// qty = qty * 0.99;
qty = this.round_to_precision("qty", qty);
return qty;
}
return 0;
};
// dapatkan target harga open sisi berdasarkan spread dari config
exports.generate_open_order_spread_price = function (type, market_price) {
const discount = (market_price * config.spread) / 100;
let target_price =
type == "buy" ? market_price - discount : market_price + discount;
target_price = this.round_to_precision("price", target_price);
return target_price;
};
// hitung target harga untuk profit yang diinginkan
exports.generate_target_price = function (type, qty, price) {
const target_profit = config.target_profit;
let target_price;
if (type == "buy") {
target_price =
Number.parseFloat(price) +
Number.parseFloat(target_profit) / Number.parseFloat(qty);
} else {
target_price =
Number.parseFloat(price) -
Number.parseFloat(target_profit) / Number.parseFloat(qty);
}
return this.round_to_precision("price", target_price);
};
// rounding precision
exports.round_to_precision = function (type, value) {
const precision = market_precision(config.symbol);
return Number.parseFloat(value).toFixed(
type == "qty" ? precision.qty : precision.price
);
};
// membuat order
exports.create_order = async function (type, qty, cause, { cross = false }) {
// hitung semua data market price
const market_price = Number.parseFloat(await this.get_market_price());
const discount = (market_price * config.spread) / 100;
let target_price =
type == "long" ? market_price - discount : market_price + discount;
target_price = this.round_to_precision("price", target_price);
// hitung target qty
let target_qty = qty;
if (cross) {
target_qty = target_qty * 2;
}
target_qty = this.round_to_precision("qty", target_qty);
if (!config.paper) {
if (type == "long") {
await binance.futuresBuy(config.symbol, target_qty, target_price);
} else {
await binance.futuresSell(config.symbol, target_qty, target_price);
}
}
// this.save_order(type, qty, cause, target_price);
const text = `${type} ${config.symbol} x${config.leverage} qty: ${qty} price : ${target_price}`;
this.send_log(text);
this.send_telegram_message(text);
};
// trigger from webhook
exports.trigger_webhook = async function ({ type, price }) {
price = this.generate_open_order_spread_price(type, price);
const qty = await this.generate_qty_order(price);
await this.submit_order(type, qty, price);
const cause = `${config.indicator} ${config.candle_period} ${type}`;
const target_price = this.generate_target_price(type, qty, price);
await this.send_log(`Webhook Trigger ${qty} ${cause} ${price}`);
// console.log(target_price, price, qty);
await this.save_order({
market: config.symbol,
period: config.candle_period,
indicator: config.indicator,
type: type,
qty: qty,
price: Number.parseFloat(price),
target_price: Number.parseFloat(target_price),
cause: cause,
});
return true;
};
// submit order
exports.submit_order = async function (type, qty, price) {
try {
const binance = this.binance();
const side = type == "buy" ? "BUY" : "SELL";
if (config.order_type == "LIMIT") {
console.info(
"future limit order",
await binance.futuresOrder(side, config.symbol, qty, price, {
type: "LIMIT",
})
);
await redisDb.set("lastOrder", {
status: "order",
side: type,
});
} else if (config.order_type == "MARKET") {
console.info(
"future market order",
await binance.futuresOrder(side, config.symbol, qty, false, {
type: "MARKET",
})
);
await redisDb.set("lastOrder", {
status: "position",
side: type,
});
}
this.send_log(`Submit order ${type} ${qty} ${price}`);
return true;
} catch (e) {
console.log(e);
// this.submit_order(type, qty, price);
}
};
exports.get_postion = async function () {
const binance = this.binance();
try {
let position_data = await binance.futuresPositionRisk(),
markets = Object.keys(position_data);
for (let market of markets) {
let obj = position_data[market],
size = Number(obj.positionAmt);
if (size == 0 || obj.symbol != config.symbol) continue;
return obj;
}
return null;
} catch {
return await this.get_postion();
}
};
// close semua posisi yang sedang aktiv
exports.close_all_position = async function () {
const binance = this.binance();
let position_data = await binance.futuresPositionRisk(),
markets = Object.keys(position_data);
for (let market of markets) {
let obj = position_data[market],
qty = Number(obj.positionAmt);
if (qty == 0) continue;
// console.log(market);
const type = qty > 0 ? "sell" : "buy";
qty = Math.abs(qty);
const current_price = await this.get_market_price();
console.info(
`close position, ${obj.leverage}x\t${obj.symbol}\t${obj.unRealizedProfit}`
);
this.submit_order(type, qty, current_price);
}
};
// submit take profit order berdasarkan type qty dan target_price
exports.submit_take_profit = async function (type, qty, target_price) {
try {
const binance = this.binance();
const side = type == "sell" ? "BUY" : "SELL";
await binance.futuresOrder(side, config.symbol, qty, target_price, {
type: "TAKE_PROFIT",
stopPrice: target_price,
// price: target_price,
});
this.send_log(`Submit Take Profit ${type} ${qty} ${target_price}`);
await redisDb.set("lastOrder", {
takeProfit: true,
});
} catch (e) {
console.log(e);
this.submit_take_profit(type, qty, target_price);
}
};
// menyimpan data order kedalam database
exports.save_order = async function ({
market,
period,
indicator,
time,
type,
last_status,
qty,
price,
target_price,
cause,
}) {
if (config.mysql == true) {
db("trades")
.insert({
market: market,
period: period,
indicator: indicator,
time: this.current_time(),
type: type,
last_status: last_status,
qty: qty,
price: Number.parseFloat(price),
target_price: Number.parseFloat(target_price),
cause: cause,
})
.then((x) => {
console.log(x);
})
.catch((x) => {
console.log(x);
});
}
};
exports.check_fill_and_close = async function (price) {
if (!config.mysql) {
return false;
}
// open buy, harga kurang dari opensisi
console.log(
await db("trades")
.where("type", "buy")
.where("fill_at", null)
.where("price", ">=", price)
.update({
fill_at: this.current_time(),
})
);
// close buy, harga lebih dari open posisi
console.log(
await db("trades")
.where("type", "buy")
.where("close_at", null)
.where("target_price", "<=", price)
.update({
close_at: this.current_time(),
})
);
// open sell, harga lebih dari opensisi
console.log(
await db("trades")
.where("type", "sell")
.where("fill_at", null)
.where("price", "<=", price)
.update({
fill_at: this.current_time(),
})
);
// close buy, harga kurang dari open posisi
console.log(
await db("trades")
.where("type", "sell")
.where("close_at", null)
.where("target_price", ">=", price)
.update({
close_at: this.current_time(),
})
);
};
// send telegram notification
exports.send_telegram_message = function (message) {
try {
const bot = new TelegramBot(config.telegram_bot_token);
bot.sendMessage(config.telegram_chat_id, message);
} catch (e) {
console.log("error send Telegram", e);
}
};
// get balance yang tersedia berdasarkan base_asset di config
exports.get_base_asset_balance = async function () {
const binance = this.binance();
try {
let balance;
const balances = await binance.futuresBalance();
for (const i of balances) {
if (i.asset == config.base_asset) {
balance = i.balance;
break;
}
}
return balance;
} catch {
return this.get_base_asset_balance();
}
};
// dapatkan open order yang tersedia
exports.get_open_orders = async function () {
const binance = this.binance();
try {
return await binance.futuresOpenOrders(config.symbol);
} catch {
return this.get_open_orders();
}
};
exports.get_take_profit_order = async function () {
try {
const orders = await this.get_open_orders();
let takeProfitOrder;
for (let order of orders) {
const orderType = order.type;
if (config.symbol == order.symbol && orderType.includes("TAKE_PROFIT")) {
takeProfitOrder = order;
break;
}
}
return takeProfitOrder;
} catch {
return await exports.get_take_profit_order();
}
};
exports.cancel_all_order = async function () {
const binance = this.binance();
console.info(await binance.futuresCancelAll(config.symbol));
};
exports.getFundingRateValid = async function () {
const binance = this.binance();
const markPrice = await binance.futuresMarkPrice(config.symbol);
const nextFundingTime = moment
.unix(markPrice.nextFundingTime / 1000);
const diff = nextFundingTime.diff(moment(), 'minutes');
console.log({
nextFundingTime: nextFundingTime.format("YYYY-MM-DD HH:mm:ss"),
});
return diff >= 60;
};