forked from KillerInk/binance-java-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserDataStreamExample.java
More file actions
executable file
·56 lines (45 loc) · 2.44 KB
/
UserDataStreamExample.java
File metadata and controls
executable file
·56 lines (45 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.binance.api.examples;
import com.binance.api.client.api.BinanceApiWebSocketClient;
import com.binance.api.client.api.sync.BinanceApiSpotRestClient;
import com.binance.api.client.domain.event.AccountUpdateEvent;
import com.binance.api.client.domain.event.ExecutionReport;
import com.binance.api.client.factory.BinanceAbstractFactory;
import com.binance.api.client.factory.BinanceSpotApiClientFactory;
import static com.binance.api.client.domain.event.UserDataUpdateEventType.ACCOUNT_UPDATE;
/**
* User data stream endpoints examples.
* <p>
* It illustrates how to create a stream to obtain updates on a user account,
* as well as update on trades/orders on a user account.
*/
public class UserDataStreamExample {
public static void main(String[] args) {
BinanceSpotApiClientFactory factory = BinanceAbstractFactory.createSpotFactory("YOUR_API_KEY", "YOUR_SECRET");
BinanceApiSpotRestClient client = factory.newRestClient();
// First, we obtain a listenKey which is required to interact with the user data stream
String listenKey = client.startUserDataStream();
// Then, we open a new web socket client, and provide a callback that is called on every update
BinanceApiWebSocketClient webSocketClient = factory.newWebSocketClient();
// Listen for changes in the account
webSocketClient.onUserDataUpdateEvent(listenKey, response -> {
if (response.getEventType() == ACCOUNT_UPDATE) {
AccountUpdateEvent accountUpdateEvent = response.getAccountUpdateEvent();
// Print new balances of every available asset
System.out.println(accountUpdateEvent.getBalances());
} else {
ExecutionReport executionReport = response.getExecutionReport();
// Print details about an order/trade
System.out.println(executionReport);
// Print original quantity
System.out.println(executionReport.getOriginalQuantity());
// Or price
System.out.println(executionReport.getPrice());
}
});
System.out.println("Waiting for events...");
// We can keep alive the user data stream
// client.keepAliveUserDataStream(listenKey);
// Or we can invalidate it, whenever it is no longer needed
// client.closeUserDataStream(listenKey);
}
}