# CCXT 通用交易所 API

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

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

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

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

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

```javascript
const ccxt = require ('ccxt');

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

## ![](https://3415061728-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M0u4DA5jITnh37VYXmj%2Fuploads%2FUGuZK73nwyqhltZSt6Ga%2F%E6%88%AA%E5%9C%96%202023-02-09%20%E4%B8%8A%E5%8D%8811.30.07.png?alt=media\&token=62b79df6-04fb-405e-a6c0-1a768f7520a0)

## 獲取可呼叫方法

```javascript
const ccxt = require ('ccxt');

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

## ![](https://3415061728-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M0u4DA5jITnh37VYXmj%2Fuploads%2FHNTT3E7ypWTgSZF69Etr%2F%E6%88%AA%E5%9C%96%202023-02-09%20%E4%B8%8A%E5%8D%8811.29.37.png?alt=media\&token=6feeaabd-244b-4e9d-8826-0d2342e20f2c)

## 獲取所有可用交易對

```javascript
const ccxt = require ('ccxt');

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

> 最小下單金額可以從 `['TOKEN/TOKEN']['limits']['cost']['min']` 獲取
>
> ```javascript
> const markets = await exchange.loadMarkets ();
> console.log(markets["ETH/USDT"]?.limits?.cost?.min)
> ```
>
> <https://github.com/ccxt/ccxt/issues/1972#issuecomment-366834844>

## 建立市價訂單

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

## 建立限價訂單

```javascript
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

```javascript
(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)

```javascript
(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
```

> ![](https://3415061728-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0u4DA5jITnh37VYXmj%2F-M0u4EflenDYR7clyEKz%2F-M0u4TXc-w50il8L7yJY%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202020-01-05%20%E4%B8%8B%E5%8D%886.11.53.png?generation=1582596248228654\&alt=media)然後將TimeFrom 在放入下一筆的toTs繼續往更早之前的資料抓取
