@@ -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 } )
0 commit comments