Last active
June 5, 2025 06:37
-
-
Save t-nissie/22a4a9525d423208c7f784ce507d0bc5 to your computer and use it in GitHub Desktop.
メルカリの暗号資産(ビットコイン、イーサリアム、XRP)の月間取引報告書CSVファイルから確定申告のために年間の損益計算用のCSVファイルを作るRubyスクリプト
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 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, xrp_buy.csv, xrp_sel.csv | |
# 申告: 出力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: | |
# * Use csv gem version 3.3.3 or higher. https://rubygems.org/gems/csv | |
# * 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 | |
# 2025年の月間取引報告書CSVファイルでテスト | |
## | |
class OutputCSV | |
def initialize(coin,bs) | |
@buy_or_sell = bs | |
@filename = coin+"_"+bs+".csv" | |
@file = open(@filename, "w"); | |
@file << "取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考\n" | |
@counter = 1 # Line number counter for the output CSV file | |
@total = 0.0 | |
@jpy = 0 | |
end | |
def transaction(line, add, yen) | |
@file << line | |
@counter += 1 | |
@total += add.to_f | |
@jpy += yen.to_i | |
end | |
def close() # Before closing, add a SUM line in the end of the file. | |
printf("%s has %3i transactions. Total: %.9f JPY: %7i\n", @filename, @counter-1, @total, @jpy) | |
if (@buy_or_sell=="buy") then | |
@file << ",,,,,=SUM(F2:F#{@counter}),,,=SUM(I2:I#{@counter}),,,,,,,,,\n" | |
elsif (@buy_or_sell=="sel") then | |
@file << ",,,,,,,=SUM(H2:H#{@counter}),=SUM(I2:I#{@counter}),,,,,,,,,\n" | |
end | |
@file.close | |
end | |
end | |
# コインの種類×2個のCSV出力ファイルを*2次元*hashとして用意 | |
files = {"btc"=>{}, "eth"=>{}, "xrp"=>{}} | |
files.each{|coin,h| | |
["buy", "sel"].each{|bs| h.store(bs,OutputCSV.new(coin,bs))} | |
} | |
# コマンドライン引数で与えられた月間取引報告書CSVファイルを読んで選別して出力ファイルへ書き出し | |
ARGF.each do |line| | |
row = line.split(',') | |
if (row[0] =~ /^20[2-9][0-9]/) then | |
files.each{|coin,h| | |
cu = coin.upcase | |
if (row[1]=="購入" and (row[3]=="#{cu}/JPY" or row[3]=="#{cu}/POINT")) then | |
h["buy"].transaction(line, row[5], row[8]) | |
elsif (row[1]=="受取" and row[3]==cu) then | |
h["buy"].transaction(line, row[5], row[8]) | |
h["sel"].transaction(line, row[7], row[8]) | |
elsif (row[1]=="売却" and row[3]=="#{cu}/JPY") then | |
h["sel"].transaction(line, row[7], row[8]) | |
end | |
} | |
end | |
end | |
# 全部の出力ファイルをclose | |
files.each{|coin,h| | |
h.each{|bs,f| f.close()} | |
} | |
#Local variables: | |
# compile-command: "./test2024.sh" | |
#End: |
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
実行例: | |
$ LANG=ja_JP.UTF-8 ruby mercari.rb report_2024?.csv report_20241?.csv | |
btc_buy.csv has 7 transactions. Total: 0.000411220 JPY: 4237 | |
btc_sel.csv has 5 transactions. Total: 0.000323540 JPY: 4680 | |
eth_buy.csv has 2 transactions. Total: 0.005641040 JPY: 2132 | |
eth_sel.csv has 2 transactions. Total: 0.000695680 JPY: 500 | |
xrp_buy.csv has 0 transactions. Total: 0.000000000 JPY: 0 | |
xrp_sel.csv has 0 transactions. Total: 0.000000000 JPY: 0 |
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.
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
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考 | |
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.
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
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考 | |
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.
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
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考 | |
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.
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
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考 | |
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.
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
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考 | |
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.
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
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考 | |
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.
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
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考 | |
,,,,,,,,,,,,,,,作成基準日時: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.
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
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考 | |
,,,,,,,,,,,,,,,作成基準日時: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.
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
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考 | |
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.
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
取引日時,取引種別,取引形態,通貨ペア,増加通貨名,増加数量,減少通貨名,減少数量,約定金額,約定価格,手数料通貨,手数料数量,登録番号,社名,備考 | |
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 |
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
#!/bin/bash | |
## | |
LANG=ja_JP.UTF-8 ruby mercari.rb report_2024?.csv report_20241?.csv && | |
wc -l btc_buy.csv | grep '9 btc_buy.csv' && | |
wc -l btc_sel.csv | grep '7 btc_sel.csv' && | |
wc -l eth_buy.csv | grep '4 eth_buy.csv' && | |
wc -l eth_sel.csv | grep '4 eth_sel.csv' && | |
echo -n -e " \033[1;32mmercaro.rb passed some tests!\033[0;39m\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment