-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (52 loc) · 1.71 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
62
63
64
const webSocket = require("ws");
const ws = new webSocket(
`${process.env.STREAM_URL}/${process.env.SYMBOL.toLowerCase()}@ticker`
);
const PROFITABILITY = parseFloat(process.env.PROFITABILITY);
let sellPrice = 0;
ws.onmessage = (event) => {
console.clear();
const obj = JSON.parse(event.data);
console.log("symbol: " + obj.s);
console.log("best ask: " + obj.a);
const currentPrice = parseFloat(obj.a);
if (sellPrice === 0 && currentPrice < 75000) {
console.log("Bom para compra");
newOrder("0.001", "BUY");
sellPrice = currentPrice * PROFITABILITY;
} else if (sellPrice !== 0 && currentPrice >= sellPrice) {
console.log("Bom para vender");
newOrder("0.001", "SELL");
sellPrice = 0;
} else {
console.log("Esperando...Sell Price: " + sellPrice);
}
};
const axios = require("axios");
const crypto = require("crypto");
async function newOrder(quantity, side) {
const data = {
Symbol: process.env.SYMBOL,
type: "MARKET",
side,
quantity,
};
const timesTamp = Date.now();
const racvWindow = 5000;
const signature = crypto
.createHmac("sha256", process.env.SECRET_KEY)
.update(`${new URLSearchParams({ ...data, timesTamp, racvWindow })}`)
.digest("hex");
const newData = { ...data, timesTamp, racvWindow, signature };
const qs = `?${new URLSearchParams(newData)}`;
try {
const result = await axios({
method: "POST",
url: `${process.env.API_URL}/v3/order${qs}`,
headers: { "X-MBX-APIKEY": process.env.API_KEY },
});
console.log(result.data);
} catch (error) {
console.log(error);
}
}