Last active
May 26, 2017 09:34
-
-
Save namakemono/aba4bae86aa89fc56b35f9ecfa9cd934 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import requests | |
import pandas as pd | |
import datetime | |
""" | |
coincheckのPublic API用データ取得ツール | |
- c.f. https://coincheck.com/ja/documents/exchange/api#public | |
- 取引所の注文状況や公開されている取引の履歴、板情報を参照 | |
""" | |
def fetch_ticker(save_to_dir): | |
""" | |
last 最後の取引の価格 | |
bid 現在の買い注文の最高価格 | |
ask 現在の売り注文の最安価格 | |
high 24時間での最高取引価格 | |
low 24時間での最安取引価格 | |
volume 24時間での取引量 | |
timestamp 現在の時刻 | |
""" | |
fpath = os.path.join(save_to_dir, "coincheck_ticker_%s.csv" % datetime.datetime.now().strftime("%Y-%m-%d")) | |
if not os.path.exists(fpath): | |
fp = open(fpath, "w") | |
fp.write("datetime,last,bid,ask,high,low,volume,timestamp\n") | |
with open(fpath, "a") as fp: | |
r = requests.get("https://coincheck.com/api/ticker") | |
if r.status_code == 200: | |
item = r.json() | |
item["datetime"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
line = "%(datetime)s,%(last)s,%(bid)s,%(ask)s,%(high)s,%(low)s,%(volume)s,%(timestamp)s" % item | |
print line | |
fp.write(line + "\n") | |
def fetch_trades(save_to_dir): | |
""" | |
最新の取引履歴 | |
""" | |
fpath = os.path.join(save_to_dir, "coincheck_trades_%s.csv" % datetime.datetime.now().strftime("%Y-%m-%d")) | |
if not os.path.exists(fpath): | |
fp = open(fpath, "w") | |
fp.write("id,datetime,amount,rate,order_type,created_at\n") | |
with open(fpath, "a") as fp: | |
r = requests.get("https://coincheck.com/api/trades") | |
if r.status_code == 200: | |
items = r.json() | |
for item in items: | |
item["datetime"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
line = "%(id)s,%(datetime)s,%(amount)s,%(rate)s,%(order_type)s,%(created_at)s" % item | |
print line | |
fp.write(line + "\n") | |
def fetch_order_books(save_to_dir): | |
""" | |
板情報を取得 | |
asks 売り注文の情報 | |
bids 買い注文の情報 | |
""" | |
fpath = os.path.join(save_to_dir, "coincheck_order_books_%s.csv" % datetime.datetime.now().strftime("%Y-%m-%d")) | |
if not os.path.exists(fpath): | |
fp = open(fpath, "w") | |
fp.write("datetime,order_type,price,amount\n") | |
with open(fpath, "a") as fp: | |
r = requests.get("https://coincheck.com/api/order_books") | |
if r.status_code == 200: | |
item = r.json() | |
for order_type, records in item.items(): | |
for price, amount in records: | |
item = { | |
"datetime": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), | |
"order_type": order_type, | |
"price": price, | |
"amount": amount | |
} | |
line = "%(datetime)s,%(order_type)s,%(price)s,%(amount)s" % item | |
print line | |
fp.write(line + "\n") | |
def fetch_rate(save_to_dir): | |
pair_list = [ | |
"btc_jpy", "eth_jpy", "etc_jpy", "dao_jpy", "lsk_jpy", "fct_jpy", "xmr_jpy", "rep_jpy", "xrp_jpy", "zec_jpy", "xem_jpy", "ltc_jpy", "dash_jpy", | |
"eth_btc", "etc_btc", "lsk_btc", "fct_btc", "xmr_btc", "rep_btc", "xrp_btc", "zec_btc", "xem_btc", "ltc_btc", "dash_btc"] | |
fpath = os.path.join(save_to_dir, "coincheck_rate_%s.csv" % datetime.datetime.now().strftime("%Y-%m-%d")) | |
if not os.path.exists(fpath): | |
fp = open(fpath, "w") | |
fp.write("datetime,pair,rate\n") | |
with open(fpath, "a") as fp: | |
for pair in pair_list: | |
r = requests.get("https://coincheck.com/api/rate/" + pair) | |
if r.status_code == 200: | |
item = r.json() | |
line = "%s,%s,%s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), pair, item["rate"]) | |
print line | |
fp.write(line + "\n") | |
def run(save_to_dir): | |
fetch_ticker(save_to_dir) | |
fetch_trades(save_to_dir) | |
fetch_order_books(save_to_dir) | |
fetch_rate(save_to_dir) | |
if __name__ == "__main__": | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--save_to_dir', type=str, default="/tmp") | |
args = parser.parse_args() | |
run(args.save_to_dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment