return -1 price = -1 try: dat = json.loads(requests.get("https://api.coinone.co.kr/ticker/", params={"currency":coin.lower(), "format": "json"}).text) price = (float) (dat["last"]) /currency_converter.get_exchange_rate("KRW") except Exception as e: WARNING("{}: {}: {}".format(__name__, coin, e)) return price @staticmethod def get_one_day_volume(coin, currency_converter): if coin.upper() in coinone.notsupported_coins: return -1 vol = -1 try: dat = json.loads(requests.get("https://api.coinone.co.kr/ticker/", params={"currency":coin.lower(), "format": "json"}).text) vol = (float) (dat["volume"]) except Exception as e: WARNING("{}: {}: {}".format(__name__, coin, e)) return vol if __name__ == "__main__": cc = fiatCurrencyConverter() pprint(coinone.get_current_price("ETH", cc))
def run(coin_file="coin", price_compare_interval=3600, sleep_time=10, min_alert_interval=1200): """ runnable function Keyword Arguments: coin_file {str} -- the loc of file containing all coins (default: {"coin"}) price_compare_interval {number} -- current price compared to price_compare_interval seconds ago (default: {3600}) sleep_time {number} -- the time to sleep between each check (default: {10}) min_alert_interval {number} -- ask the system to send out less alert, after the previous alert, it will wait for min_alert_interval seconds before sending next alert (default: {1200}) """ if not os.path.exists(OUTPUT_FOLDER): os.makedirs(OUTPUT_FOLDER) coin_list = load_coin(coin_file) # the list of coins to monitor coin_prices_deque = defaultdict( dict) # coin -> deque(price_list of fixed size) # coin -> {exhange-exchange -> its price ratio} coin_last_ratio_dict = {coin: defaultdict(float) for coin in coin_list} fcc = fiatCurrencyConverter() # for converting currency exchange_list = EXCHANGE_LIST # a list of exchanges # a dict coin -> coin_price_file_pointer output_file_dict = { coin: open("{}/{}.price.exchange".format(OUTPUT_FOLDER, coin), "a") for coin in coin_list } # time of last alert last_alert_time = 0 # initialize file header for coin, file in output_file_dict.items(): if file.tell() == 0: file.write("time \t{}\n".format("\t".join( [exchange.__name__ for exchange in exchange_list]))) while 1: topic = "" all_msg = "" have_exchange_alert = False have_price_history_alert = False coin_price_dict = get_coin_price_from_all_exchange( exchange_list, coin_list, fcc) write_coin_price(output_file_dict, exchange_list, coin_price_dict) for coin in coin_list: for exchange in exchange_list: if exchange in coin_prices_deque[coin]: coin_prices_deque[coin][exchange].append( coin_price_dict[coin][exchange]) if len(coin_prices_deque[coin] [exchange]) > price_compare_interval // sleep_time: coin_prices_deque[coin][exchange].popleft() else: coin_prices_deque[coin][exchange] = deque( [coin_price_dict[coin][exchange]]) msg = analyze_coin_price_history(coin, exchange, coin_prices_deque) if msg: have_price_history_alert = True all_msg += "<br><br><br><br><br><br><br>" + msg msg = analyze_coin_price_between_exchanges(coin, coin_prices_deque, exchange_list, coin_last_ratio_dict) # pprint("{} {}".format(coin, msg)) if msg: have_exchange_alert = True all_msg += "<br><br><br><br><br><br><br>" + msg # print("{}\n{}".format(time.strftime("%H:%M:%S", time.localtime(time.time())), msg)) if len(msg) and time.time() - last_alert_time >= min_alert_interval: topic = "alert " if have_exchange_alert: topic += "exchange, " if have_price_history_alert: topic += "history" alert(msg=all_msg, topic=topic) last_alert_time = time.time() time.sleep(sleep_time) for file in output_file_dict.values(): file.close()