-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
166 lines (127 loc) · 4.67 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
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
const dotenv = require('dotenv').config();
const Binance = require('binance-api-node').default;
const Twit = require('twit');
const TelegramBot = require('node-telegram-bot-api');
const binanceClient = Binance({
apiKey: process.env.BINANCE_API_KEY,
apiSecret: process.env.BINANCE_API_SECRET,
});
const T = new Twit({
consumer_key: process.env.TWIITER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token: process.env.TWITTER_ACCESS_TOKEN,
access_token_secret: process.env.TWITTER_ACCESS_SECRET,
});
const Telegramtoken = process.env.TELEGRAM_TOKEN;
const bot = new TelegramBot(Telegramtoken, {
polling: true,
});
const ELON_TWITTER_ID = '44196397';
const PAIR_COIN = process.env.PAIR_COIN || 'USDT';
const MINUTES_TO_SELL = process.env.MINUTES_TO_SELL || 3;
const ALLOW_REPLIES = process.env.ALLOW_REPLIES || false;
const NOTIFY_ALL_TWEETS = process.env.NOTIFY_ALL_TWEETS || false;
const TRADE_ENABLED = process.env.TRADE_ENABLED || true;
const TRADE_PERCENTAGE = process.env.TRADE_PERCENTAGE || 100;
const elonTracker = () => {
console.log('Listening to new tweets of ELON to waste my money . . .');
sendTelegram('Listening to new tweets of ELON to waste my money . . .');
const stream = T.stream('statuses/filter', {
follow: ELON_TWITTER_ID,
});
stream.on('tweet', (tweet) => {
if (tweet.user.id.toString() === ELON_TWITTER_ID) {
const isReply = tweet.in_reply_to_status_id;
if ((!isReply || (isReply && ALLOW_REPLIES)) && TRADE_ENABLED) {
console.log('New tweet of ELON\n\n', tweet.text);
if (/.*doge.*/gi.test(tweet.text)) {
createOrder('DOGE', PAIR_COIN);
}
if (/.*shib.*/gi.test(tweet.text)) {
createOrder('SHIB', PAIR_COIN);
}
}
if (NOTIFY_ALL_TWEETS) {
sendTelegram(`New Tweet of Elon Musk:\n\n${tweet.text}`);
}
}
});
};
const createOrder = async (coinToBuy, pairCoin) => {
let price = await binanceClient.prices({
symbol: `${coinToBuy}${pairCoin}`,
});
price = price[`${coinToBuy}${pairCoin}`];
let freeBalance = await (
await binanceClient.accountInfo({ recvWindow: 60000 })
).balances.find((asset) => asset.asset === pairCoin).free;
freeBalance = freeBalance * (TRADE_PERCENTAGE / 100);
let buyAmount = freeBalance / price;
buyAmount = parseInt(buyAmount);
const buyOrder = await binanceClient.order({
symbol: `${coinToBuy}${pairCoin}`,
side: 'BUY',
quantity: buyAmount,
type: 'MARKET',
recvWindow: 60000,
});
if (buyOrder && buyOrder.fills) {
let price = 0,
quantity = 0,
commission = 0;
for (let fill of buyOrder.fills) {
console.log(fill);
price = parseFloat(price) + parseFloat(fill.price);
quantity = parseFloat(quantity) + parseFloat(fill.qty);
commission = parseFloat(commission) + parseFloat(fill.commission);
}
price = price / buyOrder.fills.length;
const log = `Bought ${buyOrder.origQty} ${buyOrder.symbol} at price ${price} (${quantity} ${buyOrder.symbol}) - Commission = ${commission} (${buyOrder.fills[0].commissionAsset})`;
console.log(log);
sendTelegram(log);
setTimeout(
await makeProfit,
60000 * MINUTES_TO_SELL,
coinToBuy,
pairCoin,
price
);
}
};
const makeProfit = async (coinToSell, pairCoin, buyPrice) => {
let price = await binanceClient.prices({
symbol: `${coinToSell}${pairCoin}`,
});
price = price[`${coinToSell}${pairCoin}`];
const freeBalance = await (
await binanceClient.accountInfo({ recvWindow: 60000 })
).balances.find((asset) => asset.asset === coinToSell).free;
let sellAmount = parseInt(freeBalance);
const sellOrder = await binanceClient.order({
symbol: `${coinToSell}${pairCoin}`,
side: 'SELL',
quantity: sellAmount,
type: 'MARKET',
recvWindow: 60000,
});
if (sellOrder && sellOrder.fills) {
let sellPrice = 0,
quantity = 0,
commission = 0;
for (let fill of sellOrder.fills) {
sellPrice = parseFloat(sellPrice) + parseFloat(fill.price);
quantity = parseFloat(quantity) + parseFloat(fill.qty);
commission = parseFloat(commission) + parseFloat(fill.commission);
}
sellPrice = sellPrice / sellOrder.fills.length;
const profit = (((sellPrice - buyPrice) / buyPrice) * 100).toFixed(3);
const log = `Sold ${sellOrder.origQty} ${sellOrder.symbol} at price ${sellPrice} (${quantity} ${sellOrder.symbol}) - Commission = ${commission} (${sellOrder.fills[0].commissionAsset})`;
console.log(log);
sendTelegram(log);
sendTelegram(`Profit: ${profit}%`);
}
};
const sendTelegram = async (text) => {
bot.sendMessage(process.env.TELEGRAM_CHATID, text);
};
elonTracker();