Pos.php
2.4 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
<?php
namespace trader\struct;
require_once __DIR__ . '/WsDataPos.php';
use trader\struct\WsDataPos;
class Pos
{
public $symbol;
public $posSide;
public $qty;
public $lot;
public $avgPrice;
public $pnl;
public $lever;
public $margin;
public function __construct($symbol, $posSide, $qty, $lot, $avgPrice, $pnl, $lever, $margin)
{
$this->symbol = $symbol;
$this->posSide = $posSide;
$this->qty = $qty;
$this->lot = $lot;
$this->avgPrice = $avgPrice;
$this->pnl = $pnl;
$this->lever = $lever;
$this->margin = $margin;
}
public function toArray()
{
return [
'symbol' => $this->symbol,
'posSide' => $this->posSide,
'qty' => $this->qty,
'lot' => $this->lot,
'avgPrice' => $this->avgPrice,
'pnl' => $this->pnl,
'lever' => $this->lever,
'margin' => $this->margin,
];
}
public static function transferOkxPos($pos, $symbolInfos, callable $toSymbolSt)
{
$symbol = call_user_func($toSymbolSt, $pos['instId']);
/** @var SymbolInfo $symbolInfo */
$symbolInfo = $symbolInfos[$symbol];
$posSide = strtoupper($pos['posSide']);
$lot = abs($pos['pos']);
$qty = round($lot * $symbolInfo->ctVal, $symbolInfo->qtyPrec);
$avgPrice = $pos['avgPx'];
$pnl = $pos['upl'];
$lever = $pos['lever'];
$margin = $pos['imr'];
return new Pos($symbol, $posSide, $qty, $lot, $avgPrice, $pnl, $lever, $margin);
}
public static function transferWsDataPos(WsDataPos $wsDataPos)
{
return new Pos($wsDataPos->symbol, $wsDataPos->posSide, $wsDataPos->qty, $wsDataPos->lot, $wsDataPos->avgPrice, $wsDataPos->pnl, 0, 0);
}
public static function transferBinancePos($pos, $symbolInfos, callable $toSymbolSt)
{
$symbol = call_user_func($toSymbolSt, $pos['symbol']);
/** @var SymbolInfo $symbolInfo */
$symbolInfo = $symbolInfos[$symbol];
$posSide = strtoupper($pos['positionSide']);
$qty = abs($pos['positionAmt']);
$lot = round($qty / $symbolInfo->ctVal, $symbolInfo->qtyPrec);
$avgPrice = -1;
$pnl = $pos['unrealizedProfit'];
$lever = -1;
$margin = $pos['initialMargin'];
return new Pos($symbol, $posSide, $qty, $lot, $avgPrice, $pnl, $lever, $margin);
}
}