Skip to content

Commit 1a4600f

Browse files
authored
Merge pull request #8 from tiagosiebler/BybitUpdate
chore(): updated to Unified account
2 parents c617f4b + 475768b commit 1a4600f

File tree

1 file changed

+119
-114
lines changed

1 file changed

+119
-114
lines changed

src/exchanges/bybit/orders/usdt-perp-orders.ts

Lines changed: 119 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import {
22
API_ERROR_CODE,
33
APIMarket,
4-
LinearClient,
5-
LinearPositionIdx,
64
RestClientV5,
75
WebsocketClient,
86
} from 'bybit-api';
@@ -17,8 +15,8 @@ const secret = "APISECRETHERE";
1715
1816
the below code reads it from env vars first. If none are provided, it defaults to the hardcoded strings:
1917
**/
20-
const apiKey = process.env.API_KEY_COM || 'yourAPIKeyHere';
21-
const apiSecret = process.env.API_SECRET_COM || 'yourAPISecretHere';
18+
const apiKey = process.env.API_KEY_COM || 'your_api_key_here';
19+
const apiSecret = process.env.API_SECRET_COM || 'your_api_secret_here';
2220
const testnet = false;
2321

2422
/**
@@ -66,136 +64,143 @@ function connectAndListenToAccountWebsocketEvents(
6664
}
6765

6866
async function submitUsdtPerpOrders(apiKey: string, apiSecret: string) {
69-
const restClient = new RestClientV5({
70-
key: apiKey,
71-
secret: apiSecret,
72-
testnet: testnet,
73-
});
74-
75-
const TARGET_LEVERAGE = 5;
76-
const TARGET_SYMBOL = 'BTCUSDT';
77-
const BTC_AMOUNT_TO_TRADE = 0.001;
78-
79-
const walletBalanceResponse = await restClient.getWalletBalance({
80-
accountType: 'CONTRACT',
81-
});
82-
const USDTBalanceObject = walletBalanceResponse.result.list[0].coin.find(
83-
(coinBalance) => coinBalance.coin === 'USDT',
84-
);
85-
86-
const usdtBalance = USDTBalanceObject.walletBalance;
67+
try {
68+
const restClient = new RestClientV5({
69+
key: apiKey,
70+
secret: apiSecret,
71+
testnet: testnet,
72+
});
8773

88-
console.log(
89-
'usdtBalance: ',
90-
usdtBalance,
91-
// JSON.stringify(walletBalanceResponse, null, 2),
92-
);
74+
const TARGET_LEVERAGE = 5;
75+
const TARGET_SYMBOL = 'BTCUSDT';
76+
const BTC_AMOUNT_TO_TRADE = 0.001;
9377

94-
// set mode to one-way (easier than hedge-mode, if you don't care for hedge mode)
95-
const positionModeResult = await restClient.switchPositionMode({
96-
category: 'linear',
97-
symbol: TARGET_SYMBOL,
98-
mode: 0,
99-
});
78+
const walletBalanceResponse = await restClient.getWalletBalance({
79+
accountType: 'UNIFIED',
80+
});
81+
const USDTBalanceObject = walletBalanceResponse.result.list[0].coin.find(
82+
(coinBalance) => coinBalance.coin === 'USDT',
83+
);
10084

101-
if (
102-
positionModeResult.retCode === API_ERROR_CODE.POSITION_MODE_NOT_MODIFIED
103-
) {
104-
console.log('position mode was already correct: ', positionModeResult);
105-
} else {
106-
console.log('position mode change result: ', positionModeResult);
107-
}
85+
const usdtBalance = USDTBalanceObject.walletBalance;
10886

109-
// log current positions (and leverage per symbol)
110-
const positionResult = await restClient.getPositionInfo({
111-
category: 'linear',
112-
symbol: TARGET_SYMBOL,
113-
});
87+
console.log(
88+
'usdtBalance: ',
89+
usdtBalance,
90+
// JSON.stringify(walletBalanceResponse, null, 2),
91+
);
11492

115-
console.log(
116-
'positions: ',
117-
positionResult.result.list.map((pos) => {
118-
return {
119-
symbol: pos.symbol,
120-
leverage: pos.leverage,
121-
mode: pos.tradeMode,
122-
size: pos.size,
123-
side: pos.side,
124-
};
125-
}),
126-
);
93+
// set mode to one-way (easier than hedge-mode, if you don't care for hedge mode)
94+
const positionModeResult = await restClient.switchPositionMode({
95+
category: 'linear',
96+
symbol: TARGET_SYMBOL,
97+
mode: 0,
98+
});
12799

128-
// change leverage, only if needed
129-
const leverageToChange = positionResult.result.list.filter(
130-
(pos) => pos.leverage !== TARGET_LEVERAGE.toString(),
131-
);
100+
if (
101+
positionModeResult.retCode === API_ERROR_CODE.POSITION_MODE_NOT_MODIFIED
102+
) {
103+
console.log('position mode was already correct: ', positionModeResult);
104+
} else {
105+
console.log('position mode change result: ', positionModeResult);
106+
}
132107

133-
if (leverageToChange.length) {
134-
const setLeverageResult = await restClient.setLeverage({
135-
symbol: TARGET_SYMBOL,
136-
buyLeverage: TARGET_LEVERAGE.toString(),
137-
sellLeverage: TARGET_LEVERAGE.toString(),
108+
// log current positions (and leverage per symbol)
109+
const positionResult = await restClient.getPositionInfo({
138110
category: 'linear',
111+
symbol: TARGET_SYMBOL,
139112
});
140113

141114
console.log(
142-
'setLeverageResult: ',
143-
JSON.stringify(setLeverageResult, null, 2),
115+
'positions: ',
116+
positionResult.result.list.map((pos) => {
117+
return {
118+
symbol: pos.symbol,
119+
leverage: pos.leverage,
120+
mode: pos.tradeMode,
121+
size: pos.size,
122+
side: pos.side,
123+
};
124+
}),
144125
);
145-
} else {
146-
console.log('no leverage change needed');
147-
}
148126

149-
console.log('entering long position: ');
150-
const successEntryLong = await enterLongPosition(
151-
restClient,
152-
TARGET_SYMBOL,
153-
BTC_AMOUNT_TO_TRADE,
154-
);
155-
if (!successEntryLong) {
156-
// dont continue on fail
157-
return;
158-
}
127+
// change leverage, only if needed
128+
const leverageToChange = positionResult.result.list.filter(
129+
(pos) => pos.leverage !== TARGET_LEVERAGE.toString(),
130+
);
159131

160-
const sleepSecondsBetweenOrder = 1;
161-
await new Promise((resolve) =>
162-
setTimeout(resolve, sleepSecondsBetweenOrder * 1000),
163-
);
132+
if (leverageToChange.length) {
133+
const setLeverageResult = await restClient.setLeverage({
134+
symbol: TARGET_SYMBOL,
135+
buyLeverage: TARGET_LEVERAGE.toString(),
136+
sellLeverage: TARGET_LEVERAGE.toString(),
137+
category: 'linear',
138+
});
139+
140+
console.log(
141+
'setLeverageResult: ',
142+
JSON.stringify(setLeverageResult, null, 2),
143+
);
144+
} else {
145+
console.log('no leverage change needed');
146+
}
147+
148+
console.log('entering long position: ');
149+
const successEntryLong = await enterLongPosition(
150+
restClient,
151+
TARGET_SYMBOL,
152+
BTC_AMOUNT_TO_TRADE,
153+
);
154+
if (!successEntryLong) {
155+
// dont continue on fail
156+
return;
157+
}
158+
159+
const sleepSecondsBetweenOrder = 1;
160+
await new Promise((resolve) =>
161+
setTimeout(resolve, sleepSecondsBetweenOrder * 1000),
162+
);
164163

165-
console.log('closing long position: ');
166-
const successExitLong = await closeLongPosition(restClient, TARGET_SYMBOL);
167-
if (!successExitLong) {
168-
// dont continue on fail
169-
return;
170-
}
164+
console.log('closing long position: ');
165+
const successExitLong = await closeLongPosition(restClient, TARGET_SYMBOL);
166+
if (!successExitLong) {
167+
// dont continue on fail
168+
return;
169+
}
171170

172-
await new Promise((resolve) =>
173-
setTimeout(resolve, sleepSecondsBetweenOrder * 1000),
174-
);
171+
await new Promise((resolve) =>
172+
setTimeout(resolve, sleepSecondsBetweenOrder * 1000),
173+
);
175174

176-
console.log('entering short position: ');
177-
const successEntryShort = await enterShortPosition(
178-
restClient,
179-
TARGET_SYMBOL,
180-
BTC_AMOUNT_TO_TRADE,
181-
);
182-
if (!successEntryShort) {
183-
// dont continue on fail
184-
return;
185-
}
175+
console.log('entering short position: ');
176+
const successEntryShort = await enterShortPosition(
177+
restClient,
178+
TARGET_SYMBOL,
179+
BTC_AMOUNT_TO_TRADE,
180+
);
181+
if (!successEntryShort) {
182+
// dont continue on fail
183+
return;
184+
}
186185

187-
await new Promise((resolve) =>
188-
setTimeout(resolve, sleepSecondsBetweenOrder * 1000),
189-
);
186+
await new Promise((resolve) =>
187+
setTimeout(resolve, sleepSecondsBetweenOrder * 1000),
188+
);
190189

191-
console.log('closing short position: ');
192-
const successExitShort = await closeShortPosition(restClient, TARGET_SYMBOL);
193-
if (!successExitShort) {
194-
// dont continue on fail
195-
return;
190+
console.log('closing short position: ');
191+
const successExitShort = await closeShortPosition(
192+
restClient,
193+
TARGET_SYMBOL,
194+
);
195+
if (!successExitShort) {
196+
// dont continue on fail
197+
return;
198+
}
199+
200+
console.log('reached end - hit ctrl + C to kill the process');
201+
} catch (error) {
202+
console.error('Error submitting USDT perp orders:', error);
196203
}
197-
198-
console.log('reached end - hit ctrl + C to kill the process');
199204
}
200205

201206
async function enterLongPosition(

0 commit comments

Comments
 (0)