Skip to content

Latest commit

 

History

History
861 lines (639 loc) · 20.6 KB

README.md

File metadata and controls

861 lines (639 loc) · 20.6 KB

Poloniex Node.js API CI Status npm version Coverage Status Known Vulnerabilities code style: prettier Contributor Covenant semantic-release Conventional Commits NPM license node version npm downloads GitHub top language

Node.js library for Poloniex.

Installation

npm install poloniex-node-api

Usage

See here

import { PublicClient } from "poloniex-node-api";
const client = new PublicClient();
const markets = await client.getMarkets();
const symbol = "ETH_BTC";
const volume = await client.getMarket({ symbol });
const currency = "BNB";
const currency_info = await client.getCurrency({ currency });

or

const includeMultiChainCurrencies = true;
const all = await client.getCurrency({ includeMultiChainCurrencies });
const time = await client.getSystemTime();
const prices = await client.getPrices();
const symbol = "ETH_BTC";
const price = await client.getPrice({ symbol });
const prices = await client.getMarkPrices();
const symbol = "ZEC_USDT";
const price = await client.getMarkPrice({ symbol });
const symbol = "ZEC_USDT";
const prices = await client.getMarkPriceComponents({ symbol });
const symbol = "ETH_BTC";
const limit = 5;
const scale = "0.01";
const book = await client.getOrderBook({ symbol, limit, scale });
const symbol = "ETH_BTC";
const interval = "HOUR_1";
const limit = 2;
const endTime = Date.now();
const startTime = endTime - 1000 * 60 * 60 * 24;
const candles = await client.getOrderBook({
  symbol,
  interval,
  limit,
  startTime,
  endTime,
});
const symbol = "ETH_BTC";
const limit = 2;
const trades = await client.getPublicTrades({ symbol, limit });
const tickers = await client.getTickers();
const symbol = "ETH_BTC";
const ticker = await client.getTicker({ symbol });
const currency = "ETH";
const collateral = await client.getCollateral({ currency });

or

const all = await client.getCollateral();
const symbol = "ETH_BTC";
const ticker = await client.getTicker({ symbol });
import { AuthenticatedClient } from "poloniex-node-api";
const key = "poloniex-api-key";
const secret = "poloniex-api-secret";
const client = new AuthenticatedClient({ key, secret });
const accounts = await client.getAccounts();
const balances = await client.getAccountBalances();
const activity = await client.getAccountActivity();
const currency = "USDT";
const amount = "10.5";
const fromAccount = "SPOT";
const toAccount = "FUTURES";
const { transferId } = await client.transfer({
  currency,
  amout,
  fromAccount,
  toAccount,
});
const transfers = await client.getAccountTransfers();
const fee_info = await client.getFeeInfo();
const wallets = await client.getWallets();
const { deposits, withdrawals } = await client.getWalletsActivity();
const { address } = await client.newAddress();
const { withdrawalRequestsId } = await client.withdraw();
const info = await client.getMargin();
const status = await client.getBorrowStatus();
const size = await client.getMaxSize();
const symbol = "BTC_USDT";
const type = "LIMIT";
const quantity = "100";
const side = "BUY";
const price = "40000.50000";
const timeInForce = "IOC";
const clientOrderId = "1234Abc";
const { id, clientOrderId } = await client.createOrder({
  symbol,
  type,
  quantity,
  side,
  price,
  timeInForce,
  clientOrderId,
});

or

const symbol = "BTC_USDT";
const quantity = "100";
const side = "BUY";
const { id } = await client.createOrder({ symbol, quantity, side });
const orders = [
  { symbol: "BTC_USDT", amount: "100", side: "BUY" },
  {
    symbol: "BTC_USDT",
    type: "LIMIT",
    quantity: "100",
    side: "BUY",
    price: "40000.50000",
    timeInForce: "IOC",
    clientOrderId: "1234Abc",
  },
  { symbol: "ETH_USDT", amount: "1000", side: "BUY" },
  {
    symbol: "TRX_USDT",
    type: "LIMIT",
    quantity: "15000",
    side: "SELL",
    price: "0.0623423423",
    timeInForce: "IOC",
    clientOrderId: "456Xyz",
  },
];
const response = await client.createOrders(orders);
const id = "234235233423";
const price = "18000";
const clientOrderId = "1234";
const response = await client.replaceOrder({ id }, { price, clientOrderId });

or

const clientOrderId = "1234Abc";
const price = "18000";
const quantity = "20";
const response = await client.replaceOrder(
  { clientOrderId },
  { price, quantity },
);
const symbol = "ELON_USDC";
const side = "SELL";
const direction = "PRE";
const limit = 10;
const orders = await client.getOpenOrders({ symbol, side, direction, limit });

or

const orders = await client.getOpenOrders();
const id = "21934611974062080";
const order = await client.getOrder({ id });

or by clientOrderId

const clientOrderId = "123";
const order = await client.getOrder({ clientOrderId });
const id = "21934611974062080";
const order = await client.cancelOrder({ id });

or by clientOrderId

const clientOrderId = "123";
const order = await client.cancelOrder({ clientOrderId });
const orders = [{ id: "12345" }, { clientOrderId: "myId-1" }];
const results = await client.cancelOrders(orders);
const symbols = ["BTC_USDT", "ETH_USDT"];
const accountTypes = ["SPOT"];
const results = await client.cancelAllOrders({ symbols, accountTypes });

or (to cancel all orders)

