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
Example TradesUpdate message
{
"marketUpdate": {
"market": {
"exchangeId": "3",
"currencyPairId": "9",
"marketId": "74"
},
"tradesUpdate": {
"trades": [
{
"externalId": "97996223",
"timestamp": "1569879480",
"timestampNano": "1569879480000000000",
"priceStr": "8206.51",
"amountStr": "0.00728105",
"price": 8206.51, // DEPRECATED
"amount": 0.00728105 // DEPRECATED
}
]
}
}

SDK Example: stream trades from all markets

Node.js
Go
Node.js
import { StreamClient, RESTClient } from 'cw-sdk-node';
const rc = new RESTClient();
const streamClient = new StreamClient({
creds: {
// These can also be read from ~/.cw/credentials.yml
apiKey: '<your api key>',
secretKey: '<your secret key>'
},
subscriptions: [
// Subscription key for all trades from all markets
'markets:*:trades'
]
});
async function run() {
const markets = await rc.getMarkets();
const marketCache = {};
markets.forEach((market) => {
marketCache[market.id] = market; // Cache all market identifiers
});
// Listen for received trades and print them
streamClient.onMarketUpdate((marketData) => {
const tradesUpdate = marketData.tradesUpdate;
tradesUpdate.forEach((tradeUpdate) => {
console.log(
marketCache[marketData.market.id], // access market info from cache
tradeUpdate.side,
'Price: ',
tradeUpdate.price,
'Amount: ',
tradeUpdate.amount
);
});
});
// Connect to stream
streamClient.connect();
}
run().catch((e) => {
console.error(e);
});
Go
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"code.cryptowat.ch/cw-sdk-go/client/rest"
"code.cryptowat.ch/cw-sdk-go/client/websocket"
"code.cryptowat.ch/cw-sdk-go/common"
)
func main() {
restclient := rest.NewRESTClient(nil)
// Get market descriptions, to know symbols (like "btcusd") by integer ID.
marketsSlice, err := restclient.GetMarketsIndex()
if err != nil {
log.Fatalf("failed to get markets: %s", err)
}
markets := map[common.MarketID]rest.MarketDescr{}
for _, market := range marketsSlice {
markets[common.MarketID(market.ID)] = market
}
// Create a new stream connection instance. Note that the actual connection
// will happen later
c, err := websocket.NewStreamClient(&websocket.StreamClientParams{
WSParams: &websocket.WSParams{
// The following credentials can also be read from ~/.cw/credentials.yml
// APIKey: <your api key>,
// SecretKey: <your secret key>,
},
Subscriptions: []*websocket.StreamSubscription{
&websocket.StreamSubscription{
// Subscription key for all trades from all markets
Resource: "markets:*:trades",
},
},
})
if err != nil {
log.Fatal(err)
}
// Listen for received trades and print them
c.OnMarketUpdate(func(marketID common.MarketID, md common.MarketUpdate) {
if md.TradesUpdate == nil {
return
}
tradesUpdate := md.TradesUpdate
for _, trade := range tradesUpdate.Trades {
log.Printf(
"%s %s %-25s %-25s",
markets[marketID].Exchange,
trade.OrderSide,
fmt.Sprintf("Price: %s", trade.Price),
fmt.Sprintf("Amount: %s", trade.Amount),
)
}
})
// Connect to stream
if err := c.Connect(); err != nil {
log.Fatal(err)
}
// Wait until an OS signal is received, at which point we'll close the connection and quit
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
<-signals
log.Print("Closing connection...")
if err := c.Close(); err != nil {
log.Fatalf("Failed to close connection: %s", err)
}
}