-
Notifications
You must be signed in to change notification settings - Fork 1
/
marketMaker.js
118 lines (89 loc) · 4.68 KB
/
marketMaker.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
require('dotenv').config();
const ccxt = require('ccxt-xpr');
const BigNumber = require('bignumber.js');
const symbolProtonDEX = 'XPR_XMD';
const dollarAmount = 42; // Minimal amount in dollars at least $1.5
const exchangeProtonDEX = new ccxt.protondex({
'secret': process.env.PROTONDEX_API_SECRET,
'verbose': false,
'timeout': 60000,
});
async function marketMaking() {
// Fetch the ticker to get the latest price
const ticker = await exchangeProtonDEX.fetchTicker(symbolProtonDEX);
//console.log('Ticker:', ticker);
// get all open orders
const orders = await exchangeProtonDEX.fetchOpenOrders('XPR_XMD', 1, 100, {
'account': process.env.PROTONDEX_ACCOUNT, 'offset': 0, 'ordinal_order_ids': ''
});
//console.log('Orders:', orders);
// cancel all orders
for (let i = 0; i < orders.length; i++) {
const order = orders[i];
try {
const response = await exchangeProtonDEX.cancelOrder(order.id, symbolProtonDEX, {'account': process.env.PROTONDEX_ACCOUNT});
console.log(`Order ${order.id} cancelled.`);
} catch (error) {
console.error(`Error cancelling order ${order.id}:`, error);
}
}
// Cancel all open orders
//const cancelAllOrders = await exchangeProtonDEX.cancelAllOrders('XPR_XMD');
// Fetch the balance to ensure we have enough funds
//const balance = await exchangeProtonDEX.fetchBalance({'account': process.env.PROTONDEX_ACCOUNT});
// Fetch the markets to get precision info
const markets = await exchangeProtonDEX.fetchMarkets();
const marketProtonDEX = markets.find(market => market.symbol === symbolProtonDEX);
const precisionProtonDEXAsk = Number(marketProtonDEX.info.ask_token.precision);
const precisionProtonDEXBid = Number(marketProtonDEX.info.bid_token.precision);
// console.log('Market:', marketProtonDEX);
let price = new BigNumber(ticker.last).toFixed(precisionProtonDEXAsk);
const incrementPercentage = new BigNumber(0.0025); // 1 percent
// buy
let buyPrice = Number(new BigNumber(price).times(0.995)).toFixed(precisionProtonDEXAsk); // 0.5% less than the current ask price
console.log('buyPrice', buyPrice.toString());
let buyAmount = Number(new BigNumber(dollarAmount).toFixed(precisionProtonDEXAsk)); // Amount in base currency
console.log('buyAmount', buyAmount);
try {
for (let i = 0; i < 7; i++) {
let adjustedBuyPrice = new BigNumber(buyPrice).times(new BigNumber(1).minus(incrementPercentage.times(i)));
adjustedBuyPrice = adjustedBuyPrice.toFixed(precisionProtonDEXAsk);
const buyOrder = await exchangeProtonDEX.createOrder(symbolProtonDEX, 1, 1, dollarAmount, adjustedBuyPrice.toString(), {
'account': process.env.PROTONDEX_ACCOUNT,
'filltype': 0,
'triggerprice': 0,
});
console.log(`Buy order ${i+1} placed at price ${adjustedBuyPrice.toString()}:`);
// console.log(`Buy order ${i+1} placed at price ${adjustedBuyPrice.toString()}:`, buyOrder);
}
} catch (error) {
console.error('Error placing buy order:', error);
}
// For example, to place a sell order:
let sellPrice = new BigNumber(price).times(1.005); // 1% more than the current bid price
console.log('sellPrice', sellPrice.toString());
//const sellAmount = new BigNumber(dollarAmount).dividedBy(price).toFixed(precisionProtonDEXBid);
const sellAmount = Number(new BigNumber(dollarAmount).dividedBy(price).toFixed(precisionProtonDEXBid));
console.log('sellAmount', sellAmount);
try {
for (let i = 0; i < 10; i++) {
let adjustedSellPrice = new BigNumber(sellPrice.times(BigNumber(1).plus(incrementPercentage.times(i))));
adjustedSellPrice = adjustedSellPrice.toFixed(precisionProtonDEXAsk);
const sellOrder = await exchangeProtonDEX.createOrder(symbolProtonDEX, 1, 2, sellAmount, adjustedSellPrice.toString(), {
'account': process.env.PROTONDEX_ACCOUNT,
'filltype': 0,
'triggerprice': 0,
});
console.log(`Sell order ${i+1} placed at price ${adjustedSellPrice.toString()}:`);
// console.log(`Sell order ${i+1} placed at price ${adjustedSellPrice.toString()}:`, sellOrder);
}
} catch (error) {
console.error('Error placing sell order:', error);
}
// // create a delay of 1 minute
//await new Promise(resolve => setTimeout(resolve, 120000));
}
marketMaking();
setInterval(marketMaking, 2400000); // Run the marketMaking function every minute
console.log(`Wait for 40 minutes`);
//marketMaking();