· 4 years ago · Apr 11, 2021, 07:02 AM
1# CEK HARGA CRYPTO INDODAX
2# DIBUAT OLEH PAPANARA/NARAMEDIA
3
4from indodax import indodax
5import json, time
6import requests
7
8KEY = 'ISI-DENGAN-KEY-INDODAX'
9SECRET = 'isi-secret-akun-indodax'
10
11
12def cekHarga():
13 """
14 Fungsi untuk melakukan pengecekan harga Cryptocurrency
15 Data didapatkan dari Public API Indodax dan untuk memanggilnya
16 Cukup dengan mengisi parameter nama crypto/asset
17 """
18 print('='*57)
19 print('| Untuk mengambil data harga crytocurrency terkini\t|')
20 print('| cukup masukan nama asset yang ingin dicek atau ketik\t|')
21 print('| "semua" asset untuk menampilkan semua data harga\t|')
22 print('='*57)
23 print('')
24
25 asset_name = ['semua']
26 account = indodax(KEY, SECRET)
27 add_asset_to_list = json.loads(account.get_info())
28 get_asset_name = add_asset_to_list['return']['address']
29
30 # Menambahkan keys yang didapatkan dari dictionary ke dalam list asset nama
31 # sebagai validasi inputan
32 for k in get_asset_name.keys():
33 asset_name.append(k)
34
35 # Untuk formatting rupiah
36 def rupiah(nilai):
37 y = str(nilai)
38 if len(y) <= 3 :
39 return 'Rp ' + y
40 else :
41 p = y[-3:]
42 q = y[:-3]
43 return rupiah(q) + '.' + p
44
45 # Inputan user
46 while True:
47 print('')
48 asset = input('Masukan nama asset atau ketik "semua" untuk menampilkan semua harga : ')
49 val_asset = str.lower(asset) # Validasi untuk mencegah huruf besar
50 if val_asset not in asset_name:
51 print('Kesalahan!. Masukan nama aset yang benar')
52 if val_asset == 'semua':
53 ticker = []
54 url = 'https://indodax.com/api/summaries'
55 req = requests.get(url)
56 data = req.json()['tickers']
57 print('-'*82)
58 print('PAIR\t\tLAST_PRICE HIGH_PRICE LOW_PRICE BID_PRICE ASK_PRICE')
59 print('-'*82)
60 for k in data.keys():
61 ticker.append(k)
62 for i in ticker:
63 print(i,'\t',data[i]['last'],' - ',data[i]['high'],' - ',data[i]['low'],' - ',data[i]['buy'],' - ',data[i]['sell'])
64 else:
65 price = indodax.get_price(val_asset)['ticker']
66 high_price = price['high']
67 low_price = price['low']
68 volume = price['vol_idr']
69 last_price = price['last']
70 ask_price = price['sell']
71 bid_price = price['buy']
72 print('-'*67)
73 # untuk formating tabel
74 print('Asset Name :','\t',str.upper(val_asset))
75 print('Last Price :','\t',rupiah(last_price))
76 print('High Price :','\t',rupiah(high_price))
77 print('Low Price :','\t',rupiah(low_price))
78 print('Buy Price :','\t',rupiah(bid_price))
79 print('Sell Price :','\t',rupiah(ask_price))
80 print('Volume 24h :','\t',rupiah(volume))