-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharbTrade.js
195 lines (156 loc) · 9.96 KB
/
arbTrade.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
require('dotenv').config();
const ccxt = require('ccxt-xpr');
const BigNumber = require('bignumber.js');
const TelegramBot = require('node-telegram-bot-api');
const fetchExchanges = require('./src/fetchExchanges.js');
const fetchPricesAndPrecision = require('./src/fetchPrices.js');
const fetchBalances = require('./src/fetchBalances.js');
const exchangeSymbols = require('./src/exchangeSymbols.js');
const testMode = false; // if true, will only search for opportunties and won't actually execute trades
const enableTelegram = true; // if true will send to telegram bot when arb trade happens
const forceMode = true; // if true will keep trying in even if insufficient balance
const exchangeName = 'Kucoin'; // the name of the exchange, can use 'Kucoin' instead
// const symbolProtonDEX = 'XLTC_XMD';
// const symbolExchange = 'LTC/USDT';
const tradingPairs = [
{ symbolProtonDEX: 'XLTC_XMD', symbolExchange: 'LTC/USDT' },
{ symbolProtonDEX: 'XDOGE_XMD', symbolExchange: 'DOGE/USDT' },
];
const dollarAmount = 10; // Minimal amount in dollars at least $1.5
const arbitrageThreshold = 3;
// replace the values in .env with your own
const bot = new TelegramBot(process.env.TELEGRAM_BOT_TOKEN, {polling: false});
const chatId = process.env.TELEGRAM_CHAT_ID;
// const symbols = exchangeSymbols(symbolProtonDEX, symbolExchange);
// const { baseTokenProtonDEX, baseTokenExchange, quoteTokenProtonDEX, quoteTokenExchange } = symbols;
async function arbTrading(symbolProtonDEX, symbolExchange, exchangeName) {
const symbols = exchangeSymbols(symbolProtonDEX, symbolExchange, exchangeName);
const { baseTokenProtonDEX, baseTokenExchange, quoteTokenProtonDEX, quoteTokenExchange } = symbols;
const { exchangeProtonDEX, exchange } = await fetchExchanges(exchangeName);
// Fetch prices and precision
const { priceProtonDEX, priceExchange, precisionProtonDEXAsk, precisionProtonDEXBid, precisionExchange } =
await fetchPricesAndPrecision(exchangeProtonDEX, exchange, symbolProtonDEX, symbolExchange, baseTokenProtonDEX, baseTokenExchange, quoteTokenProtonDEX, quoteTokenExchange);
// Fetch balances
const { balanceProtonDEX, balanceExchange } =
await fetchBalances({exchangeProtonDEX, exchange, priceProtonDEX, priceExchange,exchangeName, ...symbols});
try {
console.log('ProtonDEX Price:', priceProtonDEX);
console.log(`${exchangeName} Price:`, priceExchange);
if (priceProtonDEX && priceExchange) {
const spreadPercentage = Math.abs(((priceExchange - priceProtonDEX) / priceProtonDEX) * 100);
console.log('Spread Percentage:', spreadPercentage.toFixed(2) + '%');
if (spreadPercentage > arbitrageThreshold) {
console.log('Arbitrage opportunity detected!');
const maxPrice = Math.max(priceProtonDEX, priceExchange); // Use the higher price
let amount = Number(new BigNumber(dollarAmount / maxPrice).toFixed(12)); // Amount in base currency
console.log('Amount ', amount);
let sufficientBalance = true;
// If ProtonDEX price is lower, BUY on ProtonDEX and SELL on Exchange
if (priceExchange > priceProtonDEX) {
const decimalPlaces = (precisionExchange.toString().split('.')[1] || []).length;
const amountProtonDEX = Number(new BigNumber(dollarAmount).toFixed(precisionProtonDEXAsk));
const amountExchange = Number(amount.toFixed(decimalPlaces));
const action = (`Buy ${amountProtonDEX} ${quoteTokenProtonDEX} of ${baseTokenProtonDEX} on ProtonDEX at price ${priceProtonDEX} and sell ${amountExchange} ${baseTokenExchange} on ${exchangeName} at price ${priceExchange}`);
console.log(action);
console.log(amountProtonDEX);
console.log(amountExchange);
console.log(precisionProtonDEXAsk);
console.log(precisionProtonDEXBid);
console.log(precisionExchange);
if (balanceProtonDEX.free[quoteTokenProtonDEX] < dollarAmount ||
balanceExchange.free[baseTokenExchange] < amountExchange) {
console.error('Insufficient balance!');
sufficientBalance = false;
//if (enableTelegram) {bot.sendMessage(chatId, "Insufficient balance!");}
//console.log(sufficientBalance);
//process.exit(1);
// We can keep trying until we have sufficient balance in one direction
}
// now buy and sell
if (!testMode && sufficientBalance) {
if (enableTelegram) {bot.sendMessage(chatId, action);}
// Buy on ProtonDEX
try {
const orderProtonDEX = await exchangeProtonDEX.createOrder(symbolProtonDEX, 1, 1, amountProtonDEX, priceProtonDEX, {
'account': process.env.PROTONDEX_ACCOUNT_2,
'filltype': 0,
'triggerprice': 0,
});
console.log('ProtonDEX buy order placed:', orderProtonDEX);
} catch (error) {
console.error('Error placing ProtonDEX order:', error);
}
// Sell on Exchange
try {
const orderExchange = await exchange.createOrder(symbolExchange, 'limit', 'sell', amountExchange, priceExchange);
console.log(`${exchangeName} sell order placed:`, orderExchange);
} catch (error) {
console.error(`Error placing ${exchangeName} order:`, error);
}
} else {
console.log('Insufficient Balalance or Test Mode enabled, no order placed.');
}
}
// If ProtonDEX price is higher, SELL on ProtonDEX and BUY on Exchange
else {
// Sell on ProtonDEX
const amountProtonDEX = Number(new BigNumber(dollarAmount / priceProtonDEX).toFixed(precisionProtonDEXBid)); // Amount to sell on ProtonDEX
const amountExchange = Number((dollarAmount / priceExchange).toFixed(8)); // Amount to buy on Exchange
const action = (`Sell ${amountProtonDEX} ${baseTokenProtonDEX} worth ${dollarAmount.toFixed(precisionProtonDEXAsk)} ${quoteTokenProtonDEX} on ProtonDEX at price ${priceProtonDEX} and buy ${dollarAmount} ${quoteTokenExchange} worth of ${baseTokenExchange} on ${exchangeName} at price ${priceExchange}`);
console.log(action);
console.log(amountProtonDEX);
console.log(amountExchange);
console.log(precisionProtonDEXAsk);
console.log(precisionProtonDEXBid);
console.log(precisionExchange);
if (balanceProtonDEX.free[baseTokenProtonDEX] < amountProtonDEX ||
balanceExchange.free[quoteTokenExchange] < dollarAmount) {
console.error('Insufficient balance!');
console.log(sufficientBalance);
//if (enableTelegram) {bot.sendMessage(chatId, "Insufficient balance!");}
//process.exit(1);
// We can keep trying until we have sufficient balance in one direction
}
if (!testMode && sufficientBalance) {
if (enableTelegram) { bot.sendMessage(chatId, action);};
try {
const orderProtonDEX = await exchangeProtonDEX.createOrder(symbolProtonDEX, 1, 2, amountProtonDEX, priceProtonDEX, {
'account': process.env.PROTONDEX_ACCOUNT_2,
'filltype': 0,
'triggerprice': 0,
});
console.log('ProtonDEX sell order placed:', orderProtonDEX);
} catch (error) {
console.error('Error placing ProtonDEX order:', error);
}
// Buy on Exchange
try {
const orderExchange = await exchange.createOrder(symbolExchange, 'limit', 'buy', amountExchange, priceExchange);
console.log(`${exchangeName} buy order placed:`, orderExchange);
} catch (error) {
console.error(`Error placing ${exchangeName} order:`, error);
}
} else {
console.log('Insufficient Balalance or Test Mode enabled, no order placed.');
}
}
} else {
console.log('No arbitrage opportunity at the moment.');
}
} else {
console.log('Unable to calculate spread percentage. Prices are missing.');
}
} catch (error) {
if (error instanceof ccxt.DDoSProtection || error.message.includes('429')) {
console.log('Rate limit exceeded, waiting for 1 minute before retrying.');
setTimeout(fetchPricesAndPrecision, 60000, exchangeProtonDEX, exchange, symbolProtonDEX, symbolExchange);
} else {
console.error('Error fetching prices or placing orders:', error.message);
process.exit(1);
}
}
}
// setInterval(arbTrading, 60000);
tradingPairs.forEach(pair => {
setInterval(arbTrading, 60000, pair.symbolProtonDEX, pair.symbolExchange, exchangeName);
});