v1.0.6: fix() fix type differences for spot vs futures candles. Add example for fetching last 1k futures candles
This commit is contained in:
30
examples/rest-public-futures.ts
Normal file
30
examples/rest-public-futures.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { FuturesClient, SpotClient } from '../src/index';
|
||||
|
||||
// or
|
||||
// import { SpotClient } from 'bitget-api';
|
||||
|
||||
const futuresClient = new FuturesClient();
|
||||
|
||||
const symbol = 'BTCUSDT_UMCBL';
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
// Fetch the last 1000 1min candles for a symbol
|
||||
const timestampNow = Date.now();
|
||||
const msPerCandle = 60 * 1000; // 60 seconds x 1000
|
||||
const candlesToFetch = 1000;
|
||||
const msFor1kCandles = candlesToFetch * msPerCandle;
|
||||
const startTime = timestampNow - msFor1kCandles;
|
||||
|
||||
const response = await futuresClient.getCandles(
|
||||
symbol,
|
||||
'1m',
|
||||
startTime.toString(),
|
||||
timestampNow.toString(),
|
||||
candlesToFetch.toString(),
|
||||
);
|
||||
console.log('getCandles returned ' + response.length + ' candles');
|
||||
} catch (e) {
|
||||
console.error('request failed: ', e);
|
||||
}
|
||||
})();
|
||||
@@ -3,13 +3,16 @@ import { SpotClient } from '../src/index';
|
||||
// or
|
||||
// import { SpotClient } from 'bitget-api';
|
||||
|
||||
const client = new SpotClient();
|
||||
const spotClient = new SpotClient();
|
||||
|
||||
const symbol = 'BTCUSDT_SPBL';
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
console.log('getCandles: ', await client.getCandles(symbol, '1min'));
|
||||
const response = await spotClient.getCandles(symbol, '1min', {
|
||||
limit: '1000',
|
||||
});
|
||||
console.log('getCandles: ', response.data.length);
|
||||
} catch (e) {
|
||||
console.error('request failed: ', e);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "bitget-api",
|
||||
"version": "1.0.5",
|
||||
"version": "1.0.6",
|
||||
"description": "Node.js connector for Bitget REST APIs and WebSockets, with TypeScript & end-to-end tests.",
|
||||
"main": "lib/index.js",
|
||||
"types": "lib/index.d.ts",
|
||||
|
||||
@@ -13,6 +13,7 @@ export const API_ERROR_CODE = {
|
||||
QTY_LESS_THAN_MINIMUM: '43006',
|
||||
/** Parameter verification exception margin mode == FIXED */
|
||||
PARAMETER_EXCEPTION: '40808',
|
||||
PLAN_ORDER_REACHED_UPPER_LIMIT: '40889',
|
||||
ORDER_NOT_FOUND: '43001',
|
||||
FUTURES_ORDER_TPSL_NOT_FOUND: '43020',
|
||||
PLAN_ORDER_NOT_FOUND: '43025',
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import {
|
||||
APIResponse,
|
||||
KlineInterval,
|
||||
FuturesProductType,
|
||||
FuturesAccountBillRequest,
|
||||
FuturesBusinessBillRequest,
|
||||
@@ -24,6 +23,7 @@ import {
|
||||
GetHistoricTradesParams,
|
||||
FuturesMarketTrade,
|
||||
FuturesPlanType,
|
||||
FuturesKlineInterval,
|
||||
} from './types';
|
||||
import { REST_CLIENT_TYPE_ENUM } from './util';
|
||||
import BaseRestClient from './util/BaseRestClient';
|
||||
@@ -97,7 +97,7 @@ export class FuturesClient extends BaseRestClient {
|
||||
/** Get Candle Data */
|
||||
getCandles(
|
||||
symbol: string,
|
||||
granularity: KlineInterval,
|
||||
granularity: FuturesKlineInterval,
|
||||
startTime: string,
|
||||
endTime: string,
|
||||
limit?: string,
|
||||
|
||||
@@ -4,7 +4,6 @@ import {
|
||||
NewWalletTransfer,
|
||||
Pagination,
|
||||
APIResponse,
|
||||
KlineInterval,
|
||||
CoinBalance,
|
||||
SymbolRules,
|
||||
NewSpotSubTransfer,
|
||||
@@ -21,6 +20,7 @@ import {
|
||||
SpotMarketTrade,
|
||||
GetHistoricTradesParams,
|
||||
VIPFeeRate,
|
||||
SpotKlineInterval,
|
||||
} from './types';
|
||||
import { REST_CLIENT_TYPE_ENUM } from './util';
|
||||
import BaseRestClient from './util/BaseRestClient';
|
||||
@@ -108,7 +108,7 @@ export class SpotClient extends BaseRestClient {
|
||||
/** Get Candle Data */
|
||||
getCandles(
|
||||
symbol: string,
|
||||
period: KlineInterval,
|
||||
period: SpotKlineInterval,
|
||||
pagination?: Pagination,
|
||||
): Promise<APIResponse<any>> {
|
||||
return this.get('/api/spot/v1/market/candles', {
|
||||
|
||||
@@ -8,6 +8,28 @@ export type FuturesProductType =
|
||||
| 'sdmcbl'
|
||||
| 'scmcbl';
|
||||
|
||||
export type FuturesKlineInterval =
|
||||
| '1m'
|
||||
| '3m'
|
||||
| '5m'
|
||||
| '15m'
|
||||
| '30m'
|
||||
| '1H'
|
||||
| '2H'
|
||||
| '4H'
|
||||
| '6H'
|
||||
| '12H'
|
||||
| '1D'
|
||||
| '3D'
|
||||
| '1W'
|
||||
| '1M'
|
||||
| '6Hutc'
|
||||
| '12Hutc'
|
||||
| '1Dutc'
|
||||
| '3Dutc'
|
||||
| '1Wutc'
|
||||
| '1Mutc';
|
||||
|
||||
export type FuturesHoldSide = 'long' | 'short';
|
||||
|
||||
export type FuturesMarginMode = 'fixed' | 'crossed';
|
||||
|
||||
@@ -2,6 +2,25 @@ import { OrderTimeInForce } from './shared';
|
||||
|
||||
export type WalletType = 'spot' | 'mix_usdt' | 'mix_usd';
|
||||
|
||||
export type SpotKlineInterval =
|
||||
| '1min'
|
||||
| '5min'
|
||||
| '15min'
|
||||
| '30min'
|
||||
| '1h'
|
||||
| '4h'
|
||||
| '6h'
|
||||
| '12h'
|
||||
| '1M'
|
||||
| '1W'
|
||||
| '1week'
|
||||
| '6Hutc'
|
||||
| '12Hutc'
|
||||
| '1Dutc'
|
||||
| '3Dutc'
|
||||
| '1Wutc'
|
||||
| '1Mutc';
|
||||
|
||||
export interface NewWalletTransfer {
|
||||
fromType: WalletType;
|
||||
toType: WalletType;
|
||||
|
||||
@@ -4,6 +4,9 @@ export type numberInString = string;
|
||||
|
||||
export type OrderSide = 'Buy' | 'Sell';
|
||||
|
||||
/**
|
||||
* @deprecated use SpotKlineInterval or FuturesKlineInterval, depending on which API group you're using
|
||||
*/
|
||||
export type KlineInterval =
|
||||
| '1min'
|
||||
| '5min'
|
||||
|
||||
@@ -26,6 +26,47 @@ interface UnsignedRequest<T extends object | undefined = {}> {
|
||||
|
||||
type SignMethod = 'bitget';
|
||||
|
||||
if (
|
||||
typeof process === 'object' &&
|
||||
typeof process.env === 'object' &&
|
||||
process.env.BITGETTRACE
|
||||
) {
|
||||
axios.interceptors.request.use((request) => {
|
||||
console.log(
|
||||
new Date(),
|
||||
'Starting Request',
|
||||
JSON.stringify(
|
||||
{
|
||||
url: request.url,
|
||||
method: request.method,
|
||||
params: request.params,
|
||||
data: request.data,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
return request;
|
||||
});
|
||||
axios.interceptors.response.use((response) => {
|
||||
console.log(new Date(), 'Response:', {
|
||||
// request: {
|
||||
// url: response.config.url,
|
||||
// method: response.config.method,
|
||||
// data: response.config.data,
|
||||
// headers: response.config.headers,
|
||||
// },
|
||||
response: {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
headers: response.headers,
|
||||
data: response.data,
|
||||
},
|
||||
});
|
||||
return response;
|
||||
});
|
||||
}
|
||||
|
||||
export default abstract class BaseRestClient {
|
||||
private options: RestClientOptions;
|
||||
private baseUrl: string;
|
||||
|
||||
@@ -274,8 +274,11 @@ describe('Private Spot REST API POST Endpoints', () => {
|
||||
data: expect.any(String),
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('cancelPlanOrder(): ', e);
|
||||
expect(e).toBeNull();
|
||||
// console.error('cancelPlanOrder(): ', e);
|
||||
// expect(e).toBeNull();
|
||||
expect(e.body).toMatchObject({
|
||||
code: API_ERROR_CODE.PLAN_ORDER_NOT_FOUND,
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user