Skip to content

Instantly share code, notes, and snippets.

@tpai
Created August 4, 2020 17:05
Show Gist options
  • Select an option

  • Save tpai/93782398c7940218935d9ee95bcd6221 to your computer and use it in GitHub Desktop.

Select an option

Save tpai/93782398c7940218935d9ee95bcd6221 to your computer and use it in GitHub Desktop.
bfx ws
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
};
}
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");
})();
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