仮想通貨で遊ぶ日記

リスクは低く! 志も低く。そんな仮想通貨の遊び方

DashCoinの最近の取引情報を集計してみる

 めんどくせーとボヤきつつ、ちょろちょろとコードを書いています。件のHitBTCですが、PHP用のライブラリしか用意してくれていないんだもんなぁ。
 
 とりあえずはデータの整形やらエラー処理をすっ飛ばしてはいるものの、マーケット情報を取得するクラスは一応書きました。これでようやくデータを貯めこんであれこれ集計したり機械学習に使ってみたりとできるようになったわけです。
 が、肝心の機械学習の方の学習がちっとも進んでいないので、今回はちょろっと日足のようなデータを出力する程度になってしまいます。

 APIを叩くクラスはURLを組み立ててjsonをもらってくるだけなので割愛。

最近の取引情報を1000件もらってきてデータベースに保存

# coding: utf-8

import sqlite3
from hitbtcapi.restreader import RestReader

dbcon = sqlite3.connect('recent.db')
cursol = dbcon.cursor()
cursol.execute('''
create table if not exists recent(
    tid integer primary key,
    price float,
    amount integer,
    time long,
    side varchar(4));
''')
dbcon.commit()

reader = RestReader()
recents = reader.recent()
try:
    for line in recents:
        tid = int(line['tid'])
        price = float(line['price'])
        amount = int(line['amount'])
        date = int(line['date'])
        side = line['side']

        cursol.execute('''
            insert into recent(tid, price, amount, time, side)
                values(?, ?, ?, ?, ?);
        ''', (tid, price, amount, date, side))
except Exception as e:
    print(e.args)
finally:
    dbcon.commit()
    dbcon.close()

一番新しいものから一日毎に(始値終値、高値、安値、出来高)を集計

# coding: utf-8
import sqlite3

try:
    dbcon = sqlite3.connect('recent.db')
    dbcon.row_factory = sqlite3.Row
    cursol = dbcon.cursor()

    cursol.execute('select * from recent order by time desc limit 1;')
    line = cursol.fetchone()
    linetime = line['time']

    while True:
        cursol.execute(
            'select * from recent where time between ? and ? order by time desc;',
            (linetime - 3600 * 24 * 1000 + 1, linetime))
        lines = cursol.fetchall()
        
        if not len(lines):
            break

        first = None
        last = None
        high = None
        low = None
        amount = 0
        for line in lines:
            price = round(line['price'] * 100000000, 1)
            if last is None:
                last = price
                high = price
                low = price
            if high < price: high = price
            if low > price: low = price
            first = price
            amount += line['amount']
                
        print('first:{0} last:{1} high:{2} low:{3} amount:{4}'
            .format(first, last, high, low, amount))
        
        linetime -= 3600 * 24 * 1000
            

except Exception as e:
    print(e.args)
finally:
    dbcon.close()

 一つ目のプログラムはHitBTCから直近のDashCoin取引についての情報を1000件もらってきてデータベースに保存しとります。
 元となるデータはこのURLで参照できますよ。

http://api.hitbtc.com/api/1/public/DSHBTC/trades/recent?max_result=1000&side=true

 もっともらってくればいいのに、と思うでしょうけどこれが最大件数だから仕方がない。もっと遡る場合は微妙に使いづらい別のパスを何度も叩く必要がありますが、今回はこれでいいでしょう。

 集計用のプログラムは、一番新しい取引情報を引っ張ってきてからその時刻から一日遡ったデータを集計しては次の一日、と繰り返しているだけです。
 走らせてみた結果がこちら。

:~/test$ python3 test_candle.py
first:778.9 last:702.1 high:787.9 low:659.0 amount:51088
first:660.7 last:779.0 high:787.9 low:641.0 amount:22193
first:675.0 last:660.0 high:788.0 low:560.0 amount:27501
first:600.0 last:788.0 high:799.2 low:570.0 amount:17761
first:557.3 last:562.5 high:784.2 low:557.0 amount:3471

 一番下の行の出来高が妙に少ないのは単にデータ不足なだけです。始値などの値はそのまま表示すると指数になって鬱陶しかったのでSatoshi表記にしてみました。
 今回のデータの中での最高値は7.992μBTCだそうで。

 やっすいなぁ、DashCoin

 そのおかげで数ドルあれば遊べるわけでもありますけどね。
 次はもうちょいデータを貯めて何か別のことをしてみる予定。少しくらいおもしろいことができるといいんですけどねー。