-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.ts
112 lines (94 loc) · 3.43 KB
/
server.ts
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
import ccxt from "ccxt";
import dotenv from "dotenv";
dotenv.config();
// env variables
console.log("Bybit API Key:", process.env.BYBIT_API_KEY);
const BYBIT_API_KEY = process.env.BYBIT_API_KEY;
const BYBIT_SECRET = process.env.BYBIT_API_SECRET;
// bybit exchange instance
const Bybit = new ccxt.bybit({
apiKey: BYBIT_API_KEY,
secret: BYBIT_SECRET,
});
// enable sandbox mode or testnet
Bybit.setSandboxMode(true);
const getBalance = async () => {
try {
const balance = await Bybit.fetchBalance();
return balance;
} catch (error) {
console.log(error);
return null;
}
};
const getTickerInfo = async (ticker: string) => {
try {
const tickerInfo = await Bybit.fetchTicker(ticker);
return tickerInfo;
} catch (error) {
console.log(error);
return null;
}
};
const createCustomEthOrder = async () => {
try {
const EthTickerInfo = await Bybit.fetchTicker("ETH/USDT");
if (!EthTickerInfo) throw new Error("Failed to fetch ETH ticker info");
const EthLTP = EthTickerInfo.last;
console.log("ETH LTP:", EthLTP);
if (!EthLTP) throw new Error("Failed to fetch ETH LTP");
const order_price = EthLTP * 0.99; // 0.99 i.e 99 % of LTP or 1% less than LTP
console.log("Order Price:", order_price);
const order = await Bybit.createOrder(
"ETH/USDT",
"limit",
"buy",
0.01,
order_price
);
if (!order) throw new Error("Failed to create order");
// check if order is created successfully and if order is created successfully then cancel that order after 10 sec
setTimeout(async () => {
try {
const cancelOrder = await Bybit.cancelOrder(order.id, "ETH/USDT");
console.log("Order cancelled successfully:", cancelOrder);
} catch (error: any) {
console.error("Failed to cancel order:", error.message);
}
}, 10000);
return order;
} catch (error) {
console.log(error);
return null;
}
};
const createRandomOrder = async (basePrice: number) => {
// Generate a random order side: buy or sell
const randomOrderSide = Math.random() > 0.5 ? "buy" : "sell";
// Calculate a random spread between -0.05 (-5%) and 0.05 (5%) of the LTP
const spread = Math.random() * 0.1 - 0.05; // Spread from -5% to +5%
// Adjust the price by the spread
const price = basePrice * (1 + spread);
// Place the order with a fixed amount for simplicity, could also be randomized
await Bybit.createOrder("DOT/USDT", "limit", randomOrderSide, 0.001, price);
console.log(`Created ${randomOrderSide} order at price: ${price}`);
};
const main = async () => {
console.log(await getBalance());
const tickerInfo = await getTickerInfo("DOT/USDT");
// Assuming getTickerInfo returns an object with a 'last' property for the last price
let lastPrice = tickerInfo?.last;
// Create a random order based on the last traded price
await createRandomOrder(lastPrice);
// Wait for 20 seconds before placing another order
await new Promise((resolve) => setTimeout(resolve, 20000));
// Get updated ticker info in case the price has changed
const updatedTickerInfo = await getTickerInfo("DOT/USDT");
lastPrice = updatedTickerInfo?.last;
// Create another random order based on the updated last traded price
await createRandomOrder(lastPrice);
};
// Run the main function in a loop with a 6-second interval
setInterval(() => {
main().catch(console.error); // Catch and log any errors
}, 60000); // Adjusted to 60 seconds for practicality