前言
這個SDK是我們團隊的成果,現在開源它,讓php做虛擬貨幣量化交易這塊更為簡單,目前已經大量運用在我們的專案上,效果良好,在請求時間上與 go c++ python相差不大,因為主要網路IO問題。 當然硬體資源的利用率靜態語言肯定比動態強很多。中前期用PHP來開發(就算後期也木有壓力),後期用靜態語言分離模組做微服務化,與PHP結合非常強悍,目前我們正在用GO開始做部分分離了。
這SDK集合了目前交易量最大的多家交易所的API,讓開發人員只關注業務層。它是基於Bitmex Okex Huobi Binance等等,這些底層API再次封裝。它的優點同時支援多平臺,支援統一引數輸入與輸出,也支援原生引數輸入,簡單的量化交易完全滿足你的需求。就算你有特殊的需求你可以單獨通過該方法getPlatform()返回例項,呼叫底層API。
2019.9.11修改:新增了kucoin交易所,以及各種小BUG的修復等等
2019.12.2修改:市價深度、買賣查詢預設關閉、穩定版本釋出等等已經其他BUG
2020.1.2:新增幣安期貨介面
其他交易所API
Exchanges 它包含以下所有交易所,強烈推薦使用該SDK。
安裝方式
composer require linwj/exchanges
交易所初始化
//公共介面初始化物件
$exchanges=new Exchanges('binance');
$exchanges=new Exchanges('bitmex');
$exchanges=new Exchanges('okex');
$exchanges=new Exchanges('huobi');
$exchanges=new Exchanges('kucoin');
...
...
//私有介面初始化物件
$exchanges=new Exchanges('binance',$key,$secret);
$exchanges=new Exchanges('bitmex',$key,$secret);
$exchanges=new Exchanges('okex',$key,$secret,$passphrase);
$exchanges=new Exchanges('huobi',$key,$secret,$account_id);
$exchanges=new Exchanges('kucoin',$key,$secret,$passphrase);
...
...
統一引數返回
所有提交引數與返回引數只要第一個字元為下劃線的_
全部為自定義引數。
/**
* Buy() Sell() Show() 三個方法都返回相同引數
* @return [
* ***返回原始資料
* ...
* ...
* ***返回自定義資料,帶'_'下劃線的是統一返回引數格式。
* _status=>NEW 進行中 PART_FILLED 部分成交 FILLED 完全成交 CANCELING:撤銷中 CANCELLED 已撤銷 FAILURE 下單失敗
* _filled_qty=>已交易完成數量
* _price_avg=>成交均價
* _filed_amount=>交易價格
* _order_id=>系統ID
* _client_id=>自定義ID
* ]
*
* */
/**
* 系統錯誤
* http request code 400 403 500 503
* @return [
* _error=>[
* ***返回原始資料
* ...
* ...
* ***返回自定義資料,帶'_'下劃線的是統一返回引數格式。
* _method => POST
* _url => https://testnet.bitmex.com/api/v1/order
* _httpcode => 400
* ]
* ]
* */
Buy Sell 方法預設有2秒的等待查詢,因為交易所是撮合交易所以查詢需要等待。該預設2秒查詢可以關閉如:buy($data,false)
買賣查詢統一引數返回 詳情
系統錯誤統一引數返回
binance
okex
huobi
bitmex
kucoin
該SDK目前只支援REST請求,暫時不支援Websocket,後期會加入這塊。
支援更多的請求設定 More
$exchanges->setOptions([
//設定請求超時時間,預設60s
'timeout'=>10,
//如果您正在本地開發需要代理,您可以這樣設定
'proxy'=>true,
//更靈活的代理設定
/* 'proxy'=>[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
], */
//是否開啟證照
//'verify'=>false,
]);
現貨
市價交易
//binance
//統一提交引數
$exchanges->trader()->buy([
'_symbol'=>'BTCUSDT',
'_number'=>'0.01',
]);
//也支援原生引數,與上等同
$exchanges->trader()->buy([
'symbol'=>'BTCUSDT',
'type'=>'MARKET',
'quantity'=>'0.01',
]);
//okex
//統一提交引數
$exchanges->trader()->buy([
'_symbol'=>'BTC-USDT',
'_price'=>'10',
]);
//也支援原生引數,與上等同
$exchanges->trader()->buy([
'instrument_id'=>'btc-usdt',
'type'=>'market',
'notional'=>'10'
]);
//huobi
//統一提交引數
$exchanges->trader()->buy([
'_symbol'=>'btcusdt',
'_price'=>'10',
]);
//也支援原生引數,與上等同
$exchanges->trader()->buy([
'account-id'=>$account_id,
'symbol'=>'btcusdt',
'type'=>'buy-market',
'amount'=>10
]);
限價交易
//binance
//統一提交引數
$exchanges->trader()->buy([
'_symbol'=>'BTCUSDT',
'_number'=>'0.01',
'_price'=>'2000',
]);
//也支援原生引數,與上等同
$exchanges->trader()->buy([
'symbol'=>'BTCUSDT',
'type'=>'LIMIT',
'quantity'=>'0.01',
'price'=>'2000',
'timeInForce'=>'GTC',
]);
//okex
//統一提交引數
$exchanges->trader()->buy([
'_symbol'=>'BTC-USDT',
'_number'=>'0.001',
'_price'=>'2000',
]);
//也支援原生引數,與上等同
$exchanges->trader()->buy([
'instrument_id'=>'btc-usdt',
'price'=>'100',
'size'=>'0.001',
]);
//huobi
//統一提交引數
$exchanges->trader()->buy([
'_symbol'=>'btcusdt',
'_number'=>'0.001',
'_price'=>'2000',
]);
//也支援原生引數,與上等同
$exchanges->trader()->buy([
'account-id'=>$account_id,
'symbol'=>'btcusdt',
'type'=>'buy-limit',
'amount'=>'0.001',
'price'=>'2001',
]);
期貨
市價交易
//bitmex
//統一提交引數
$exchanges->trader()->buy([
'_symbol'=>'XBTUSD',
'_number'=>'1',
]);
//也支援原生引數,與上等同
$exchanges->trader()->buy([
'symbol'=>'XBTUSD',
'orderQty'=>'1',
'ordType'=>'Market',
]);
//okex
//統一提交引數
$exchanges->trader()->buy([
'_symbol'=>'BTC-USD-190628',
'_number'=>'1',
'_entry'=>true,//open long
]);
//也支援原生引數,與上等同
$exchanges->trader()->buy([
'instrument_id'=>'BTC-USD-190628',
'size'=>1,
'type'=>1,//1:open long 2:open short 3:close long 4:close short
//'price'=>2000,
'leverage'=>10,//10x or 20x leverage
'match_price' => 1,
'order_type'=>0,
]);
//huobi
$exchanges->trader()->buy([
'_symbol'=>'ETC191227',
'_number'=>'1',
'_entry'=>true,//true:open false:close
]);
//也支援原生引數,與上等同
$exchanges->trader()->buy([
'symbol'=>'XRP',//string false "BTC","ETH"...
'contract_type'=>'quarter',//string false Contract Type ("this_week": "next_week": "quarter":)
'contract_code'=>'XRP190927',//string false BTC180914
//'price'=>'0.3',// decimal true Price
'volume'=>'1',//long true Numbers of orders (amount)
//'direction'=>'buy',// string true Transaction direction
'offset'=>'open',// string true "open", "close"
'order_price_type'=>'opponent',//"limit", "opponent"
'lever_rate'=>20,//int true Leverage rate [if“Open”is multiple orders in 10 rate, there will be not multiple orders in 20 rate
]);
限價交易
//bitmex
//統一提交引數
$exchanges->trader()->buy([
'_symbol'=>'XBTUSD',
'_number'=>'1',
'_price'=>100
]);
//也支援原生引數,與上等同
$exchanges->trader()->buy([
'symbol'=>'XBTUSD',
'price'=>'100',
'orderQty'=>'1',
'ordType'=>'Limit',
]);
//okex
//統一提交引數
$exchanges->trader()->buy([
'_symbol'=>'BTC-USD-190628',
'_number'=>'1',
'_price'=>'2000',
'_entry'=>true,//open long
]);
//也支援原生引數,與上等同
$exchanges->trader()->buy([
'instrument_id'=>'BTC-USD-190628',
'size'=>1,
'type'=>1,//1:open long 2:open short 3:close long 4:close short
'price'=>2000,
'leverage'=>10,//10x or 20x leverage
'match_price' => 0,
'order_type'=>0,
]);
//huobi
$exchanges->trader()->buy([
'_symbol'=>'XRP190927',
'_number'=>'1',
'_price'=>'0.3',
'_entry'=>true,//true:open false:close
]);
//也支援原生引數,與上等同
$exchanges->trader()->buy([
'symbol'=>'XRP',//string false "BTC","ETH"...
'contract_type'=>'quarter',//string false Contract Type ("this_week": "next_week": "quarter":)
'contract_code'=>'XRP190927',// string false BTC180914
'price'=>'0.3',//decimal true Price
'volume'=>'1',//long true Numbers of orders (amount)
//'direction'=>'buy',// string true Transaction direction
'offset'=>'open',// string true "open", "close"
'order_price_type'=>'limit',//"limit", "opponent"
'lever_rate'=>20,//int true Leverage rate [if“Open”is multiple orders in 10 rate, there will be not multiple orders in 20 rate
]);
訂單查詢
//binance
$exchanges->trader()->show([
'_symbol'=>'BTCUSDT',
'_order_id'=>'324314658',
//'_client_id'=>'1bc3e974577a6ad9ce730006eafb5522',
]);
//bitmex
$exchanges->trader()->show([
'_symbol'=>'XBTUSD',
'_order_id'=>'7d03ac2a-b24d-f48c-95f4-2628e6411927',
//'_client_id'=>'1bc3e974577a6ad9ce730006eafb5522',
]);
//okex
$exchanges->trader()->show([
'_symbol'=>'BTC-USDT',
'_order_id'=>'2671215997495296',
//'_client_id'=>'1bc3e974577a6ad9ce730006eafb5522',
]);
$exchanges->trader()->show([
'_symbol'=>'BTC-USD-190927',
'_order_id'=>'2671566274710528',
//'_client_id'=>'1bc3e974577a6ad9ce730006eafb5522',
]);
$exchanges->trader()->show([
'_symbol'=>'BTC-USD-SWAP',
'_order_id'=>'2671566274710528',
//'_client_id'=>'1bc3e974577a6ad9ce730006eafb5522',
]);
//huobi spot
$exchanges->trader()->show([
'_order_id'=>'29897313869',
//'_client_id'=>'1bc3e974577a6ad9ce730006eafb5522',
]);
//huobi future
$exchanges->trader()->show([
'_symbol'=>'XRP190927',
'_order_id'=>'2715696586',
//'_client_id'=>'1bc3e974577a6ad9ce730006eafb5522',
]);
賬號餘額與倉位獲取
//binance
//Get current account information.
$exchanges->account()->get();
//bitmex
//bargaining transaction
$exchanges->account()->get([
//不填寫預設返回所有倉位
//'_symbol'=>'XBTUSD'
]);
//okex spot
//This endpoint supports getting the balance, amount available/on hold of a token in spot account.
$exchanges->account()->get([
'_symbol'=>'BTC',
]);
//okex future
//Get the information of holding positions of a contract.
$exchanges->account()->get([
'_symbol'=>'BTC-USD-190628',
]);
//okex swap
$exchanges->account()->get([
'_symbol'=>'BTC-USD-SWAP',
]);
//huobi spot
$exchanges->account()->get([
'_symbol'=>'btcusdt',
]);
//huobi future
$exchanges->account()->get([
'_symbol'=>'BTC190927',
]);
支援更底層API物件請求
使用前建議先去看看這些Bitmex Okex Huobi Binance底層已經封裝過的SDK。
以下是呼叫底層API的發起一個新的訂單例項
//binance
$exchanges->getPlatform()->trade()->postOrder([
'symbol'=>'BTCUSDT',
'side'=>'BUY',
'type'=>'LIMIT',
'quantity'=>'0.01',
'price'=>'2000',
'timeInForce'=>'GTC',
]);
//bitmex
$exchanges->getPlatform()->order()->post([
'symbol'=>'XBTUSD',
'price'=>'100',
'side'=>'Buy',
'orderQty'=>'1',
'ordType'=>'Limit',
]);
//okex
$exchanges->getPlatform('spot')->order()->post([
'instrument_id'=>'btc-usdt',
'side'=>'buy',
'price'=>'100',
'size'=>'0.001',
//'type'=>'market',
//'notional'=>'100'
]);
$exchanges->getPlatform('future')->order()->post([
'instrument_id'=>'btc-usd-190628',
'type'=>'1',
'price'=>'100',
'size'=>'1',
]);
$result=$exchanges->getPlatform('swap')->order()->post([
'instrument_id'=>'BTC-USD-SWAP',
'type'=>'1',
'price'=>'5000',
'size'=>'1',
]);
//huobi
$exchanges->getPlatform('spot')->order()->postPlace([
'account-id'=>$account_id,
'symbol'=>'btcusdt',
'type'=>'buy-limit',
'amount'=>'0.001',
'price'=>'100',
]);
$exchanges->getPlatform('future')->contract()->postOrder([
'symbol'=>'XRP',//string false "BTC","ETH"...
'contract_type'=>'quarter',// string false Contract Type ("this_week": "next_week": "quarter":)
'contract_code'=>'XRP190927',// string false BTC180914
'price'=>'0.3',// decimal true Price
'volume'=>'1',// long true Numbers of orders (amount)
'direction'=>'buy',// string true Transaction direction
'offset'=>'open',// string true "open", "close"
'order_price_type'=>'limit',//"limit", "opponent"
'lever_rate'=>20,//int true Leverage rate [if“Open”is multiple orders in 10 rate, there will be not multiple orders in 20 rate
//'client_order_id'=>'',//long false Clients fill and maintain themselves, and this time must be greater than last time
]);
本作品採用《CC 協議》,轉載必須註明作者和本文連結