-
Notifications
You must be signed in to change notification settings - Fork 12
Adding combined streams, parseFloats #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gemmell
wants to merge
28
commits into
luzzif:master
Choose a base branch
from
gemmell:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
1aef48e
Added the code necessary to use a single ws to deliver many updates.
gemmell 82387c2
Removed code I accidentally merged in.
gemmell 7910741
Fix filter detection and mapping
10d17df
Update documentation
89fb21c
Version 2.5.0
e32dc21
Merge branch 'combined-ws-streams'
gemmell 0865ede
Fixed price filter
gemmell 07f4ba3
merged
gemmell d8a7af6
Added raw trade stream
gemmell 96d9531
Added export.
gemmell fcfd3c7
Needs to be RawTradeUpdate
gemmell 9ef4ef1
Added missing field and fixed the float/string thing.
gemmell 2b67fe1
Added missing getters
gemmell d05a4be
Update to my own version of websocket-heartbeats
gemmell 42dad3a
So much easier.
gemmell 29b28cd
Need to parse floats
gemmell 8d90f75
Added the parseFloat to the relevant structures.
gemmell 431361f
Go through and parseFloat as needed.
gemmell 16da175
OrderResult price should be a number.
gemmell 383025f
In fact quantities should be numbers too.
gemmell 126f094
Merged from master
gemmell 80e1c89
Binance expects a string for the price
gemmell 58ec04c
Added print out
gemmell 78c553f
Added print out
gemmell 2c3e79b
Added print out
gemmell eb3e0d7
Use the string version so we don't end up sending 1.1e-8
gemmell 91ff5e0
Don't toFixed on the stopprice cuz I don't use it
gemmell 27a71cf
Remove debug statement that allowed me to track down errors
gemmell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ import * as WebSocket from "ws"; | |
| import { OrderBookUpdate } from "./models/websocket/depth/OrderBookUpdate"; | ||
| import { CandlestickUpdate } from "./models/websocket/candlestick/CandlestickUpdate"; | ||
| import { TradeUpdate } from "./models/websocket/trade/TradeUpdate"; | ||
| import { RawTradeUpdate } from "./models/websocket/trade/RawTradeUpdate"; | ||
| import { AccountUpdate } from "./models/websocket/account/AccountUpdate"; | ||
| import { OrderUpdate } from "./models/websocket/order/OrderUpdate"; | ||
| import { ExchangeInfo } from "./models/misc/ExchangeInfo"; | ||
|
|
@@ -39,6 +40,7 @@ import { HeartbeatHandler } from "websocket-heartbeats"; | |
| */ | ||
| export class BinanceApiClient { | ||
|
|
||
| private static readonly COMBINED_WS_BASE_URL: string = "wss://stream.binance.com:9443/stream?streams="; | ||
| private static readonly WS_BASE_URL: string = "wss://stream.binance.com:9443/ws/"; | ||
| private static readonly DEFAULT_WS_TIMEOUT: number = 60000; | ||
|
|
||
|
|
@@ -257,6 +259,9 @@ export class BinanceApiClient { | |
| icebergQuantity?: number, | ||
| responseType?: ResponseType ): Promise< OrderAcknowledgement | OrderResult | OrderFull > { | ||
|
|
||
|
|
||
| const priceAsString: string = price.toFixed(8); | ||
| console.log("BinanceApiClient says the price is" + priceAsString); | ||
| let jsonResponse: any = await this.makeRequest( | ||
| HttpMethod.POST, | ||
| ApiVersion.V3, | ||
|
|
@@ -272,7 +277,7 @@ export class BinanceApiClient { | |
| [ "quantity", quantity ], | ||
| [ | ||
| "price", | ||
| type === OrderType.MARKET || type === OrderType.STOP_LOSS ? null : price | ||
| (type === OrderType.MARKET || type === OrderType.STOP_LOSS) ? null : priceAsString | ||
| ], | ||
| [ "newClientOrderId", clientOrderId ], | ||
| [ "stopPrice", stopPrice ], | ||
|
|
@@ -587,6 +592,49 @@ export class BinanceApiClient { | |
|
|
||
| } | ||
|
|
||
| /** | ||
| * Initializes a web socket data stream that gives us information about a combined | ||
| * stream of an array of symbol's order book updates. | ||
| * | ||
| * @param symbols The symbols of which we want to get the order book updates. | ||
| * @param onUpdate A function to be called when a new update is received. | ||
| * @param connectionTimeout Timeout based on which the web socket connection is | ||
| * considered to be broken based on a heartbeat monitor. | ||
| * @param onLostConnection A callback to be invoked when the web socket connection | ||
| * is detected as broken. | ||
| */ | ||
| public monitorOrderBookCombined( | ||
| symbol: string[], | ||
| onUpdate: ( update: OrderBookUpdate ) => any, | ||
| connectionTimeout: number, | ||
| onLostConnection: () => any ): void { | ||
|
|
||
| let url: string = ""; | ||
| url = BinanceApiClient.COMBINED_WS_BASE_URL; | ||
| for ( let s of symbol ) { | ||
| url += s.toLowerCase() + "@depth" + "/"; | ||
| } | ||
| // Trim the final slash | ||
| url.slice( 0, -1 ); | ||
| const websocket: WebSocket = new WebSocket( | ||
| url, | ||
| { perMessageDeflate: false } | ||
| ); | ||
|
|
||
| new HeartbeatHandler( | ||
| websocket, | ||
| isNullOrUndefined( connectionTimeout ) ? BinanceApiClient.DEFAULT_WS_TIMEOUT : connectionTimeout, | ||
| onLostConnection | ||
| ).handle(); | ||
|
|
||
| websocket.on( "message", ( data: any ) => { | ||
| // For a combined stream the data is wrapped in an object with the | ||
| // streamname and the raw data. | ||
| const rawData = JSON.parse( data ); | ||
| onUpdate( new OrderBookUpdate( rawData.data ) ); | ||
| } ); | ||
|
|
||
| } | ||
| /** | ||
| * Initializes a web socket data stream that gives us information about a | ||
| * single symbol's order book updates. Stream keepalive is performed through | ||
|
|
@@ -625,6 +673,50 @@ export class BinanceApiClient { | |
|
|
||
| /** | ||
| * Initializes a web socket data stream that gives us information about | ||
| * Kline/candlestick updates for a number of symbols on the one socket. | ||
| * | ||
| * @param symbols The symbols of which we want to get the candlestick updates. | ||
| * @param interval The interval to which the requested candlestick updates | ||
| * refer to. | ||
| * @param onUpdate A function to be called when a new update is received. | ||
| * @param connectionTimeout Timeout based on which the web socket connection is | ||
| * considered to be broken based on a heartbeat monitor. | ||
| * @param onLostConnection A callback to be invoked when the web socket connection | ||
| * is detected as broken. | ||
| */ | ||
| public async monitorCandlesticksCombined( | ||
| symbols: string[], | ||
| interval: CandlestickInterval, | ||
| onUpdate: ( update: CandlestickUpdate ) => any, | ||
| connectionTimeout?: number, | ||
| onLostConnection?: () => any ): Promise< void > { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd remove the spaces in the diamond notation here. And it's my bad, I should add a linter to the project. |
||
|
|
||
| let url: string = ""; | ||
| url = BinanceApiClient.COMBINED_WS_BASE_URL; | ||
| for ( let s of symbols ) { | ||
| url += s.toLowerCase() + "@kline_" + interval + "/"; | ||
| } | ||
| // Trim the final slash | ||
| url.slice( 0, -1 ); | ||
| const websocket: WebSocket = new WebSocket( | ||
| url, | ||
| { perMessageDeflate: false } | ||
| ); | ||
|
|
||
| new HeartbeatHandler( | ||
| websocket, | ||
| isNullOrUndefined( connectionTimeout ) ? BinanceApiClient.DEFAULT_WS_TIMEOUT : connectionTimeout, | ||
| onLostConnection | ||
| ).handle(); | ||
|
|
||
| websocket.on( "message", ( data: any ) => { | ||
| const rawData = JSON.parse( data ); | ||
| onUpdate( new CandlestickUpdate( rawData.data ) ); | ||
| } ); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Kline/candlestick updates. Stream keepalive is performed through | ||
| * [[keepAliveUserStream]] following the rules described | ||
| * [here](https://github.com/binance-exchange/binance-official-api-docs/blob/master/web-socket-streams.md) | ||
|
|
@@ -662,6 +754,89 @@ export class BinanceApiClient { | |
|
|
||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Initializes a web socket data stream that gives us information about | ||
| * trade updates for a number of symbols on the one socket. | ||
| * | ||
| * @param symbols The symbols of which we want to get the trade updates. | ||
| * @param onUpdate A function to be called when a new update is received. | ||
| * @param connectionTimeout Timeout based on which the web socket connection is | ||
| * considered to be broken based on a heartbeat monitor. | ||
| * @param onLostConnection A callback to be invoked when the web socket connection | ||
| * is detected as broken. | ||
| */ | ||
| public monitorRawTradesCombined( | ||
| symbols: string[], | ||
| onUpdate: ( update: RawTradeUpdate ) => any, | ||
| connectionTimeout: number, | ||
| onLostConnection: () => any ): void { | ||
| let url: string = ""; | ||
| url = BinanceApiClient.COMBINED_WS_BASE_URL; | ||
| for ( let s of symbols ) { | ||
| url += s.toLowerCase() + "@trade" + "/"; | ||
| } | ||
| // Trim the final slash | ||
| url.slice( 0, -1 ); | ||
| const websocket: WebSocket = new WebSocket( | ||
| url, | ||
| { perMessageDeflate: false } | ||
| ); | ||
|
|
||
| new HeartbeatHandler( | ||
| websocket, | ||
| isNullOrUndefined( connectionTimeout ) ? BinanceApiClient.DEFAULT_WS_TIMEOUT : connectionTimeout, | ||
| onLostConnection | ||
| ).handle(); | ||
|
|
||
| websocket.on( "message", ( data: any ) => { | ||
| const rawData = JSON.parse( data ); | ||
| onUpdate( new RawTradeUpdate( rawData.data ) ); | ||
| } ); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Initializes a web socket data stream that gives us information about | ||
| * trade updates for a number of symbols on the one socket. | ||
| * | ||
| * @param symbols The symbols of which we want to get the trade updates. | ||
| * @param onUpdate A function to be called when a new update is received. | ||
| * @param connectionTimeout Timeout based on which the web socket connection is | ||
| * considered to be broken based on a heartbeat monitor. | ||
| * @param onLostConnection A callback to be invoked when the web socket connection | ||
| * is detected as broken. | ||
| */ | ||
| public monitorTradesCombined( | ||
| symbols: string[], | ||
| onUpdate: ( update: TradeUpdate ) => any, | ||
| connectionTimeout: number, | ||
| onLostConnection: () => any ): void { | ||
| let url: string = ""; | ||
| url = BinanceApiClient.COMBINED_WS_BASE_URL; | ||
| for ( let s of symbols ) { | ||
| url += s.toLowerCase() + "@aggTrade" + "/"; | ||
| } | ||
| // Trim the final slash | ||
| url.slice( 0, -1 ); | ||
| const websocket: WebSocket = new WebSocket( | ||
| url, | ||
| { perMessageDeflate: false } | ||
| ); | ||
|
|
||
| new HeartbeatHandler( | ||
| websocket, | ||
| isNullOrUndefined( connectionTimeout ) ? BinanceApiClient.DEFAULT_WS_TIMEOUT : connectionTimeout, | ||
| onLostConnection | ||
| ).handle(); | ||
|
|
||
| websocket.on( "message", ( data: any ) => { | ||
| const rawData = JSON.parse( data ); | ||
| onUpdate( new TradeUpdate( rawData.data ) ); | ||
| } ); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Initializes a web socket data stream that gives us information about | ||
| * trade updates. Stream keepalive is performed through | ||
|
|
@@ -790,7 +965,7 @@ export class BinanceApiClient { | |
| this.setupAuthentication( httpMethod, apiUrl, requiredAuthentication ); | ||
|
|
||
| try { | ||
|
|
||
| return await request( { | ||
|
|
||
| method: HttpMethod[ httpMethod ], | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd remove this
console.log