-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-bot-easyUse3.js
332 lines (214 loc) · 10.3 KB
/
main-bot-easyUse3.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
require('punycode/')
const id = ""; // trading view // sessionid
const certificate = ""; // trading view // sessionid_sign
const dbname="suckit"
const uri = ""; //mongodb
key = ""; // bybit
secret = ""; // bybit
spreadsheetId = ''; // google sheets
let webhookUrl = ''; //discord
const leverage = '2.8';
let NofCoins = 20;
let lev = 1;
const limit_persentage_ofset = 5; // 0.1 is suggested // 0.1 is 0.1% // 5 is 5% <--- for testing use 5
const speed= 10000 // 10000 == 10 sec this always work, I use 1000 == 1 sec
let weights_no_prov = [1.25 ,13.00, 1.25, 2.19, 1.10, 4.06, 1.10, 1.10, 1.00, 1.06]; // Example weights
let weights_prov =[66.78, 33.75, 39.98, 37.35,0, 46.02,0, 0, 29.86, 20.05]; // Example weights
const indicator_id = "USER;03d7ea932b9044e6aefc5d264f0e214f"
const timeframes = ['4H','12H','1D', '2D', '3D', '4D', '5D', '6D', 'W', '1M'];
// let markets = [
// 'BYBIT:TIAUSDT.P',
// 'BYBIT:ROSEUSDT.P', 'BYBIT:BANDUSDT.P',
// 'BYBIT:ENJUSDT.P', 'BYBIT:1INCHUSDT.P',
// 'BYBIT:MINAUSDT.P',
// 'BYBIT:RNDRUSDT.P',
// 'BYBIT:ALGOUSDT.P',
// 'BYBIT:BAKEUSDT.P', 'BYBIT:AXSUSDT.P',
// 'BYBIT:ONDOUSDT.P', 'BYBIT:OPUSDT.P',
// 'BYBIT:CFXUSDT.P', 'BYBIT:DOGEUSDT.P',
// 'BYBIT:LDOUSDT.P', 'BYBIT:UNIUSDT.P','BYBIT:BTCUSDT.P','BYBIT:ETHUSDT.P',
// 'BYBIT:MATICUSDT.P','BYBIT:SOLUSDT.P','BYBIT:BNBUSDT.P','BYBIT:XRPUSDT.P',
// 'BYBIT:ADAUSDT.P','BYBIT:AVAXUSDT.P','BYBIT:TRXUSDT.P','BYBIT:DOTUSDT.P',
// 'BYBIT:LINKUSDT.P','BYBIT:TONUSDT.P','BYBIT:ICPUSDT.P','BYBIT:LTCUSDT.P',
// 'BYBIT:BCHUSDT.P','BYBIT:ATOMUSDT.P','BYBIT:UNIUSDT.P','BYBIT:XLMUSDT.P',
// 'BYBIT:NEARUSDT.P','BYBIT:AAVEUSDT.P'
// ];
let markets = [
'BYBIT:MATICUSDT.P', 'BYBIT:NEARUSDT.P','BYBIT:AAVEUSDT.P'
];
const { RestClientV5 }= require('bybit-api');
const TradingView = require('@mathieuc/tradingview');
// const {
// fetchAllMarketCapAndCorrelationData,
// RetreveTV_Data_MongoDBpush,
// updateGooglesheets,
// getValidCredentials,
// makeNonprovisionaDailySignal,
// weightedaverage,
// MultipleWeightedAverage,
// alerts,
// MakeAlerts,
// MakeAlertsFast,
// fetchActiveOrders,
// DisplayBinanceData
// // import other utilities as needed
// } = require('@blackphoenixslo/trading-bot-framework');
const fetchAllMarketCapAndCorrelationData = require('./utils/main-bot--GetMArketCap_Corr');
const RetreveTV_Data_MongoDBpush = require('./utils/main-bot--TVretrive'); // Adjust the path as needed
const updateGooglesheets = require('./utils/main-bot--GoogleSheets'); // Adjust the path as needed
const {getValidCredentials} = require('./utils/main-bot__testcredentials'); // Adjust the path as needed
const makeNonprovisionaDailySignal = require('./utils/main-bot--nonProvisional'); // Adjust the path as needed
const {weightedaverage,MultipleWeightedAverage } = require('./utils/main-bot--CalculateWaightedSignals'); // Adjust the path as needed
const {alerts, MakeAlerts ,MakeAlertsFast} = require("./utils/main-bot--alerts");
const {fetchActiveOrders, DisplayBinanceData} = require("./utils/main-bot--bybit");
const cron = require('node-cron');
const { MongoClient } = require('mongodb');
const client = new MongoClient(uri);
async function connectToMongoDB() {
try {
await client.connect();
console.log("Connected successfully to MongoDB");
} catch (error) {
console.error(`Connection to MongoDB failed: ${error}`);
}
}
// Connect to MongoDB when the application starts
connectToMongoDB();
async function main3(id,certificate) {
const type_mybe_provisional = "summary_data_for_"
const type_mybe_provisional2 = "no_provisional_summary_"
// await RetreveTV_Data_MongoDBpush(client_bybit,client,markets,timeframes,indicator_id,id,certificate,dbname); // check if fata exist and not call it
// await makeNonprovisionaDailySignal(client,markets,timeframes,dbname);
// await updateGooglesheets( client,markets, timeframes, type_mybe_provisional,sheetTitle,spreadsheetId);
// const apiKey = '_replace_';
// const url_coinbase = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest';
// const cryptoSymbols = ['BTC', 'ETH', 'XRP', 'SOL', 'BNB','INJ']; // Ensure this is an array
// const periods = [5,15, 30, 60, 90, 120,200];
// const marketCapAndCorrelationData = await fetchAllMarketCapAndCorrelationData(client, dbname, apiKey, url_coinbase, cryptoSymbols, periods);
// console.log('Market Cap and Correlation Data:', marketCapAndCorrelationData);
console.log("done");
let client_bybit = new RestClientV5({
key: key,
secret: secret,
});
// const allocations = {
// 'MATICUSDT': 0.5, // 50% allocation
// 'ENJUSDT': 0.5 // 50% allocation
// };
let client_tv = new TradingView.Client({ token: id, signature: certificate });
// key = "_replace_";
// secret = "_replace_"; // btc eth
client_bybit = new RestClientV5({
key: key,
secret: secret,
});
//DisplayBinanceData(client_bybit,webhookUrl,lev);
await TraderBotS(client,client_tv, client_bybit, weights_no_prov,weights_prov, indicator_id, id, certificate, dbname, markets, timeframes, webhookUrl,lev,NofCoins,leverage,dbname)
MakeAlerts(markets, weights_prov, client, type_mybe_provisional, timeframes,webhookUrl,"ProvisionalGiants",dbname,2);
await sleep(1500 );
MakeAlerts(markets, weights_no_prov, client, type_mybe_provisional2, timeframes,webhookUrl,"NoProvisionalGiants",dbname,2);
await sleep(1500 );
updateGooglesheets( client,markets, timeframes, type_mybe_provisional,"ProvisionalGiants",spreadsheetId,dbname);
await sleep(1500 );
updateGooglesheets( client,markets, timeframes, type_mybe_provisional2,"NoProvisionalGiants",spreadsheetId,dbname);
DisplayBinanceData(client_bybit,webhookUrl,lev);
client_tv.end();
console.log('DONE');
}
async function abs(a){
if (a>0) { return a}
return -a
}
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function TraderBotS(client,client_tv, client_bybit, weights_no_prov,weights_prov, indicator_id, id, certificate, dbname, markets, timeframes, webhookUrl, lev, NofCoins, leverage,dbname) {
const type_mybe_provisional = "summary_data_for_";
const type_mybe_provisional2 = "no_provisional_summary_";
const processMarket = async (market) => {
try {
console.log(`Starting operations for market: ${market}`);
await RetreveTV_Data_MongoDBpush(client_tv,client, [market], timeframes, indicator_id, id, certificate, dbname);
await makeNonprovisionaDailySignal(client, [market], timeframes, dbname);
await sleep(500 );
const weight = await weightedaverage(market, weights_prov, client, type_mybe_provisional, timeframes,dbname, 0);
const weight2 = await weightedaverage(market, weights_no_prov, client, type_mybe_provisional2, timeframes,dbname, 0);
console.log(`weight ${weight}`);
console.log(`weight2 ${weight2}`);
await sleep(500 );
let signal = weight;
if (weight != 0) {
signal = weight / Math.abs(weight)
console.log(`signal1 ${signal}`);
}
if (weight2 != 0) {
signal += weight2 / Math.abs(weight2) ;
console.log(`signal2 ${weight2 / Math.abs(weight2)}`);
}
signal /= 2;
let coin_bybit = market.split(':')[1].replace('.P', '');
let allocationKey = coin_bybit.replace('.P', '');
const allocations = { [allocationKey]: signal / NofCoins };
console.log(`allocations ${signal}`);
await fetchActiveOrders(client_bybit, [market], allocations, webhookUrl, leverage, NofCoins, lev, limit_persentage_ofset);
await sleep(500 );
// Uncomment these if alerts are needed
// MakeAlerts([market], weights, client, type_mybe_provisional, timeframes, webhookUrl);
// MakeAlerts([market], weights, client, type_mybe_provisional2, timeframes, webhookUrl);
console.log(`Operations completed for market: ${market}`);
} catch (error) {
console.error(`Error in processing market: ${market}:`, error);
}
}
for (const market of markets) {
processMarket(market);
// Wait for 10 seconds before starting the next market
await delay(speed); // 10000
}
console.log('All markets initiated.');
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const { promisify } = require('util');
const setTimeoutPromise = promisify(setTimeout);
async function M() {
// Run main3 with a timeout of 15 minutes (1800000 milliseconds)
const main3Promise = main3(id, certificate);
const timeoutPromise = setTimeoutPromise(1800000, 'Timeout');
try {
await Promise.race([main3Promise, timeoutPromise]);
} catch (error) {
if (error === 'Timeout') {
console.log('main3 function timed out after 15 minutes.');
} else {
console.error('Error in main3:', error);
}
}
console.log('DONE');
}
// async function M(){
// // const credentials = await getValidCredentials()
// // const id = credentials.session
// // const certificate = credentials.signature
// const id = "fyb8tc8yps6ju7uarv8vippr62gsi7l5";
// const certificate = "v2:WFPAGDvBqzuPp0YrFtMXiovmN5fJAvfgMytaPpLeIKI=";
// //if (false){
// await main3(id,certificate)
// //}
// }
// M();
// Schedule M to run every 4 hours
cron.schedule('1 */4 * * *', () => {
console.log('Running M every 4 hours');
connectToMongoDB();
M();
});
console.log('Running M every 4 hours startig 00:01 UTC');
console.log('Running M every 4 hours startig 00:01 UTC');
console.log('Running M every 4 hours startig 00:01 UTC');
console.log('Running M every 4 hours startig 00:01 UTC');
console.log('Running M every 4 hours startig 00:01 UTC');
console.log('Running M every 4 hours startig 00:01 UTC');
console.log('Running M every 4 hours startig 00:01 UTC');
M();