Automatic buying CEX.io GHSs for BTC and NMC


SUBMITTED BY: Guest

DATE: March 19, 2014, 4:02 p.m.

FORMAT: Text only

SIZE: 10.0 kB

HITS: 3289

  1. <?php
  2. /**
  3. * Author: nikcorp
  4. * e-mail: nickcorp@rambler.ru
  5. * Donation:
  6. * BTC : 1GQu8Gki7y9ZLXQuCN23vnTeoJpgMuZ9ez
  7. * LTC : LXLBByJR7FhMmjCqbQLj8kwfhUXSy7dcSf
  8. * NMC : NBtzwP158sZTogSqydgJEfD4suYKvVCDYa
  9. *
  10. *
  11. * Modified by Son_Of_Diablo
  12. */
  13. $StartDate = date("Y-m-d H:i:s");
  14. $GHSBought = 0.00;
  15. // MAIN LOOP
  16. while(true)
  17. {
  18. // Working Function
  19. $StartTime = FirstFunc('UserName','Your_APIKey','YOUR_APISecret');
  20. // Pause :)
  21. if ($StartTime > 3) $StartTime = 3;
  22. if($StartTime == 1)
  23. {
  24. echo " \n Waiting 1 minute ";
  25. for($i=0; $i<60; $i++)
  26. {
  27. echo ".";
  28. sleep(1);
  29. }
  30. }elseif($StartTime == 2)
  31. {
  32. echo " \n Waiting 2 minute ";
  33. for($i=0; $i<60; $i++)
  34. {
  35. echo ".";
  36. sleep(2);
  37. }
  38. }elseif($StartTime == 3)
  39. {
  40. echo " \n Waiting 3 minute ";
  41. for($i=0; $i<60; $i++)
  42. {
  43. echo ".";
  44. sleep(3);
  45. }
  46. }
  47. echo " \n ";
  48. passthru('clear');
  49. }
  50. // Work Function
  51. function FirstFunc($username, $api_key, $api_secret)
  52. {
  53. global $StartDate;
  54. global $GHSBought;
  55. $TimeNow = date("Y-m-d H:i:s");
  56. $first = new DateTime( $StartDate );
  57. $second = new DateTime( $TimeNow );
  58. $diff = $first->diff( $second );
  59. $hours = $diff->format( '%d' );
  60. $hours = $hours * 24;
  61. $hours2 = $diff->format( '%H' );
  62. $hours = $hours + $hours2;
  63. echo "Time Established: ";
  64. if($hours < 10){
  65. echo '0';
  66. }
  67. echo $hours;
  68. echo $diff->format( ':%I:%S' );
  69. for($i = 0; $i < 37; $i++){
  70. echo " ";
  71. }
  72. echo "GHS Bought: ";
  73. printf("%0.2f", $GHSBought);
  74. echo "\n";
  75. for($i=1; $i<81; $i++)
  76. {
  77. echo "-";
  78. }
  79. echo "\n \n";
  80. static $StartTime = 0;
  81. echo " \n Reading DATA from CEX.IO... \n";
  82. for($i=1; $i<81; $i++)
  83. {
  84. echo "=";
  85. }
  86. echo "\n \n";
  87. // Reading
  88. $BTCPrice = getBTC();
  89. $NMCPrice = getNMC();
  90. // Ballance
  91. $nonce = round(microtime(true)*100);
  92. $myvars = 'key=' . $api_key .
  93. '&signature=' . make_signature($username,$api_key,$api_secret,$nonce) .
  94. '&nonce=' . $nonce;
  95. $OutRes = getCEX($myvars);
  96. $BTC_Ball = $OutRes->BTC->available ; // $btcPrice
  97. $NMC_Ball = $OutRes->NMC->available ; // $btcPrice
  98. $GHS_Ball = $OutRes->GHS->available ;
  99. $GHSBTC_available = $BTC_Ball / ($BTCPrice*1.0001) ;
  100. $GHSNMC_available = $NMC_Ball / ($NMCPrice*1.0001) ;
  101. $BTC_left = ($BTCPrice*0.01) - $BTC_Ball ;
  102. $NMC_left = ($NMCPrice*0.01) - $NMC_Ball ;
  103. // Print to Screen
  104. echo ' GHS/BTC : ', $BTCPrice, "\n";
  105. echo ' GHS/NMC : ', $NMCPrice, "\n";
  106. echo '-----', "\n" ;
  107. echo ' BTC in account - ', $BTC_Ball, "\n" ;
  108. echo ' NMC in account - ', $NMC_Ball, "\n" ;
  109. echo '-----', "\n" ;
  110. echo ' BTC left to buy - ' ;
  111. printf("%0.8f", $BTC_left);
  112. echo "\n" ;
  113. echo ' NMC left to buy - ' ;
  114. printf("%0.8f", $NMC_left);
  115. echo "\n" ;
  116. echo '-----', "\n" ;
  117. echo ' GHS in account - ', $GHS_Ball, "\n \n" ;
  118. for($i=1; $i<81; $i++)
  119. {
  120. echo "=";
  121. }
  122. $StartTime++;
  123. // BUY GHS for BTC
  124. if ($GHSBTC_available > 0.01)
  125. {
  126. $RealGH = intval( $GHSBTC_available / 0.01 )*0.01;
  127. echo "\n \n";
  128. echo 'Ready to buy (GHS/BTC) - ', $RealGH, " (", $GHSBTC_available, " GHS) \n";
  129. $nonce = round(microtime(true)*100);
  130. $buyVars = 'key=' . $api_key .
  131. '&signature=' . make_signature($username,$api_key,$api_secret,$nonce) .
  132. '&nonce=' . $nonce .
  133. '&type=buy' .
  134. '&price=' . number_format(1.0001 * $BTCPrice,8) .
  135. '&amount=0.01'; // . $buyAmnt;
  136. echo " Placing order (",number_format(1.0001 * $BTCPrice,8), " x 0.01)... ";
  137. $OutRes = buy_GHSBTC($buyVars);
  138. $ResPlacing = $OutRes->id;
  139. if ($ResPlacing > 0)
  140. {
  141. echo "Order is Placed. Order ID = ", $ResPlacing, " \n";
  142. $GHSBought += 0.01;
  143. }
  144. else
  145. {
  146. echo "Error placing order: \n";
  147. var_dump($OutRes);
  148. }
  149. $StartTime = 1;
  150. }
  151. // BUY GHS for NMC
  152. if ($GHSNMC_available >0.01)
  153. {
  154. $RealGH = intval( $GHSNMC_available / 0.01 )*0.01;
  155. echo 'Ready to buy (GHS/NMC) - ', $RealGH, " (", $GHSNMC_available, " GHS) \n";
  156. $nonce = round(microtime(true)*100);
  157. $buyVars = 'key=' . $api_key .
  158. '&signature=' . make_signature($username,$api_key,$api_secret,$nonce) .
  159. '&nonce=' . $nonce .
  160. '&type=buy' .
  161. '&price=' . number_format(1.0001 * $NMCPrice,8) .
  162. '&amount=0.01'; // . $buyAmnt;
  163. echo " Placing order (",number_format(1.0001 * $NMCPrice,8), " x 0.01)... ";
  164. $OutRes = buy_GHSNMC($buyVars);
  165. $ResPlacing = $OutRes->id;
  166. if ($ResPlacing > 0)
  167. {
  168. echo "Order is Placed. Order ID = ", $ResPlacing, " \n";
  169. $GHSBought += 0.01;
  170. }
  171. else
  172. {
  173. echo "Error placing order: \n";
  174. var_dump($OutRes);
  175. }
  176. $StartTime = 1;
  177. }
  178. echo "\n";
  179. return $StartTime;
  180. }
  181. function make_signature($username,$api_key,$api_secret,$nonce)
  182. {
  183. $string = $nonce . $username . $api_key; //Create string
  184. $hash = hash_hmac('sha256', $string, $api_secret); //Create hash
  185. $hash = strtoupper($hash);
  186. return $hash;
  187. }
  188. function getBTC ()
  189. {
  190. $url = 'https://cex.io/api/ticker/GHS/BTC';
  191. $contents = file_get_contents($url);
  192. $answer = json_decode($contents ,true);
  193. $current = $answer['last'];
  194. return $current;
  195. }
  196. function getNMC ()
  197. {
  198. $url = 'https://cex.io/api/ticker/GHS/NMC';
  199. $contents = file_get_contents($url);
  200. $answer = json_decode($contents ,true);
  201. $currentNMC = $answer['last'];
  202. return $currentNMC;
  203. }
  204. function getCEX($myvars)
  205. {
  206. $url = 'https://cex.io/api/balance/';
  207. $ch = curl_init();
  208. curl_setopt($ch, CURLOPT_URL, $url);
  209. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  210. curl_setopt($ch, CURLOPT_POST, true);
  211. curl_setopt($ch, CURLOPT_USERAGENT, 'phpAPI');
  212. curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
  213. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  214. $out = json_decode(curl_exec($ch));
  215. curl_close($ch);
  216. return $out;
  217. }
  218. // Buy GHS`s for BTC
  219. function buy_GHSBTC($buyVars)
  220. {
  221. $url = 'https://cex.io/api/place_order/GHS/BTC';
  222. $ch = curl_init();
  223. curl_setopt($ch, CURLOPT_URL, $url);
  224. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  225. curl_setopt($ch, CURLOPT_POST, true);
  226. curl_setopt($ch, CURLOPT_USERAGENT, 'phpAPI');
  227. curl_setopt($ch, CURLOPT_POSTFIELDS, $buyVars);
  228. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  229. $out = json_decode(curl_exec($ch));
  230. curl_close($ch);
  231. return $out;
  232. }
  233. // Buy GHS`s for NMC
  234. function buy_GHSNMC($buyVars)
  235. {
  236. $url = 'https://cex.io/api/place_order/GHS/NMC';
  237. $ch = curl_init();
  238. curl_setopt($ch, CURLOPT_URL, $url);
  239. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  240. curl_setopt($ch, CURLOPT_POST, true);
  241. curl_setopt($ch, CURLOPT_USERAGENT, 'phpAPI');
  242. curl_setopt($ch, CURLOPT_POSTFIELDS, $buyVars);
  243. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  244. $out = json_decode(curl_exec($ch));
  245. curl_close($ch);
  246. return $out;
  247. }
  248. ?>

comments powered by Disqus