Order.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?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;
}
public 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;
}
if ($this->ordType == "ONLYMAKER") {
$ordType = "LIMIT";
$timeInForce = "GTX";
}
$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;
}
public function toBybitOrder(array $symbolInfos, callable $toSymbolOri)
{
$symbol = call_user_func($toSymbolOri, $this->symbol);
/** @var SymbolInfo $symbolInfo */
$symbolInfo = $symbolInfos[$this->symbol];
$order = [
'category' => 'linear',
'symbol' => $symbol,
'qty' => (string)round($this->qty, $symbolInfo->qtyPrec),
'price' => (string)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";
$order['orderType'] = "Limit";
}
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;
}
public function toBitgetOrder(array $symbolInfos, callable $toSymbolOri)
{
$symbol = call_user_func($toSymbolOri, $this->symbol);
/** @var SymbolInfo $symbolInfo */
$symbolInfo = $symbolInfos[$this->symbol];
$tradeSide = "open";
$side = $this->side;
if ($this->posSide == "SHORT" && $this->side == "BUY") {
$tradeSide = "close";
$side = "SELL";
}
if ($this->posSide == "LONG" && $this->side == "SELL") {
$tradeSide = "close";
$side = "BUY";
}
$timeInForce = "GTC";
if ($this->ordType == "IOC") {
$timeInForce = "IOC";
$ordType = "LIMIT";
}
if ($this->ordType == "FOK") {
$timeInForce = "FOK";
$ordType = "LIMIT";
}
$order = [
'productType' => 'USDT-FUTURES',
'marginMode' => 'crossed',
'marginCoin' => 'USDT',
'symbol' => $symbol,
'side' => strtolower($side),
'orderType' => strtolower($this->ordType),
'price' => (string)round($this->price, $symbolInfo->pricePrec),
'size' => (string)round($this->qty, $symbolInfo->qtyPrec),
'tradeSide' => $tradeSide,
'force' => $timeInForce,
'clientOid' => $this->cliOrdId,
];
return $order;
}
}