作者 karlet

feat:监听account

@@ -14,6 +14,7 @@ use trader\struct\WsDataTrade; @@ -14,6 +14,7 @@ use trader\struct\WsDataTrade;
14 use trader\struct\WsDataPos; 14 use trader\struct\WsDataPos;
15 use trader\struct\SymbolInfo; 15 use trader\struct\SymbolInfo;
16 use trader\struct\WsDataOrder; 16 use trader\struct\WsDataOrder;
  17 +use trader\struct\WsDataAccount;
17 use trader\struct\Pos; 18 use trader\struct\Pos;
18 use \Exception; 19 use \Exception;
19 use function jytools\timeFormat; 20 use function jytools\timeFormat;
@@ -93,6 +94,12 @@ class CmBroker @@ -93,6 +94,12 @@ class CmBroker
93 $this->positions = $positions; 94 $this->positions = $positions;
94 return; 95 return;
95 } 96 }
  97 + if (isset($data['arg']) && $data['arg']['channel'] == 'account') {
  98 + $wsDataAccount = WsDataAccount::TransferOkxAccount($data['data']);
  99 + $wsData = new WsData($this->plat, 'account', $trade = null, $pos = null, $order = null, $account = $wsDataAccount);
  100 + $onData($wsData);
  101 + return;
  102 + }
96 $this->msg("okx 无处理ws数据实现", $data); 103 $this->msg("okx 无处理ws数据实现", $data);
97 } 104 }
98 }); 105 });
@@ -75,6 +75,13 @@ class Api @@ -75,6 +75,13 @@ class Api
75 $method = 'POST'; 75 $method = 'POST';
76 return $this->request($path, $method, $param, $this->apiInfo); 76 return $this->request($path, $method, $param, $this->apiInfo);
77 } 77 }
  78 + //查看账户余额
  79 + public function accountBalance($param)
  80 + {
  81 + $path = '/api/v5/account/balance';
  82 + $method = 'GET';
  83 + return $this->request($path, $method, $param, $this->apiInfo);
  84 + }
78 85
79 //------------------------------------- 86 //-------------------------------------
80 87
@@ -135,6 +135,10 @@ class ExBroker @@ -135,6 +135,10 @@ class ExBroker
135 'channel' => 'positions', 135 'channel' => 'positions',
136 'instType' => 'SWAP', 136 'instType' => 'SWAP',
137 ], 137 ],
  138 + [
  139 + 'channel' => 'account',
  140 + 'ccy' => 'USDT',
  141 + ]
138 ]; 142 ];
139 $this->wsAcc->push(json_encode($subData)); 143 $this->wsAcc->push(json_encode($subData));
140 } 144 }
@@ -7,6 +7,7 @@ require_once __DIR__ . '/WsDataTrade.php'; @@ -7,6 +7,7 @@ require_once __DIR__ . '/WsDataTrade.php';
7 use trader\struct\WsDataTrade; 7 use trader\struct\WsDataTrade;
8 use trader\struct\WsDataPos; 8 use trader\struct\WsDataPos;
9 use trader\struct\WsDataOrder; 9 use trader\struct\WsDataOrder;
  10 +use trader\struct\WsDataAccount;
10 11
11 // 综合ws数据 12 // 综合ws数据
12 class WsData 13 class WsData
@@ -16,15 +17,17 @@ class WsData @@ -16,15 +17,17 @@ class WsData
16 public ?WsDataTrade $trade = null; 17 public ?WsDataTrade $trade = null;
17 public ?WsDataPos $pos = null; 18 public ?WsDataPos $pos = null;
18 public ?WsDataOrder $order = null; 19 public ?WsDataOrder $order = null;
  20 + public ?WsDataAccount $account = null;
19 21
20 22
21 - public function __construct(string $platform, string $dataType, WsDataTrade $trade = null, WsDataPos $pos = null, WsDataOrder $order = null) 23 + public function __construct(string $platform, string $dataType, WsDataTrade $trade = null, WsDataPos $pos = null, WsDataOrder $order = null, $account = null)
22 { 24 {
23 $this->platform = $platform; 25 $this->platform = $platform;
24 $this->dataType = $dataType; 26 $this->dataType = $dataType;
25 $this->trade = $trade; 27 $this->trade = $trade;
26 $this->pos = $pos; 28 $this->pos = $pos;
27 $this->order = $order; 29 $this->order = $order;
  30 + $this->account = $account;
28 } 31 }
29 32
30 public function toArray() 33 public function toArray()
@@ -35,6 +38,7 @@ class WsData @@ -35,6 +38,7 @@ class WsData
35 'trade' => $this->trade?->toArray(), 38 'trade' => $this->trade?->toArray(),
36 'pos' => $this->pos?->toArray(), 39 'pos' => $this->pos?->toArray(),
37 'order' => $this->order?->toArray(), 40 'order' => $this->order?->toArray(),
  41 + 'account' => $this->account?->toArray(),
38 ]; 42 ];
39 } 43 }
40 } 44 }
  1 +<?php
  2 +
  3 +namespace trader\struct;
  4 +
  5 +require_once __DIR__ . '/../autoload.php';
  6 +
  7 +use trader\struct\SymbolInfo;
  8 +
  9 +class WsDataAccount
  10 +{
  11 + public array $assets;
  12 +
  13 + public static function TransferOkxAccount(array $data): array
  14 + {
  15 + $ret = [];
  16 + foreach ($data as $item) {
  17 + $asset = new Asset();
  18 + $asset->coin = $item['ccy'];
  19 + $asset->totalRight = $item['availBal'];
  20 + $asset->avail = $item['availEq'];
  21 + $asset->frozen = $item['frozenBal'];
  22 + $asset->upl = $item['upl'];
  23 + $ret[$asset->coin] = $asset;
  24 + }
  25 + return $ret; //WsDataAccoun
  26 + }
  27 +
  28 + public function toArray(): array
  29 + {
  30 + $ret = [];
  31 + foreach ($this->assets as $asset) {
  32 + $ret[$asset->coin] = $asset->toArray();
  33 + }
  34 + return $ret;
  35 + }
  36 +}
  37 +
  38 +class Asset
  39 +{
  40 + public string $coin;
  41 + public float $totalRight; //总权益
  42 + public float $avail; //可用
  43 + public float $frozen; //冻结
  44 + public float $upl; //未实现盈亏
  45 +
  46 + public function toArray(): array
  47 + {
  48 + return [
  49 + 'coin' => $this->coin,
  50 + 'totalRight' => $this->totalRight,
  51 + 'avail' => $this->avail,
  52 + 'frozen' => $this->frozen,
  53 + 'upl' => $this->upl,
  54 + ];
  55 + }
  56 +}