Last active
December 21, 2017 05:27
-
-
Save vtno/db49daf7e80a993b8e94aeb1e7d1817f to your computer and use it in GitHub Desktop.
(WIP) Calculate realized profit and some other values from BX transaction csv
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
# Run it like this in shell | |
# | |
# PATH_TO_REPORT=xxx/bx-999999.csv bx_port_calculation | |
# | |
# Notes: | |
# - PATH_TO_REPORT is relative to your home directory. | |
# - Change currencies to match your portfolio | |
# - WIP so it's probably need more work. | |
require 'csv' | |
report_path = File.join(ENV['HOME'], ENV['PATH_TO_REPORT']) | |
port_headers = ['THB invested', 'Profit BTC', 'Profit ETH', 'Profit OMG', 'Profit XRP', 'Profit REP', 'Profit XZC'] | |
currencies = { THB: 0, BTC: 1, ETH: 2, OMG: 3, XRP: 4, REP: 5, XZC: 6 } | |
deposit = 0 | |
withdraw = 0 | |
sum_profit_by_coin = Array.new(7, 0) | |
def buy_sell_text(amount) | |
amount > 0 ? 'Buy' : 'Sell' | |
end | |
def depo_withd_text(amount) | |
amount > 0 ? 'Deposit' : 'Withdraw' | |
end | |
CSV.foreach(report_path) do |row| | |
if row[2] == 'THB' && row[-2] == 'Trade' | |
columns = row[-1].split(' ') | |
currency = columns[1].to_sym | |
thb_amount = row[1].delete(',').to_f | |
sum_index = currencies[currency] | |
sum_profit_by_coin[sum_index] += thb_amount | |
puts "#{buy_sell_text(thb_amount)} - #{currency}: #{thb_amount} THB" | |
end | |
if (row[-2] == 'Deposit' || row[-2] == 'Withdraw') && row[2] == 'THB' | |
thb_invest = row[1].delete(',').to_f | |
sum_profit_by_coin[0] += thb_invest | |
if row[-2] == 'Deposit' | |
deposit += thb_invest | |
else | |
withdraw += thb_invest | |
end | |
puts "#{depo_withd_text(thb_invest)} #{thb_invest} THB" | |
end | |
end | |
CSV.open("bx_port_#{Time.now.strftime('%d%-m%y%H%M%S')}.csv", 'w') do |csv| | |
csv << port_headers | |
csv << sum_profit_by_coin | |
end | |
puts "Total investment: #{deposit}" | |
puts "Realized profit: #{-withdraw}" | |
puts 'Sum profit by coin:' | |
port_headers.each { |h| print h.to_s + " " } | |
puts '' | |
sum_profit_by_coin.each { |p| print p.to_s + " " } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment