CCXT 通用交易所 API

其為一套可在任何交易所進行交易的 API

https://github.com/ccxt/ccxt/wiki/Manual#market-orders

https://docs.ccxt.com/en/latest/manual.html

獲取初始化需要放入參數的 Credentials

從 requiredCredentials 可以知道在上方交易所內的 object 參數放入哪些欄位

const ccxt = require ('ccxt');

(async function () {
    const exchange = new ccxt.binance ({...})
    console.log(exchange.requiredCredentials)
  },
}) ();

獲取可呼叫方法

const ccxt = require ('ccxt');

(async function () {
    const exchange = new ccxt.binance ({...})
    console.log(exchange.has)
  },
}) ();

獲取所有可用交易對

const ccxt = require ('ccxt');

(async function () {
    const exchange = new ccxt.binance ({...})
    console.log(await exchange.loadMarkets ())
  },
}) ();

最小下單金額可以從 ['TOKEN/TOKEN']['limits']['cost']['min'] 獲取

const markets = await exchange.loadMarkets ();
console.log(markets["ETH/USDT"]?.limits?.cost?.min)

https://github.com/ccxt/ccxt/issues/1972#issuecomment-366834844

建立市價訂單

const ccxt = require("ccxt");
const FTX_API_KEY = process.env.ftx_api_key;
const FTX_API_SECRET = process.env.ftx_api_secret;
(async function() {
  let ftx = new ccxt.ftx({
    apiKey: FTX_API_KEY,
    secret: FTX_API_SECRET
  });
  const symbol = 'BTC/USD'
  const amount = 0.0001 // BTC
  const order = await ftx.createOrder(symbol, 'market', 'buy', amount);

  console.log (order)
})();

建立限價訂單

const ccxt = require ('ccxt');

const bitgetKey = "";
const bitgetSecret = "";

(async function () {
    const exchange = new ccxt.bitget ({
        apiKey: bitgetKey,
        secret: bitgetSecret,
        password: ""
    })
    const symbol = "ETH/USDT";
    const amount = 0.0065;
    const triggerPrice = 1000;
    const order = await exchange.createOrder(symbol, 'limit', 'buy', amount, triggerPrice);
    console.log('order', order);
}) ();

獲取交易所資金費率 Funding Rate

(async function () {
  const exchange = new ccxt.binance({
  });

  const fr = await exchange.fetchFundingRateHistory(
    "BTCUSD_PERP",
    Date.now() - 24 * 1000 * 60 * 60, // 24 小時內
    20
  );
  console.log(fr);
})();

獲取交易所 Open Interest (OI)

(async function () {
  const exchange = new ccxt.binance({
  });
  const oi = await exchange.fetchOpenInterestHistory(
    "BTC/USDT:USDT",
    "15m", // timeframe
    Date.now() - 1000 * 24 * 60 * 60, // since
    10 // limit
  );
  console.log(oi);
})();

取得歷史價格

推薦使用https://min-api.cryptocompare.com/documentation

如果要取得歷史資料可以用toTs,會返回其timestamp之前的limit參數筆資料,然後用batch方式去不斷抓資料。

e.g.

https://min-api.cryptocompare.com/data/v2/histominute?fsym=BTC&tsym=USD&limit=2000&toTs=1578217560

Last updated