· 8 years ago · Dec 17, 2017, 10:34 AM
1<?php
2
3 //variable
4 $var_margin = 0.1;
5 $var_dana = 100;
6 $pair = 'btc_idr';
7 $produk =
8
9 $openOrders = btcid_query('openOrders');
10
11 if($openOrders['return']['orders'] != ""){
12 echo "Ada Transaksi<br>";
13 //get openOrder
14 print_r($openOrders['return']['orders']);
15
16
17 }else{
18 //get info
19 $info = btcid_query('getInfo');
20 //print_r($info);
21
22 //get public data
23 ini_set("allow_url_fopen", 1);
24 $res = file_get_contents('https://vip.bitcoin.co.id/api/'.$pair.'/ticker');
25 $res = json_decode($res, true);
26 $last = $res['ticker']['last'];
27 $last = intval($last);
28
29 if($info['return']['balance'] !="" && $last !=0){
30 $idr = intval($info['return']['balance']['idr']);
31 $btc = $info['return']['balance']['btc'];
32
33 if($btc == "0"){
34 //echo "Beli";
35 $harga_beli = $last - ($last*$var_margin/100);
36 $harga_beli = round($harga_beli);
37 $jumlah_beli = $idr*$var_dana/100;
38
39 $arr = array(
40 'pair' => $pair,
41 'type' => 'buy',
42 'price' => $harga_beli,
43 'idr' => $jumlah_beli
44 );
45
46 //Beli
47 $result = btcid_query('trade', $arr);
48 $order_id = $result['return']['order_id'];
49 if($order_id !=""){
50 echo "Berhasil Beli<br>";
51 echo "order_id : ".$order_id."<br>";
52 echo "last : ".$last."<br>";
53 echo "harga beli : ".$harga_beli."<br>";
54 }else{
55 echo "Gagal Beli<br>";
56 echo "Parameter trade function : <br>";
57 print_r($arr);
58 echo "<br>";
59 echo "Result trade function : <br>";
60 print_r($result);
61 }
62
63 }else{
64 //echo "Jual";
65
66 $arr = array(
67 'count' => 1,
68 'order' => 'desc',
69 'pair' => $pair
70 );
71
72 //get tradeHistory
73 $res = btcid_query('tradeHistory', $arr);
74 $harga_beli = $res['return']['trades'][0]['price'];
75 $harga_beli = intval($harga_beli);
76
77 if($harga_beli>$last){
78 $harga_jual = $harga_beli + ($harga_beli*$var_margin/100);
79 } else{
80 $harga_jual = $last + ($last*$var_margin/100);
81 }
82
83 $harga_jual = round($harga_jual);
84
85 $arr = array(
86 'pair' => $pair,
87 'type' => 'sell',
88 'price' => $harga_jual,
89 'btc' => $btc
90 );
91
92 //Jual
93 $result = btcid_query('trade', $arr);
94 $order_id = $result['return']['order_id'];
95
96 if($order_id !=""){
97 echo "Berhasil Jual<br>";
98 echo "last price : ".$last."<br>";
99 echo "harga beli : ".$harga_beli."<br>";
100 echo "harga jual : ".$harga_jual."<br>";
101 echo "order_id : ".$order_id."<br>";
102 }else{
103 echo "Gagal Jual<br>";
104 echo "Parameter trade function : <br>";
105 print_r($arr);
106 echo "<br>";
107 echo "Result trade function : <br>";
108 print_r($result);
109 }
110
111 }
112 }else{
113 echo "Gagal Get Info atau Public API";
114 echo "Result getInfo Function : <br>";
115 print_r($info);
116 echo "<br>";
117 echo "Result public API : <br>";
118 print_r($res);
119 }
120
121 }
122
123function btcid_query($method, array $req = array()) {
124 // API settings
125 $key = 'BMJLBEPZ-KY8DLYHK-OAG2ERSU-NKC3A8QT-P1OKAFXC'; // your API-key
126 $secret = '8a0c8ce82c24281b420a4c21fe43af07daac19c3f551bde3a305154db78ad4720c52c1aa8a9202c6'; // your Secret-key
127 $req['method'] = $method;
128 $req['nonce'] = time();
129
130 // generate the POST data string
131 $post_data = http_build_query($req, '', '&');
132 $sign = hash_hmac('sha512', $post_data, $secret);
133 // generate the extra headers
134 $headers = array(
135 'Sign: '.$sign,
136 'Key: '.$key,
137 );
138 // our curl handle (initialize if required)
139 static $ch = null;
140 if (is_null($ch)) {
141 $ch = curl_init();
142 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
143 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible;
144 BITCOINCOID PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
145 }
146 curl_setopt($ch, CURLOPT_URL, 'https://vip.bitcoin.co.id/tapi/');
147 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
148 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
149 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
150
151 // run the query
152 $res = curl_exec($ch);
153 if ($res === false) throw new Exception('Could not get reply:
154 '.curl_error($ch));
155 $dec = json_decode($res, true);
156 if (!$dec) throw new Exception('Invalid data received, please make sure
157 connection is working and requested API exists: '.$res);
158
159 curl_close($ch);
160 $ch = null;
161 return $dec;
162}
163
164?>