实例化CCTX交易所类
要在代码中连接到交易所并开始交易数字货币,你首先需要 利用ccxt库实例化一个交易所类:
查询支持的交易所
你可以使用JavaScript、Python或PHP获取ccxt当前支持的交易所ID的完整清单:
JavaScript:
const ccxt = require ('ccxt')
console.log (ccxt.exchanges)
Python:
import ccxt
print (ccxt.exchanges)
PHP:
include 'ccxt.php';
var_dump (\ccxt\Exchange::$exchanges);
交易所类的实例化
可以使用JavaScript、Python、PHP实例化指定的交易所类:
JavaScript:
const ccxt = require ('ccxt')
let exchange = new ccxt.kraken () // default id
let kraken1 = new ccxt.kraken ({ id: 'kraken1' })
let kraken2 = new ccxt.kraken ({ id: 'kraken2' })
let id = 'gdax'
let gdax = new ccxt[id] ();
// from variable id
const exchangeId = 'binance'
, exchangeClass = ccxt[exchangeId]
, exchange = new exchangeClass ({
'apiKey': 'YOUR_API_KEY',
'secret': 'YOUR_SECRET',
'timeout': 30000,
'enableRateLimit': true,
})
Python:
import ccxt
exchange = ccxt.okcoinusd () # default id
okcoin1 = ccxt.okcoinusd ({ 'id': 'okcoin1' })
okcoin2 = ccxt.okcoinusd ({ 'id': 'okcoin2' })
id = 'btcchina'
btcchina = eval ('ccxt.%s ()' % id)
gdax = getattr (ccxt, 'gdax') ()
# from variable id
exchange_id = 'binance'
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class({
'apiKey': 'YOUR_API_KEY',
'secret': 'YOUR_SECRET',
'timeout': 30000,
'enableRateLimit': True,
})
PHP版本的CCXT库使用内置的UTC/GMT时间函数,因此你需要在
php.ini
中设置date.timezone
,或者在使用ccxt之前调用
date_default_timezone_set ()
函数。推荐的时区设置为 "UTC"。
PHP:
date_default_timezone_set ('UTC');
include 'ccxt.php';
$bitfinex = new \ccxt\bitfinex (); // default id
$bitfinex1 = new \ccxt\bitfinex (array ('id' => 'bitfinex1'));
$bitfinex2 = new \ccxt\bitfinex (array ('id' => 'bitfinex2'));
$id = 'kraken';
$exchange = '\\ccxt\\' . $id
$kraken = new $exchange ();
// from variable id
$exchange_id = 'binance';
$exchange_class = "\\ccxt\\$exchange_id";
$exchange = new $exchange_class (array (
'apiKey' => 'YOUR_API_KEY',
'secret' => 'YOUR_SECRET',
'timeout' => 30000,
'enableRateLimit' => true,
));