Skip to content

Commit

Permalink
bitmex
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Oct 29, 2018
0 parents commit 59988fc
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 0 deletions.
Empty file added README.md
Empty file.
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "bitmex",
"type": "library",
"description": "Bitmex API",
"license": "MIT",
"authors": [
{
"name": "lin",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Lin\\Bitmex\\": "./src/"
}
},
"require": {
"guzzlehttp/guzzle": "^6.3"
}
}
30 changes: 30 additions & 0 deletions src/Api/ApiKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace Lin\Bitmex\Api;

use Lin\Bitmex\Bitmex;

class ApiKey extends Bitmex
{
public function get(){
$this->type='GET';
$this->path='/api/v1/apiKey';

return $this->exec();
}

public function post(){

}

public function postDisable(){

}

public function delete($data){
$this->type='DELETE';
$this->path='/api/v1/apiKey';
$this->data=$data;

return $this->exec();
}
}
68 changes: 68 additions & 0 deletions src/Api/Order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* @author lin <[email protected]>
* */

namespace Lin\Bitmex\Api;

use Lin\Bitmex\Bitmex;

class Order extends Bitmex
{
/**
* $data=[
'symbol'=>'ADAZ18',
//可选参数 具体参考API https://testnet.bitmex.com/api/explorer/#!/Order/Order_getOrders
* 数量
* 时间
];
* */
public function get($data){
$this->type='GET';
$this->path='/api/v1/order';
$this->data=$data;

return $this->exec();
}

public function put($data){

}

/**
* $data=[
'symbol'=>'XBTUSD',
'price'=>'10',
'orderQty'=>'10',
'ordType'=>'Limit',
];
* */
public function post($data){
$this->type='POST';
$this->path='/api/v1/order';
$this->data=$data;

return $this->exec();
}

public function delete($data){

}

public function putBulk($data){

}

public function postBulk($data){

}

public function postCancelAllAfter($data){

}

public function postClosePosition($data){

}
}
27 changes: 27 additions & 0 deletions src/Api/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Lin\Bitmex\Api;

use Lin\Bitmex\Bitmex;
use Lin\Bitmex\Exceptions\UserException;

class User extends Bitmex
{
public function get(){
$this->type='GET';
$this->path='/api/v1/user';

$this->exec();
}

public function put($data){
$this->type='PUT';
$this->path='/api/v1/user';
$this->data=$data;

$this->exec();
}

public function post($data){

}
}
131 changes: 131 additions & 0 deletions src/Bitmex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/**
* @author lin <[email protected]>
* */

namespace Lin\Bitmex;

use GuzzleHttp\Exception\RequestException;

class Bitmex
{
/**
* 是否开启bitmex测试账号,需要先去申请测试账号。
* */
private $test=false;

protected $key='';

protected $secret='';

protected $host='';

protected $nonce='';

protected $signature='';

protected $headers=[];

protected $type='';

protected $path='';

protected $data=[];

protected $timeout=15;

public function __construct(string $key,string $secret)
{
$this->key = $key;

$this->secret=$secret;

if(empty($host)) $this->host='https://www.bitmex.com';
}

/**
* 是否开启测试
* */
public function test($value=true){
$this->test=$value;

if($this->test) {
$this->host='https://testnet.bitmex.com';
}else{
$this->host='https://www.bitmex.com';
}
}

/**
* 认证
* */
protected function auth(){
$this->nonce();

$this->signature();

$this->headers();
}

/**
* 过期时间
* */
protected function nonce(){
$this->nonce = (string) number_format(round(microtime(true) * 100000), 0, '.', '');
}

/**
* 签名
* */
protected function signature(){
$endata=http_build_query($this->data);
$this->signature=hash_hmac('sha256', $this->type.$this->path.$this->nonce.$endata, $this->secret);
}

/**
* 默认头部信息
* */
protected function headers(){
$this->headers=[
'accept' => 'application/json',
'api-expires' => $this->nonce,
'api-key'=>$this->key,
'api-signature' => $this->signature,
];

if(!empty($this->data)) $this->headers['content-type']='application/x-www-form-urlencoded';
}

/**
* 发送http
* */
protected function send(){
$client = new \GuzzleHttp\Client();

$data=[
'headers'=>$this->headers,
'timeout'=>$this->timeout
];

if(!empty($this->data)) $data['form_params']=$this->data;

$response = $client->request($this->type, $this->host.$this->path, $data);

return $response->getBody()->getContents();
}

/*
* 执行流程
* */
protected function exec(){
$this->auth();

//可以记录日志
try {
return $this->send();
}catch (RequestException $e){
//TODO 该流程记录日志
print_r($e->getMessage());
}
}
}
7 changes: 7 additions & 0 deletions src/Exceptions/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace Lin\Bitmex\Exceptions;

class Exception extends \Exception
{

}
7 changes: 7 additions & 0 deletions src/Exceptions/OrderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace Lin\Bitmex\Exceptions;

class OrderException extends Exception
{

}
7 changes: 7 additions & 0 deletions src/Exceptions/UserException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace Lin\Bitmex\Exceptions;

class UserException extends Exception
{

}

0 comments on commit 59988fc

Please sign in to comment.