作者 karlet

feat:平台定义常量

@@ -35,6 +35,17 @@ use function jytools\timeFormat; @@ -35,6 +35,17 @@ use function jytools\timeFormat;
35 35
36 class CmBroker 36 class CmBroker
37 { 37 {
  38 + const PLAT_OKX = 'okx';
  39 + const PLAT_BINANCE = 'binance';
  40 + const PLAT_BYBIT = 'bybit';
  41 + const PLAT_BITGET = 'bitget';
  42 + /**
  43 + * @var string 当前使用的交易平台
  44 + * @see PLAT_OKX
  45 + * @see PLAT_BINANCE
  46 + * @see PLAT_BYBIT
  47 + * @see PLAT_BITGET
  48 + */
38 public $plat; 49 public $plat;
39 private ?OkxBroker $exBroker; 50 private ?OkxBroker $exBroker;
40 /** @var SymbolInfo[] $symbolInfos */ 51 /** @var SymbolInfo[] $symbolInfos */
@@ -49,10 +60,10 @@ class CmBroker @@ -49,10 +60,10 @@ class CmBroker
49 { 60 {
50 $this->plat = $plat; 61 $this->plat = $plat;
51 $exBroker = null; 62 $exBroker = null;
52 - if ($plat == 'okx') { 63 + if ($plat == self::PLAT_OKX) {
53 $exBroker = new OkxBroker($apiInfo); 64 $exBroker = new OkxBroker($apiInfo);
54 } 65 }
55 - if ($plat == 'binance') { 66 + if ($plat == self::PLAT_BINANCE) {
56 $exBroker = new BinanceBroker($apiInfo); 67 $exBroker = new BinanceBroker($apiInfo);
57 } 68 }
58 $this->exBroker = $exBroker; 69 $this->exBroker = $exBroker;
@@ -77,10 +88,10 @@ class CmBroker @@ -77,10 +88,10 @@ class CmBroker
77 { 88 {
78 $this->exBroker->accListen(function ($data) use ($onData) { 89 $this->exBroker->accListen(function ($data) use ($onData) {
79 // output("ws 有效数据", $data); 90 // output("ws 有效数据", $data);
80 - if ($this->plat == 'binance') { 91 + if ($this->plat == self::PLAT_BINANCE) {
81 $this->msg("binance 无处理ws数据实现"); 92 $this->msg("binance 无处理ws数据实现");
82 } 93 }
83 - if ($this->plat == 'okx') { 94 + if ($this->plat == self::PLAT_OKX) {
84 if (isset($data['arg']) && $data['arg']['channel'] == 'orders') { 95 if (isset($data['arg']) && $data['arg']['channel'] == 'orders') {
85 foreach ($data['data'] as $key => $value) { 96 foreach ($data['data'] as $key => $value) {
86 $wsDataTrade = WsDataTrade::TransferOkxOrder($value, $this->symbolInfos, function ($symbol) { 97 $wsDataTrade = WsDataTrade::TransferOkxOrder($value, $this->symbolInfos, function ($symbol) {
@@ -155,10 +166,10 @@ class CmBroker @@ -155,10 +166,10 @@ class CmBroker
155 public function getSymbolOri($symbol, $platTarget) 166 public function getSymbolOri($symbol, $platTarget)
156 { 167 {
157 $symbolSt = $this->getSymbolSt($symbol); 168 $symbolSt = $this->getSymbolSt($symbol);
158 - if ($platTarget == 'binance') { 169 + if ($platTarget == self::PLAT_BINANCE) {
159 return $symbolSt; 170 return $symbolSt;
160 } 171 }
161 - if ($platTarget == 'okx') { 172 + if ($platTarget == self::PLAT_OKX) {
162 return str_replace('USDT', '-USDT-SWAP', $symbolSt); 173 return str_replace('USDT', '-USDT-SWAP', $symbolSt);
163 } 174 }
164 throw new Exception('平台错误' + $platTarget); 175 throw new Exception('平台错误' + $platTarget);
@@ -166,7 +177,7 @@ class CmBroker @@ -166,7 +177,7 @@ class CmBroker
166 177
167 public function placeOrder(Order $order) 178 public function placeOrder(Order $order)
168 { 179 {
169 - if ($this->plat == 'okx') { 180 + if ($this->plat == self::PLAT_OKX) {
170 $orderOri = $order->toOkxOrder($this->symbolInfos, function ($symbol) { 181 $orderOri = $order->toOkxOrder($this->symbolInfos, function ($symbol) {
171 return $this->getSymbolOri($symbol, $this->plat); 182 return $this->getSymbolOri($symbol, $this->plat);
172 }); 183 });
@@ -209,7 +220,7 @@ class CmBroker @@ -209,7 +220,7 @@ class CmBroker
209 } 220 }
210 private function initSymbolInfos() 221 private function initSymbolInfos()
211 { 222 {
212 - if ($this->plat == 'okx') { 223 + if ($this->plat == self::PLAT_OKX) {
213 //获取所有USDT SWAP 交易对 224 //获取所有USDT SWAP 交易对
214 $res = $this->exBroker->getSymbolInfos(); 225 $res = $this->exBroker->getSymbolInfos();
215 $infos = []; 226 $infos = [];
@@ -250,7 +261,7 @@ class CmBroker @@ -250,7 +261,7 @@ class CmBroker
250 public function getLevers() 261 public function getLevers()
251 { 262 {
252 $levers = $this->exBroker->getAllLevers(); 263 $levers = $this->exBroker->getAllLevers();
253 - if ($this->plat == 'okx') { 264 + if ($this->plat == self::PLAT_OKX) {
254 $resNew = []; 265 $resNew = [];
255 foreach ($levers as $key => $value) { 266 foreach ($levers as $key => $value) {
256 $symbol = $this->getSymbolSt($key); //转换为标准交易对 267 $symbol = $this->getSymbolSt($key); //转换为标准交易对
@@ -287,7 +298,7 @@ class CmBroker @@ -287,7 +298,7 @@ class CmBroker
287 public function getPos($symbol, $posSide) 298 public function getPos($symbol, $posSide)
288 { 299 {
289 $symbolOri = $this->getSymbolOri($symbol, $this->plat); 300 $symbolOri = $this->getSymbolOri($symbol, $this->plat);
290 - if ($this->plat == 'okx') { 301 + if ($this->plat == self::PLAT_OKX) {
291 $posSide = strtolower($posSide); 302 $posSide = strtolower($posSide);
292 $symbolInfo = $this->symbolInfos[$symbol]; 303 $symbolInfo = $this->symbolInfos[$symbol];
293 $lot = $this->exBroker->getPos($symbolOri, $posSide); 304 $lot = $this->exBroker->getPos($symbolOri, $posSide);
@@ -306,7 +317,7 @@ class CmBroker @@ -306,7 +317,7 @@ class CmBroker
306 public function getKlines($symbol, $peroid, $limit = 100) 317 public function getKlines($symbol, $peroid, $limit = 100)
307 { 318 {
308 $symbolOri = $this->getSymbolOri($symbol, $this->plat); 319 $symbolOri = $this->getSymbolOri($symbol, $this->plat);
309 - if ($this->plat == 'okx') { 320 + if ($this->plat == self::PLAT_OKX) {
310 $res = $this->exBroker->getKlines($symbolOri, $peroid, $limit); 321 $res = $this->exBroker->getKlines($symbolOri, $peroid, $limit);
311 if ($res['code'] != '0') { 322 if ($res['code'] != '0') {
312 return []; 323 return [];