-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v3.9.9: feat() support demo trading apis
- Loading branch information
tiagosiebler
committed
Apr 9, 2024
1 parent
185f02a
commit cc362c4
Showing
9 changed files
with
160 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { RestClientV5, WebsocketClient } from '../src/index'; | ||
|
||
// or | ||
// import { RestClientV5 } from 'bybit-api'; | ||
|
||
const key = process.env.API_KEY_COM; | ||
const secret = process.env.API_SECRET_COM; | ||
|
||
const restClient = new RestClientV5({ | ||
key: key, | ||
secret: secret, | ||
parseAPIRateLimits: true, | ||
/** | ||
* Set this to true to enable demo trading: | ||
*/ | ||
demoTrading: true, | ||
}); | ||
|
||
const wsClient = new WebsocketClient({ | ||
market: 'v5', | ||
/** | ||
* Set this to true to enable demo trading for the private account data WS | ||
* Topics: order,execution,position,wallet,greeks | ||
*/ | ||
demoTrading: true, | ||
}); | ||
|
||
function setWsClientEventListeners( | ||
websocketClient: WebsocketClient, | ||
accountRef: string, | ||
): void { | ||
websocketClient.on('update', (data) => { | ||
console.log(new Date(), accountRef, 'data ', JSON.stringify(data)); | ||
// console.log('raw message received ', JSON.stringify(data, null, 2)); | ||
}); | ||
|
||
websocketClient.on('open', (data) => { | ||
console.log(new Date(), accountRef, 'connection opened open:', data.wsKey); | ||
}); | ||
websocketClient.on('response', (data) => { | ||
console.log( | ||
new Date(), | ||
accountRef, | ||
'log response: ', | ||
JSON.stringify(data, null, 2), | ||
); | ||
}); | ||
websocketClient.on('reconnect', ({ wsKey }) => { | ||
console.log( | ||
new Date(), | ||
accountRef, | ||
'ws automatically reconnecting.... ', | ||
wsKey, | ||
); | ||
}); | ||
websocketClient.on('reconnected', (data) => { | ||
console.log(new Date(), accountRef, 'ws has reconnected ', data?.wsKey); | ||
}); | ||
websocketClient.on('error', (data) => { | ||
console.error(new Date(), accountRef, 'ws exception: ', data); | ||
}); | ||
} | ||
|
||
(async () => { | ||
try { | ||
setWsClientEventListeners(wsClient, 'demoAcc'); | ||
|
||
const balResponse1 = await restClient.getWalletBalance({ | ||
accountType: 'CONTRACT', | ||
}); | ||
console.log('balResponse1: ', JSON.stringify(balResponse1, null, 2)); | ||
|
||
const demoFunds = await restClient.requestDemoTradingFunds(); | ||
console.log(`requested demo funds: `, demoFunds); | ||
|
||
const balResponse2 = await restClient.getWalletBalance({ | ||
accountType: 'CONTRACT', | ||
}); | ||
console.log('balResponse2: ', JSON.stringify(balResponse2, null, 2)); | ||
|
||
/** Simple examples for private REST API calls with bybit's V5 REST APIs */ | ||
const response = await restClient.getPositionInfo({ | ||
category: 'linear', | ||
symbol: 'BTCUSDT', | ||
}); | ||
|
||
console.log('response:', response); | ||
|
||
// Trade USDT linear perps | ||
const buyOrderResult = await restClient.submitOrder({ | ||
category: 'linear', | ||
symbol: 'BTCUSDT', | ||
orderType: 'Market', | ||
qty: '1', | ||
side: 'Buy', | ||
}); | ||
console.log('buyOrderResult:', buyOrderResult); | ||
|
||
const sellOrderResult = await restClient.submitOrder({ | ||
category: 'linear', | ||
symbol: 'BTCUSDT', | ||
orderType: 'Market', | ||
qty: '1', | ||
side: 'Sell', | ||
}); | ||
console.log('sellOrderResult:', sellOrderResult); | ||
|
||
const balResponse3 = await restClient.getWalletBalance({ | ||
accountType: 'CONTRACT', | ||
}); | ||
console.log('balResponse2: ', JSON.stringify(balResponse3, null, 2)); | ||
} catch (e) { | ||
console.error('request failed: ', e); | ||
} | ||
})(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters