-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunc_order.php
274 lines (220 loc) · 9.01 KB
/
func_order.php
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/**
* @author Ebo Eppenga
* @copyright 2023
*
* GoldStar Buy and Sell bot based on signals from for example TradeView
* or any other platform using PHP Bybit API from zhouaini528.
*
* func_order.php
* Trade functions required for GoldStar to run properly.
*
**/
/** Get price from a pair **/
function getPrice($symbol) {
// Declare some variables as global
global $api, $id;
// Get price for pair
try {
$result = $api->market()->getTickers([
'category' => 'spot',
'symbol' => $symbol
]);
} catch (\Exception $e) {
$message = $e->getMessage();
doError($message, $id, true);
}
logAPI($result, "getPrice");
// Get actual price
$price = $result["result"]["list"][0]["lastPrice"];
// Check if we have a price
if ($price == 0) {
$message = "Error: Price not found for pair " . $symbol;
doError($message, $id, true);
}
return $price;
}
/** Extract data from exchange order **/
function getOrder($orderId) {
// Declare some variables as global
global $api, $set_coin, $id, $debug;
// Get Order data for order ID, first via realtime query
try {
$order = $api->order()->getRealTime([
'category' => 'spot',
'orderId' => $orderId
]);
} catch (\Exception $e) {
$message = $e->getMessage();
doError($message, $id, true);
}
logAPI($order, "getOrder");
// Check if we have a valid result, if not query via history
if (empty($order['result']['list'])) {
if ($debug) {echo "Order ID not found via getRealTime, now trying getHistory!<br />";}
try {
$order = $api->order()->getHistory([
'category' => 'spot',
'orderId' => $orderId
]);
} catch (\Exception $e) {
$message = $e->getMessage();
doError($message, $id, true);
}
logAPI($order, "getOrder");
}
if ($debug) {
echo "<b>Result of getOrder with ID: $orderId</b><br />";
print_r($order);
}
// Write up order data
$transaction = [
'createdTime' => $order['result']['list'][0]['createdTime'], // Order created timestamp (ms)
'updatedTime' => $order['result']['list'][0]['updatedTime'], // Order updated timestamp (ms)
'orderId' => $order['result']['list'][0]['orderId'], // Order ID
'orderLinkId' => '0', // Order LinkId
'symbol' => $order['result']['list'][0]['symbol'], // Symbol name
'side' => $order['result']['list'][0]['side'], // Side. Buy,Sell
'orderType' => $order['result']['list'][0]['orderType'], // Order type. Market,Limit.
'orderStatus' => $order['result']['list'][0]['orderStatus'], // Order status
'price' => $order['result']['list'][0]['price'], // Order price
'avgPrice' => $order['result']['list'][0]['avgPrice'], // Average filled price. If unfilled, it is "0"
'qty' => $order['result']['list'][0]['qty'], // Order quantity
'cumExecQty' => $order['result']['list'][0]['cumExecQty'], // Cumulative executed order quantity
'cumExecValue' => $order['result']['list'][0]['cumExecValue'], // Cumulative executed order value (executed total buy / sell, should equal cumExecQty * avgPrice ).
'cumExecFee' => $order['result']['list'][0]['cumExecFee'] // Cumulative executed trading fee.
];
// Set price to order price when market order, personally I find this an error of the exchange *** CHECK ***
if ($transaction['orderType'] == 'Market') {
$transaction['price'] = $set_coin['price']; // Order price
}
return $transaction;
}
/** Posts an order to the exchange **/
function postOrder($pair, $side, $type, $quantity, $price, $link) {
// Declare $api as global
global $api, $set_coin, $id, $debug;
// Fix decimals in order
if ($side == "Buy") {
$quantity = roundStep($quantity, $set_coin['quotePrecision']);
} else {
$quantity = roundStep($quantity, $set_coin['basePrecision']);
}
$price = roundStep($price, $set_coin['tickSize']);
// Post order
try {
$result = $api->order()->postCreate([
'category' =>'spot',
'symbol' => $pair,
'side' => $side,
'orderType' => $type,
'qty' => strval($quantity),
'price' => strval($price)
]);
} catch (\Exception $e) {
$message = $e->getMessage();
doError($message, $id, true);
}
logAPI($result, "postOrder");
if ($debug) {
echo "<b>Result of postOrder</b><br />";
print_r($result);
}
// Check if we have valid order ID
$orderId = $result['result']['orderId'];
if (empty($orderId)) {
doError("Error: postOrder did not work out.", $id, true);
}
// Get order data and give it 0.5 seconds when it's a market order to fill
if ($side == "Market") {usleep(500000);}
$order = getOrder($orderId);
// Link matching BUY order to LIMIT order
$order['orderLinkId'] = $link;
return $order;
}
/** Log revenue or posted orders **/
function logRevenue($order) {
// Declare $price and $id as global
global $price, $id;
// Only on filled orders
if (strpos($order['orderStatus'], "Filled") !== false) {
// Split fee and order
$qty_fee = $order['cumExecFee'] * -1;
$qty_order = $order['cumExecValue'];
// Convert to quote asset
if ($order['side'] == "Buy") {
$qty_order = $qty_order * -1;
$qty_fee = $qty_fee * $price;
}
//revenue : createdTime,updatedTime,orderId,orderLinkId,symbol,side,orderType,orderStatus,type,qty | qty is always in quote asset, type is Order or Feee
$log_order = $order['createdTime'] . "," . $order['updatedTime'] . "," . $order['orderId'] . "," . $order['orderLinkId'] . "," . $order['symbol'] . "," . $order['side'] . "," . $order['orderType'] . "," . $order['orderStatus'] . ",Order," . $qty_order;
$log_fee = $order['createdTime'] . "," . $order['updatedTime'] . "," . $order['orderId'] . "," . $order['orderLinkId'] . "," . $order['symbol'] . "," . $order['side'] . "," . $order['orderType'] . "," . $order['orderStatus'] . ",Fee," . $qty_fee;
// Log to files
logCommand($log_order, "profit");
logCommand($log_fee, "profit");
}
return;
}
/** Show order status **/
function showOrder($order) {
// Declare $set_coin as global
global $set_coin;
// What data to display
$showFilled = false;
if (strpos($order['orderStatus'], "Filled") !== false) {
$showFilled = true;
}
/*
0. 'createdTime' // Order created timestamp (ms)
1. 'updatedTime' // Order updated timestamp (ms)
2. 'orderId' // Order ID
3. 'orderLinkId' // LIMIT Order linked to BUY Order
4. 'symbol' // Symbol name
5. 'side' // Side Buy, Sell
6. 'orderType' // Order type. Market, Limit.
7. 'orderStatus' // Order status. Filled, PatrtiallyFilled, New.
8. 'price' // Order price
9. 'avgPrice' // Average filled price. If unfilled, it is "0"
10. 'qty' // Order quantity
11. 'cumExecQty' // Cumulative executed order quantity
12. 'cumExecValue' // Cumulative executed order value (executed total buy / sell, should equal cumExecQty * avgPrice ).
13. 'cumExecFee' // Cumulative executed trading fee.
*/
echo "Created Time : " . $order['createdTime'] . "<br />";
echo "Updated Time : " . $order['updatedTime'] . "<br /><br />";
echo "Order ID : " . $order['orderId'] . "<br />";
if ($order['orderLinkId'] <> 0) {
echo "Order Linked ID : " . $order['orderLinkId'] . "<br />";
}
echo "Symbol : " . $order['symbol'] . "<br />";
echo "Side : " . $order['side'] . "<br />";
echo "Order Type : " . $order['orderType'] . "<br />";
echo "Order Status : " . $order['orderStatus'] . "<br /><br >";
echo "Price : " . $order['price'] . " " . $set_coin['quoteAsset'] . "<br />";
if ($showFilled) {
echo "Avg. Price Filled : " . $order['avgPrice'] . " " . $set_coin['quoteAsset'] . "<br />";
}
if ($order['side'] == 'Buy' && $order['orderType'] == 'Market') {
echo "Quantity : " . $order['qty'] . " " . $set_coin['quoteAsset'] . "<br />";
} else {
echo "Quantity : " . $order['qty'] . " " . $set_coin['baseAsset'] . "<br />";
echo "Value : " . ($order['price'] * $order['qty']) . " " . $set_coin['quoteAsset'] . "<br />";
}
if ($showFilled) {
echo "Cum. Exec. Quantity: " . $order['cumExecQty'] . " " . $set_coin['baseAsset'] . "<br />";
echo "Cum. Exec. Value : " . $order['cumExecValue'] . " " . $set_coin['quoteAsset'] . "<br />";
if ($order['side'] == 'Buy') {
echo "Cum. Exec. Fee : " . $order['cumExecFee'] . " " . $set_coin['baseAsset'] . "<br />";
} else {
echo "Cum. Exec. Fee : " . $order['cumExecFee'] . " " . $set_coin['quoteAsset'] . "<br />";
}
}
echo "<br />";
}
/** Round value to the nearest stepSize **/
function roundStep($value, $stepSize = 0.1) {
$factor = 1 / $stepSize;
$value = floor($value * $factor) / $factor;
return $value;
}
?>