正在显示
2 个修改的文件
包含
87 行增加
和
0 行删除
trader/exchange/gateio/Api.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace trader\exchange\gateio; | ||
| 4 | + | ||
| 5 | +require_once __DIR__ . '/../../struct/ApiInfo.php'; | ||
| 6 | +require_once __DIR__ . '/../../../jytools/func.php'; | ||
| 7 | + | ||
| 8 | +use trader\struct\ApiInfo; | ||
| 9 | +use jytools\Curl; | ||
| 10 | +use function jytools\getMicrotime; | ||
| 11 | + | ||
| 12 | +class Api | ||
| 13 | +{ | ||
| 14 | + private $host = 'https://api.bybit.com'; | ||
| 15 | + private ApiInfo $apiInfo; | ||
| 16 | + public function __construct(ApiInfo $apiInfo) | ||
| 17 | + { | ||
| 18 | + $this->apiInfo = $apiInfo; | ||
| 19 | + } | ||
| 20 | + public function setHost($host) | ||
| 21 | + { | ||
| 22 | + $this->host = $host; | ||
| 23 | + } | ||
| 24 | +} |
trader/exchange/gateio/ExBroker.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace trader\exchange\bybit; | ||
| 4 | + | ||
| 5 | +require_once __DIR__ . '/../../struct/ApiInfo.php'; | ||
| 6 | +require_once __DIR__ . '/Api.php'; | ||
| 7 | +require_once __DIR__ . '/../../../jytools/Websocket.php'; | ||
| 8 | +require_once __DIR__ . '/../../../jytools/func.php'; | ||
| 9 | + | ||
| 10 | +use trader\struct\ApiInfo; | ||
| 11 | +use trader\exchange\bybit\Api as BbApi; | ||
| 12 | +use jytools\Websocket; | ||
| 13 | +use function jytools\output; | ||
| 14 | + | ||
| 15 | +class ExBroker | ||
| 16 | +{ | ||
| 17 | + private $host = 'wss://stream.bybit.com'; | ||
| 18 | + private $pathPri = '/v5/private'; //账户信息path | ||
| 19 | + private $pathPub = '/v5/public/linear'; //行情信息path 永续 | ||
| 20 | + private ApiInfo $apiInfo; | ||
| 21 | + private BbApi $api; | ||
| 22 | + private ?Websocket $wsAcc; | ||
| 23 | + private ?Websocket $wsKline; | ||
| 24 | + | ||
| 25 | + public function __construct(ApiInfo $apiInfo) | ||
| 26 | + { | ||
| 27 | + $this->apiInfo = $apiInfo; | ||
| 28 | + $this->api = new BbApi($apiInfo); | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + public function setWsHost($host) | ||
| 32 | + { | ||
| 33 | + $this->host = $host; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + public function setRestHost($host) | ||
| 37 | + { | ||
| 38 | + $this->api->setHost($host); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + public function accListen(callable $onData) {} | ||
| 42 | + | ||
| 43 | + public function klineListen($symbol, $interval, callable $onData) | ||
| 44 | + { | ||
| 45 | + $this->wsKline = new Websocket($this->host . $this->pathPub); | ||
| 46 | + $this->wsKline->connect( | ||
| 47 | + function () use ($symbol, $interval) { | ||
| 48 | + $this->wsKline->push(json_encode([ | ||
| 49 | + 'op' => 'subscribe', | ||
| 50 | + 'args' => [ | ||
| 51 | + "kline.{$interval}.{$symbol}" | ||
| 52 | + ] | ||
| 53 | + ])); | ||
| 54 | + }, | ||
| 55 | + function ($data) use ($onData) { | ||
| 56 | + $data = json_decode($data, true); | ||
| 57 | + if (isset($data['data'])) { | ||
| 58 | + $onData($data); | ||
| 59 | + } | ||
| 60 | + }, | ||
| 61 | + ); | ||
| 62 | + } | ||
| 63 | +} |
-
请 注册 或 登录 后发表评论