ExBroker.php
4.3 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
<?php
namespace trader\exchange\binance;
require_once __DIR__ . '/../../struct/ApiInfo.php';
require_once __DIR__ . '/Api.php';
require_once __DIR__ . '/../../../jytools/Websocket.php';
require_once __DIR__ . '/../../../jytools/func.php';
use trader\struct\ApiInfo;
use trader\exchange\binance\Api as BnApi;
use jytools\Websocket;
use function jytools\output;
class ExBroker
{
private $host = 'wss://fstream.binance.com';
private $path = '/stream';
private ApiInfo $apiInfo;
private BnApi $api;
private ?Websocket $wsAcc;
private ?Websocket $wsKline;
public function __construct(ApiInfo $apiInfo)
{
$this->apiInfo = $apiInfo;
$this->api = new BnApi($apiInfo);
}
public function setWsHost($host)
{
$this->host = $host;
}
public function setRestHost($host)
{
$this->api->setHost($host);
}
public function accListen(callable $onData)
{
$listeneKey = $this->getListenKey();
$this->wsAcc = new Websocket($this->host . $this->path);
$this->wsAcc->connect(
$onOpen = function () use ($listeneKey) {
$subData = json_encode(['method' => 'SUBSCRIBE', 'params' => [$listeneKey], 'id' => 1]);
$this->wsAcc->push($subData);
},
$onMessage = function ($data) use ($onData) {
$data = json_decode($data, true);
if (!$data) {
return;
}
$onData($data);
},
$onClose = null,
);
}
public function klineListen($symbol, $interval, callable $onData)
{
$this->wsKline = new Websocket($this->host . $this->path);
$this->wsKline->connect(
$onOpen = function () use ($symbol, $interval) {
$subData = json_encode(['method' => 'SUBSCRIBE', 'params' => [strtolower($symbol) . '@kline_' . $interval], 'id' => 1]);
var_dump($subData);
$this->wsKline->push($subData);
},
$onMessage = function ($data) use ($onData) {
$data = json_decode($data, true);
if (!$data || !isset($data['data'])) {
return;
}
$onData($data['data']);
},
$onClose = null,
);
}
private function getListenKey()
{
$res = $this->api->getListenKey([]);
return $res['listenKey'];
}
//获取所有品种资金费率
public function getAllPremium()
{
$res = $this->api->getPremiumIndex([]);
}
//设置双向持仓模式
public function setLongShortMode($isLongShortMode)
{
if ($isLongShortMode) {
$res = $this->api->setPositionMode(['dualSidePosition' => 'true']);
} else {
$res = $this->api->setPositionMode(['dualSidePosition' => 'false']);
}
return $res;
}
//获取所有账户杠杆
public function getAllLevers()
{
$res = $this->api->getPositionRiskV2([]);
$retArr = [];
foreach ($res as $v) {
$retArr[$v['symbol']] = $v['leverage'];
}
return $retArr;
}
public function stopListen()
{
if (isset($this->wsAcc)) {
$this->wsAcc->close();
}
if (isset($this->wsKline)) {
$this->wsKline->close();
}
}
public function getSymbolInfos()
{
$res = $this->api->getExchangeInfo([]);
if (!isset($res['symbols'])) {
output('okx获取所有交易对信息失败');
return [];
}
return $res['symbols'];
}
public function placeOrder(array $params)
{
return $this->api->placeOrder($params);
}
public function setLever($symbol, $lever)
{
return $this->api->setLever(['symbol' => $symbol, 'leverage' => $lever]);
}
public function getAllPos()
{
$newPositions = [];
$res = $this->api->accountV3([]);
if ($res && isset($res['positions'])) {
$positions = $res['positions'];
foreach ($positions as $key => $value) {
if ($value['positionAmt'] != 0) {
$newPositions[] = $value;
}
}
} else {
output($res);
}
return $newPositions;
}
}