-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbybit-connector.ts
201 lines (129 loc) · 7.03 KB
/
bybit-connector.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { Request } from 'https://deno.land/x/request@1.3.0/request.ts'
import { IExchangeConnector } from "../interfaces/exchange-connector-interface.ts";
import { GeneralUtilityBox } from "../utility-boxes/general-utility-box.ts";
export enum getEndPoints {
"/v2/private/wallet/balance",
"/private/linear/position/list",
"/v2/public/risk-limit/list",
"/v2/private/order/list"
}
export enum postEndPoints {
"/private/linear/position/set-leverage",
"/v2/public/risk-limit/list",
"/private/linear/order/create",
"/asset/v1/private/transfer",
"/private/linear/position/trading-stop",
"/v2/private/order/cancelAll"
}
export class BybitConnector implements IExchangeConnector {
private readonly apiKey
private readonly apiSecret
private readonly accountId
private readonly baseURL = "https://api.bybit.com"
public constructor(apiKey: string, apiSecret: string, accountId: string = 'default') {
this.apiKey = apiKey
this.apiSecret = apiSecret
this.accountId = accountId
console.log(`An instance of BybitConnectorDouble has been created`)
}
public getAccountId(): string {
return this.accountId
}
public async getOrderList(symbol: string) {
const timestamp = (Date.now()).toString()
console.log(`checking orders for ${symbol}`)
const queryForSign = `api_key=${this.apiKey}&symbol=${symbol}×tamp=${timestamp}`
const sign = GeneralUtilityBox.getHMACFromQuery(queryForSign, this.apiSecret)
const url = `${this.baseURL}${getEndPoints[3]}?api_key=${this.apiKey}&symbol=${symbol}×tamp=${timestamp}&sign=${sign}`
console.log(`calling ${url}`)
const result = await Request.get(url)
return result
}
public async getFuturesAccountData() {
const url = await this.getURLSigned(getEndPoints[0])
const result = await Request.get(url)
return result
}
public async buyFuture(symbol: string, amount: number, reduceOnly: boolean) {
return this.placeMarketOrder(symbol, amount, 'Buy', reduceOnly)
}
public async sellFuture(symbol: string, amount: number, reduceOnly: boolean) {
return this.placeMarketOrder(symbol, amount, 'Sell', reduceOnly)
}
public async setLeverage(symbol: string, leverage: number) {
const timestamp = (Date.now()).toString()
const url = await this.getURL(postEndPoints[0])
const queryForSign = `api_key=${this.apiKey}&buy_leverage=${leverage}&sell_leverage=${leverage}&symbol=${symbol}×tamp=${timestamp}`
const sign = GeneralUtilityBox.getHMACFromQuery(queryForSign, this.apiSecret)
const body = { "api_key": this.apiKey, "buy_leverage": leverage, "sell_leverage": leverage, "symbol": symbol, "timestamp": timestamp, "sign": sign }
return Request.post(url, body)
}
public async setRiskLimit(symbol: string, riskId: number = 20) {
const timestamp = (Date.now()).toString()
const url = await this.getURL(postEndPoints[1])
const queryForSign = `api_key=${this.apiKey}&risk_id=${riskId}&symbol=${symbol}×tamp=${timestamp}`
const sign = GeneralUtilityBox.getHMACFromQuery(queryForSign, this.apiSecret)
const body = { "api_key": this.apiKey, "risk_id": riskId, "symbol": symbol, "timestamp": timestamp, "sign": sign }
return Request.post(url, body)
}
public async getPositions() {
const url = await this.getURLSigned(getEndPoints[1])
const response = await Request.get(url)
return response.result.filter((p: any) => p.data.size > 0)
}
public async getRiskLimits() {
const url = await this.getURLSigned(getEndPoints[2])
const result = await Request.get(url)
return result
}
public async setStopLoss(pair: string, stopLoss: number, side: string) {
const timestamp = (Date.now()).toString()
const url = await this.getURL(postEndPoints[4])
const queryForSign = `api_key=${this.apiKey}&side=${side}&stop_loss=${stopLoss}&symbol=${pair}×tamp=${timestamp}`
const sign = GeneralUtilityBox.getHMACFromQuery(queryForSign, this.apiSecret)
const body = { "api_key": this.apiKey, "symbol": pair, "side": side, "stop_loss": stopLoss, "timestamp": timestamp, "sign": sign }
console.log("body:", JSON.stringify(body))
const r = await Request.post(url, body)
// console.log(r)
return r
}
public async cancelAllActiveOrders(pair: string) {
const timestamp = (Date.now()).toString()
const url = await this.getURL(postEndPoints[5])
const queryForSign = `api_key=${this.apiKey}&symbol=${pair}×tamp=${timestamp}`
const sign = GeneralUtilityBox.getHMACFromQuery(queryForSign, this.apiSecret)
const body = { "api_key": this.apiKey, "symbol": pair, "timestamp": timestamp, "sign": sign }
console.log("body:", JSON.stringify(body))
const r = await Request.post(url, body)
// console.log(r)
return r
}
private async placeMarketOrder(pair: string, amount: number, side: string, reduceOnly: boolean) {
const timestamp = (Date.now()).toString()
const url = await this.getURL(postEndPoints[2])
const queryForSign = `api_key=${this.apiKey}&close_on_trigger=false&order_type=Market&qty=${amount}&reduce_only=${reduceOnly}&side=${side}&symbol=${pair}&time_in_force=GoodTillCancel×tamp=${timestamp}`
const sign = GeneralUtilityBox.getHMACFromQuery(queryForSign, this.apiSecret)
const body = { "api_key": this.apiKey, "close_on_trigger": false, "order_type": "Market", "qty": amount, "reduce_only": reduceOnly, "side": side, "symbol": pair, "time_in_force": "GoodTillCancel", "timestamp": timestamp, "sign": sign }
const r = await Request.post(url, body)
// console.log(r)
return r
}
private async getURLSigned(endPoint: string) {
const timestamp = (Date.now()).toString()
const queryForSign = `api_key=${this.apiKey}×tamp=${timestamp}`
const sign = GeneralUtilityBox.getHMACFromQuery(queryForSign, this.apiSecret)
// return `${this.baseURL}/${endPoint}?api_key=${this.apiKey}×tamp=${timestamp}&sign=${sign}`
return `${this.baseURL}${endPoint}?api_key=${this.apiKey}×tamp=${timestamp}&sign=${sign}`
}
private async getURL(endPoint: string) {
return `${this.baseURL}${endPoint}`
}
public async transferUSDT(fromAccountType: string, toAccountType: string, amount: number, coin: string, transferId: string) {
// const timestamp = (Date.now()).toString()
// const url = await this.getURL(postEndPoints[2])
// const queryForSign = `api_key=${this.apiKey}&risk_id=${riskId}&symbol=${symbol}×tamp=${timestamp}`
// const sign = GeneralUtilityBox.getHMACFromQuery(queryForSign, this.apiSecret)
// const body = { "api_key": this.apiKey, "risk_id": riskId, "symbol": symbol, "timestamp": timestamp, "sign": sign }
// return Request.post(url, body)
}
}