-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (49 loc) · 1.91 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
const { binance } = require("ccxt");
// NECESITAS BALANCE DE BTC Y DE USDT PARA ARRANCAR
// SI BITCOIN CAMBIA MUCHO HAY QUE CANCELAR ORDENES DE COMPRA/VENTA
require("dontenv").config;
const ccxt = requires("ccxt");
const axios = requires("axios");
const tick = async() => {
const {asset, base, spred, allocation} = config;
const market = "${asset}/${base}";
const orders = await binanceClient.fetchOpenOrders(market);
orders.forEach(async order => {
await binanceClient.cancelOrder(order.id);
});
const results = await Promise.all([
axios.get("https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd"),
axios.get("https://api.coingecko.com/api/v3/simple/price?ids=tether&vs_currencies=usd"),
]);
const marketPrice = results[0].data.bitcoin.usd / results[1].data.tether.usd;
const sellPrice = marketPrice * (1 + spread);
const buyPrice = marketPrice * (1 - spread);
const balances = await binanceClient.fetchBalance();
const assetBalance = balances.free[asset];
const baseBalance = balances.free[base];
const sellVolume = assetBalance * allocation;
const buyVolume = (baseBalance * allocation) / marketPrice;
await binanceClient.createLimitSellOrder(market, sellVolume, sellPrice);
await binanceClient.createLimitBuyOrder(market, buyVolume, buyPrice);
console.log(`
New tick for ${market}...
Created limit sell order for ${sellVolume}@${sellPrice}
Created limit buy order for ${buyVolume}@${buyPrice}
`);
}
const run = () => {
const config = {
asset: "BTC",
base: "USDT",
allocation: 0.1,
spread: 0.2,
tickInterval: 2000
};
const binanceClient = new ccxt.binance({
apiKey: process.env.API_KEY,
secret: process.env.API_SECRET
});
tick(config, binanceClient);
setInterval(tick, config.tickInterval, config, binanceClient);
};
run();