· 6 years ago · Nov 26, 2019, 11:42 AM
1#!/usr/bin/php -q
2<?php
3error_reporting(E_ERROR | E_WARNING | E_PARSE);
4
5//$shouldibuyZYN_ETH = shouldibuyZYN_ETH($getBalance);
6//$shouldisellZYN_ETH = shouldisellZYN_ETH($getBalance);
7//$CheckCurrentOrder = CheckCurrentOrder($timestamp);
8
9
10
11//curl_setopt($GLOBALS["ch"], CURLOPT_ENCODING, "gzip,deflate");
12 $GLOBALS["ch"] = curl_init();
13 curl_setopt($GLOBALS["ch"], CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
14curl_setopt($GLOBALS["ch"], CURLOPT_RETURNTRANSFER, true);
15echo "Recuperation du TimeStamp\n";
16$timestamp = getTimeStamp();
17curl_close($GLOBALS["ch"]);
18
19$i=0;
20while (true)
21{
22 $GLOBALS["ch"] = curl_init();
23 curl_setopt($GLOBALS["ch"], CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
24curl_setopt($GLOBALS["ch"], CURLOPT_RETURNTRANSFER, true);
25
26 sleep(1);
27 if ($i > 10)
28 {
29 $timestamp = getTimeStamp();//1
30 $i=0;
31 }
32 $i++;
33 echo "\n\nANALYSE DU MARCHé --------------------------------------------\n\n";
34 curl_setopt($GLOBALS["ch"], CURLOPT_HTTPGET, true);
35 echo "Recuperation de la balance\n";
36 $getBalance = getBalance($timestamp); //1
37 echo "Recuperation de l'orderbook\n";
38 $orderbook = orderbook(); //5
39
40 //function pour instant Buy and sell
41 echo "Verification si un instant buy and sell est possible\n\n";
42 $instantBuySell = WhereInstantBuy($timestamp,$orderbook,$getBalance); //2
43
44 curl_close($GLOBALS["ch"]);
45}
46
47function ecrire_log($errtxt){
48 $fp = fopen('log.txt','a+'); // ouvrir le fichier ou le créer
49 fseek($fp,SEEK_END); // poser le point de lecture à la fin du fichier
50 $nouverr=$errtxt."\r\n"; // ajouter un retour à la ligne au fichier
51 fputs($fp,$nouverr); // ecrire ce texte
52 fclose($fp); //fermer le fichier
53 }
54
55 function WhereInstantBuy($timestamp,$orderbook,$getBalance)
56 {
57 //le prix auquel j'instant buy //Le prix auquel j'instant sell
58 if (($orderbook["ZYN_ETH"]->asks[0][0] * $orderbook["ETH_USDT"]->asks[0][0]) < ($orderbook["ZYN_BTC"]->bids[0][0] * $orderbook["BTC_USDT"]->asks[0][0]))
59 {
60 echo "\n\nAchat en ETH pour revendre en BTC\n";
61 ecrire_log("\n\nAchat en ETH pour revendre en BTC");
62 $HowmuchBuy = (($getBalance->free->ETH / ($orderbook["ZYN_ETH"]->asks[0][0])) - 0.1); // CONTROLER SI LA QUANTITER DE TOKEN A ACHETER AU NOMBRE DE LA QUANTITE QUI VA ETRE ACHETER
63 if ($HowmuchBuy > $orderbook["ZYN_ETH"]->asks[0][1])
64 {
65
66 $HowmuchBuy = $orderbook["ZYN_ETH"]->asks[0][1];
67 }
68 if ($HowmuchBuy > $orderbook["ZYN_BTC"]->asks[0][1])
69 {
70
71 $HowmuchBuy = $orderbook["ZYN_BTC"]->asks[0][1];
72 }
73 echo "achat en ETH\n";
74 buyZYN_ETH($timestamp,$orderbook,$HowmuchBuy);
75 echo "Vente en BTC\n";
76 sellZYN_BTC($timestamp,$orderbook,$HowmuchBuy);
77 }
78 else
79 echo "\n\n Instant buy Eth to sell for btc ::: Ne rien faire \n\n";
80
81
82
83
84 if (($orderbook["ZYN_ETH"]->asks[0][0] * $orderbook["ETH_USDT"]->asks[0][0]) < $orderbook["ZYN_USD"]->bids[0][0])
85 {
86 echo "\n\nAchat en ETH pour revendre en USDT";
87 ecrire_log("\n\nAchat en ETH pour revendre en USDT");
88 $HowmuchBuy = (($getBalance->free->ETH / ($orderbook["ZYN_ETH"]->asks[0][0])) - 0.1);
89 if ($HowmuchBuy > $orderbook["ZYN_ETH"]->asks[0][1])
90 {
91 $HowmuchBuy = $orderbook["ZYN_ETH"]->asks[0][1];
92 }
93 if ($HowmuchBuy > $orderbook["ZYN_USDT"]->asks[0][1])
94 {
95
96 $HowmuchBuy = $orderbook["ZYN_USDT"]->asks[0][1];
97 }
98 echo "achat en ETH\n";
99 buyZYN_ETH($timestamp,$orderbook,$HowmuchBuy);
100 echo "Vente en USDT\n";
101 sellZYN_USDT($timestamp,$orderbook,$HowmuchBuy);
102 }
103 else
104 echo "\n\n Instant buy Eth to sell for usdt ::: Ne rien faire \n\n";
105
106
107
108
109
110 if (($orderbook["ZYN_BTC"]->asks[0][0] * $orderbook["BTC_USDT"]->asks[0][0]) < ($orderbook["ZYN_ETH"]->bids[0][0] * $orderbook["ETH_USDT"]->asks[0][0]))
111 {
112 echo "\n\nAchat en BTC pour revendre en ETH";
113 ecrire_log("\n\nAchat en BTC pour revendre en ETH");
114 $HowmuchBuy = (($getBalance->free->BTC / ($orderbook["ZYN_BTC"]->asks[0][0])) - 0.1); // A optimiser pour acheter le nombre de token en vente et non pas le max que je peux au prix du asks
115 if ($HowmuchBuy > $orderbook["ZYN_BTC"]->asks[0][1])
116 {
117 $HowmuchBuy = $orderbook["ZYN_BTC"]->asks[0][1];
118 }
119 if ($HowmuchBuy > $orderbook["ZYN_ETH"]->asks[0][1])
120 {
121
122 $HowmuchBuy = $orderbook["ZYN_ETH"]->asks[0][1];
123 }
124 echo "achat en BTC\n";
125 buyZYN_BTC($timestamp,$orderbook,$HowmuchBuy);
126 echo "Vente en ETH\n";
127 sellZYN_ETH($timestamp,$orderbook,$HowmuchBuy);
128 }
129 else
130 echo "\n\n Instant buy BTC to sell for ETH ::: Ne rien faire \n\n";
131
132}
133
134function sellZYN_USDT($timestamp,$orderbook,$HowMuchisell)
135{
136
137 $HowMuchisellZYN_USDT = round($HowMuchisell,1);
138 $HowMuchisellZYN_USDT = 0;
139 $timestamp = round($timestamp,0);
140 $sign = hash_hmac('sha256', 'amount='.$HowMuchisellZYN_USDT.'&key=40ce263a673043a28b10dd0af55766b63eaaf4a4525942e2825299de47f633c7&orderSide=sell&orderType=limit&pair=ZYN_USDT&price='.($orderbook["ZYN_USDT"]->bids[0][0]).'×tamp='.$timestamp.'', "bff84dd3b8834779b5405ded35074219a3547635d5bb4be8964284a665ec2d3f");
141
142 curl_setopt($GLOBALS["ch"], CURLOPT_URL, "https://openapi.idax.pro/api/v2/placeOrder");
143 $payload = json_encode(array('amount' => $HowMuchisellZYN_USDT,
144 'key' => "40ce263a673043a28b10dd0af55766b63eaaf4a4525942e2825299de47f633c7",
145 'orderSide'=>"sell",
146 'orderType'=>"limit",
147 'pair'=>"ZYN_USDT",
148 'price'=>($orderbook["ZYN_USDT"]->bids[0][0]),
149 'sign'=>$sign,
150 'timestamp'=>$timestamp));
151
152 curl_setopt($GLOBALS["ch"], CURLOPT_POSTFIELDS, $payload);
153
154
155 $result = curl_exec($GLOBALS["ch"]);
156 $result = json_decode($result);
157 ecrire_log($result);
158 echo "\nREPONSE API A LA VENTE USDT : \n";
159
160 return $result;
161}
162
163function sellZYN_ETH($timestamp,$orderbook,$HowMuchisell)
164{
165
166 $HowMuchisellZYN_ETH = round($HowMuchisell,1);
167 $timestamp = round($timestamp,0);
168 $sign = hash_hmac('sha256', 'amount='.$HowMuchisellZYN_ETH.'&key=40ce263a673043a28b10dd0af55766b63eaaf4a4525942e2825299de47f633c7&orderSide=sell&orderType=limit&pair=ZYN_ETH&price='.($orderbook["ZYN_ETH"]->bids[0][0]).'×tamp='.$timestamp.'', "bff84dd3b8834779b5405ded35074219a3547635d5bb4be8964284a665ec2d3f");
169
170 curl_setopt($GLOBALS["ch"], CURLOPT_URL, "https://openapi.idax.pro/api/v2/placeOrder");
171
172 $payload = json_encode(array('amount' => $HowMuchisellZYN_ETH,
173 'key' => "40ce263a673043a28b10dd0af55766b63eaaf4a4525942e2825299de47f633c7",
174 'orderSide'=>"sell",
175 'orderType'=>"limit",
176 'pair'=>"ZYN_ETH",
177 'price'=>($orderbook["ZYN_ETH"]->bids[0][0]),
178 'sign'=>$sign,
179 'timestamp'=>$timestamp));
180
181 curl_setopt($GLOBALS["ch"], CURLOPT_POSTFIELDS, $payload);
182
183
184 $result = curl_exec($GLOBALS["ch"]);
185 $result = json_decode($result);
186 ecrire_log($result);
187 echo "\nREPONSE API A LA VENTE ETH : \n";
188
189 return $result;
190}
191
192
193function buyZYN_BTC($timestamp,$orderbook,$HowmuchBuy) //
194{
195
196 $HowMuchibuyZYN_BTC = round($HowmuchBuy,1);
197
198 $timestamp = round($timestamp,0);
199 $sign = hash_hmac('sha256', 'amount='.$HowMuchibuyZYN_BTC.'&key=40ce263a673043a28b10dd0af55766b63eaaf4a4525942e2825299de47f633c7&orderSide=buy&orderType=limit&pair=ZYN_BTC&price='.($orderbook["ZYN_BTC"]->asks[0][0]).'×tamp='.$timestamp.'', "bff84dd3b8834779b5405ded35074219a3547635d5bb4be8964284a665ec2d3f");
200
201 curl_setopt($GLOBALS["ch"], CURLOPT_URL, "https://openapi.idax.pro/api/v2/placeOrder");
202
203 $payload = json_encode(array('amount' => $HowMuchibuyZYN_BTC,
204 'key' => "40ce263a673043a28b10dd0af55766b63eaaf4a4525942e2825299de47f633c7",
205 'orderSide'=>"buy",
206 'orderType'=>"limit",
207 'pair'=>"ZYN_BTC",
208 'price'=>($orderbook["ZYN_BTC"]->asks[0][0]),
209 'sign'=>$sign,
210 'timestamp'=>$timestamp));
211
212 curl_setopt($GLOBALS["ch"], CURLOPT_POSTFIELDS, $payload);
213
214
215 $result = curl_exec($GLOBALS["ch"]);
216 $result = json_decode($result);
217 ecrire_log($result);
218 echo "\nREPONSE API A L'achat BTC : \n";
219
220
221
222
223
224
225 return $result;
226}
227
228function buyZYN_ETH($timestamp,$orderbook,$HowmuchBuy)
229{
230
231 $HowMuchiBuyZYN_ETH = round($HowmuchBuy,1);
232 $timestamp = round($timestamp,0);
233 $sign = hash_hmac('sha256', 'amount='.$HowMuchiBuyZYN_ETH.'&key=40ce263a673043a28b10dd0af55766b63eaaf4a4525942e2825299de47f633c7&orderSide=buy&orderType=limit&pair=ZYN_ETH&price='.($orderbook["ZYN_ETH"]->asks[0][0]).'×tamp='.$timestamp.'', "bff84dd3b8834779b5405ded35074219a3547635d5bb4be8964284a665ec2d3f");
234
235 curl_setopt($GLOBALS["ch"], CURLOPT_URL, "https://openapi.idax.pro/api/v2/placeOrder");
236
237 $payload = json_encode(array('amount' => $HowMuchiBuyZYN_ETH,
238 'key' => "40ce263a673043a28b10dd0af55766b63eaaf4a4525942e2825299de47f633c7",
239 'orderSide'=>"buy",
240 'orderType'=>"limit",
241 'pair'=>"ZYN_ETH",
242 'price'=>($orderbook["ZYN_ETH"]->asks[0][0]),
243 'sign'=>$sign,
244 'timestamp'=>$timestamp));
245
246 curl_setopt($GLOBALS["ch"], CURLOPT_POSTFIELDS, $payload);
247
248
249 $result = curl_exec($GLOBALS["ch"]);
250 $result = json_decode($result);
251
252 ecrire_log($result);
253 echo "REPONSE API A L'ACHAT ETH : \n";
254
255
256
257 return $result;
258}
259
260function sellZYN_BTC($timestamp,$orderbook,$HowMuchisell)
261{
262
263 $HowMuchisellZYN_BTC = round($HowMuchisell,1);
264 $timestamp = round($timestamp,0);
265 $sign = hash_hmac('sha256', 'amount='.$HowMuchisellZYN_BTC.'&key=40ce263a673043a28b10dd0af55766b63eaaf4a4525942e2825299de47f633c7&orderSide=sell&orderType=limit&pair=ZYN_BTC&price='.($orderbook["ZYN_BTC"]->bids[0][0]).'×tamp='.$timestamp.'', "bff84dd3b8834779b5405ded35074219a3547635d5bb4be8964284a665ec2d3f");
266
267 curl_setopt($GLOBALS["ch"], CURLOPT_URL, "https://openapi.idax.pro/api/v2/placeOrder");
268
269 $payload = json_encode(array('amount' => $HowMuchisellZYN_BTC,
270 'key' => "40ce263a673043a28b10dd0af55766b63eaaf4a4525942e2825299de47f633c7",
271 'orderSide'=>"sell",
272 'orderType'=>"limit",
273 'pair'=>"ZYN_BTC",
274 'price'=>($orderbook["ZYN_BTC"]->bids[0][0]),
275 'sign'=>$sign,
276 'timestamp'=>$timestamp));
277
278 curl_setopt($GLOBALS["ch"], CURLOPT_POSTFIELDS, $payload);
279
280
281 $result = curl_exec($GLOBALS["ch"]);
282 $result = json_decode($result);
283 ecrire_log($result);
284 echo "REPONSE API A LA VENTE BTC : \n";
285
286 echo "\n\n";
287 return $result;
288}
289
290function getTimeStamp()
291{
292//Recupere le TimeStamp
293 curl_setopt($GLOBALS["ch"], CURLOPT_URL, "https://openapi.idax.pro/api/v2/time/");
294
295
296 $result = curl_exec($GLOBALS["ch"]);
297 print_r($result);
298 $timestamp = json_decode($result);
299 $timestamp = $timestamp->timestamp;
300
301 return $timestamp;
302
303}
304
305function orderbook()
306{
307
308
309 curl_setopt($GLOBALS["ch"], CURLOPT_URL, "https://openapi.idax.pro/api/v2/depth?pair=ZYN_ETH&size=2&merge=8");
310 $orderbook["ZYN_ETH"] = curl_exec($GLOBALS["ch"]);
311 $orderbook["ZYN_ETH"] = json_decode($orderbook["ZYN_ETH"]);
312
313 curl_setopt($GLOBALS["ch"], CURLOPT_URL, "https://openapi.idax.pro/api/v2/depth?pair=ZYN_BTC&size=2&merge=8");
314 $orderbook["ZYN_BTC"] = curl_exec($GLOBALS["ch"]);
315 $orderbook["ZYN_BTC"] = json_decode($orderbook["ZYN_BTC"]);
316
317 curl_setopt($GLOBALS["ch"], CURLOPT_URL, "https://openapi.idax.pro/api/v2/depth?pair=ZYN_USDT&size=2&merge=8");
318 $orderbook["ZYN_USDT"] = curl_exec($GLOBALS["ch"]);
319 $orderbook["ZYN_USDT"] = json_decode($orderbook["ZYN_USDT"]);
320
321 curl_setopt($GLOBALS["ch"], CURLOPT_URL, "https://openapi.idax.pro/api/v2/depth?pair=BTC_USDT&size=2&merge=8");
322 $orderbook["BTC_USDT"] = curl_exec($GLOBALS["ch"]);
323 $orderbook["BTC_USDT"] = json_decode($orderbook["BTC_USDT"]);
324
325 curl_setopt($GLOBALS["ch"], CURLOPT_URL, "https://openapi.idax.pro/api/v2/depth?pair=ETH_USDT&size=2&merge=8");
326 $orderbook["ETH_USDT"] = curl_exec($GLOBALS["ch"]);
327 $orderbook["ETH_USDT"] = json_decode($orderbook["ETH_USDT"]);
328
329 return $orderbook;
330
331
332
333}
334
335function getBalance ($timestamp)
336{
337
338 $sign = hash_hmac('sha256', 'key=40ce263a673043a28b10dd0af55766b63eaaf4a4525942e2825299de47f633c7×tamp='.$timestamp.'', "bff84dd3b8834779b5405ded35074219a3547635d5bb4be8964284a665ec2d3f");
339
340 curl_setopt($GLOBALS["ch"], CURLOPT_URL, "https://openapi.idax.pro/api/v2/userinfo");
341
342 $payload = json_encode(array('key' =>'40ce263a673043a28b10dd0af55766b63eaaf4a4525942e2825299de47f633c7',
343 'timestamp'=>$timestamp,
344 'sign'=>$sign));
345
346 curl_setopt($GLOBALS["ch"], CURLOPT_POSTFIELDS, $payload);
347
348
349 $result = curl_exec($GLOBALS["ch"]);
350 print_r($result);
351 $result = json_decode($result);
352 return $result;
353}
354
355function shouldibuyZYN_ETH($balance)
356{
357 if ($balance->free->ETH > 0.06)
358 {
359 return true;
360 }
361 else
362 {
363 return false;
364 }
365}
366
367function shouldisellZYN_ETH($balance)
368{
369 if ($balance->freezed->ZYN > 5)
370 {
371 return True;
372 }
373 else
374 {
375 return false;
376 }
377}
378
379function CheckCurrentOrder($timestamp)
380{
381 $sign = hash_hmac('sha256', 'key=40ce263a673043a28b10dd0af55766b63eaaf4a4525942e2825299de47f633c7&orderId=-1&pageIndex=1&pageSize=1&pair=ZYN_ETH×tamp='.$timestamp.'', "bff84dd3b8834779b5405ded35074219a3547635d5bb4be8964284a665ec2d3f");
382 curl_setopt($GLOBALS["ch"], CURLOPT_URL, "https://openapi.idax.pro/api/v2/orderInfo");
383
384 $payload = json_encode(array('key' => '40ce263a673043a28b10dd0af55766b63eaaf4a4525942e2825299de47f633c7',
385 'pair' => 'ZYN_ETH',
386 'orderId'=>-1,
387 'pageIndex'=>1,
388 'pageSize'=>1,
389 'timestamp'=>$timestamp,
390 'sign'=>$sign));
391
392 curl_setopt($GLOBALS["ch"], CURLOPT_POSTFIELDS, $payload);
393
394
395 $result = curl_exec($GLOBALS["ch"]);
396 $numberCurrentOrder = $result->total;
397 return $numberCurrentOrder;
398}
399
400?>