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:
Tiago Siebler
2023-04-04 11:58:09 +01:00
parent f367061665
commit f3d837bde6
11 changed files with 131 additions and 9 deletions

View 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);
}
})();

View File

@@ -3,13 +3,16 @@ import { SpotClient } from '../src/index';
// or // or
// import { SpotClient } from 'bitget-api'; // import { SpotClient } from 'bitget-api';
const client = new SpotClient(); const spotClient = new SpotClient();
const symbol = 'BTCUSDT_SPBL'; const symbol = 'BTCUSDT_SPBL';
(async () => { (async () => {
try { 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) { } catch (e) {
console.error('request failed: ', e); console.error('request failed: ', e);
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "bitget-api", "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.", "description": "Node.js connector for Bitget REST APIs and WebSockets, with TypeScript & end-to-end tests.",
"main": "lib/index.js", "main": "lib/index.js",
"types": "lib/index.d.ts", "types": "lib/index.d.ts",

View File

@@ -13,6 +13,7 @@ export const API_ERROR_CODE = {
QTY_LESS_THAN_MINIMUM: '43006', QTY_LESS_THAN_MINIMUM: '43006',
/** Parameter verification exception margin mode == FIXED */ /** Parameter verification exception margin mode == FIXED */
PARAMETER_EXCEPTION: '40808', PARAMETER_EXCEPTION: '40808',
PLAN_ORDER_REACHED_UPPER_LIMIT: '40889',
ORDER_NOT_FOUND: '43001', ORDER_NOT_FOUND: '43001',
FUTURES_ORDER_TPSL_NOT_FOUND: '43020', FUTURES_ORDER_TPSL_NOT_FOUND: '43020',
PLAN_ORDER_NOT_FOUND: '43025', PLAN_ORDER_NOT_FOUND: '43025',

View File

@@ -1,6 +1,5 @@
import { import {
APIResponse, APIResponse,
KlineInterval,
FuturesProductType, FuturesProductType,
FuturesAccountBillRequest, FuturesAccountBillRequest,
FuturesBusinessBillRequest, FuturesBusinessBillRequest,
@@ -24,6 +23,7 @@ import {
GetHistoricTradesParams, GetHistoricTradesParams,
FuturesMarketTrade, FuturesMarketTrade,
FuturesPlanType, FuturesPlanType,
FuturesKlineInterval,
} from './types'; } from './types';
import { REST_CLIENT_TYPE_ENUM } from './util'; import { REST_CLIENT_TYPE_ENUM } from './util';
import BaseRestClient from './util/BaseRestClient'; import BaseRestClient from './util/BaseRestClient';
@@ -97,7 +97,7 @@ export class FuturesClient extends BaseRestClient {
/** Get Candle Data */ /** Get Candle Data */
getCandles( getCandles(
symbol: string, symbol: string,
granularity: KlineInterval, granularity: FuturesKlineInterval,
startTime: string, startTime: string,
endTime: string, endTime: string,
limit?: string, limit?: string,

View File

@@ -4,7 +4,6 @@ import {
NewWalletTransfer, NewWalletTransfer,
Pagination, Pagination,
APIResponse, APIResponse,
KlineInterval,
CoinBalance, CoinBalance,
SymbolRules, SymbolRules,
NewSpotSubTransfer, NewSpotSubTransfer,
@@ -21,6 +20,7 @@ import {
SpotMarketTrade, SpotMarketTrade,
GetHistoricTradesParams, GetHistoricTradesParams,
VIPFeeRate, VIPFeeRate,
SpotKlineInterval,
} from './types'; } from './types';
import { REST_CLIENT_TYPE_ENUM } from './util'; import { REST_CLIENT_TYPE_ENUM } from './util';
import BaseRestClient from './util/BaseRestClient'; import BaseRestClient from './util/BaseRestClient';
@@ -108,7 +108,7 @@ export class SpotClient extends BaseRestClient {
/** Get Candle Data */ /** Get Candle Data */
getCandles( getCandles(
symbol: string, symbol: string,
period: KlineInterval, period: SpotKlineInterval,
pagination?: Pagination, pagination?: Pagination,
): Promise<APIResponse<any>> { ): Promise<APIResponse<any>> {
return this.get('/api/spot/v1/market/candles', { return this.get('/api/spot/v1/market/candles', {

View File

@@ -8,6 +8,28 @@ export type FuturesProductType =
| 'sdmcbl' | 'sdmcbl'
| 'scmcbl'; | '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 FuturesHoldSide = 'long' | 'short';
export type FuturesMarginMode = 'fixed' | 'crossed'; export type FuturesMarginMode = 'fixed' | 'crossed';

View File

@@ -2,6 +2,25 @@ import { OrderTimeInForce } from './shared';
export type WalletType = 'spot' | 'mix_usdt' | 'mix_usd'; 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 { export interface NewWalletTransfer {
fromType: WalletType; fromType: WalletType;
toType: WalletType; toType: WalletType;

View File

@@ -4,6 +4,9 @@ export type numberInString = string;
export type OrderSide = 'Buy' | 'Sell'; export type OrderSide = 'Buy' | 'Sell';
/**
* @deprecated use SpotKlineInterval or FuturesKlineInterval, depending on which API group you're using
*/
export type KlineInterval = export type KlineInterval =
| '1min' | '1min'
| '5min' | '5min'

View File

@@ -26,6 +26,47 @@ interface UnsignedRequest<T extends object | undefined = {}> {
type SignMethod = 'bitget'; 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 { export default abstract class BaseRestClient {
private options: RestClientOptions; private options: RestClientOptions;
private baseUrl: string; private baseUrl: string;

View File

@@ -274,8 +274,11 @@ describe('Private Spot REST API POST Endpoints', () => {
data: expect.any(String), data: expect.any(String),
}); });
} catch (e) { } catch (e) {
console.error('cancelPlanOrder(): ', e); // console.error('cancelPlanOrder(): ', e);
expect(e).toBeNull(); // expect(e).toBeNull();
expect(e.body).toMatchObject({
code: API_ERROR_CODE.PLAN_ORDER_NOT_FOUND,
});
} }
}); });
}); });