作者 karlet

feat:完善部分币安

@@ -7,14 +7,14 @@ require_once __DIR__ . '/../jytools/BinanceFutures.php'; @@ -7,14 +7,14 @@ require_once __DIR__ . '/../jytools/BinanceFutures.php';
7 use trader\struct\ApiInfo; 7 use trader\struct\ApiInfo;
8 use trader\CmBroker; 8 use trader\CmBroker;
9 9
10 -$key = "nQMzzJlhYBkguxwtx5o4MbuiGsDfBIsrOCOUSiqCwR3wmM9gSkQnY1wczW2sVuTP";  
11 -$secret = "GUES7aaM54voJHKrrbXueWXfrJFqmqroVXCF430dolQ5uPK3i7t3Zy4nUgo2W0ec"; 10 +$key = "gfqf3key8Ywq4NkdgXl9P7eZAfH2mxNLfLAiik9xjRQ64fWitkMeOrC0HNEk5VC3";
  11 +$secret = "W0WN7WeFINJpHi7BJam6xxeNquKkcrZCiPdrr4I6PZuYGiWXDomPbmgFmGINevSB";
12 $apiInfo = new ApiInfo($key, $secret, ""); 12 $apiInfo = new ApiInfo($key, $secret, "");
13 $broker = new CmBroker(CmBroker::PLAT_BINANCE, $apiInfo); 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"); 14 +$broker->setWsHost("ws://bnws.keetu.com");
  15 +$broker->setRestHost("http://bnapi.keetu.com");
16 $broker->accListen(function ($data) { 16 $broker->accListen(function ($data) {
17 - var_dump($data); 17 + // var_dump($data);
18 }); 18 });
19 19
20 20
@@ -89,59 +89,69 @@ class CmBroker @@ -89,59 +89,69 @@ class CmBroker
89 $this->exBroker->accListen(function ($data) use ($onData) { 89 $this->exBroker->accListen(function ($data) use ($onData) {
90 // output("ws 有效数据", $data); 90 // output("ws 有效数据", $data);
91 if ($this->plat == self::PLAT_BINANCE) { 91 if ($this->plat == self::PLAT_BINANCE) {
92 - $this->msg("binance 无处理ws数据实现"); 92 + $this->binanceAccDataHandle($data, $onData);
93 } 93 }
94 if ($this->plat == self::PLAT_OKX) { 94 if ($this->plat == self::PLAT_OKX) {
95 - if (isset($data['arg']) && $data['arg']['channel'] == 'orders') {  
96 - foreach ($data['data'] as $key => $value) {  
97 - $wsDataTrade = WsDataTrade::TransferOkxOrder($value, $this->symbolInfos, function ($symbol) {  
98 - return $this->getSymbolSt($symbol);  
99 - });  
100 - if ($wsDataTrade != null) {  
101 - $wsData = new WsData($this->plat, 'trade', $trade = $wsDataTrade);  
102 - $onData($wsData);  
103 - }  
104 - $wsDataOrd = WsDataOrder::TransferOkxOrder($value, $this->symbolInfos, function ($symbol) {  
105 - return $this->getSymbolSt($symbol);  
106 - });  
107 - if ($wsDataOrd != null) {  
108 - $wsData = new WsData($this->plat, 'order', $trade = null, $pos = null, $order = $wsDataOrd);  
109 - $onData($wsData);  
110 - }  
111 - }  
112 - return; 95 + $this->okxAccDataHandle($data, $onData);
  96 + }
  97 + });
  98 + }
  99 + //处理币安相关账户数据监听
  100 + private function binanceAccDataHandle($data, $onData)
  101 + {
  102 + $this->msg("binance 推送", $data);
  103 + }
  104 + //处理欧意账户相关数据监听
  105 + private function okxAccDataHandle($data, $onData)
  106 + {
  107 + if (isset($data['arg']) && $data['arg']['channel'] == 'orders') {
  108 + foreach ($data['data'] as $key => $value) {
  109 + $wsDataTrade = WsDataTrade::TransferOkxOrder($value, $this->symbolInfos, function ($symbol) {
  110 + return $this->getSymbolSt($symbol);
  111 + });
  112 + if ($wsDataTrade != null) {
  113 + $wsData = new WsData($this->plat, 'trade', $trade = $wsDataTrade);
  114 + $onData($wsData);
113 } 115 }
114 - if (isset($data['arg']) && $data['arg']['channel'] == 'positions') {  
115 - $positions = [];  
116 - $eventType = $data['eventType'];  
117 - foreach ($data['data'] as $key => $value) {  
118 - $wsDataPos = WsDataPos::TransferOkxPos($value, $this->symbolInfos, function ($symbol) {  
119 - return $this->getSymbolSt($symbol);  
120 - });  
121 - if ($wsDataPos) {  
122 - $pos = Pos::transferWsDataPos($wsDataPos);  
123 - $positions[$wsDataPos->symbol . "_" . $wsDataPos->posSide] = $pos;  
124 - $wsData = new WsData($this->plat, 'pos', $trade = null, $pos = $wsDataPos);  
125 - $onData($wsData);  
126 - if ($eventType == 'event_update') {  
127 - $this->positions[$wsDataPos->symbol . "_" . $wsDataPos->posSide] = $pos;  
128 - }  
129 - }  
130 - }  
131 - if ($eventType == 'snapshot') {  
132 - $this->positions = $positions;  
133 - }  
134 - return; 116 + $wsDataOrd = WsDataOrder::TransferOkxOrder($value, $this->symbolInfos, function ($symbol) {
  117 + return $this->getSymbolSt($symbol);
  118 + });
  119 + if ($wsDataOrd != null) {
  120 + $wsData = new WsData($this->plat, 'order', $trade = null, $pos = null, $order = $wsDataOrd);
  121 + $onData($wsData);
135 } 122 }
136 - if (isset($data['arg']) && $data['arg']['channel'] == 'account') {  
137 - $wsDataAccount = WsDataAccount::TransferOkxAccount($data['data']);  
138 - $wsData = new WsData($this->plat, 'account', $trade = null, $pos = null, $order = null, $account = $wsDataAccount); 123 + }
  124 + return;
  125 + }
  126 + if (isset($data['arg']) && $data['arg']['channel'] == 'positions') {
  127 + $positions = [];
  128 + $eventType = $data['eventType'];
  129 + foreach ($data['data'] as $key => $value) {
  130 + $wsDataPos = WsDataPos::TransferOkxPos($value, $this->symbolInfos, function ($symbol) {
  131 + return $this->getSymbolSt($symbol);
  132 + });
  133 + if ($wsDataPos) {
  134 + $pos = Pos::transferWsDataPos($wsDataPos);
  135 + $positions[$wsDataPos->symbol . "_" . $wsDataPos->posSide] = $pos;
  136 + $wsData = new WsData($this->plat, 'pos', $trade = null, $pos = $wsDataPos);
139 $onData($wsData); 137 $onData($wsData);
140 - return; 138 + if ($eventType == 'event_update') {
  139 + $this->positions[$wsDataPos->symbol . "_" . $wsDataPos->posSide] = $pos;
  140 + }
141 } 141 }
142 - $this->msg("okx 无处理ws数据实现", $data);  
143 } 142 }
144 - }); 143 + if ($eventType == 'snapshot') {
  144 + $this->positions = $positions;
  145 + }
  146 + return;
  147 + }
  148 + if (isset($data['arg']) && $data['arg']['channel'] == 'account') {
  149 + $wsDataAccount = WsDataAccount::TransferOkxAccount($data['data']);
  150 + $wsData = new WsData($this->plat, 'account', $trade = null, $pos = null, $order = null, $account = $wsDataAccount);
  151 + $onData($wsData);
  152 + return;
  153 + }
  154 + $this->msg("okx 无处理ws数据实现", $data);
