Skip to content

Instantly share code, notes, and snippets.

@t-nissie
Last active February 25, 2025 19:32
Show Gist options
  • Save t-nissie/22a4a9525d423208c7f784ce507d0bc5 to your computer and use it in GitHub Desktop.
Save t-nissie/22a4a9525d423208c7f784ce507d0bc5 to your computer and use it in GitHub Desktop.
メルカリの暗号資産(ビットコイン、イーサリアム)の月間取引報告書CSVファイルから確定申告のために年間の損益計算用のCSVファイルを作るRuby スクリプト
#!/usr/bin/env ruby
# coding: utf-8
# 目的 (Purpose): メルカリで暗号資産取引をしている人の確定申告 (Japanese tax return)
# 使い方 (Usage): LANG=ja_JP.UTF-8 ruby mercari.rb report_2024?.csv report_20241?.csv
# 出力CSVファイル: btc_buy.csv, btc_sel.csv, eth_buy.csv, eth_sel.csv を開いて計算された値を国税庁の総平均法用002.xlsxにコピペ
# Author: Takeshi Nishimatsu
# Licence: GPLv3
# 完全無保証: 税務署に怒られてもしりません
# 参考:
# https://help.jp.mercari.com/guide/articles/1513/ メルカル ビットコイン取引 取引報告書・損益計算方法
# ToDo:
# Use CSV library instead of split(','). But, note that:
# * Old CSV.filter can't read more than two files from ARGF. https://github.com/ruby/csv/issues/328
# * We can't remove any rows by CSV.filter. https://github.com/ruby/csv/discussions/293
# * We can't reset ARGF with ARGF.rewind. It just rewind a file, not whole files.
# Commonalize if lines for BTC and ETH, but 1 pass parse.
##
flag_header_not_yet = true
all_file = open( "all.csv", "w") # all transactions
btc_buy_file = open("btc_buy.csv", "w"); btc_buy_count=1 # all buys of btc
btc_sel_file = open("btc_sel.csv", "w"); btc_sel_count=1 # all sells of btc
eth_buy_file = open("eth_buy.csv", "w"); eth_buy_count=1 # all buys of eth
eth_sel_file = open("eth_sel.csv", "w"); eth_sel_count=1 # all sells of eth
ARGF.each do |line|
row = line.split(',')
if (flag_header_not_yet) then
all_file << line
btc_buy_file << line
btc_sel_file << line
eth_buy_file << line
eth_sel_file << line
flag_header_not_yet = false
elsif (row[0] =~ /^20[2-9][0-9]/) then
all_file << line
if (row[1]=="購入" and (row[3]=="BTC/JPY" or row[3]=="BTC/POINT")) then
btc_buy_file << line; btc_buy_count+=1
elsif (row[1]=="受取" and row[3]=="BTC") then
btc_buy_file << line; btc_buy_count+=1
btc_sel_file << line; btc_sel_count+=1
elsif (row[1]=="売却" and row[3]=="BTC/JPY") then
btc_sel_file << line; btc_sel_count+=1
elsif (row[1]=="購入" and (row[3]=="ETH/JPY" or row[3]=="ETH/POINT")) then
eth_buy_file << line; eth_buy_count+=1
elsif (row[1]=="受取" and row[3]=="ETH") then
eth_buy_file << line; eth_buy_count+=1
eth_sel_file << line; eth_sel_count+=1
elsif (row[1]=="売却" and row[3]=="ETH/JPY") then
eth_sel_file << line; eth_sel_count+=1
end
end
end
btc_buy_file << ",,,,,=SUM(F2:F#{btc_buy_count}),,,=SUM(I2:I#{btc_buy_count}),,,,,,,,,\n"
btc_sel_file << ",,,,,,,=SUM(H2:H#{btc_sel_count}),=SUM(I2:I#{btc_sel_count}),,,,,,,,,\n"
eth_buy_file << ",,,,,=SUM(F2:F#{eth_buy_count}),,,=SUM(I2:I#{eth_buy_count}),,,,,,,,,\n"
eth_sel_file << ",,,,,,,=SUM(H2:H#{eth_sel_count}),=SUM(I2:I#{eth_sel_count}),,,,,,,,,\n"
all_file.close
btc_buy_file.close
btc_sel_file.close
eth_buy_file.close
eth_sel_file.close
#Local variables:
# compile-command: "./test2024.sh"
#End:
We can make this file beautiful and searchable if this error is corrected: It looks like row 4 should actually have 15 columns, instead of 18 in line 3.
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考
2024/10/31 14:58:17,入金,,JPY,JPY,1319,,,,,,,関東00030,株式会社メルコイン,
2024/10/31 14:58:19,購入,自己,BTC/JPY,BTC,0.00011633,JPY,1319,1319,11338135,,,関東00030,株式会社メルコイン,
,,,,,,,,,,,,,,,作成基準日時:2024/10/31 23:59:59,,
,,,,,,,,,,,,,,,作成対象期間:2024/10/1~2024/10/31,,
,,,,,,,,,,,,,,,月末残高,,
,,,,,,,,,,,,,,,JPY,BTC,ETH
,,,,,,,,,,,,,,,0,0.00035113,0.00564104
We can make this file beautiful and searchable if this error is corrected: It looks like row 6 should actually have 15 columns, instead of 18 in line 5.
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考
2024/11/8 10:36:00,売却,自己,ETH/JPY,JPY,300,ETH,0.00069568,300,431236,,,関東00030,株式会社メルコイン,
2024/11/8 10:36:23,出金,,JPY,,,JPY,300,,,,,関東00030,株式会社メルコイン,
2024/11/16 06:11:29,出金,,JPY,,,JPY,2700,,,,,関東00030,株式会社メルコイン,暗号資産決済
2024/11/16 06:11:29,売却,自己,BTC/JPY,JPY,2700,BTC,0.00019615,2700,13764977,,,関東00030,株式会社メルコイン,暗号資産決済
,,,,,,,,,,,,,,,作成基準日時:2024/11/30 23:59:59,,
,,,,,,,,,,,,,,,作成対象期間:2024/11/1~2024/11/30,,
,,,,,,,,,,,,,,,月末残高,,
,,,,,,,,,,,,,,,JPY,BTC,ETH
,,,,,,,,,,,,,,,0,0.00015498,0.00494536
We can make this file beautiful and searchable if this error is corrected: It looks like row 4 should actually have 15 columns, instead of 18 in line 3.
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考
2024/12/24 08:17:37,出金,,JPY,,,JPY,980,,,,,関東00030,株式会社メルコイン,暗号資産決済
2024/12/24 08:17:37,売却,自己,BTC/JPY,JPY,980,BTC,0.0000673,980,14562799,,,関東00030,株式会社メルコイン,暗号資産決済
,,,,,,,,,,,,,,,作成基準日時:2024/12/31 23:59:59,,
,,,,,,,,,,,,,,,作成対象期間:2024/12/1~2024/12/31,,
,,,,,,,,,,,,,,,月末残高,,
,,,,,,,,,,,,,,,JPY,BTC,ETH
,,,,,,,,,,,,,,,0,0.00008768,0.00494536
We can make this file beautiful and searchable if this error is corrected: It looks like row 6 should actually have 15 columns, instead of 17 in line 5.
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考
2024/3/17 16:07:32,受取,,BTC,BTC,0.00005032,,,500,9935037,,,関東00030,株式会社メルコイン,キャンペーン付与
2024/3/17 16:08:14,入金,,JPY,JPY,941,,,,,,,関東00030,株式会社メルコイン,
2024/3/17 16:08:38,購入,自己,BTC/JPY,BTC,0.0000947,JPY,941,941,9936499,,,関東00030,株式会社メルコイン,
2024/3/17 16:08:38,購入,自己,BTC/POINT,BTC,0.0000005,MERPAY_POINT,5,5,9936499,,,関東00030,株式会社メルコイン,
,,,,,,,,,,,,,,,作成基準日時:2024/3/31 23:59:59,
,,,,,,,,,,,,,,,作成対象期間:2024/3/1~2024/3/31,
,,,,,,,,,,,,,,,月末残高,
,,,,,,,,,,,,,,,JPY,BTC
,,,,,,,,,,,,,,,0,0.00014552
We can make this file beautiful and searchable if this error is corrected: It looks like row 4 should actually have 15 columns, instead of 17 in line 3.
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考
2024/4/26 18:42:54,出金,,JPY,,,JPY,100,,,,,関東00030,株式会社メルコイン,暗号資産決済
2024/4/26 18:42:54,売却,自己,BTC/JPY,JPY,100,BTC,0.00001011,100,9897581,,,関東00030,株式会社メルコイン,暗号資産決済
,,,,,,,,,,,,,,,作成基準日時:2024/4/30 23:59:59,
,,,,,,,,,,,,,,,作成対象期間:2024/4/1~2024/4/30,
,,,,,,,,,,,,,,,月末残高,
,,,,,,,,,,,,,,,JPY,BTC
,,,,,,,,,,,,,,,0,0.00013541
We can make this file beautiful and searchable if this error is corrected: It looks like row 4 should actually have 15 columns, instead of 18 in line 3.
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考
2024/5/27 11:37:00,入金,,JPY,JPY,582,,,,,,,関東00030,株式会社メルコイン,
2024/5/27 11:37:02,購入,自己,BTC/JPY,BTC,0.00005249,JPY,582,582,11086191,,,関東00030,株式会社メルコイン,
,,,,,,,,,,,,,,,作成基準日時:2024/5/31 23:59:59,,
,,,,,,,,,,,,,,,作成対象期間:2024/5/1~2024/5/31,,
,,,,,,,,,,,,,,,月末残高,,
,,,,,,,,,,,,,,,JPY,BTC,ETH
,,,,,,,,,,,,,,,0,0.0001879,0
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 15 columns, instead of 18 in line 1.
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考
,,,,,,,,,,,,,,,作成基準日時:2024/6/30 23:59:59,,
,,,,,,,,,,,,,,,作成対象期間:2024/6/1~2024/6/30,,
,,,,,,,,,,,,,,,月末残高,,
,,,,,,,,,,,,,,,JPY,BTC,ETH
,,,,,,,,,,,,,,,0,0.0001879,0
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 15 columns, instead of 18 in line 1.
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考
,,,,,,,,,,,,,,,作成基準日時:2024/7/31 23:59:59,,
,,,,,,,,,,,,,,,作成対象期間:2024/7/1~2024/7/31,,
,,,,,,,,,,,,,,,月末残高,,
,,,,,,,,,,,,,,,JPY,BTC,ETH
,,,,,,,,,,,,,,,0,0.0001879,0
We can make this file beautiful and searchable if this error is corrected: It looks like row 5 should actually have 15 columns, instead of 18 in line 4.
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考
2024/8/6 14:24:33,入金,,JPY,JPY,1932,,,,,,,関東00030,株式会社メルコイン,
2024/8/6 14:24:36,購入,自己,ETH/JPY,ETH,0.00511182,JPY,1932,1932,377947,,,関東00030,株式会社メルコイン,
2024/8/6 14:24:37,受取,,ETH,ETH,0.00052922,,,200,377914,,,関東00030,株式会社メルコイン,キャンペーン付与
,,,,,,,,,,,,,,,作成基準日時:2024/8/31 23:59:59,,
,,,,,,,,,,,,,,,作成対象期間:2024/8/1~2024/8/31,,
,,,,,,,,,,,,,,,月末残高,,
,,,,,,,,,,,,,,,JPY,BTC,ETH
,,,,,,,,,,,,,,,0,0.0001879,0.00564104
We can make this file beautiful and searchable if this error is corrected: It looks like row 7 should actually have 15 columns, instead of 18 in line 6.
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考
2024/9/16 13:02:59,出金,,JPY,,,JPY,400,,,,,関東00030,株式会社メルコイン,暗号資産決済
2024/9/16 13:02:59,売却,自己,BTC/JPY,JPY,400,BTC,0.00004998,400,8003826,,,関東00030,株式会社メルコイン,暗号資産決済
2024/9/20 11:48:41,入金,,JPY,JPY,690,,,,,,,関東00030,株式会社メルコイン,
2024/9/20 11:48:44,購入,自己,BTC/JPY,BTC,0.00007511,JPY,690,690,9186034,,,関東00030,株式会社メルコイン,
2024/9/20 11:48:44,購入,自己,BTC/POINT,BTC,0.00002177,MERPAY_POINT,200,200,9186034,,,関東00030,株式会社メルコイン,
,,,,,,,,,,,,,,,作成基準日時:2024/9/30 23:59:59,,
,,,,,,,,,,,,,,,作成対象期間:2024/9/1~2024/9/30,,
,,,,,,,,,,,,,,,月末残高,,
,,,,,,,,,,,,,,,JPY,BTC,ETH
,,,,,,,,,,,,,,,0,0.0002348,0.00564104
#!/bin/sh
##
LANG=ja_JP.UTF-8 ruby mercari.rb report_2024?.csv report_20241?.csv &&
wc -l all.csv | grep ' 25' &&
wc -l btc_buy.csv | grep ' 9' &&
wc -l btc_sel.csv | grep ' 7' &&
wc -l eth_buy.csv | grep ' 4' &&
wc -l eth_sel.csv | grep ' 4' &&
echo " \033[1;32m!!!ALL TESTS SUCCEEDED!!!\033[0;39m"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment