BlockChain區塊鏈
  • 本書簡介
  • 區塊鏈運作原理
  • Bitcoin介紹
    • 簡介
    • Bitcoin其他知識
  • Bitcoin原理與實作
  • BitcoinJS
    • BTC 地址格式種類
    • 從 Mnemonic 轉為地址
  • Serverless 架構實作
  • Docker常用指令
  • ethereum初探
    • ethereum歷史
    • EVM
  • ethereum PoS 節點架設
  • ethereum(Docker)
  • ethereum(Geth)
    • Parity
  • ethereum(智能合約)
    • 合約測試 Unit Test
    • DAO
    • 可升級合約
    • 使用合約進行 multiswap
    • 合約安全
    • 開發工具
    • Hardhat 教學
      • Hardhat 寫測試
    • ERC-721 範例
      • 白名單機制
    • OpenZeppelin 合約 library
    • Truffle
    • 合約部屬
    • solidity 教學
  • ethereum(Dapp)
    • 相關 SDK
    • Multicall
    • Ethers.js 使用
    • Remix IDE
    • web3.js 使用
    • 在網頁上使用 web3 並操作區塊鏈
      • solidity筆記
  • Hyperledger Fabric
  • blockchainDB
  • 挖礦程式使用教學
    • 門羅幣/Monero (XMR)
  • Bitfinex API 使用
  • FTX API
  • CCXT 通用交易所 API
  • Solana 教學
  • Ethereum BigQuery
  • The Graph
    • yaml 定義
    • mapping 語法
    • Schema 定義
    • Query 範例
    • Unit test
  • DeFi 筆記
    • MEV 相關
    • Dex 聚合
    • Yearn
    • Curve
    • Uniswap
      • Swap 互動
    • AAVE、Compound
      • Compound 原理
      • AAVE 合約開發
Powered by GitBook
On this page
  • 獲取初始化需要放入參數的 Credentials
  • 獲取可呼叫方法
  • 獲取所有可用交易對
  • 建立市價訂單
  • 建立限價訂單
  • 獲取交易所資金費率 Funding Rate
  • 獲取交易所 Open Interest (OI)
  • 取得歷史價格

Was this helpful?

CCXT 通用交易所 API

PreviousFTX APINextSolana 教學

Last updated 2 years ago

Was this helpful?

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

獲取初始化需要放入參數的 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)

建立市價訂單

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);
})();

取得歷史價格

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

e.g.

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

推薦使用

然後將TimeFrom 在放入下一筆的toTs繼續往更早之前的資料抓取

https://github.com/ccxt/ccxt/issues/1972#issuecomment-366834844
https://min-api.cryptocompare.com/documentation
https://github.com/ccxt/ccxt/wiki/Manual#market-orders
https://docs.ccxt.com/en/latest/manual.html