-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
rest-private-trade.ts
136 lines (110 loc) · 4.21 KB
/
rest-private-trade.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { RestClient } from '../src/index';
import { OrderRequest } from '../src/types/rest';
// or
// import { SpotClient } from 'okx-api';
// read from environmental variables
const API_KEY = process.env.API_KEY_COM;
const API_SECRET = process.env.API_SECRET_COM;
const API_PASS = process.env.API_PASS_COM;
if (!API_KEY || !API_SECRET || !API_PASS) {
throw new Error(
`Missing api credentials. Use environmental variables or hard code in the script`
);
}
console.log(new Date(), 'Using credentials: ', {
API_KEY,
API_SECRET,
API_PASS,
});
const client = new RestClient({
apiKey: API_KEY,
// apiKey: 'apiKeyHere',
apiSecret: API_SECRET,
// apiSecret: 'apiSecretHere',
apiPass: API_PASS,
// apiPass: 'apiPassHere',
});
// const wsClient = new WebsocketClient({
// apiKey: API_KEY,
// apiSecret: API_SECRET,
// apiPass: API_PASS,
// });
function logWSEvent(type, data) {
console.log(new Date(), `WS ${type} event: `, data);
}
// simple sleep function
function promiseSleep(milliseconds) {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}
// WARNING: for sensitive math you should be using a library such as decimal.js!
function roundDown(value, decimals) {
return Number(
Math.floor(parseFloat(value + 'e' + decimals)) + 'e-' + decimals
);
}
/** This is a simple script wrapped in a immediately invoked function expression, designed to check for any available BTC balance and immediately sell the full amount for USDT */
(async () => {
try {
// // Add event listeners to log websocket events on account
// wsClient.on('update', (data) => logWSEvent('update', data));
// wsClient.on('open', (data) => logWSEvent('open', data));
// wsClient.on('response', (data) => logWSEvent('response', data));
// wsClient.on('reconnect', (data) => logWSEvent('reconnect', data));
// wsClient.on('reconnected', (data) => logWSEvent('reconnected', data));
// wsClient.on('authenticated', (data) => logWSEvent('authenticated', data));
// wsClient.on('exception', (data) => logWSEvent('exception', data));
// // Subscribe to private account topics
// wsClient.subscribeTopic('SPBL', 'account');
// wsClient.subscribeTopic('SPBL', 'orders');
// wait briefly for ws to be ready (could also use the response or authenticated events, to make sure topics are subscribed to before starting)
await promiseSleep(2.5 * 1000);
const allBalances = await client.getBalance();
// const balances = allBalances.filter((bal) => Number(bal.available) != 0);
const usdtBalanceResult = allBalances[0].details.find(
(bal) => bal.ccy === 'USDT'
);
console.log('BTC balance result: ', usdtBalanceResult);
const usdtBalance = Number(usdtBalanceResult?.availBal);
// console.log('balance: ', JSON.stringify(balances, null, 2));
if (!usdtBalanceResult || !usdtBalance) {
console.error('No USDT to trade');
return;
}
console.log(`USDT available: ${usdtBalance}`);
const symbol = 'BTC-USDT';
const quantity = 0.002;
const order: OrderRequest = {
instId: symbol,
ordType: 'market',
side: 'buy',
sz: String(usdtBalance * 0.5),
tdMode: 'cash',
tgtCcy: 'base_ccy',
};
const buyResult = await client.submitOrder(order);
console.log('result: ', buyResult);
return;
// const symbol = 'BTCUSDT_SPBL';
// const symbolsResult = await client.getSymbols();
// const btcRules = symbolsResult.data.find((rule) => rule.symbol === symbol);
// console.log('btc trading rules: ', btcRules);
// if (!btcRules) {
// return console.log('no rules found for trading ' + symbol);
// }
// const quantityScale = Number(btcRules.quantityScale);
// // const quantityRoundedDown = btcAmount - btcAmount % 0.01
// const quantity = roundDown(btcAmount, quantityScale);
// const order = {
// symbol: symbol,
// side: 'sell',
// force: 'normal',
// orderType: 'market',
// quantity: String(quantity),
// } as const;
// console.log('submitting order: ', order);
// const sellResult = await client.submitOrder(order);
// console.log('sell result: ', sellResult);
} catch (e) {
console.error('request failed: ', e);
}
})();