-
Notifications
You must be signed in to change notification settings - Fork 0
/
binance.js
205 lines (175 loc) · 6.88 KB
/
binance.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
const Binance = require('node-binance-api');
const fx = require('./fx');
const logFile = process.env.LOG;
let binanceConfigSpot
if (process.env.TEST === "TRUE") {
binanceConfigSpot = {
recvWindow: 60000,
urls: {
base: 'https://testnet.binance.vision/api/'
},
'family': 4, // ajuste para funcionar no node 18
};
} else {
binanceConfigSpot = {
recvWindow: 60000,
'family': 4, // ajuste para funcionar no node 18
};
}
async function getSpotTrades(apiKey, apiSecret) {
// Configuração da conexão com a API
const binance = new Binance({
APIKEY: apiKey,
APISECRET: apiSecret,
...binanceConfigSpot,
});
binance.websockets.trades([], (trades) => {
let { e: eventType, E: eventTime, s: symbol, p: price, q: quantity, m: maker, a: tradeId } = trades;
console.info(symbol + " trade update. price: " + price + ", quantity: " + quantity + ", maker: " + maker);
});
return true;
}
async function sendSpotBuyMarket(apiKey, apiSecret, symbol, quantity) {
// Configuração da conexão com a API
const binance = new Binance({
APIKEY: apiKey,
APISECRET: apiSecret,
...binanceConfigSpot,
});
try {
const r = await binance.marketBuy(symbol, quantity);
return r;
} catch (error) {
console.error('ERROR: ', error);
fx.escreveLogJson2("ERROR", error, logFile);
return null;
}
}
async function sendSpotSellMarket(apiKey, apiSecret, symbol, quantity) {
// Configuração da conexão com a API
const binance = new Binance({
APIKEY: apiKey,
APISECRET: apiSecret,
...binanceConfigSpot,
});
try {
const r = await binance.marketSell(symbol, quantity);
return r;
} catch (error) {
console.error('ERROR: ', error);
fx.escreveLogJson2("ERROR", error, logFile);
return null;
}
}
async function getSpotBalances(apiKey, apiSecret) {
// Configuração da conexão com a API
const binance = new Binance({
APIKEY: apiKey,
APISECRET: apiSecret,
...binanceConfigSpot,
});
const r = binance.balance();
return r;
}
async function exchangeInfo(apiKey, apiSecret) {
// Configuração da conexão com a API
const binance = new Binance({
APIKEY: apiKey,
APISECRET: apiSecret,
...binanceConfigSpot,
});
return binance.exchangeInfo();
}
async function getPrice(symbol) {
const binance = new Binance({
...binanceConfigSpot
});
try {
const bookTicker = await binance.bookTickers(symbol);
const precoSpot = parseFloat(bookTicker.askPrice);
return precoSpot;
} catch (error) {
console.error('Ocorreu um erro ao obter o preço do mercado spot:', error);
return null;
}
}
async function getPrices() {
const binance = new Binance({
...binanceConfigSpot
});
try {
const precos = await binance.prices();
return precos;
} catch (error) {
console.error('Ocorreu um erro ao obter os preços de todos os pares:', error);
return null;
}
}
async function getOrder(apiKey, apiSecret, symbol, orderId) {
const binance = new Binance({
APIKEY: apiKey,
APISECRET: apiSecret,
...binanceConfigSpot
});
try {
const precos = await binance.orderStatus(symbol, orderId);
return precos;
} catch (error) {
console.error('Ocorreu um erro ao obter os preços de todos os pares:', error);
return null;
}
}
async function BBS(apiKey, apiSecret, symbolB1, symbolPriceB1, symbolB2, symbolPriceB2, symbolS1, invest, symbolsInfo) {
const symbolPropsB1 = symbolsInfo.find(s => s.symbol === symbolB1);
const symbolPropsB2 = symbolsInfo.find(s => s.symbol === symbolB2);
const symbolPropsS1 = symbolsInfo.find(s => s.symbol === symbolS1);
const calc = fx.calc3(invest, symbolPriceB1, symbolPropsB2.decimals);
const quantityB1 = calc.quantidadeMoedas
const B1 = await sendSpotBuyMarket(apiKey, apiSecret, symbolB1, quantityB1);
fx.escreveLog(`${symbolB1} invest ${calc.valorFinalInvestido} preco ${symbolPriceB1} quanti ${quantityB1}`, logFile);
fx.escreveLog(`order: executedQty: ${B1.executedQty}`, logFile);
const decimals2 = symbolPropsB2.decimals;
const quantityB2 = fx.calc2(B1.executedQty, symbolPriceB2, decimals2);
const B2 = await sendSpotBuyMarket(apiKey, apiSecret, symbolB2, quantityB2);
fx.escreveLog(`${symbolB2} invest ${B1.executedQty} preco ${symbolPriceB2} quanti ${quantityB2}`, logFile);
fx.escreveLog(`order: executedQty: ${B2.executedQty}`, logFile);
const quantityS1 = fx.removeExcessDecimals(B2.executedQty, symbolPropsS1.decimals);
const S1 = await sendSpotSellMarket(apiKey, apiSecret, symbolS1, quantityS1);
fx.escreveLog(`${symbolS1} vende ${B2.executedQty} quanti ${quantityS1}`, logFile);
fx.escreveLog(`order: executedQty: ${S1.executedQty}`, logFile);
fx.escreveLog(`order: cummulativeQuoteQty: ${S1.cummulativeQuoteQty}`, logFile);
return true;
}
async function BSS(apiKey, apiSecret, symbolB1, symbolPriceB1, symbolS1, symbolS2, invest, symbolsInfo) {
const symbolPropsB1 = symbolsInfo.find(s => s.symbol === symbolB1);
const symbolPropsS1 = symbolsInfo.find(s => s.symbol === symbolS1);
const symbolPropsS2 = symbolsInfo.find(s => s.symbol === symbolS2);
const calc = fx.calc3(invest, symbolPriceB1, symbolPropsS1.decimals);
const quantityB1 = calc.quantidadeMoedas
const B1 = await sendSpotBuyMarket(apiKey, apiSecret, symbolB1, quantityB1);
fx.escreveLog(`${symbolB1} invest ${calc.valorFinalInvestido} preco ${symbolPriceB1} quanti ${quantityB1}`, logFile);
fx.escreveLog(`order ${B1.orderId}: executedQty: ${B1.executedQty}`, logFile);
const quantityS1 = fx.removeExcessDecimals(B1.executedQty, symbolPropsS1.decimals);
const S1 = await sendSpotSellMarket(apiKey, apiSecret, symbolS1, quantityS1);
fx.escreveLog(`${symbolS1} vende ${B1.executedQty} quanti ${quantityS1}`, logFile);
fx.escreveLog(`order ${S1.orderId}: executedQty: ${S1.executedQty}`, logFile);
fx.escreveLog(`order ${S1.orderId}: cummulativeQuoteQty: ${S1.cummulativeQuoteQty}`, logFile);
const quantityS2 = fx.removeExcessDecimals(S1.cummulativeQuoteQty, symbolPropsS2.decimals);
const S2 = await sendSpotSellMarket(apiKey, apiSecret, symbolS2, quantityS2);
fx.escreveLog(`${symbolS2} vende ${S1.cummulativeQuoteQty} quanti ${quantityS2}`, logFile);
fx.escreveLog(`order ${S2.orderId}: executedQty: ${S2.executedQty}`, logFile);
fx.escreveLog(`order ${S2.orderId}: cummulativeQuoteQty: ${S2.cummulativeQuoteQty}`, logFile);
return true;
}
module.exports = {
getSpotTrades,
sendSpotBuyMarket,
sendSpotSellMarket,
getSpotBalances,
exchangeInfo,
getPrice,
getPrices,
BBS,
BSS,
getOrder
};