-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
314 lines (274 loc) · 11.4 KB
/
index.js
File metadata and controls
314 lines (274 loc) · 11.4 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import puppeteer from 'puppeteer-extra';
import StealthPlugin from 'puppeteer-extra-plugin-stealth';
import dotenv from 'dotenv';
import fs from 'fs-extra';
import { injectBotUI, updateBotUI } from './ui.js';
import app from './server.js';
import { readDB } from './db.js';
import { handleNewTrade, closeAllEciSubTrades } from './trade.js';
import { v4 as uuidv4 } from 'uuid';
import winston from 'winston';
import { readExchangeDB } from './db.js';
import { monitorTrades } from './trade.js';
import { monitorExchangeTrades } from './exchangeTrade.js';
import EventEmitter from 'events';
// Wczytaj .env
dotenv.config();
// Stealth Puppeteer
puppeteer.use(StealthPlugin());
// Logger
const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf(({ timestamp, level, message }) => `${timestamp} ${level}: ${message}`)
),
transports: [
new winston.transports.File({ filename: 'monitor-error.log', level: 'error' }),
new winston.transports.File({ filename: 'monitor-combined.log' }),
new winston.transports.Console()
],
});
// Alert -> trade
const eventEmitter = new EventEmitter();
eventEmitter.on('newAlert', async (alertData) => {
logger.info('New alert received. Running monitors...');
await monitorTrades();
await monitorExchangeTrades();
});
// Globalne
let browserInstance;
let pageInstance;
let isTrading = false;
// User agents
const userAgents = [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64)...',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)...'
];
// Port
const PORT = process.env.WEBHOOK_PORT || 3000;
async function monitorTradingView() {
if (isTrading) {
logger.warn('Monitoring is already active.');
return;
}
isTrading = true;
try {
const randomUserAgent = userAgents[Math.floor(Math.random() * userAgents.length)];
browserInstance = await puppeteer.launch({
headless: false,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--window-size=1920,1080',
'--disable-dev-shm-usage',
'--disable-extensions',
'--disable-gpu',
],
defaultViewport: null,
});
pageInstance = await browserInstance.newPage();
await pageInstance.setUserAgent(randomUserAgent);
// Logi z konsoli/błędy strony
pageInstance.on('console', (msg) => {
for (let i = 0; i < msg.args().length; ++i) {
logger.info(`PAGE LOG: ${msg.args()[i]}`);
}
});
pageInstance.on('pageerror', (error) => {
logger.error(`PAGE ERROR: ${error?.message || error}`);
});
// ----------------- 1) Czy mamy cookies? -----------------
let hasCookies = false;
try {
if (await fs.pathExists('cookies.json')) {
const content = await fs.readFile('cookies.json', 'utf8');
if (content.trim().length > 0) {
const cookies = JSON.parse(content);
if (cookies.length > 0) {
await pageInstance.setCookie(...cookies);
hasCookies = true;
logger.info('Cookies found and loaded. Assuming user is logged in.');
} else {
logger.info('cookies.json is empty array, skipping...');
}
} else {
logger.info('cookies.json is empty file, skipping...');
}
} else {
logger.info('cookies.json does not exist, skipping...');
}
} catch (err) {
logger.warn(`Error loading cookies.json: ${err.message}`);
}
// ----------------- 2) Jeśli mamy cookies, idziemy od razu na wykres -----------------
if (hasCookies) {
logger.info('Going directly to chart page (assuming we are logged in)...');
await pageInstance.goto('https://www.tradingview.com/chart/?symbol=BINANCE:BTCUSDT', { waitUntil: 'networkidle2' });
} else {
// ----------------- 2B) Brak cookies -> logowanie -----------------
logger.info('No valid cookies. Logging in...');
// Idziemy na stronę logowania
await pageInstance.goto('https://www.tradingview.com/accounts/signin/', { waitUntil: 'networkidle2' });
// Pola logowania
const userSelector = 'input[name="username"], input[data-name="username"], input[type="text"]';
const passSelector = 'input[name="password"], input[data-name="password"], input[type="password"]';
try {
// Czekamy na pola
await pageInstance.waitForSelector(userSelector, { timeout: 10000 });
await pageInstance.waitForSelector(passSelector, { timeout: 10000 });
logger.info('Typing credentials...');
await pageInstance.type(userSelector, process.env.TRADINGVIEW_USER, { delay: 100 });
await pageInstance.type(passSelector, process.env.TRADINGVIEW_PASS, { delay: 100 });
// Przyciski "Sign in" (zmień, jeśli inny selektor)
const signInBtn = 'button[data-overflow-tooltip-text="Sign in"]';
await pageInstance.waitForSelector(signInBtn, { timeout: 10000 });
await pageInstance.click(signInBtn);
logger.info('Clicked sign in, waiting for navigation...');
await pageInstance.waitForNavigation({ waitUntil: 'networkidle2' });
// Poczekaj dodatkowe 5s
await new Promise(r => setTimeout(r, 5000));
// Zapis cookies
try {
const newCookies = await pageInstance.cookies();
await fs.writeJson('cookies.json', newCookies);
logger.info('Cookies saved after login.');
} catch (err) {
logger.error(`Error writing cookies.json: ${err.message}`);
}
// Teraz przechodzimy na wykres
logger.info('Going to chart page...');
await pageInstance.goto('https://www.tradingview.com/chart/?symbol=BINANCE:BTCUSDT', { waitUntil: 'networkidle2' });
} catch (err) {
logger.error(`Login process failed: ${err.message}`);
// W razie błędu też warto przejść na wykres, może cookies i tak są...
await pageInstance.goto('https://www.tradingview.com/chart/?symbol=BINANCE:BTCUSDT', { waitUntil: 'networkidle2' });
}
}
// ----------------- 3) W tym miejscu powinniśmy być na wykresie -----------------
logger.info('Now injecting Bot UI...');
await injectBotUI(pageInstance);
const initialDbData = await readDB();
await updateBotUI(initialDbData);
// Przechwyt alert() w oknie
await pageInstance.evaluate(() => {
const originalAlert = window.alert;
window.alert = function(message) {
window.handleAlert(message);
originalAlert.apply(this, arguments);
};
});
// Definiujemy handleAlert
await pageInstance.exposeFunction('handleAlert', async (alertJson) => {
logger.info('handleAlert invoked');
try {
logger.info(`Received alert JSON: ${alertJson}`);
let alertData;
try {
alertData = JSON.parse(alertJson);
} catch (err) {
logger.warn(`Alert is not valid JSON: ${alertJson}`);
return;
}
// Minimalna walidacja
const required = ['type','symbol','price','indicator'];
const hasAll = required.every((f) => alertData[f] !== undefined && alertData[f] !== null);
if (!hasAll) {
logger.warn(`Alert missing fields: ${alertJson}`);
return;
}
// Obsługa ECI-LONG, gdzie w JSON jest "indicator":"eci_long" i "version":"A/B/C/D/E"
if (alertData.indicator === 'eci_long') {
const version = alertData.version;
if (!['A','B','C','D','E'].includes(version)) {
logger.warn(`ECI-Long alert has unknown version: ${version}`);
return;
}
// finalny indicator np. eci_longE
alertData.indicator = `eci_long${version}`;
}
// AutoTrade? (czy zamieniać na papierowy)
const dbData = await readDB();
const autoTradeEnabled = dbData.settings.autoTrade || false;
// Zastąp placeholdery
if (typeof alertData.symbol === 'string' && alertData.symbol.includes('{')) {
logger.warn(`Placeholder symbol detected: ${alertData.symbol} -> forcing "BTCUSDT"`);
alertData.symbol = 'BTCUSDT';
}
if (typeof alertData.price === 'string') {
let tryPrice = parseFloat(alertData.price);
if (isNaN(tryPrice)) {
logger.warn(`Placeholder price detected: ${alertData.price} -> forcing price=0`);
tryPrice = 0;
}
alertData.price = tryPrice;
}
// Emit event, np. do monitorTrades
eventEmitter.emit('newAlert', alertData);
// ECI slCross/tpCross => zamknięcie sub-wersji
// if (['tpCross','slCross'].includes(alertData.type) && alertData.indicator.startsWith('eci_long')) {
//logger.info(`Closing all trades for subindicator ${alertData.indicator} at price ${alertData.price}`);
// await closeAllEciSubTrades(alertData.indicator, parseFloat(alertData.price), async (upd) => {
// await updateBotUI(upd);
// });
// return;
// }
// Nowa transakcja (paper vs real)
alertData._paper = !autoTradeEnabled; // jeśli autoTrade= false -> paper trade
logger.info(`Spawning handleNewTrade: ${alertData.indicator}, type=${alertData.type}, paper=${alertData._paper}`);
await handleNewTrade(alertData);
} catch (error) {
logger.error(`Error in handleAlert: ${error}`);
}
});
// MutationObserver do wykrywania alertów w HTML (injected do strony):
await pageInstance.evaluate(() => {
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === Node.ELEMENT_NODE) {
const alertElement =
node.querySelector('[class*="description-"]') ||
node.querySelector('.tv-alert__description');
if (alertElement) {
const alertText = alertElement.textContent.trim();
console.log('Alert found:', alertText);
if (alertText.startsWith('{') && alertText.endsWith('}')) {
window.handleAlert(alertText);
}
}
}
});
});
});
const targetNode = document.body;
if (targetNode) {
observer.observe(targetNode, { childList: true, subtree: true });
console.log('MutationObserver is observing the body.');
} else {
console.error('Body element not found.');
}
});
// Obsługa 429
pageInstance.on('response', async (resp) => {
if (resp.status() === 429) {
logger.error('Too many requests (429). Closing browser...');
await browserInstance.close();
setTimeout(monitorTradingView, 60000);
}
});
logger.info('Monitoring TradingView started.');
} catch (error) {
logger.error(`Error in monitorTradingView: ${error}`);
if (browserInstance) {
await browserInstance.close();
}
setTimeout(monitorTradingView, 60000);
}
}
// Uruchom serwer i start monitorowania
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
logger.info(`Server running on port ${PORT}`);
monitorTradingView();
});