forked from zhouaini528/bitmex-php
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
root
committed
Oct 29, 2018
0 parents
commit 59988fc
Showing
9 changed files
with
297 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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){ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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){ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
namespace Lin\Bitmex\Exceptions; | ||
|
||
class Exception extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
namespace Lin\Bitmex\Exceptions; | ||
|
||
class OrderException extends Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
namespace Lin\Bitmex\Exceptions; | ||
|
||
class UserException extends Exception | ||
{ | ||
|
||
} |