WsDataOrder.php
6.2 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
<?php
namespace trader\struct;
require_once __DIR__ . '/SymbolInfo.php';
use trader\struct\SymbolInfo;
class WsDataOrder
{
public string $platform;
public string $posSide; //大写
public string $symbol; //大写
public string $side; //大写
public float $price; //下单价格
public float $avgPx; //成交均价
public float $qty; //币种真实数量
public float $lot; //合约张数
public float $pnl; //盈亏
public int $ts; //下单时间戳 ms
public int $uts; //更新时间戳 ms
public string $ordType; //下单类型
public string $cliOrdId; //客户端订单号
public string $ordId; //订单号
public string $status; //订单状态
public function __construct(string $platform, string $posSide, string $symbol, string $side, float $price, float $avgPx, float $qty, float $lot, float $pnl, int $ts, int $uts, string $ordType, string $cliOrdId, string $ordId, string $status)
{
$this->platform = $platform;
$this->posSide = $posSide;
$this->symbol = $symbol;
$this->side = $side;
$this->price = $price;
$this->avgPx = $avgPx;
$this->qty = $qty;
$this->lot = $lot;
$this->pnl = $pnl;
$this->ts = $ts;
$this->uts = $uts;
$this->ordType = $ordType;
$this->cliOrdId = $cliOrdId;
$this->ordId = $ordId;
$this->status = $status;
}
public function toArray()
{
return [
'platform' => $this->platform,
'posSide' => $this->posSide,
'symbol' => $this->symbol,
'side' => $this->side,
'price' => $this->price,
'avgPx' => $this->avgPx,
'qty' => $this->qty,
'lot' => $this->lot,
'pnl' => $this->pnl,
'ts' => $this->ts,
'uts' => $this->uts,
'ordType' => $this->ordType,
'cliOrdId' => $this->cliOrdId,
'ordId' => $this->ordId,
'status' => $this->status,
];
}
public static function TransferOkxOrder(array $data, array $symbolInfos, callable $toSymbolSt): WsDataOrder|null
{
$symbol = call_user_func($toSymbolSt, $data['instId']);
/** @var SymbolInfo $symbolInfo */
$symbolInfo = $symbolInfos[$symbol] ?? null;
if ($symbolInfo == null) {
return null;
}
$platform = 'okx';
$posSide = strtoupper($data['posSide']);
$side = strtoupper($data['side']);
$price = (float)$data['px'];
$avgPx = (float)$data['avgPx'];
$lot = (float)$data['sz'];
$qty = round($lot * $symbolInfo->ctVal, $symbolInfo->qtyPrec);
$pnl = (float)$data['pnl'];
$ts = (int)$data['cTime'];
$uts = (int)$data['uTime'];
$ordType = strtoupper($data['ordType']);
$cliOrdId = $data['clOrdId'];
$ordId = $data['ordId'];
$status = self::getOkStatus($data['state']);
if ($data['cancelSource'] != '') {
$status = 'CANCELED';
}
return new WsDataOrder($platform, $posSide, $symbol, $side, $price, $avgPx, $qty, $lot, $pnl, $ts, $uts, $ordType, $cliOrdId, $ordId, $status);
}
private static function getOkStatus(string $state)
{
if ($state == 'live') {
return 'LIVE';
}
if ($state == 'partially_filled') {
return 'PARTIALLY_FILLED';
}
if ($state == 'filled') {
return 'FILLED';
}
if ($state == 'canceled') {
return 'CANCELED';
}
return 'UNKNOWN';
}
public static function TransferBinanceOrder($data, $symbolInfos, callable $toSymbolSt): WsDataOrder|null
{
$order = $data['o'];
$symbol = call_user_func($toSymbolSt, $order['s']);
/** @var SymbolInfo $symbolInfo */
$symbolInfo = $symbolInfos[$symbol] ?? null;
if ($symbolInfo == null) {
return null;
}
$platform = 'binance';
$posSide = strtoupper($order['S']);
$side = strtoupper($order['ps']);
$price = (float)$order['p'];
$avgPx = (float)$order['ap'];
$lot = (float)$order['q'];
$qty = $lot;
$pnl = 0;
$ts = (int)$order['T'];
$uts = (int)$order['T'];
$ordType = strtoupper($order['o']);
$cliOrdId = $order['c'];
$ordId = $order['i'];
$status = self::getBnStatus($order['X']);
return new WsDataOrder($platform, $posSide, $symbol, $side, $price, $avgPx, $qty, $lot, $pnl, $ts, $uts, $ordType, $cliOrdId, $ordId, $status);
}
public static function TransferBybitOrder($data, $symbolInfos, callable $toSymbolSt): WsDataOrder|null
{
$order = $data['data'];
$symbol = call_user_func($toSymbolSt, $order['symbol']);
/** @var SymbolInfo $symbolInfo */
$symbolInfo = $symbolInfos[$symbol] ?? null;
if ($symbolInfo == null) {
return null;
}
$platform = 'bybit';
$posSide = strtoupper($order['side']);
$side = strtoupper($order['side']);
$price = (float)$order['price'];
$avgPx = (float)$order['avg_price'];
$lot = (float)$order['qty'];
$qty = $lot;
$pnl = (float)$order['realised_pnl'];
$ts = (int)$order['transact_time'];
$uts = (int)$order['transact_time'];
$ordType = strtoupper($order['order_type']);
$cliOrdId = $order['order_link_id'];
$ordId = $order['order_id'];
$status = self::getBnStatus($order['order_status']);
return new WsDataOrder($platform, $posSide, $symbol, $side, $price, $avgPx, $qty, $lot, $pnl, $ts, $uts, $ordType, $cliOrdId, $ordId, $status);
}
private static function getBnStatus(string $state)
{
if ($state == 'NEW') {
return 'LIVE';
}
if ($state == 'PARTIALLY_FILLED') {
return 'PARTIALLY_FILLED';
}
if ($state == 'FILLED') {
return 'FILLED';
}
if ($state == 'CANCELED') {
return 'CANCELED';
}
if ($state == 'EXPIRED' || $state == "EXPIRED_IN_MATCH") {
return 'CANCELED';
}
return 'UNKNOWN';
}
}