Skip to content

Commit 2ff8301

Browse files
authored
Merge pull request #931 from ccxt/candles-type-fixes
fix(client): candles return type
2 parents c7d8a53 + e53da9d commit 2ff8301

File tree

4 files changed

+129
-21
lines changed

4 files changed

+129
-21
lines changed

src/node-binance-api.ts

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3718,6 +3718,25 @@ export default class Binance {
37183718
return await this.publicSpotRequest('v3/ping', {});
37193719
}
37203720

3721+
parseAggTrades(symbol: string, trades: any[]): AggregatedTrade[] {
3722+
const parsedTrades: AggregatedTrade[] = [];
3723+
for (const trade of trades) {
3724+
const aggT: AggregatedTrade = {
3725+
aggId: trade.a,
3726+
symbol: symbol,
3727+
price: trade.p,
3728+
quantity: trade.q,
3729+
firstId: trade.f,
3730+
lastId: trade.l,
3731+
timestamp: trade.T,
3732+
isBuyerMaker: trade.m,
3733+
};
3734+
if (trade.M) aggT.wasBestPrice = trade.M;
3735+
parsedTrades.push(aggT);
3736+
}
3737+
return parsedTrades;
3738+
}
3739+
37213740
/**
37223741
* Get agg trades for given symbol
37233742
* @see https://developers.binance.com/docs/binance-spot-api-docs/rest-api/market-data-endpoints#compressedaggregate-trades-list
@@ -3727,7 +3746,8 @@ export default class Binance {
37273746
*/
37283747
async aggTrades(symbol: string, params: Dict = {}): Promise<AggregatedTrade[]> { //fromId startTime endTime limit
37293748
const parameters = Object.assign({ symbol }, params);
3730-
return await this.publicSpotRequest('v3/aggTrades', parameters);
3749+
const res = await this.publicSpotRequest('v3/aggTrades', parameters);
3750+
return this.parseAggTrades(symbol, res);
37313751
}
37323752

37333753
/**
@@ -3809,7 +3829,8 @@ export default class Binance {
38093829
async candlesticks(symbol: string, interval: Interval = '5m', params: Dict = {}): Promise<Candle[]> {
38103830
if (!params.limit) params.limit = 500;
38113831
params = Object.assign({ symbol: symbol, interval: interval }, params);
3812-
return await this.publicSpotRequest('v3/klines', params);
3832+
const res = await this.publicSpotRequest('v3/klines', params);
3833+
return this.parseCandles(res);
38133834
}
38143835

38153836
/**
@@ -3825,6 +3846,42 @@ export default class Binance {
38253846
return await this.candlesticks(symbol, interval, params); // make name consistent with futures
38263847
}
38273848

3849+
parseCandles(candles: any[]): Candle[] {
3850+
const res: Candle[] = [];
3851+
// spot
3852+
// [
3853+
// [
3854+
// 1499040000000, // Open time
3855+
// "0.01634790", // Open
3856+
// "0.80000000", // High
3857+
// "0.01575800", // Low
3858+
// "0.01577100", // Close
3859+
// "148976.11427815", // Volume
3860+
// 1499644799999, // Close time
3861+
// "2434.19055334", // Quote asset volume
3862+
// 308, // Number of trades
3863+
// "1756.87402397", // Taker buy base asset volume
3864+
// "28.46694368", // Taker buy quote asset volume
3865+
// "17928899.62484339" // Ignore.
3866+
// ]
3867+
// ]
3868+
for (const rawCandle of candles) {
3869+
const candle: Candle = {
3870+
openTime: rawCandle[0],
3871+
open: rawCandle[1],
3872+
high: rawCandle[2],
3873+
low: rawCandle[3],
3874+
close: rawCandle[4],
3875+
volume: rawCandle[5],
3876+
closeTime: rawCandle[6],
3877+
quoteAssetVolume: rawCandle[7],
3878+
trades: rawCandle[8],
3879+
};
3880+
res.push(candle);
3881+
}
3882+
return res;
3883+
}
3884+
38283885
// /**
38293886
// * Queries the public api
38303887
// * @param {string} url - the public api endpoint
@@ -3958,16 +4015,15 @@ export default class Binance {
39584015
async futuresCandles(symbol: string, interval: Interval = "30m", params: Dict = {}): Promise<Candle[]> {
39594016
params.symbol = symbol;
39604017
params.interval = interval;
3961-
return await this.publicFuturesRequest('v1/klines', params);
4018+
const res = await this.publicFuturesRequest('v1/klines', params);
4019+
return this.parseCandles(res);
39624020
}
39634021

39644022
/**
39654023
* @see https://developers.binance.com/docs/derivatives/usds-margined-futures/market-data/rest-api/Kline-Candlestick-Data
39664024
*/
39674025
async futuresCandlesticks(symbol: string, interval: Interval = "30m", params: Dict = {}): Promise<Candle[]> {
3968-
params.symbol = symbol;
3969-
params.interval = interval;
3970-
return await this.publicFuturesRequest('v1/klines', params);
4026+
return await this.futuresCandles(symbol, interval, params); // make name consistent with spot
39714027
}
39724028

