-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
58 lines (51 loc) · 1.54 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
require("dotenv").config();
const Binance = require("binance-api-node").default;
const moment = require("moment");
const axios = require("axios");
axios.defaults.headers.common["X-MBX-APIKEY"] = process.env.API_KEY; // for POST requests
// Authenticated client, can make signed calls
const client2 = Binance({
apiKey: process.env.API_KEY,
apiSecret: process.env.API_SECRET,
});
const output = [];
function addToOutput(action, url, arr, result = true) {
arr.push({
Time: moment().format("D/MM/YYYY hh:mm:ss"),
Action: action,
Url: url,
Result: result ? "PASS" : "FAIL",
});
}
async function newOrder(base, quote, qty, side, type, test = false) {
const action = `${side} ${base}/${quote}`;
const pathOrder = test ? "/api/v3/order/test" : "/api/v3/order";
try {
const { balances } = await client2.privateRequest("GET", "/api/v3/account");
const quoteBalance = Number(
balances.find((b) => b["asset"] === quote)["free"]
);
if (quoteBalance >= qty) {
const buy = await client2.privateRequest("POST", pathOrder, {
symbol: `${base}${quote}`,
side: side,
type: type,
quoteOrderQty: qty,
});
addToOutput(action, pathOrder, output);
}
} catch (e) {
console.log(e);
addToOutput(action, pathOrder, output, false);
}
}
//call all functions in order
(async () => {
const BASE = "XVG";
const QUOTE = "BUSD";
const QTY = 12;
const SIDE = "BUY";
const TYPE = "MARKET";
await newOrder(BASE, QUOTE, QTY, SIDE, TYPE, true);
console.table(output);
})();