Order.php
4.5 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
<?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;
}
function toBybitOrder(array $symbolInfos, callable $toSymbolOri)
{
$symbol = call_user_func($toSymbolOri, $this->symbol);
/** @var SymbolInfo $symbolInfo */
$symbolInfo = $symbolInfos[$this->symbol];
$order = [
'symbol' => $symbol,
'qty' => round($this->qty, $symbolInfo->qtyPrec),
'price' => round($this->price, $symbolInfo->pricePrec),
'orderLinkId' => $this->cliOrdId,
];
if ($this->ordType == "MARKET") {
$order['orderType'] = "Market";
unset($order['price']);
}
if ($this->ordType == "LIMIT") {
$order['timeInForce'] = "GTC";
}
if ($this->ordType == "IOC") {
$order['timeInForce'] = "IOC";
$order['orderType'] = "Limit";
}
if ($this->side == "BUY") {
$order['side'] = "Buy";
} else {
$order['side'] = "Sell";
}
if ($this->posSide == "LONG") {
$order['positionIdx'] = "1";
} else if ($this->posSide == "SHORT") {
$order['positionIdx'] = "2";
} else {
$order['positionIdx'] = "0";
}
return $order;
}
}