LoginSignup
0
1

More than 5 years have passed since last update.

仮想通貨の保有資産を定期的にメールで受け取る

Posted at

経緯

・貯金通帳を眺めるのが好き
・せっかくだし遊びの範囲で仮想通貨買ってみたい

先人の方々は売買用のAPIなどを用いて自動化などをされているかと思いますが今回はあくまで現在の保有資産をみて「へー、ほーん」ってなるだけのものを作ります。

利用サービス

・Zaif Exchange API(https://corp.zaif.jp/api-docs/)
Zaifという仮想通貨取引所が提供しているRESTAPIです。
現在の仮想通貨の価格の取得を行います。

・GoogleAppsScript(https://developers.google.com/apps-script/)
Googleが提供しているサーバレスで動作するJavascriptベースのサービスです。
ここからZaifAPIを呼び出して現在の総資産を計算し、Gmail宛にメールを送ります。

コード

zaif.gs
//保有資産
xem = xxxx.xxxx
btc = xxxx.xxxx
mona= xxxx.xxxx
eth = xxxx.xxxx
zaif = xxxx.xxxx

//購入時金額
purchase_price_xem = 10
purchase_price_btc = 10
purchase_price_mona = 10
purchase_price_eth = 10
purchase_price_zaif = 10

///API URL
btc_url = "https://api.zaif.jp/api/1/ticker/btc_jpy"
xem_url = "https://api.zaif.jp/api/1/ticker/xem_jpy"
mona_url = "https://api.zaif.jp/api/1/ticker/mona_jpy"
eth_url = "https://api.zaif.jp/api/1/ticker/eth_jpy"

function calc_asset(asset, url, buy){
  res = UrlFetchApp.fetch(url)
  ticker = JSON.parse(res.getContentText())
  last = ticker['last'] * asset
  high = ticker['high'] * asset
  low = ticker['low'] * asset

  str = "    最新値            :" + last + "\n"
  str = str + "直近24時間最高値:" + high + "\n"
  str = str + "直近24時間最低値:" + low + "\n"
  return str
}

function myFunction() {
  mail_body = ""

  header = "* * * BTC * * *\n"
  body = calc_asset(btc, btc_url, purchase_price_btc)
  footer = "* * * * * * * * *\n\n\n"

  mail_body = header + body + footer

  header = "* * * XEM * * *\n"
  body = calc_asset(xem, xem_url, purchase_price_xem)
  footer = "* * * * * * * * *\n\n\n"
  mail_body = mail_body + header + body + footer

  header = "* * * ETH * * *\n"
  body = calc_asset(eth, eth_url, purchase_price_eth)
  footer = "* * * * * * * * *\n\n\n"
  mail_body = mail_body + header + body + footer

  header = "* * * MONA * * *\n"
  body = calc_asset(mona, mona_url, purchase_price_mona)
  footer = "* * * * * * * * *\n\n\n"  
  mail_body = mail_body + header + body + footer

  Logger.log(mail_body)
  MailApp.sendEmail('hogehoge@gmail.com', '仮想通貨保有資産のお知らせ', mail_body);

}

中身はすごくシンプルです。

res = UrlFetchApp.fetch(url)
ticker = JSON.parse(res.getContentText())

UrlFetchApp.fetchで指定のURLにアクセスし、tickerの情報を取得します。

戻り値は下記のようになっているので

JSONディクショナリを返します:

last – last price : 終値
high – last 24 hours price high : 過去24時間の高値
low – last 24 hours price low : 過去24時間の安値
vwap – last 24 hours volume weighted average price : 過去24時間の加重平均(詳細は下記)
volume – last 24 hours volume : 過去24時間の出来高
bid – highest buy order : 買気配値
ask – lowest sell order : 売気配値

現在持っている資産と終値や直近の高値などをかけ合わせた文章を作成し

MailApp.sendEmail('宛先', 'Title', 'Body');

でGmailへ送信します。

実行するとメールが送られてきます。
実際に送られてきたメールがこちら
kasou.PNG

これを時間主導型でイベントトリガーを設定しておけば12時間毎に通知メールが届くようになります。

終わりに

拡張の候補として売買APIの追加やスプレッドシートの書き出しなどしてみても楽しいのではないかと思います。
web通帳よりも実際の通帳を見るほうがワクワクするのでエクセル送ったら通帳作ってくれるサービスとかないかなーとか考えながら書きました。

以上、最後までありがとうございました。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1