WsDataTrade.php
6.1 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
<?php
namespace trader\struct;
require_once __DIR__ . '/SymbolInfo.php';
use trader\struct\SymbolInfo;
class WsDataTrade
{
public string $platform;
public string $posSide; //大写
public string $symbol; //大写
public string $side; //大写
public float $price; //成交价格
public float $qty; //币种真实数量
public float $lot; //合约张数
public float $pnl; //盈亏
public float $fee; //手续费
public float $quoteVol; //计价货币数量 成交额
public int $ts; //时间戳 ms
public string $tradeId;
public string $ordId;
public string $cliOrdId;
public int $lever;
public function __construct($platform, $posSide, $symbol, $side, $price, $qty, $lot, $pnl, $fee, $quoteVol, $ts, $tradeId, $ordId, $cliOrdId, $lever)
{
$this->platform = $platform;
$this->posSide = $posSide;
$this->symbol = $symbol;
$this->side = $side;
$this->price = $price;
$this->qty = $qty;
$this->lot = $lot;
$this->pnl = $pnl;
$this->fee = $fee;
$this->quoteVol = $quoteVol;
$this->ts = $ts;
$this->tradeId = $tradeId;
$this->ordId = $ordId;
$this->cliOrdId = $cliOrdId;
$this->lever = $lever;
}
public function toArray()
{
return [
'platform' => $this->platform,
'posSide' => $this->posSide,
'symbol' => $this->symbol,
'side' => $this->side,
'price' => $this->price,
'qty' => $this->qty,
'lot' => $this->lot,
'pnl' => $this->pnl,
'fee' => $this->fee,
'quoteVol' => $this->quoteVol,
'ts' => $this->ts,
'tradeId' => $this->tradeId,
'ordId' => $this->ordId,
'cliOrdId' => $this->cliOrdId,
'lever' => $this->lever,
];
}
public static function TransferOkxOrder(array $data, array $symbolInfos, callable $toSymbolSt): WsDataTrade|null
{
if ($data['fillSz'] != "" && $data['fillSz'] != "0") {
$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['fillPx'];
$lot = (float)$data['fillSz'];
$qty = round($lot * $symbolInfo->ctVal, $symbolInfo->qtyPrec);
$pnl = (float)$data['fillPnl'];
$fee = (float)$data['fillFee'];
$quoteVol = $price * $qty;
$ts = $data['uTime'];
$tradeId = $data['tradeId'];
$lever = (int)$data['lever'];
$ordId = $data['ordId'];
$cliOrdId = $data['clOrdId'];
return new WsDataTrade($platform, $posSide, $symbol, $side, $price, $qty, $lot, $pnl, $fee, $quoteVol, $ts, $tradeId, $ordId, $cliOrdId, $lever);
}
return null;
}
public static function TransferBinanceTrade($data, $symbolInfos, callable $toSymbolSt): WsDataTrade|null
{
$platform = 'binance';
$order = $data['o'];
$symbol = call_user_func($toSymbolSt, $order['s']);
$posSide = strtoupper($order['ps']);
$side = strtoupper($order['S']);
$price = (float)$order['L'];
$qty = (float)$order['l'];
$lot = $qty;
$pnl = (float)$order['rp'];
$fee = (float)$order['n'];
if ($order['N'] == "BNB") {
$fee = $fee * 600;
}
$quoteVol = $price * $qty;
$ts = $order['T'];
$tradeId = $order['t'];
$ordId = $order['i'];
$cliOrdId = $order['c'];
$lever = 0;
return new WsDataTrade($platform, $posSide, $symbol, $side, $price, $qty, $lot, $pnl, $fee, $quoteVol, $ts, $tradeId, $ordId, $cliOrdId, $lever);
}
public static function TransferBybitTrade($data, $symbolInfos, callable $toSymbolSt): WsDataTrade|null
{
$platform = 'bybit';
$symbol = call_user_func($toSymbolSt, $data['symbol']);
$posSide = "";
$side = strtoupper($data['side']);
$price = (float)$data['execPrice'];
$qty = (float)$data['execQty'];
$lot = $qty;
$pnl = (float)$data['execPnl'];
if (($pnl == 0 && $side == "BUY") || ($pnl != 0 && $side == "SELL")) {
$posSide = "LONG";
}
if (($pnl == 0 && $side == "SELL") || ($pnl != 0 && $side == "BUY")) {
$posSide = "SHORT";
}
$fee = (float)$data['execFee'];
$quoteVol = $price * $qty;
$ts = $data['execTime'];
$tradeId = $data['execId'];
$ordId = $data['orderId'];
$cliOrdId = $data['orderLinkId'];
$lever = 0;
return new WsDataTrade($platform, $posSide, $symbol, $side, $price, $qty, $lot, $pnl, $fee, $quoteVol, $ts, $tradeId, $ordId, $cliOrdId, $lever);
}
public static function TransferBitgetOrder(array $data, array $symbolInfos, callable $toSymbolSt): WsDataTrade|null
{
$symbol = call_user_func($toSymbolSt, $data['symbol']);
/** @var SymbolInfo $symbolInfo */
$symbolInfo = $symbolInfos[$symbol] ?? null;
if ($symbolInfo === null) {
return null;
}
$plat = 'bitget';
$side = strtoupper($data['side']);
$price = (float)$data['price'];
$qty = (float)$data['baseVolume'];
$lot = $qty;
$fee = (float)$data['feeDetail'][0]['totalFee'];
$pnl = (float)$data['profit'];
if (($pnl == 0 && $side == "BUY") || ($pnl != 0 && $side == "SELL")) {
$posSide = "LONG";
} else {
$posSide = "SHORT";
}
$quoteVol = (float)$data['quoteVolume'];
$ts = $data['uTime'];
$tradeId = $data['tradeId'];
$ordId = $data['orderId'];
$cliOrdId = $data['clientOid'];
$lever = 0;
return new WsDataTrade($plat, $posSide, $symbol, $side, $price, $qty, $lot, $pnl, $fee, $quoteVol, $ts, $tradeId, $ordId, $cliOrdId, $lever);
}
}