Online interface testing https://www.bitmex.com/api/explorer/
Address of the test https://testnet.bitmex.com
The official address https://www.bitmex.com
All interface methods are initialized the same as those provided by Bitmex. See details src/api
Support Websocket
Most of the interface is now complete, and the user can continue to extend it based on my design, working with me to improve it.
Exchanges It includes all of the following exchanges and is highly recommended.
If you don't find the exchange SDK you want, you can tell me and I'll join them.
composer require linwj/bitmex
Support for more request Settings More
$bitmex=new Bitmex();
//You can set special needs
$bitmex->setOptions([
//Set the request timeout to 60 seconds by default
'timeout'=>10,
//https://github.com/guzzle/guzzle
'proxy'=>[],
//https://www.php.net/manual/en/book.curl.php
'curl'=>[],
]);
Book Data More
//Get market data
//Book data may be key and secret
try {
$bitmex=new Bitmex();
$result=$bitmex->orderBook()->get([
'symbol'=>'ETHUSD',
'depth'=>20
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
Order More
//Test API address default https://www.bitmex.com
$key='eLB_l505a_cuZL8Cmu5uo7EP';
$secret='wG3ndMquAPl6c-jHUQNhyBQJKGBwdFenIF2QxcgNKE_g8Kz3';
$host='https://testnet.bitmex.com';
$bitmex=new Bitmex($key,$secret,$host);
//bargaining transaction
try {
$result=$bitmex->order()->post([
'symbol'=>'XBTUSD',
'price'=>'100',
'side'=>'Buy',
'orderQty'=>'1',
'ordType'=>'Limit',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
//track the order
try {
$result=$bitmex->order()->getOne([
'symbol'=>'XBTUSD',
'orderID'=>$result['orderID'],
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
//update the order
try {
$result=$bitmex->order()->put([
'symbol'=>'XBTUSD',
'orderID'=>$result['orderID'],
'price'=>'200',
'orderQty'=>'2',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
//cancellation of order
try {
$result=$bitmex->order()->delete([
'symbol'=>'XBTUSD',
'orderID'=>$result['orderID'],
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
Postion More
//bargaining transaction
try {
//Default return all
$result=$bitmex->position()->get([
//'filter'=>'{"symbol": "XBTUSD"}',
//'columns'=>'markPrice',
//'count'=>1,
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
Websocket has two services, server and client. The server is responsible for dealing with the new connection of the exchange, data receiving, authentication and login. Client is responsible for obtaining and processing data.
Server initialization must be started in Linux CLI mode.
use \Lin\Bitmex\BitmexWebSocket;
require __DIR__ .'./vendor/autoload.php';
$bitmex=new BitmexWebSocket();
$bitmex->config([
//Do you want to enable local logging,default false
//'log'=>true,
//Or set the log name
'log'=>['filename'=>'bitmex'],
//Daemons address and port,default 0.0.0.0:2211
//'global'=>'127.0.0.1:2211',
//Channel subscription monitoring time,2 seconds
//'listen_time'=>2,
//Channel data update time,default 0.5 seconds
//'data_time'=>0.5,
//Heartbeat time,default 30 seconds
//'ping_time'=>30,
//Number of messages WS queue shuold hold, default 100
//'queue_count'=>100,
//baseurl host
//'baseurl'=>'ws://www.bitmex.com/realtime',//default
//'baseurl'=>'ws://testnet.bitmex.com/realtime',//test
]);
$bitmex->start();
If you want to test, you can "php server.php start" immediately outputs the log at the terminal.
If you want to deploy, you can "php server.php start -d" enables resident process mode, and enables "log=>true" to view logs.
Client side initialization.
$bitmex=new BitmexWebSocket();
$bitmex->config([
//Do you want to enable local logging,default false
//'log'=>true,
//Or set the log name
'log'=>['filename'=>'bitmex'],
//Daemons address and port,default 0.0.0.0:2216
//'global'=>'127.0.0.1:2216',
//Channel subscription monitoring time,2 seconds
//'listen_time'=>2,
//Channel data update time,default 0.5 seconds
//'data_time'=>0.5,
//Heartbeat time,default 30 seconds
//'ping_time'=>30,
//Number of messages WS queue shuold hold, default 100
//'queue_count'=>100,
//私有数据队列默认保存100条
//'queue_count'=>100,
//baseurl host
//'baseurl'=>'ws://www.bitmex.com/realtime',//default
//'baseurl'=>'ws://testnet.bitmex.com/realtime',//test
]);
Subscribe
//You can only subscribe to public channels
$bitmex->subscribe([
//public
'orderBook10:XBTUSD',
'quoteBin5m:XBTUSD',
]);
//You can also subscribe to both private and public channels.If keysecret() is set, all private channels will be subscribed by default
$bitmex->keysecret([
'key'=>'xxxxxxxxx',
'secret'=>'xxxxxxxxx',
]);
$bitmex->subscribe([
//public
'orderBook10:XBTUSD',
'quoteBin5m:XBTUSD',
//private
"affiliate",
"execution",
"order",
"margin",
"position",
"privateNotifications",
"transact",
"wallet"
]);
Unsubscribe
//Unsubscribe from public channels
$bitmex->unsubscribe([
//public
'orderBook10:XBTUSD',
'quoteBin5m:XBTUSD',
]);
//Unsubscribe from public and private channels.If keysecret() is set, private channels will be Unsubscribed by default
$bitmex->keysecret([
'key'=>'xxxxxxxxx',
'secret'=>'xxxxxxxxx',
]);
$bitmex->unsubscribe([
//public
'orderBook10:XBTUSD',
'quoteBin5m:XBTUSD',
//private
"affiliate",
"execution",
"order",
"margin",
"position",
"privateNotifications",
"transact",
"wallet"
]);
Get all channel subscription data
//The first way
$data=$bitmex->getSubscribe();
print_r(json_encode($data));
//The second way callback
$bitmex->getSubscribe(function($data){
print_r(json_encode($data));
});
//The third way is to guard the process
$bitmex->getSubscribe(function($data){
print_r(json_encode($data));
},true);
Get partial channel subscription data
//The first way
$data=$bitmex->getSubscribe([
'orderBook10:XBTUSD',
'quoteBin5m:XBTUSD',
]);
print_r(json_encode($data));
//The second way callback
$bitmex->getSubscribe([
'orderBook10:XBTUSD',
'quoteBin5m:XBTUSD',
],function($data){
print_r(json_encode($data));
});
//The third way is to guard the process
$bitmex->getSubscribe([
'orderBook10:XBTUSD',
'quoteBin5m:XBTUSD',
],function($data){
print_r(json_encode($data));
},true);
Get partial private channel subscription data
//The first way
$bitmex->keysecret($key_secret);
$data=$bitmex->getSubscribe();//Return all data of private channel
print_r(json_encode($data));
//The second way callback
$bitmex->keysecret($key_secret);
$bitmex->getSubscribe([//Return data private and market
//public
'orderBook10:XBTUSD',
'quoteBin5m:XBTUSD',
//private
"affiliate",
"execution",
"order",
"margin",
"position",
"privateNotifications",
"transact",
"wallet"
],function($data){
print_r(json_encode($data));
});
//The third way is to guard the process
$bitmex->keysecret($key_secret);
$bitmex->getSubscribe([//Resident process to get data return frequency $bitmex->config['data_time']=0.5s
//public
'orderBook10:XBTUSD',
'quoteBin5m:XBTUSD',
//private
"affiliate",
"execution",
"order",
"margin",
"position",
"privateNotifications",
"transact",
"wallet"
],function($data){
print_r(json_encode($data));
},true);