145 } 155 }
146 public function klineListen($symbol, $peroid, $onData) 156 public function klineListen($symbol, $peroid, $onData)
147 { 157 {
@@ -37,20 +37,23 @@ class ExBroker @@ -37,20 +37,23 @@ class ExBroker
37 $this->api->setHost($host); 37 $this->api->setHost($host);
38 } 38 }
39 39
40 - public function accListen() 40 + public function accListen(callable $onData)
41 { 41 {
42 $listeneKey = $this->getListenKey(); 42 $listeneKey = $this->getListenKey();
43 - var_dump($this->host . $this->path);  
44 $this->wsAcc = new Websocket($this->host . $this->path); 43 $this->wsAcc = new Websocket($this->host . $this->path);
45 $this->wsAcc->connect( 44 $this->wsAcc->connect(
46 $onOpen = function () use ($listeneKey) { 45 $onOpen = function () use ($listeneKey) {
47 - $this->wsAcc->push(json_encode(['method' => 'SUBSCRIBE', 'params' => [$listeneKey], 'id' => 1])); 46 + $subData = json_encode(['method' => 'SUBSCRIBE', 'params' => [$listeneKey], 'id' => 1]);
  47 + $this->wsAcc->push($subData);
48 }, 48 },
49 - $onMessage = function ($msg) {  
50 - output($msg);  
51 - // $data = json_decode($msg, true); 49 + $onMessage = function ($data) use ($onData) {
  50 + $data = json_decode($data, true);
  51 + if (!$data) {
  52 + return;
  53 + }
  54 + $onData($data);
52 }, 55 },
53 - $onClose = null 56 + $onClose = null,
54 ); 57 );
55 } 58 }
56 59