WsDataPos.php
1.7 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
<?php
namespace trader\struct;
require_once __DIR__ . '/SymbolInfo.php';
use trader\struct\SymbolInfo;
class WsDataPos
{
public string $symbol;
public string $posSide; //大写
public float $qty; //币种真实数量
public float $lot; //合约张数
public float $avgPrice; //平均持仓价
public bool $isSnapshot; //是否快照;
public float $pnl; //盈亏
public function __construct($symbol, $posSide, $qty, $lot, $avgPrice, $isSnapshot, $pnl)
{
$this->symbol = $symbol;
$this->posSide = $posSide;
$this->qty = $qty;
$this->lot = $lot;
$this->avgPrice = $avgPrice;
$this->isSnapshot = $isSnapshot;
$this->pnl = $pnl;
}
public function toArray()
{
return [
'symbol' => $this->symbol,
'posSide' => $this->posSide,
'qty' => $this->qty,
'lot' => $this->lot,
'avgPrice' => $this->avgPrice,
'isSnapshot' => $this->isSnapshot,
'pnl' => $this->pnl,
];
}
public static function TransferOkxPos(array $data, array $symbolInfos, callable $toSymbolSt): WsDataPos|null
{
$symbol = call_user_func($toSymbolSt, $data['instId']);
/** @var SymbolInfo $symbolInfo */
$symbolInfo = $symbolInfos[$symbol] ?? null;
if ($symbolInfo === null) {
return null;
}
$posSide = strtoupper($data['posSide']);
$lot = (float)$data['pos'];
$qty = round($lot * $symbolInfo->ctVal, $symbolInfo->qtyPrec);
$avgPrice = (float)$data['avgPx'];
$pnl = (float)$data['upl'];
return new WsDataPos($symbol, $posSide, $qty, $lot, $avgPrice, false, $pnl);
}
}