This repository has been archived by the owner on Jan 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicm-noti.js
126 lines (118 loc) · 3.2 KB
/
icm-noti.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
channel = '#c-crazy';
enableTrade = false;
openAt = undefined;
lastSend = Date.now();
lastCountTrades = 0;
backendApi='https://backend.xbot.com.vn:3050/api';
tradeNew = (rows) => {
fetch(`${backendApi}/trade/new`, {
method: 'POST',
body: JSON.stringify({
rows,
}),
headers: {
'Content-type': 'application/json; charset=UTF-8'
}
}).then(console.log).catch(console.log)
};
tradeClose = () => {
fetch(`${backendApi}/trade/close`, {
method: 'POST',
body: JSON.stringify({
}),
headers: {
'Content-type': 'application/json; charset=UTF-8'
}
}).then(console.log).catch(console.log)
};
send = (text, customChannel) => {
fetch(`${backendApi}/noti/new`, {
method: 'POST',
body: JSON.stringify({
payload: {
channel: customChannel || channel,
username: 'webhookbot',
icon_emoji: ':ghost:',
text,
}
}),
headers: {
'Content-type': 'application/json; charset=UTF-8'
}
}).then(console.log).catch(console.log)
};
send(`Started ${channel}`, "c-log");
parseRow = (node) => {
return [...node.children].map(node => {
if (node.children[0].children[0]) {
return node.children[0].children[0].innerText;
} else {
return node.children[0].innerText;
}
});
};
parseNum = (num) => parseFloat(parseFloat(num).toFixed(2));
run = () => {
const checkArr = document.querySelectorAll("div[data-test-id^=table-header-cell-created]");
if (!checkArr.length) {
if (Date.now() - lastSend > 1000 * 60 * 5) {
lastSend = Date.now();
send('ERROR! Page is sleeping!');
}
} else {
const rows = document.querySelectorAll("div[data-test-id^=table-row-root]");
if (rows.length) {
if (rows.length !== lastCountTrades) {
lastCountTrades = rows.length;
const trades = [];
rows.forEach(row => {
trades.push(parseRow(row));
})
console.log(trades);
if (enableTrade) tradeNew(trades);
}
if (!openAt) {
openAt = Date.now();
lastSend = openAt;
const pos = parseRow(rows[0]).join(', ');
const text = `New pos: ${pos}`;
send(text);
} else {
if (Date.now() - lastSend > 1000 * 60 * 5) {
lastSend = Date.now();
let profit = 0;
let pips = 0;
rows.forEach(row => {
const pos = parseRow(row);
profit += parseNum(pos[9]);
pips += parseNum(pos[7]);
})
const firstPos = parseRow(rows[rows.length - 1]).join(', ');
const lastPos = parseRow(rows[0]).join(', ');
const text = `Last pos: ${lastPos}.\nFirst pos: ${firstPos}\nNum pos: ${rows.length}. Profit: ${profit}. Pips: ${pips}`;
send(text);
}
}
} else {
if (openAt) {
tradeClose();
openAt = undefined;
lastSend = Date.now();
lastCountTrades = 0;
send('Close pos!');
} else {
if (Date.now() - lastSend > 1000 * 60 * 60) {
lastSend = Date.now();
send(`Heartbeat! ${channel}`, "c-log");
}
}
}
}
}
loop = setInterval(() => {
try {
run();
} catch (error) {
send(`Error: ${error.message}`);
}
}, 1000);