正在显示
4 个修改的文件
包含
117 行增加
和
8 行删除
examples/bnBroker.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +require_once __DIR__ . '/../trader/CmBroker.php'; | ||
| 4 | +require_once __DIR__ . '/../trader/struct/ApiInfo.php'; | ||
| 5 | +require_once __DIR__ . '/../jytools/BinanceFutures.php'; | ||
| 6 | + | ||
| 7 | +use trader\struct\ApiInfo; | ||
| 8 | +use trader\CmBroker; | ||
| 9 | + | ||
| 10 | +$key = "nQMzzJlhYBkguxwtx5o4MbuiGsDfBIsrOCOUSiqCwR3wmM9gSkQnY1wczW2sVuTP"; | ||
| 11 | +$secret = "GUES7aaM54voJHKrrbXueWXfrJFqmqroVXCF430dolQ5uPK3i7t3Zy4nUgo2W0ec"; | ||
| 12 | +$apiInfo = new ApiInfo($key, $secret, ""); | ||
| 13 | +$broker = new CmBroker(CmBroker::PLAT_BINANCE, $apiInfo); | ||
| 14 | +$broker->setWsHost("ws://bnws.a.indigo888.com/futures"); | ||
| 15 | +$broker->setRestHost("http://bnfapi.a.indigo888.com"); | ||
| 16 | +$broker->accListen(function ($data) { | ||
| 17 | + var_dump($data); | ||
| 18 | +}); | ||
| 19 | + | ||
| 20 | + | ||
| 21 | +// use jytools\BinanceFutures; | ||
| 22 | +// $api = new BinanceFutures($key, $secret); | ||
| 23 | +// $res = $api->listenKey(); | ||
| 24 | +// var_dump($res); |
| @@ -47,7 +47,7 @@ class CmBroker | @@ -47,7 +47,7 @@ class CmBroker | ||
| 47 | * @see PLAT_BITGET | 47 | * @see PLAT_BITGET |
| 48 | */ | 48 | */ |
| 49 | public $plat; | 49 | public $plat; |
| 50 | - private ?OkxBroker $exBroker; | 50 | + private OkxBroker|BinanceBroker $exBroker; |
| 51 | /** @var SymbolInfo[] $symbolInfos */ | 51 | /** @var SymbolInfo[] $symbolInfos */ |
| 52 | public $symbolInfos = []; | 52 | public $symbolInfos = []; |
| 53 | /** @var Pos[] $positions */ | 53 | /** @var Pos[] $positions */ |
| @@ -3,8 +3,11 @@ | @@ -3,8 +3,11 @@ | ||
| 3 | namespace trader\exchange\binance; | 3 | namespace trader\exchange\binance; |
| 4 | 4 | ||
| 5 | require_once __DIR__ . '/../../struct/ApiInfo.php'; | 5 | require_once __DIR__ . '/../../struct/ApiInfo.php'; |
| 6 | +require_once __DIR__ . '/../../../jytools/func.php'; | ||
| 6 | 7 | ||
| 7 | use trader\struct\ApiInfo; | 8 | use trader\struct\ApiInfo; |
| 9 | +use jytools\Curl; | ||
| 10 | +use function jytools\getMicrotime; | ||
| 8 | 11 | ||
| 9 | class Api | 12 | class Api |
| 10 | { | 13 | { |
| @@ -14,10 +17,61 @@ class Api | @@ -14,10 +17,61 @@ class Api | ||
| 14 | { | 17 | { |
| 15 | $this->apiInfo = $apiInfo; | 18 | $this->apiInfo = $apiInfo; |
| 16 | } | 19 | } |
| 20 | + public function setHost($host) | ||
| 21 | + { | ||
| 22 | + $this->host = $host; | ||
| 23 | + } | ||
| 17 | 24 | ||
| 18 | - public function getPremiumIndex() | 25 | + //-----------public interface ------------ |
| 26 | + public function getPremiumIndex($params) | ||
| 19 | { | 27 | { |
| 20 | $url = "/fapi/v1/premiumIndex"; | 28 | $url = "/fapi/v1/premiumIndex"; |
| 21 | $method = "GET"; | 29 | $method = "GET"; |
| 30 | + return $this->request($method, $url, $params); | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + //-------private interface ------------ | ||
| 34 | + public function getListenKey($params) | ||
| 35 | + { | ||
| 36 | + $url = "/fapi/v1/listenKey"; | ||
| 37 | + $method = "POST"; | ||
| 38 | + return $this->request($method, $url, [], $this->apiInfo); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + //------------------------------------ | ||
| 42 | + | ||
| 43 | + private function createSign($secret, $param) | ||
| 44 | + { | ||
| 45 | + $len = count($param); | ||
| 46 | + if ($len == 0) { | ||
| 47 | + return $param; | ||
| 48 | + } | ||
| 49 | + $paramStr = http_build_query($param); | ||
| 50 | + return hash_hmac('sha256', $paramStr, $secret); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + public function request($method, $path, $param, ?ApiInfo $apiInfo = null) | ||
| 54 | + { | ||
| 55 | + $url = $this->host . $path; | ||
| 56 | + if (!in_array(strtoupper($method), ['GET', 'POST', 'DELETE'])) { | ||
| 57 | + return 0; | ||
| 58 | + } | ||
| 59 | + $header = []; | ||
| 60 | + if ($apiInfo != null) { | ||
| 61 | + $param['timestamp'] = getMicrotime(); | ||
| 62 | + $param['signature'] = $this->createSign($apiInfo->secret, $param); | ||
| 63 | + $header[] = "X-MBX-APIKEY:" . $apiInfo->key; | ||
| 64 | + } | ||
| 65 | + $data = json_encode([]); | ||
| 66 | + if (strtoupper($method) == 'POST') { | ||
| 67 | + $data = Curl::httpPost($url, $param, $header); | ||
| 68 | + } | ||
| 69 | + if (strtoupper($method) == 'GET') { | ||
| 70 | + $data = Curl::httpGet($url, $param, $header); | ||
| 71 | + } | ||
| 72 | + if (strtoupper($method) == 'DELETE') { | ||
| 73 | + $data = Curl::httpDelete($url, $param, $header); | ||
| 74 | + } | ||
| 75 | + return json_decode($data, true); | ||
| 22 | } | 76 | } |
| 23 | } | 77 | } |
| @@ -5,18 +5,17 @@ namespace trader\exchange\binance; | @@ -5,18 +5,17 @@ namespace trader\exchange\binance; | ||
| 5 | require_once __DIR__ . '/../../struct/ApiInfo.php'; | 5 | require_once __DIR__ . '/../../struct/ApiInfo.php'; |
| 6 | require_once __DIR__ . '/Api.php'; | 6 | require_once __DIR__ . '/Api.php'; |
| 7 | require_once __DIR__ . '/../../../jytools/Websocket.php'; | 7 | require_once __DIR__ . '/../../../jytools/Websocket.php'; |
| 8 | +require_once __DIR__ . '/../../../jytools/func.php'; | ||
| 8 | 9 | ||
| 9 | use trader\struct\ApiInfo; | 10 | use trader\struct\ApiInfo; |
| 10 | use trader\exchange\binance\Api as BnApi; | 11 | use trader\exchange\binance\Api as BnApi; |
| 11 | use jytools\Websocket; | 12 | use jytools\Websocket; |
| 13 | +use function jytools\output; | ||
| 12 | 14 | ||
| 13 | class ExBroker | 15 | class ExBroker |
| 14 | { | 16 | { |
| 15 | - // private $host = 'wss://ws.okx.com:8443'; | ||
| 16 | - private $host = 'ws://okws.keetu.com'; | ||
| 17 | - private $pathPrivate = '/ws/v5/private'; | ||
| 18 | - private $pathPublic = '/ws/v5/public'; | ||
| 19 | - private $pathBusiness = '/ws/v5/business'; | 17 | + private $host = 'wss://fstream.binance.com'; |
| 18 | + private $path = '/stream'; | ||
| 20 | private ApiInfo $apiInfo; | 19 | private ApiInfo $apiInfo; |
| 21 | private BnApi $api; | 20 | private BnApi $api; |
| 22 | private ?Websocket $wsAcc; | 21 | private ?Websocket $wsAcc; |
| @@ -28,9 +27,41 @@ class ExBroker | @@ -28,9 +27,41 @@ class ExBroker | ||
| 28 | $this->api = new BnApi($apiInfo); | 27 | $this->api = new BnApi($apiInfo); |
| 29 | } | 28 | } |
| 30 | 29 | ||
| 30 | + public function setWsHost($host) | ||
| 31 | + { | ||
| 32 | + $this->host = $host; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + public function setRestHost($host) | ||
| 36 | + { | ||
| 37 | + $this->api->setHost($host); | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + public function accListen() | ||
| 41 | + { | ||
| 42 | + $listeneKey = $this->getListenKey(); | ||
| 43 | + var_dump($this->host . $this->path); | ||
| 44 | + $this->wsAcc = new Websocket($this->host . $this->path); | ||
| 45 | + $this->wsAcc->connect( | ||
| 46 | + $onOpen = function () use ($listeneKey) { | ||
| 47 | + $this->wsAcc->push(json_encode(['method' => 'SUBSCRIBE', 'params' => [$listeneKey], 'id' => 1])); | ||
| 48 | + }, | ||
| 49 | + $onMessage = function ($msg) { | ||
| 50 | + output($msg); | ||
| 51 | + // $data = json_decode($msg, true); | ||
| 52 | + }, | ||
| 53 | + $onClose = null | ||
| 54 | + ); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + private function getListenKey() | ||
| 58 | + { | ||
| 59 | + $res = $this->api->getListenKey([]); | ||
| 60 | + return $res['listenKey']; | ||
| 61 | + } | ||
| 31 | //获取所有品种资金费率 | 62 | //获取所有品种资金费率 |
| 32 | public function getAllPremium() | 63 | public function getAllPremium() |
| 33 | { | 64 | { |
| 34 | - $res = $this->api->getPremiumIndex(); | 65 | + $res = $this->api->getPremiumIndex([]); |
| 35 | } | 66 | } |
| 36 | } | 67 | } |
-
请 注册 或 登录 后发表评论