CmBroker.php
7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
namespace trader;
require_once __DIR__ . '/struct/ApiInfo.php';
require_once __DIR__ . '/exchange/okx/ExBroker.php';
require_once __DIR__ . '/exchange/binance/ExBroker.php';
require_once __DIR__ . '/struct/Kline.php';
require_once __DIR__ . '/struct/Order.php';
require_once __DIR__ . '/struct/WsData.php';
require_once __DIR__ . '/struct/WsDataTrade.php';
require_once __DIR__ . '/struct/WsDataPos.php';
require_once __DIR__ . '/struct/SymbolInfo.php';
require_once __DIR__ . '/struct/WsDataOrder.php';
require_once __DIR__ . '/struct/WsDataAccount.php';
require_once __DIR__ . '/struct/Pos.php';
require_once __DIR__ . '/../jytools/func.php';
require_once __DIR__ . '/../jytools/Websocket.php';
use trader\struct\ApiInfo;
use trader\okx\ExBroker as OkxBroker;
use trader\binance\ExBroker as BinanceBroker;
use trader\struct\Kline;
use trader\struct\Order;
use trader\struct\WsData;
use trader\struct\WsDataTrade;
use trader\struct\WsDataPos;
use trader\struct\SymbolInfo;
use trader\struct\WsDataOrder;
use trader\struct\WsDataAccount;
use trader\struct\Pos;
use \Exception;
use function jytools\timeFormat;
class CmBroker
{
private $plat;
private ?OkxBroker $exBroker;
/** @var SymbolInfo[] $symbolInfos */
public $symbolInfos = [];
/** @var Pos[] $positions */
private $positions = [];
private $name = '';
public function __construct($plat, ApiInfo $apiInfo)
{
$this->plat = $plat;
$exBroker = null;
if ($plat == 'okx') {
$exBroker = new OkxBroker($apiInfo);
}
if ($plat == 'binance') {
$exBroker = new BinanceBroker($apiInfo);
}
$this->exBroker = $exBroker;
}
public function setName($name)
{
$this->name = $name;
}
public function init()
{
$this->initSymbolInfos();
}
public function accListen(callable $onData)
{
$this->exBroker->accListen(function ($data) use ($onData) {
// output("ws 有效数据", $data);
if ($this->plat == 'binance') {
$this->msg("binance 无处理ws数据实现");
}
if ($this->plat == 'okx') {
if (isset($data['arg']) && $data['arg']['channel'] == 'orders') {
foreach ($data['data'] as $key => $value) {
$wsDataTrade = WsDataTrade::TransferOkxOrder($value, $this->symbolInfos, function ($symbol) {
return $this->getSymbolSt($symbol);
});
if ($wsDataTrade != null) {
$wsData = new WsData($this->plat, 'trade', $trade = $wsDataTrade);
$onData($wsData);
}
$wsDataOrd = WsDataOrder::TransferOkxOrder($value, $this->symbolInfos, function ($symbol) {
return $this->getSymbolSt($symbol);
});
if ($wsDataOrd != null) {
$wsData = new WsData($this->plat, 'order', $trade = null, $pos = null, $order = $wsDataOrd);
$onData($wsData);
}
}
return;
}
if (isset($data['arg']) && $data['arg']['channel'] == 'positions') {
$positions = [];
foreach ($data['data'] as $key => $value) {
$wsDataPos = WsDataPos::TransferOkxPos($value, $this->symbolInfos, function ($symbol) {
return $this->getSymbolSt($symbol);
});
if ($wsDataPos) {
$pos = Pos::transferWsDataPos($wsDataPos);
$positions[$wsDataPos->symbol . "_" . $wsDataPos->posSide] = $pos;
$wsData = new WsData($this->plat, 'pos', $trade = null, $pos = $wsDataPos);
$onData($wsData);
}
}
$this->positions = $positions;
return;
}
if (isset($data['arg']) && $data['arg']['channel'] == 'account') {
$wsDataAccount = WsDataAccount::TransferOkxAccount($data['data']);
$wsData = new WsData($this->plat, 'account', $trade = null, $pos = null, $order = null, $account = $wsDataAccount);
$onData($wsData);
return;
}
$this->msg("okx 无处理ws数据实现", $data);
}
});
}
public function klineListen($symbol, $peroid, $onData)
{
$symbol = $this->getSymbolOri($symbol, $this->plat);
$this->exBroker->klineListen($symbol, $peroid, function ($data) use ($onData) {
$kline = Kline::transferOkx($data);
$onData($kline);
});
}
//转换为标准交易对
public function getSymbolSt($symbol)
{
$symbol = str_replace('-USDT-SWAP', 'USDT', $symbol);
if (preg_match('/^[A-Z0-9]+USDT$/', $symbol)) {
return $symbol;
} else {
throw new Exception('转换标准交易对错误' + $symbol + ' to ', $symbol);
}
}
//转换为原始交易对
public function getSymbolOri($symbol, $platTarget)
{
$symbolSt = $this->getSymbolSt($symbol);
if ($platTarget == 'binance') {
return $symbolSt;
}
if ($platTarget == 'okx') {
return str_replace('USDT', '-USDT-SWAP', $symbolSt);
}
throw new Exception('平台错误' + $platTarget);
}
public function placeOrder(Order $order)
{
if ($this->plat == 'okx') {
$orderOri = $order->toOkxOrder($this->symbolInfos, function ($symbol) {
return $this->getSymbolOri($symbol, $this->plat);
});
$this->msg("下单", $orderOri);
if ($orderOri['sz'] == 0) {
$this->msg("下单数量为0,不下单", $orderOri);
return ["code" => 1, "msg" => "下单数量为0,不下单"];
}
$res = $this->exBroker->placeOrder($orderOri);
$this->msg("下单结果", $res);
return $res;
}
}
public function msg()
{
$args = func_get_args();
$outStr = '[' . timeFormat('ms') . ']:' . $this->name . ": ";
foreach ($args as $key => $value) {
$value = is_array($value) ? json_encode($value) : $value;
if (is_bool($value)) {
$value = $value ? 'bool:true' : 'bool:false';
}
$outStr .= count($args) - $key > 1 ? $value . ' ' : $value;
}
echo $outStr . PHP_EOL;
}
public function getPosQty($symbol, $posSide)
{
$key = $symbol . "_" . $posSide;
if (!isset($this->positions[$key])) {
return 0;
}
/** @var Pos $pos */
$pos = $this->positions[$key];
return abs($pos->qty);
}
private function initSymbolInfos()
{
if ($this->plat == 'okx') {
//获取所有USDT SWAP 交易对
$res = $this->exBroker->getSymbolInfos();
$infos = [];
foreach ($res as $key => $value) {
if ($value["settleCcy"] == "USDT") {
$info = SymbolInfo::transferOkx($value, function ($symbol) {
return $this->getSymbolSt($symbol);
});
$infos[$info->symbol] = $info;
}
}
$this->symbolInfos = $infos;
}
//定时10m刷新
swoole_timer_after(1000 * 60 * 10, function () {
$this->initSymbolInfos();
});
}
public function stopListen()
{
$this->exBroker->stopListen();
}
public function closeAllPos()
{
$this->exBroker->closeAllPos();
}
}