-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
192 lines (149 loc) · 6.55 KB
/
functions.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
<?php
function display ($a){
echo '<pre>';
print_r($a);
echo '</pre>';
}
function display_order ($tab, $pair){
date_default_timezone_set("Europe/Paris");
echo "<table>
<thead>
<tr>
<th>Transaction Time</th>
<th>Pair</th>
<th>Side</th>
<th>Order Status</th>
<th>Order ID</th>
<th>Quantity Requested</th>
<th>Price Executed</th>
<th>Quantity Bought</th>
<th>BTC Transacted</th>
</tr></thead>";
echo "<tr><td>";
echo date('d-m-Y H:i:s.u', round($tab[$pair]['time']/1000)); // Transaction time
echo "</td><td>";
echo $pair; // Pair
echo "</td><td>";
echo $tab[$pair]['side']; // Side
echo "</td><td>";
echo $tab[$pair]['status']; // Order status
echo "</td><td>";
echo $tab[$pair]['orderid']; // Order ID
echo "</td><td>";
echo $tab[$pair]['quantity']; // Quantity requested
echo "</td><td>";
echo $tab[$pair]['price_exec']; // Price executed
echo "</td><td>";
echo $tab[$pair]['quantity_exec']; // Quantity executed
echo "</td><td>";
echo $tab[$pair]['btc_exec']; // BTC Transacted
echo "</td></tr></table>";
}
function export_order ($tab, $pair) {
$myfile = fopen("order_logs.txt", "a+");
fwrite($myfile,
date('d-m-Y H:i:s.u', round($tab[$pair]['time']/1000)).','. // Transaction time
$pair.', '.
$tab[$pair]['side'].','.
$tab[$pair]['status'].','.
$tab[$pair]['orderid'].','.
$tab[$pair]['quantity'].','.
$tab[$pair]['price_exec'].','. // Price executed
$tab[$pair]['quantity_exec'].','.
$tab[$pair]['btc_exec'].','."\n"
);
}
function market_buy ($pair, $quantity) {
$endpoint = '/api/v3/order';
$timestamp = round(microtime(true)*1000);
$parameters = [
'symbol' => $pair,
'side' => 'BUY',
'type' => 'MARKET',
'quoteOrderQty' => $quantity,
'timestamp' => $timestamp
];
$qs = http_build_query($parameters); // query string encode the parameters
$sig = hash_hmac('sha256',$qs,secret); // sign the parameters
$request = URL.$endpoint."?".$qs."&signature=".$sig; // create the API request
$curl = curl_init(); // Get cURL resource
curl_setopt_array($curl, array( // Set cURL options
CURLOPT_URL => $request, // set the request URL
CURLOPT_POST => true, // POST request
CURLOPT_HTTPHEADER => header, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = curl_exec($curl); // Send the request, save the response
$result = json_decode($response, true);
curl_close($curl); // Close request
return $result;
}
function market_sell ($pair, $quantity) {
$endpoint = '/api/v3/order';
$timestamp = round(microtime(true)*1000);
$parameters = [
'symbol' => $pair,
'side' => 'SELL',
'type' => 'MARKET',
'quantity' => $quantity,
'timestamp' => $timestamp
];
$qs = http_build_query($parameters); // query string encode the parameters
$sig = hash_hmac('sha256',$qs,secret); // sign the parameters
$request = URL.$endpoint."?".$qs."&signature=".$sig; // create the API request
$curl = curl_init(); // Get cURL resource
curl_setopt_array($curl, array( // Set cURL options
CURLOPT_URL => $request, // set the request URL
CURLOPT_POST => true, // POST request
CURLOPT_HTTPHEADER => header, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = curl_exec($curl); // Send the request, save the response
$result = json_decode($response, true);
curl_close($curl); // Close request
return $result;
}
function trade_history ($pair, $starttime, $endtime) {
$endpoint = '/api/v3/aggTrades';
$timestamp = round(microtime(true)*1000);
$parameters = [
'symbol' => $pair,
'startTime' => $starttime,
'endTime' => $endtime,
'limit' => 1000
];
$qs = http_build_query($parameters); // query string encode the parameters
$request = URL.$endpoint."?".$qs; // create the API request
$curl = curl_init(); // Get cURL resource
curl_setopt_array($curl, array( // Set cURL options
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => header, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = curl_exec($curl); // Send the request, save the response
$result = json_decode($response, true);
curl_close($curl); // Close request
return $result;
}
function time_now () {
$timenow = DateTime::createFromFormat('U.u', microtime(true));
return ($timenow->format("d-m-Y H:i:s.u"));
}
function binance_info ($pair) {
$endpoint = '/api/v3/exchangeInfo';
$parameters = [
'symbol' => $pair
];
$qs = http_build_query($parameters); // query string encode the parameters
$request = URL.$endpoint."?".$qs; // create the API request
$curl = curl_init(); // Get cURL resource
curl_setopt_array($curl, array( // Set cURL options
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => header, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = curl_exec($curl); // Send the request, save the response
$result = json_decode($response, true);
return $result;
}
?>