Advertisement
Guest User

bitfinex2ledger.py

a guest
Dec 30th, 2013
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.61 KB | None | 0 0
  1. import json
  2. import re
  3. import datetime
  4. import locale
  5.  
  6. #set locale to en_US/english for date formatting
  7. #Linux:
  8. #locale.setlocale(locale.LC_TIME, "en_US")
  9. #Windows:
  10. locale.setlocale(locale.LC_TIME, "english")
  11.  
  12.  
  13. def process_ledger(ledgerjson, currency):
  14.     for entry in ledgerjson:
  15.         #different texts:
  16.  
  17.         #lending
  18.         #Interest Payment on wallet deposit
  19.         if re.match(r"^Interest Payment on wallet (deposit|trading|exchange)$", entry[0]):
  20.             wallet = re.match(r"^Interest Payment on wallet (deposit|trading|exchange)$", entry[0]).group(1).capitalize()
  21.            
  22.             print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " Interest received for lending " + currency
  23.             print "\tAssets:Bitfinex:" + wallet + "\t" + entry[1] + " " + currency
  24.             print "\tIncome:Bitfinex:Lending"
  25.  
  26.         #affiliation
  27.         #Earned fees from user xxx on wallet trading
  28.         elif re.match(r"^Earned fees from user \d+ on wallet (deposit|trading|exchange)$",entry[0]):
  29.             userid = re.match(r"^Earned fees from user (\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(1).capitalize()
  30.             wallet = re.match(r"^Earned fees from user (\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(2).capitalize()
  31.            
  32.             print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " Affiliation income in " + currency
  33.             print "\tAssets:Bitfinex:" + wallet + "\t" + entry[1] + " " + currency
  34.             print "\tIncome:Bitfinex:Affiliation:User" + userid
  35.  
  36.         #trading
  37.         #Trading fees for xxx.xxx BTC @ xxx.xxx on BFX (x.xx%) on wallet trading
  38.         elif re.match(r"^Trading fees for \d+\.\d+ (BTC|USD|LTC) @ \d+\.\d+ on (BFX|BSTP|MTGOX) \(\d+\.\d+%\) on wallet (deposit|trading|exchange)$",entry[0]):
  39.             wallet = re.match(r"^Trading fees for (\d+\.\d+) (BTC|USD|LTC) @ (\d+\.\d+) on (BFX|BSTP|MTGOX) \((\d+\.\d+)%\) on wallet (deposit|trading|exchange)$", entry[0]).group(6).capitalize()
  40.            
  41.             print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " Paid trading fees in "  + currency + " for a trade"
  42.             #entry[2] - this is a debit transaction!
  43.             print "\tAssets:Bitfinex:" + wallet + "\t-" + entry[2] + " " + currency
  44.             print "\tExpenses:Bitfinex:Fees:Trade"
  45.  
  46.         #Position #xxx swap on wallet trading
  47.         elif re.match(r"^Position #\d+ swap on wallet (deposit|trading|exchange)$",entry[0]):
  48.             positionid = re.match(r"^Position #(\d+) swap on wallet (deposit|trading|exchange)$", entry[0]).group(1)
  49.             wallet = re.match(r"^Position #(\d+) swap on wallet (deposit|trading|exchange)$", entry[0]).group(2).capitalize()
  50.        
  51.             print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + positionid + ") Paid swap fees in "  + currency + " for a trade"
  52.             #entry[2] - this is a debit transaction!
  53.             print "\tAssets:Bitfinex:" + wallet + "\t-" + entry[2] + " " + currency
  54.             print "\tExpenses:Bitfinex:Fees:Swap"
  55.  
  56.         #Position #xxx closed @ xxx.xxx on wallet trading
  57.         elif re.match(r"^Position #\d+ closed @ \d+\.\d+ on wallet (deposit|trading|exchange)$",entry[0]):
  58.             positionid = re.match(r"^Position #(\d+) closed @ (\d+\.\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(1)
  59.             wallet = re.match(r"^Position #(\d+) closed @ (\d+\.\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(3).capitalize()
  60.        
  61.             print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + positionid + ") Closed a trade and settled in "  + currency
  62.             if entry[1] == "":
  63.                 #entry[2] - this is a debit transaction!
  64.                 print "\tAssets:Bitfinex:" + wallet + "\t-" + entry[2] + " " + currency
  65.             else:
  66.                 print "\tAssets:Bitfinex:" + wallet + "\t" + entry[1] + " " + currency
  67.             print "\tIncome:Bitfinex:Trades:Close"
  68.  
  69.         #Position #xxx claimed @ xxx.xxx on wallet trading
  70.         elif re.match(r"^Position #\d+ claimed @ \d+\.\d+ on wallet (deposit|trading|exchange)$",entry[0]):
  71.             positionid = re.match(r"^Position #(\d+) claimed @ (\d+\.\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(1)
  72.             wallet = re.match(r"^Position #(\d+) claimed @ (\d+\.\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(3).capitalize()
  73.        
  74.             print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + positionid + ") Claimed a trade and settled in "  + currency
  75.             if entry[1] == "":
  76.                 #entry[2] - this is a debit transaction!
  77.                 print "\tAssets:Bitfinex:" + wallet + "\t-" + entry[2] + " " + currency
  78.             else:
  79.                 print "\tAssets:Bitfinex:" + wallet + "\t" + entry[1] + " " + currency
  80.             print "\tIncome:Bitfinex:Trades:Claim"
  81.  
  82.         #Position #xxx claimed @ xxx.xxx (fee: xxx.xxx BTC) on wallet trading
  83.         elif re.match(r"^Position #\d+ claimed @ \d+\.\d+ \(fee: \d+\.\d+ (BTC|USD|LTC)\) on wallet (deposit|trading|exchange)$",entry[0]):
  84.             positionid = re.match(r"^Position #(\d+) claimed @ (\d+\.\d+) \(fee: (\d+\.\d+) (BTC|USD|LTC)\) on wallet (deposit|trading|exchange)$", entry[0]).group(1)
  85.             fee = re.match(r"^Position #(\d+) claimed @ (\d+\.\d+) \(fee: (\d+\.\d+) (BTC|USD|LTC)\) on wallet (deposit|trading|exchange)$", entry[0]).group(3)
  86.             feecurrency = re.match(r"^Position #(\d+) claimed @ (\d+\.\d+) \(fee: (\d+\.\d+) (BTC|USD|LTC)\) on wallet (deposit|trading|exchange)$", entry[0]).group(4)
  87.             wallet = re.match(r"^Position #(\d+) claimed @ (\d+\.\d+) \(fee: (\d+\.\d+) (BTC|USD|LTC)\) on wallet (deposit|trading|exchange)$", entry[0]).group(5).capitalize()
  88.        
  89.             print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + positionid + ") Claimed a trade and settled in "  + currency
  90.             if entry[1] == "":
  91.                 #entry[2] - this is a debit transaction!
  92.                 print "\tAssets:Bitfinex:" + wallet + "\t-" + entry[2] + " " + currency
  93.             else:
  94.                 print "\tAssets:Bitfinex:" + wallet + "\t" + entry[1] + " " + currency
  95.             print "\tIncome:Bitfinex:Trades:Claim"
  96.  
  97.             # seems like the fee is already deducted in the displayed amount,
  98.             # we first need to credit it from income, then deduct it to expenses
  99.             print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + positionid + ") Received fee for claimed trade in "  + currency
  100.             print "\tAssets:Bitfinex:" + wallet + "\t" + fee + " " + feecurrency
  101.             print "\tIncome:Bitfinex:Trades:Claim"
  102.             print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + positionid + ") Paid fee for claimed trade in "  + currency
  103.             print "\tAssets:Bitfinex:" + wallet + "\t-" + fee + " " + feecurrency
  104.             print "\tExpenses:Bitfinex:Fees:Claim"
  105.  
  106.         #Settlement @ xxx.xxx on wallet trading
  107.         elif re.match(r"^Settlement @ \d+\.\d+ on wallet (deposit|trading|exchange)$",entry[0]):
  108.             wallet = re.match(r"^Settlement @ (\d+\.\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(2).capitalize()
  109.        
  110.             print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " Force liquidated a trade and settled in "  + currency
  111.             if entry[1] == "":
  112.                 #entry[2] - this is a debit transaction!
  113.                 print "\tAssets:Bitfinex:" + wallet + "\t-" + entry[2] + " " + currency
  114.             else:
  115.                 print "\tAssets:Bitfinex:" + wallet + "\t" + entry[1] + " " + currency
  116.             print "\tIncome:Bitfinex:Trades:Settle"
  117.  
  118.         #external deposits/withdrawals
  119.         #Wire Transfer Withdrawal #xxx on wallet deposit
  120.         elif re.match(r"^Wire Transfer Withdrawal #\d+ on wallet (deposit|trading|exchange)$",entry[0]):
  121.             withdrawalid = re.match(r"^Wire Transfer Withdrawal #(\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(1)
  122.             wallet = re.match(r"^Wire Transfer Withdrawal #(\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(2).capitalize()
  123.            
  124.             print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + withdrawalid +") Wire withdrawal of " + currency
  125.             #entry[2] - this is a debit transaction!
  126.             print "\tAssets:Bitfinex:" + wallet + "\t-" + entry[2] + " " + currency
  127.             #you might want to change this to the account your payment actually went to
  128.             print "\tAssets:Yourbank:Bitfinex:Withdrawals"
  129.            
  130.         #Bitcoin Withdrawal #xxx on wallet deposit
  131.         elif re.match(r"^Bitcoin Withdrawal #\d+ on wallet (deposit|trading|exchange)$",entry[0]):
  132.             withdrawalid = re.match(r"^Bitcoin Withdrawal #(\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(1)
  133.             wallet = re.match(r"^Bitcoin Withdrawal #(\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(2).capitalize()
  134.            
  135.             print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + withdrawalid +") Bitcoin withdrawal of " + currency
  136.             #entry[2] - this is a debit transaction!
  137.             print "\tAssets:Bitfinex:" + wallet + "\t-" + entry[2] + " " + currency
  138.             #you might want to change this to the account your payment actually went to
  139.             print "\tAssets:Bitcoin:Bitfinex:Withdrawals"
  140.  
  141.         #Deposit (WIRE) #xxx on wallet deposit
  142.         elif re.match(r"^Deposit \(WIRE\) #\d+ on wallet (deposit|trading|exchange)$",entry[0]):
  143.             depositid = re.match(r"^Deposit \(WIRE\) #(\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(1)
  144.             wallet = re.match(r"^Deposit \(WIRE\) #(\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(2).capitalize()
  145.            
  146.             print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + depositid +") Wire deposit of " + currency
  147.             print "\tAssets:Bitfinex:" + wallet + "\t" + entry[1] + " " + currency
  148.             #you might want to change this to the account your payment actually originated from
  149.             print "\tAssets:Yourbank:Bitfinex:Deposits"
  150.            
  151.         #Deposit (BITCOIN) #xxx on wallet deposit
  152.         elif re.match(r"^Deposit \(BITCOIN\) #\d+ on wallet (deposit|trading|exchange)$",entry[0]):
  153.             depositid = re.match(r"^Deposit \(BITCOIN\) #(\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(1)
  154.             wallet = re.match(r"^Deposit \(BITCOIN\) #(\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(2).capitalize()
  155.            
  156.             print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + depositid +") Bitcoin deposit of " + currency
  157.             print "\tAssets:Bitfinex:" + wallet + "\t" + entry[1] + " " + currency
  158.             #you might want to change this to the account your payment actually originated from
  159.             print "\tAssets:Bitcoin:Bitfinex:Deposits"
  160.  
  161.         #internal movement between accounts
  162.         #warning: entries are always in pairs here! One for crediting one account, one debiting the other
  163.         #probably just use one of both, discard the other
  164.         #Transfer of x.xxx USD from wallet trading to deposit on wallet deposit
  165.         elif re.match(r"^Transfer of \d+\.\d+ (BTC|USD|LTC) from wallet (deposit|trading|exchange) to (deposit|trading|exchange) on wallet (deposit|trading|exchange)$",entry[0]):
  166.             if entry[1] == "":
  167.                 #No credit, this is the second line for the transfer.
  168.                 #It is assumed that the debit transaction was captured too and is processed.
  169.                 None
  170.             else:
  171.                 amount = re.match(r"^Transfer of (\d+\.\d+) (BTC|USD|LTC) from wallet (deposit|trading|exchange) to (deposit|trading|exchange) on wallet (deposit|trading|exchange)$", entry[0]).group(1)
  172.                 currency = re.match(r"^Transfer of (\d+\.\d+) (BTC|USD|LTC) from wallet (deposit|trading|exchange) to (deposit|trading|exchange) on wallet (deposit|trading|exchange)$", entry[0]).group(2)
  173.                 fromwallet = re.match(r"^Transfer of (\d+\.\d+) (BTC|USD|LTC) from wallet (deposit|trading|exchange) to (deposit|trading|exchange) on wallet (deposit|trading|exchange)$", entry[0]).group(3).capitalize()
  174.                 towallet = re.match(r"^Transfer of (\d+\.\d+) (BTC|USD|LTC) from wallet (deposit|trading|exchange) to (deposit|trading|exchange) on wallet (deposit|trading|exchange)$", entry[0]).group(4).capitalize()
  175.  
  176.                 print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " Moving " + currency + " between wallets"
  177.                 print "\tAssets:Bitfinex:" + fromwallet + "\t-" + entry[1] + " " + currency
  178.                 print "\tAssets:Bitfinex:" + towallet + "\t" + entry[1] + " " + currency
  179.  
  180.         else:
  181.             print "UNKNOWN ENTRY... DATA: ", entry
  182.  
  183.  
  184. with open("usd.json", "rb") as jsonfile:
  185.     ledgerjson = json.loads(jsonfile.read())
  186. currency = "USD"
  187.  
  188. process_ledger(ledgerjson, currency)
  189.  
  190. with open("btc.json", "rb") as jsonfile:
  191.     ledgerjson = json.loads(jsonfile.read())
  192. currency = "BTC"
  193.  
  194. process_ledger(ledgerjson, currency)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement