· 8 years ago · Dec 17, 2017, 06:40 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";
56 }
57
58 }else{
59 //echo "Jual";
60
61 $arr = array(
62 'count' => 1,
63 'order' => 'desc',
64 'pair' => $pair
65 );
66
67 //get tradeHistory
68 $res = btcid_query('tradeHistory', $arr);
69 $harga_beli = $res['return']['trades'][0]['price'];
70 $harga_beli = intval($harga_beli);
71
72 if($harga_beli>$last){
73 $harga_jual = $harga_beli + ($harga_beli*$var_margin/100);
74 } else{
75 $harga_jual = $last + ($last*$var_margin/100);
76 }
77
78 $harga_jual = round($harga_jual);
79
80 $arr = array(
81 'pair' => $pair,
82 'type' => 'sell',
83 'price' => $harga_jual,
84 'btc' => $btc
85 );
86
87 //Jual
88 $result = btcid_query('trade', $arr);
89 $order_id = $result['return']['order_id'];
90
91 if($order_id !=""){
92 echo "Berhasil Jual<br>";
93 echo "last price : ".$last."<br>";
94 echo "harga beli : ".$harga_beli."<br>";
95 echo "harga jual : ".$harga_jual."<br>";
96 echo "order_id : ".$order_id."<br>";
97 }else{
98 echo "Gagal Jual";
99 }
100
101 }
102 }
103
104 }
105
106function btcid_query($method, array $req = array()) {
107 // API settings
108 $key = 'BMJLBEPZ-KY8DLYHK-OAG2ERSU-NKC3A8QT-P1OKAFXC'; // your API-key
109 $secret = '8a0c8ce82c24281b420a4c21fe43af07daac19c3f551bde3a305154db78ad4720c52c1aa8a9202c6'; // your Secret-key
110 $req['method'] = $method;
111 $req['nonce'] = time();
112
113 // generate the POST data string
114 $post_data = http_build_query($req, '', '&');
115 $sign = hash_hmac('sha512', $post_data, $secret);
116 // generate the extra headers
117 $headers = array(
118 'Sign: '.$sign,
119 'Key: '.$key,
120 );
121 // our curl handle (initialize if required)
122 static $ch = null;
123 if (is_null($ch)) {
124 $ch = curl_init();
125 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
126 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible;
127 BITCOINCOID PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
128 }
129 curl_setopt($ch, CURLOPT_URL, 'https://vip.bitcoin.co.id/tapi/');
130 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
131 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
132 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
133
134 // run the query
135 $res = curl_exec($ch);
136 if ($res === false) throw new Exception('Could not get reply:
137 '.curl_error($ch));
138 $dec = json_decode($res, true);
139 if (!$dec) throw new Exception('Invalid data received, please make sure
140 connection is working and requested API exists: '.$res);
141
142 curl_close($ch);
143 $ch = null;
144 return $dec;
145}
146
147?>