-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
299 lines (238 loc) · 12.1 KB
/
app.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
require('dotenv').config();
const apiConnect = require('./lib/apiConnect');
const debug = true
const mysql = require('mysql2/promise');
process.on('uncaughtException', err => {
console.log('UNCAUGHT EXCEPTION!!! shutting down...');
console.log(err.name, err.message);
process.exit(1);
});
const checkAndOrder = async (pairObj, con) => {
const tempPairDataObject = pairObj
let oneDayInMilliSeconds = 60000 * 60 *24
let dayCounter=0
let apiFailureCount = 0
let barsLow
let barsHigh
let levels
let targetCurrencyPrecision
let tradingUnit
let quantity
let buyOrderStatus
let sellOrderStatus
let dt
while (true) {
//query for keeping connection alive
con.ping(async function (err){
console.log(err)
await apiConnect.pushBulletNoti('Alert!', 'SQL Connection Failed: Bot Closed');
process.exit()
})
// const bars = await apiConnect.getCandles(`https://public.coindcx.com/market_data/candles?pair=${pairObj.pair}&interval=5m&limit=1`);
const bars = await apiConnect.getCandles(`https://api.binance.com/api/v3/klines?symbol=${pairObj.symbol}&interval=5m&limit=1`);
if (!bars) {
await apiConnect.sleep(60000)
apiFailureCount += 1
if (debug) console.log("API FAILURES")
if (apiFailureCount == 30) {
notiStatus = await apiConnect.pushBulletNoti('Alert!', 'MULTIPLE Binance API FAILURES Bot Closed!');
await apiConnect.telegramNoti({'Alert!': 'MULTIPLE Binance API FAILURES Bot Closed!' })
// process.exit()
}
continue;
}
apiFailureCount = 0
barsLow= bars[0][3]
barsHigh=bars[0][2]
console.log(barsHigh, barsLow);
levels = pairObj.levels
tradingUnit = pairObj.tradingUnit
targetCurrencyPrecision = pairObj.target_currency_precision
console.log(levels)
for (let i = 0; i < levels.length; i++) {
if (levels[i][0] >= barsLow && levels[i][0] <= barsHigh) {
if (debug) console.log('met condition of candle intersection')
//check possible buying event
if (levels[i][1] == 'empty') {
if (debug) console.log('entered for buy level condition')
//place buying order
quantity = (tradingUnit / levels[i][0]).toFixed(targetCurrencyPrecision)
if (debug) console.log(quantity)
if (debug) console.log({
"side": "buy",
"order_type": "market_order",
"market": pairObj.symbol,
"total_quantity": quantity
})
buyOrderStatus = await apiConnect.createOrder({
"side": "buy",
"order_type": "market_order",
"market": pairObj.symbol,
"total_quantity": quantity
})
//Sample data of buyOrderStatus object
// buyOrderStatus = {
// orders: [
// {
// id: '41f74e24-b0fb-11ec-8e2f-1b98e6d5b6d9',
// client_order_id: null,
// order_type: 'market_order',
// side: 'buy',
// status: 'open',
// fee_amount: 0,
// fee: 0.1,
// maker_fee: 0.1,
// taker_fee: 0.1,
// total_quantity: 9,
// remaining_quantity: 9,
// source: null,
// base_currency_name: null,
// target_currency_name: null,
// base_currency_short_name: null,
// target_currency_short_name: null,
// base_currency_precision: null,
// target_currency_precision: null,
// avg_price: 0,
// price_per_unit: 1.671,
// stop_price: 0,
// market: 'MATICUSDT',
// time_in_force: 'good_till_cancel',
// created_at: 1648735373000,
// updated_at: 1648735373000,
// trades: null
// }
// ]
// }
if (buyOrderStatus) {
if (buyOrderStatus.orders[0].id) {
//trigger push notifications
await apiConnect.pushBulletNoti(`${pairObj.symbol} Buy order placed`, `price:${levels[i][0]}, quantity:${quantity}`);
await apiConnect.telegramNoti({
'Buy order placed': pairObj.symbol,
'Price': levels[i][0],
'quantity': quantity
})
console.log('entered buyOrderStatus')
levels[i][1] = 'filled'
//update db
tempPairDataObject.levels = levels
try{
dt = await con.execute('update pairs set pairs=? where name=? ', [JSON.stringify(tempPairDataObject), tempPairDataObject.name])
console.log(dt)
//add buy entry in orders
dt = await con.execute('INSERT INTO `orders`( `order_id`, `side`, `fee_amount`, `quantity`, `price`, `symbol`, `timestamp`) VALUES (?, ?, ?, ?, ?,?,?)', [buyOrderStatus.orders[0].id, buyOrderStatus.orders[0].side, buyOrderStatus.orders[0].fee_amount, buyOrderStatus.orders[0].total_quantity, buyOrderStatus.orders[0].price_per_unit, buyOrderStatus.orders[0].market, buyOrderStatus.orders[0].created_at])
}
catch(err) {
console.error(err)
}
if (debug) console.log(buyOrderStatus)
}
}
}
//check possible selling event
if (i != (levels.length - 1)) {
if (levels[i + 1][1] == 'filled') {
if (debug) console.log('entered for sell level condition')
quantity = (tradingUnit / levels[i + 1][0]).toFixed(targetCurrencyPrecision)
if (debug) console.log(quantity)
if (debug) console.log({
"side": "sell",
"order_type": "market_order",
"market": pairObj.symbol,
"total_quantity": quantity
})
sellOrderStatus = await apiConnect.createOrder({
"side": "sell",
"order_type": "market_order",
"market": pairObj.symbol,
"total_quantity": quantity
})
// Sample data of sellOrderStatus object
// sellOrderStatus = {
// orders: [
// {
// id: '495e0c16-b0fb-11ec-beef-23856932a931',
// client_order_id: null,
// order_type: 'market_order',
// side: 'sell',
// status: 'open',
// fee_amount: 0,
// fee: 0.1,
// maker_fee: 0.1,
// taker_fee: 0.1,
// total_quantity: 9,
// remaining_quantity: 9,
// source: null,
// base_currency_name: null,
// target_currency_name: null,
// base_currency_short_name: null,
// target_currency_short_name: null,
// base_currency_precision: null,
// target_currency_precision: null,
// avg_price: 0,
// price_per_unit: 0,
// stop_price: 0,
// market: 'MATICUSDT',
// time_in_force: 'good_till_cancel',
// created_at: 1648735385000,
// updated_at: 1648735385000,
// trades: null
// }
// ]
// }
if (sellOrderStatus) {
if (sellOrderStatus.orders[0].id) {
await apiConnect.pushBulletNoti(`${pairObj.symbol} Sell order placed`, `price:${levels[i + 1][0]}, quantity:${quantity}`);
await apiConnect.telegramNoti({
'Sell order placed': pairObj.symbol,
'Price': levels[i][0],
'quantity': quantity
})
levels[i + 1][1] = 'empty'
//update levels in pairs table
tempPairDataObject.levels = levels
try{
dt = await con.execute('update pairs set pairs=? where name=? ', [JSON.stringify(tempPairDataObject), tempPairDataObject.name])
console.log(dt)
//add sell entry in orders
dt = await con.execute('INSERT INTO `orders`( `order_id`, `side`, `fee_amount`, `quantity`, `price`, `symbol`, `timestamp`) VALUES (?, ?, ?, ?, ?,?,?)', [sellOrderStatus.orders[0].id, sellOrderStatus.orders[0].side, sellOrderStatus.orders[0].fee_amount, sellOrderStatus.orders[0].total_quantity, sellOrderStatus.orders[0].price_per_unit, sellOrderStatus.orders[0].market, sellOrderStatus.orders[0].created_at])
}
catch(err) {
console.error(err)
}
if (debug) console.log(sellOrderStatus)
}
}
}
}
break
}
}
await apiConnect.sleep(10000)
dayCounter+=10000
if (dayCounter>oneDayInMilliSeconds)
{
dayCounter=0
console.log(('code is running successfully!'))
await apiConnect.pushBulletNoti('code is running succesfully!')
}
}
}
const master = async () => {
var con = await mysql.createConnection({
host: process.env.host,
user: process.env.user,
password: process.env.password,
database: process.env.database
});
var queryDt = await con.execute('SELECT * FROM pairs')
if (debug) console.log(queryDt[0])
for (elem of queryDt[0]) {
checkAndOrder(JSON.parse(elem.pairs), con);
}
}
process.on('unhandledRejection', err => {
console.log('UNHANDLED REJECTION!!! shutting down ...');
console.log(err.name, err.message);
});
master()