Trades
A TradesUpdate is sent every time there are new trades on a market. There may be multiple trades inside tradesUpdate.trades.

Example Trades Resources

Example Subscription
Description
markets:68:trades
A feed of trades for a single market
markets:*:trades
A feed of trades for all markets (high volume)
exchanges:4:trades
A feed of all trades for a given exchange
instruments:232:trades
A feed of all trades for a given instrument (like BTC/EUR)
Example TradesUpdate message
1
{
2
"marketUpdate": {
3
"market": {
4
"exchangeId": "3",
5
"currencyPairId": "9",
6
"marketId": "74"
7
},
8
"tradesUpdate": {
9
"trades": [
10
{
11
"externalId": "97996223",
12
"timestamp": "1569879480",
13
"timestampNano": "1569879480000000000",
14
"priceStr": "8206.51",
15
"amountStr": "0.00728105",
16
"price": 8206.51, // DEPRECATED
17
"amount": 0.00728105 // DEPRECATED
18
}
19
]
20
}
21
}
22
Copied!
SDK Example: stream trades from all markets
Node.js
Go
1
import { StreamClient, RESTClient } from 'cw-sdk-node';
2
const rc = new RESTClient();
3
const streamClient = new StreamClient({
4
creds: {
5
// These can also be read from ~/.cw/credentials.yml
6
apiKey: '<your api key>',
7
secretKey: '<your secret key>'
8
},
9
subscriptions: [
10
// Subscription key for all trades from all markets
11
'markets:*:trades'
12
]
13
});
14
15
async function run() {
16
const markets = await rc.getMarkets();
17
const marketCache = {};
18
markets.forEach((market) => {
19
marketCache[market.id] = market; // Cache all market identifiers
20
});
21
22
// Listen for received trades and print them
23
streamClient.onMarketUpdate((marketData) => {
24
const tradesUpdate = marketData.tradesUpdate;
25
tradesUpdate.forEach((tradeUpdate) => {
26
console.log(
27
marketCache[marketData.market.id], // access market info from cache
28
tradeUpdate.side,
29
'Price: ',
30
tradeUpdate.price,
31
'Amount: ',
32
tradeUpdate.amount
33
);
34
});
35
});
36
37
// Connect to stream
38
streamClient.connect();
39
}
40
41
run().catch((e) => {
42
console.error(e);
43
});
Copied!
1
package main
2
3
import (
4
"fmt"
5
"log"
6
"os"
7
"os/signal"
8
"syscall"
9
10
"code.cryptowat.ch/cw-sdk-go/client/rest"
11
"code.cryptowat.ch/cw-sdk-go/client/websocket"
12
"code.cryptowat.ch/cw-sdk-go/common"
13
)
14
15
func main() {
16
restclient := rest.NewRESTClient(nil)
17
18
// Get market descriptions, to know symbols (like "btcusd") by integer ID.
19
marketsSlice, err := restclient.GetMarketsIndex()
20
if err != nil {
21
log.Fatalf("failed to get markets: %s", err)
22
}
23
24
markets := map[common.MarketID]rest.MarketDescr{}
25
for _, market := range marketsSlice {
26
markets[common.MarketID(market.ID)] = market
27
}
28
29
// Create a new stream connection instance. Note that the actual connection
30
// will happen later
31
c, err := websocket.NewStreamClient(&websocket.StreamClientParams{
32
WSParams: &websocket.WSParams{
33
// The following credentials can also be read from ~/.cw/credentials.yml
34
// APIKey: <your api key>,
35
// SecretKey: <your secret key>,
36
},
37
38
Subscriptions: []*websocket.StreamSubscription{
39
&websocket.StreamSubscription{
40
// Subscription key for all trades from all markets
41
Resource: "markets:*:trades",
42
},
43
},
44
})
45
if err != nil {
46
log.Fatal(err)
47
}
48
49
// Listen for received trades and print them
50
c.OnMarketUpdate(func(marketID common.MarketID, md common.MarketUpdate) {
51
if md.TradesUpdate == nil {
52
return
53
}
54
55
tradesUpdate := md.TradesUpdate
56
for _, trade := range tradesUpdate.Trades {
57
log.Printf(
58
"%s %s %-25s %-25s",
59
markets[marketID].Exchange,
60
trade.OrderSide,
61
fmt.Sprintf("Price: %s", trade.Price),
62
fmt.Sprintf("Amount: %s", trade.Amount),
63
)
64
}
65
})
66
67
// Connect to stream
68
if err := c.Connect(); err != nil {
69
log.Fatal(err)
70
}
71
72
// Wait until an OS signal is received, at which point we'll close the connection and quit
73
signals := make(chan os.Signal, 1)
74
signal.Notify(signals, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
75
<-signals
76
77
log.Print("Closing connection...")
78
79
if err := c.Close(); err != nil {
80
log.Fatalf("Failed to close connection: %s", err)
81
}
82
}
Copied!
Last modified 1yr ago
Copy link