39734029
/**
@@ -4480,10 +4536,18 @@ export default class Binance {
44804536
return res;
44814537
}
44824538

4539+
/**
4540+
* @see https://developers.binance.com/docs/derivatives/coin-margined-futures/market-data/rest-api/Kline-Candlestick-Data
4541+
* @param symbol
4542+
* @param interval
4543+
* @param params
4544+
* @returns
4545+
*/
44834546
async deliveryCandles(symbol: string, interval: Interval = "30m", params: Dict = {}): Promise<Candle[]> {
44844547
params.symbol = symbol;
44854548
params.interval = interval;
4486-
return await this.publicDeliveryRequest('v1/klines', params);
4549+
const res = await this.publicDeliveryRequest('v1/klines', params);
4550+
return this.parseCandles(res);
44874551
}
44884552

44894553
async deliveryContinuousKlines(pair: string, contractType = "CURRENT_QUARTER", interval: Interval = "30m", params: Dict = {}) {
@@ -5739,10 +5803,11 @@ export default class Binance {
57395803
}
57405804
};
57415805

5742-
const getSymbolDepthSnapshot = async (symbol: string, cb: Function) => {
5806+
const getSymbolDepthSnapshot = async (symbol: string) => {
57435807
const json = await this.publicSpotRequest('v3/depth', { symbol: symbol, limit: limit });
57445808
json.symbol = symbol;
5745-
cb(null, json);
5809+
// cb(null, json);
5810+
return json;
57465811
};
57475812

57485813
const updateSymbolDepthCache = json => {
@@ -5778,12 +5843,13 @@ export default class Binance {
57785843
const streams = symbols.map(function (symbol) {
57795844
return symbol.toLowerCase() + `@depth@100ms`;
57805845
});
5846+
const mapLimit = this.mapLimit.bind(this);
57815847
subscription = this.subscribeCombined(streams, handleDepthStreamData, reconnect, function () {
57825848
// async.mapLimit(symbols, 50, getSymbolDepthSnapshot, (err, results) => {
57835849
// if (err) throw err;
57845850
// results.forEach(updateSymbolDepthCache);
57855851
// });
5786-
this.mapLimit(symbols, 50, getSymbolDepthSnapshot)
5852+
mapLimit(symbols, 50, getSymbolDepthSnapshot)
57875853
.then(results => {
57885854
results.forEach(updateSymbolDepthCache);
57895855
})
@@ -5795,12 +5861,13 @@ export default class Binance {
57955861
} else {
57965862
const symbol = symbols;
57975863
symbolDepthInit(symbol);
5864+
const mapLimit = this.mapLimit.bind(this);
57985865
subscription = this.subscribe(symbol.toLowerCase() + `@depth@100ms`, handleDepthStreamData, reconnect, function () {
57995866
// async.mapLimit([symbol], 1, getSymbolDepthSnapshot, (err, results) => {
58005867
// if (err) throw err;
58015868
// results.forEach(updateSymbolDepthCache);
58025869
// });
5803-
this.mapLimit([symbol], 1, getSymbolDepthSnapshot)
5870+
mapLimit([symbol], 1, getSymbolDepthSnapshot)
58045871
.then(results => {
58055872
results.forEach(updateSymbolDepthCache);
58065873
})

src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ export interface Candle {
5252
close: string
5353
volume: string
5454
closeTime: number
55-
quoteVolume: string
55+
quoteVolume?: string
5656
trades: number
57-
baseAssetVolume: string
57+
baseAssetVolume?: string
5858
quoteAssetVolume: string
5959
}
6060

@@ -258,7 +258,7 @@ export interface AggregatedTrade {
258258
lastId: number
259259
timestamp: number
260260
isBuyerMaker: boolean
261-
wasBestPrice: boolean
261+
wasBestPrice?: boolean
262262
}
263263

264264
export interface Trade {

tests/binance-class-static.test.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,56 @@ describe( 'Static tests', async function () {
6565
})
6666

6767
it( 'OHLCVS', async function ( ) {
68-
await binance.candlesticks( 'BTCUSDT' )
68+
try {
69+
await binance.candlesticks( 'BTCUSDT' )
70+
} catch (e) {
71+
// console.log(e)
72+
}
6973
assert.equal( interceptedUrl, 'https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=5m&limit=500' )
7074

7175
})
7276

7377
it( 'Futures OHLCVS', async function ( ) {
74-
await binance.futuresCandles( 'BTCUSDT' )
78+
try {
79+
await binance.futuresCandles( 'BTCUSDT' )
80+
} catch (e) {
81+
// console.log(e)
82+
}
7583
assert.equal( interceptedUrl, 'https://fapi.binance.com/fapi/v1/klines?symbol=BTCUSDT&interval=30m' )
7684

7785
})
7886

79-
it( 'Trades', async function ( ) {
80-
await binance.aggTrades( 'BTCUSDT' )
87+
it( 'Recent Trades', async function ( ) {
88+
try {
89+
await binance.recentTrades( 'BTCUSDT' )
90+
} catch (e) {
91+
// console.log(e)
92+
}
93+
assert.equal( interceptedUrl, 'https://api.binance.com/api/v3/trades?symbol=BTCUSDT&limit=500' )
94+
})
95+
96+
it( 'Agg Trades', async function ( ) {
97+
try {
98+
await binance.aggTrades( 'BTCUSDT' )
99+
} catch (e) {
100+
// console.log(e)
101+
}
81102
assert.equal( interceptedUrl, 'https://api.binance.com/api/v3/aggTrades?symbol=BTCUSDT' )
82103

83104
})
84105

85106
it( 'FuturesTrades', async function ( ) {
86107
await binance.futuresTrades( 'BTCUSDT' )
87108
assert.equal( interceptedUrl, 'https://fapi.binance.com/fapi/v1/trades?symbol=BTCUSDT' )
109+
})
88110

111+
it( 'FuturesAggTrades', async function ( ) {
112+
try {
113+
await binance.futuresAggTrades( 'BTCUSDT' )
114+
} catch (e) {
115+
// console.log(e)
116+
}
117+
assert.equal( interceptedUrl, 'https://fapi.binance.com/fapi/v1/aggTrades?symbol=BTCUSDT' )
89118
})
90119

91120
it( 'PositionRisk V3', async function ( ) {

tests/static-tests.mjs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,31 @@ describe( 'Static tests', async function () {
7575
})
7676

7777
it( 'OHLCVS', async function ( ) {
78-
await binance.candlesticks( 'BTCUSDT' )
78+
try {
79+
await binance.candlesticks( 'BTCUSDT' )
80+
} catch (e) {
81+
// console.log(e)
82+
}
7983
assert.equal( interceptedUrl, 'https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=5m&limit=500' )
8084

8185
})
8286

8387
it( 'Futures OHLCVS', async function ( ) {
84-
await binance.futuresCandles( 'BTCUSDT' )
88+
try {
89+
await binance.futuresCandles( 'BTCUSDT' )
90+
} catch (e) {
91+
// console.log(e)
92+
}
8593
assert.equal( interceptedUrl, 'https://fapi.binance.com/fapi/v1/klines?symbol=BTCUSDT&interval=30m' )
8694

8795
})
8896

8997
it( 'Trades', async function ( ) {
90-
await binance.aggTrades( 'BTCUSDT' )
98+
try {
99+
await binance.aggTrades( 'BTCUSDT' )
100+
} catch (e) {
101+
// console.log(e)
102+
}
91103
assert.equal( interceptedUrl, 'https://api.binance.com/api/v3/aggTrades?symbol=BTCUSDT' )
92104

93105
})

0 commit comments

Comments
 (0)