Created
August 4, 2020 17:05
-
-
Save tpai/93782398c7940218935d9ee95bcd6221 to your computer and use it in GitHub Desktop.
bfx ws
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
| const dayjs = require("dayjs"); | |
| module.exports = { | |
| watchFundingTrades: async (ws, { symbol }, cb) => { | |
| ws.onFundingTradeEntry({ symbol }, trade => { | |
| const params = getParams(trade); | |
| const { datetime, rate, apy, period, amount } = params; | |
| console.log( | |
| `${datetime}\t${rate.toFixed(6)}\t${apy.toFixed( | |
| 6 | |
| )}\t${period}d\t${amount}` | |
| ); | |
| cb(params); | |
| }); | |
| ws.onAccountTradeEntry({ symbol }, () => {}); | |
| await ws.subscribeTrades(symbol); | |
| } | |
| }; | |
| function getParams(trade) { | |
| const { symbol, mtsCreate, offerID, amount } = trade; | |
| return { | |
| datetime: dayjs(symbol).format("YYYY/MM/DD HH:mm:ss"), | |
| ts: symbol, | |
| rate: offerID * 100, | |
| apy: offerID * 100 * 360, | |
| period: amount, | |
| amount: mtsCreate | |
| }; | |
| } |
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
| require("dotenv").config(); | |
| const { WSv2 } = require("bitfinex-api-node"); | |
| const auth = require("./utils/auth"); | |
| (async function() { | |
| const ws = new WSv2({ | |
| transform: true, | |
| apiKey: process.env.API_KEY, | |
| apiSecret: process.env.API_SECRET | |
| }); | |
| await ws.open(); | |
| console.log("open"); | |
| await ws.auth(); | |
| console.log("authenticated"); | |
| })(); |
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
| module.exports = { | |
| watchTickers: async (ws, { symbol }, cb) => { | |
| ws.onTicker({ symbol }, ticker => { | |
| const params = getParams(ticker); | |
| const { frr, lastPrice } = params; | |
| console.log( | |
| `${lastPrice.toFixed(6)}\t${frr.toFixed(6)}\t${(frr * 360).toFixed(6)}` | |
| ); | |
| cb(params); | |
| }); | |
| await ws.subscribeTicker(symbol); | |
| } | |
| }; | |
| function getParams(ticker) { | |
| return { | |
| frr: ticker.frr * 100, | |
| lastPrice: ticker.lastPrice * 100 | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment