Order.php
3.3 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
<?php
namespace trader\struct;
class Order
{
public string $symbol; //大写
public string $ordType; // 订单类型 LIMIT 限价 IOC 不成则撤 FOK 全部成交或立即取消 MARKET 市价 ONLYMAKER 仅做maker单
public string $posSide; //大写
public string $side; //大写
public float $price; //成交价格
public float $qty; //币种真实数量
private string $cliOrdId = ''; // 客户端订单号
public function __construct($symbol, $ordType, $posSide, $side, $price, $qty)
{
$this->symbol = $symbol;
$this->ordType = $ordType;
$this->posSide = $posSide;
$this->side = $side;
$this->price = $price;
$this->qty = $qty;
}
public function setCliOrdId($cliOrdId)
{
$this->cliOrdId = $cliOrdId;
}
public function getCliOrdId()
{
return $this->cliOrdId;
}
public function toArray()
{
$arr = [
'symbol' => $this->symbol,
'ordType' => $this->ordType,
'posSide' => $this->posSide,
'side' => $this->side,
'price' => $this->price,
'qty' => $this->qty,
];
if ($this->cliOrdId != "") {
$arr['cliOrdId'] = $this->cliOrdId;
}
return $arr;
}
public function toOkxOrder(array $symbolInfos, callable $toSymbolOri)
{
$instId = call_user_func($toSymbolOri, $this->symbol);
/** @var SymbolInfo $symbolInfo */
$symbolInfo = $symbolInfos[$this->symbol];
$order = [
'instId' => $instId,
'tdMode' => 'cross',
'posSide' => strtolower($this->posSide),
'side' => strtolower($this->side),
'ordType' => $this->ordType,
];
if ($this->ordType == 'LIMIT' || $this->ordType == 'IOC') {
$order['px'] = round($this->price, $symbolInfo->pricePrec);
}
$lot = $this->qty / $symbolInfo->ctVal;
$order['sz'] = round($lot, $symbolInfo->lotPrec);
if ($this->ordType == 'ONLYMAKER') {
$order['ordType'] = 'POST_ONLY';
}
$order['ordType'] = strtolower($order['ordType']);
if ($this->cliOrdId != "") {
$order['clOrdId'] = $this->cliOrdId;
}
return $order;
}
function toBinanceOrder(array $symbolInfos, callable $toSymbolOri)
{
$symbol = call_user_func($toSymbolOri, $this->symbol);
/** @var SymbolInfo $symbolInfo */
$symbolInfo = $symbolInfos[$this->symbol];
$ordType = $this->ordType;
$timeInForce = "GTC";
if ($this->ordType == "IOC" || $this->ordType == "FOK") {
$ordType = "LIMIT";
$timeInForce = $this->ordType;
}
$order = [
'symbol' => $symbol,
'positionSide' => $this->posSide,
'side' => $this->side,
'type' => $ordType,
'timeInForce' => $timeInForce,
'quantity' => round($this->qty, $symbolInfo->qtyPrec),
'price' => round($this->price, $symbolInfo->pricePrec),
];
if ($ordType == "MARKET") {
unset($order['price']);
unset($order['timeInForce']);
}
if ($this->cliOrdId != "") {
$order['newClientOrderId'] = $this->cliOrdId;
}
return $order;
}
}