Skip to content

Commit

Permalink
Add featured contents and ters + policy
Browse files Browse the repository at this point in the history
  • Loading branch information
mahasak committed Dec 18, 2023
1 parent f96eb6d commit 933fd0b
Show file tree
Hide file tree
Showing 4 changed files with 370 additions and 17 deletions.
15 changes: 0 additions & 15 deletions src/content/blog/adding-new-post.md

This file was deleted.

62 changes: 62 additions & 0 deletions src/content/blog/binance-websocket-ticker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
author: Max Pijittum
pubDatetime: 2023-12-17T20:00:00Z
title: Retrieving Binance price ticker with websocket using TypeScript
postSlug: reading-binance-price-ticker-with-wss-using-typescript
featured: true
draft: false
tags:
- trading
description: Retrieving price ticker from Binance using websocket
---

Recently, I want to collect Binance trading ticker to simulate a trade setup of a specific pair and was researched for how to do it programmatically and found Binance offered a WSS API to actully subscribe a price ticker.

This is a rough step-by-step i follow through Binance docs:

**Step #1** - Install Binance Typescript connector

```bash
npm install @binance/connector-typescript
```

**Step #2** - Exporting Binance API key to your shell session (or add it to environment variable).

```shell
export API_KEY=<your_api_key>
export API_SECRET=<you_api_secret>
```

**Step #3** - Define callaback for Binance websocket events and subscribe to ticker.

```typescript
import {
WebsocketAPI,
WsMarketTypes,
WebsocketStream,
} from "@binance/connector-typescript";

const callbacks = {
open: () => console.debug("Connected to WebSocket server"),
close: () => console.debug("Disconnected from WebSocket server"),
message: (data: string) => console.info(data),
};

const websocketStreamClient = new WebsocketStream({ callbacks });

websocketStreamClient.ticker("bnbbtc");

setTimeout(() => websocketStreamClient.pingServer(), 4000);

setTimeout(() => websocketStreamClient.disconnect(), 6000);
```

**Optional** - Reconnecting to Binance WSS

```typescript
setTimeout(() => {
websocketStreamClient.ticker("bnbbtc");
}, 9000);

setTimeout(() => websocketStreamClient.disconnect(), 12000);
```
Loading

0 comments on commit 933fd0b

Please sign in to comment.