const results = await client.cancelAllOrders();
const timeout = 60;
const status = await client.killSwitch({ timeout });
const status = await client.getKillSwitch();
const symbol = "BTC_USDT";
const side = "BUY";
const type = "STOP_LIMIT";
const quantity = "100";
const price = "60100.00";
const timeInForce = "FOK";
const stopPrice = "60000.00";
const clientOrderId = "999999910";
const { id, clientOrderId } = await client.createSmartOrder({
  symbol,
  side,
  type,
  quantity,
  price,
  timeInForce,
  stopPrice,
  clientOrderId,
});
const id = "234235233423";
const stopPrice = "18000";
const clientOrderId = "1234Abc";
const response = await client.replaceOrder(
  { id },
  { stopPrice, clientOrderId },
);

or by clientOrderId

const clientOrderId = "1234Abc";
const price = "18000";
const quantity = "20";
const response = await client.replaceOrder(
  { clientOrderId },
  { stopPrice, quantity },
);
const limit = 10;
const orders = await client.getOpenSmartOrders({ limit });

or

const orders = await client.getOpenSmartOrders();
const id = "14368195657859072";
const order = await client.getSmartOrder({ id });

or by clientOrderId

const clientOrderId = "18113";
const order = await client.getSmartOrder({ clientOrderId });
const id = "9876543";
const order = await client.cancelSmartOrder({ id });

or by clientOrderId

const clientOrderId = "88888";
const order = await client.cancelSmartOrder({ clientOrderId });
const orders = [{ id: "12345" }, { clientOrderId: "myId-1" }];
const results = await client.cancelSmartOrders(orders);
const symbols = ["BTC_USDT", "ETH_USDT"];
const accountTypes = ["SPOT"];
const results = await client.cancelAllSmartOrders({ symbols, accountTypes });

or (to cancel all orders)

const results = await client.cancelAllSmartOrders();
const type = ["MARKET", "LIMIT"];
const side = "BUY";
const symbol = "TRX_USDC";
const states = ["FILLED", "PARTIALLY_CANCELED"];
const limit = 10;
const hideCancel = true;
const startTime = 1649106321040;
const endTime = 1649427963598;
const orders = await client.getOrders({
  type,
  side,
  symbol,
  states,
  limit,
  hideCancel,
  startTime,
  endTime,
});
const limit = 10;
const endTime = 1648635115535;
const startTime = endTime - 1000 * 60 * 60;
const direction = "PRE";
const symbols = ["BTC_USDT", "ETH_USDT"];
const trades = await client.getTrades({
  limit,
  startTime,
  endTime,
  direction,
  symbols,
});
const id = "30249408733945856";
const trades = await client.getOrderTrades({ id });
const key = "<POLONIEX API KEY>";
const secret = "<POLONIEX API SECRET>";
const client = new WebSocketClient({ key, secret })
  .on("message", (msg) => {
    console.log("Message:\t", msg);
  })
  .on("error", (error) => {
    console.log("Error:\t", error);
  });
await client.connectPublicWS();
await client.connectPrivateWS();
await client.disconnectPublicWS();
await client.disconnectPrivateWS();
await client.auth();
const ac = new AbortController();
setTimeout(() => {
  ac.abort();
}, 10000).unref();
await client.pingPublic({ signal: ac.signal });
await client.pingPrivate();
await client.unsubscribePublic();
await client.unsubscribePrivate();
const { subscriptions } = await client.getPublicSubscriptions();
const { subscriptions } = await client.getPrivateSubscriptions();
const payload = {
  event: "subscribe",
  channel: ["candles_minute_1", "ticker"],
  symbols: ["BTC_USDT", "ETH_USDT"],
};
await client.send(payload, "public");

or

const payload = {
  event: "subscribe",
  channel: ["orders", "balances"],
  symbols: ["all"],
};
await client.send(payload, "private");
  • subscribeCandles
await client.subscribeCandles();
  • unsubscribeCandles
await client.unsubscribeCandles();
  • candles
const channel = "candles_day_1";
for await (const candle of client.candles()) {
  console.log(candle);
}
  • subscribeTrades
const symbols = ["BTC_USDT", "ETH_USDT"];
await client.subscribeTrades({ symbols });
  • unsubscribeTrades
const symbols = ["BTC_USDT"];
await client.unsubscribeTrades({ symbols });
  • trades
for await (const trade of client.trades()) {
  console.log(trade);
}
  • subscribeTicker
const symbols = "all";
await client.subscribeTicker({ symbols });
  • unsubscribeTicker
const symbols = "all";
await client.unsubscribeTicker({ symbols });
  • tickers
for await (const ticker of client.tickers()) {
  console.log(ticker);
}
  • subscribeBook
await client.subscribeBook();
  • unsubscribeBook
const symbols = "all";
await client.unsubscribeBook({ symbols });
  • books
const depth = 10;
for await (const book of client.books({ depth })) {
  console.log(book);
}
  • subscribeLv2Book
const symbols = ["BTC_USDT"];
await client.subscribeLv2Book({ symbols });
  • unsubscribeLv2Book
const symbols = "all";
await client.unsubscribeLv2Book({ symbols });
  • booksLv2
for await (const book of client.booksLv2()) {
  console.log(book);
}
  • subscribeOrders
const symbols = "all";
await client.subscribeOrders({ symbols });
  • unsubscribeOrders
const symbols = "all";
await client.unsubscribeOrders({ symbols });
  • orders
for await (const order of client.orders()) {
  console.log(order);
}
  • subscribeBalances
await client.subscribeBalances();
  • unsubscribeBalances
await client.unsubscribeBalances();
  • balances
for await (const balance of client.balances()) {
  console.log(balance);
}

Test

npm test

Coverage

npm run coverage