WsDataOrder.php
3.6 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
<?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::getStatus($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 getStatus(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';
}
}