#!/usr/bin/php 'KEUR', 'XXBT' => 'BTC' ); if (array_key_exists($in, $assets)) return $assets[$in]; else die('unknown asset '.$in."\n"); return $in; } function parse_to_int($string, $currency) { switch($currency) { case 'BTC': if (preg_match('/(-?)(\d+)\.(\d{8})00/', $string, $match)) { return intval($match[1].$match[2].$match[3]); } else die('unparsable BTC amount'."\n"); break; case 'KEUR': if (preg_match('/(-?)(\d+)\.(\d{4})/', $string, $match)) { return intval($match[1].$match[2].$match[3]); } else die('unparsable KEUR amount'."\n"); break; default: die("unknown asset type {$t['type']}\n"); } } function convert_KEUR2EUR($in) { if ($in%100) die("unable to convert, because input has fractional cents\n"); return intdiv($in, 100); } function print_from_int($in, $asset) { switch ($asset) { case 'BTC': $dec = 8; break; case 'KEUR': $dec = 4; break; case 'EUR': $dec = 2; break; default: die("unknown asset type $asset\n"); } $negative = 0; if ($in < 0) { $negative = 1; $in *= -1; } $d = pow(10, $dec); $whole = intdiv($in, $d); $frac = $in%$d; return (($negative?'-':' ')).$whole.'.'.str_pad($frac, $dec, '0', STR_PAD_LEFT); } foreach ($data['result']['ledger'] as $key => $entry) { $refid = $entry['refid']; if (!array_key_exists($refid, $transactions)) { //echo("new refid $refid\n"); $transactions[$refid] = array('type' => $entry['type'], 'postings' => array()); } if ($entry['aclass'] != 'currency') continue; $asset = dereference_asset($entry['asset']); $transactions[$refid]['postings'][$key] = array( 'time' => $entry['time'], 'asset' => $asset, 'amount' => parse_to_int($entry['amount'], $asset), 'fee' => parse_to_int($entry['fee'], $asset), 'balance' => parse_to_int($entry['balance'], $asset) ); //print_r($entry); //print_r($transactions[$refid]); } function avgtime($postings) { $count = 0; $time = 0; foreach ($postings as $p) { $time += $p['time']; $count++; } if ($count == 0) die('no data to compute average'."\n"); return $time/$count; } foreach (array_reverse($transactions) as $key => $t) { $postings = $t['postings']; $date= date('Y/m/d', avgtime($postings)); echo("; $key\n"); echo("$date * Kraken\n"); switch ($t['type']) { case 'deposit': if (count($postings) != 1) die("unsuppored amount of postings in deposit\n"); $id = array_key_first($postings); $posting = array_shift($postings); echo("\t; $id\n"); switch ($posting['asset']) { case 'KEUR': echo("\t{$bank}EUR".print_from_int(convert_KEUR2EUR(-$posting['amount']), 'EUR')."\n"); echo("\t{$kraken}KEUR".print_from_int($posting['amount'], 'KEUR')." = KEUR".print_from_int($posting['balance'], 'KEUR')."\n"); if ($posting['fee'] != 0) die("unexpected deposit fee\n"); break; default: die("unsupported asset for deposit {$posting['asset']}\n"); } break; case 'withdrawal': if (count($postings) != 1) die("unsupported amount of postings in withdrawal\n"); $id = array_key_first($postings); $posting = array_shift($postings); echo("\t; $id\n"); switch ($posting['asset']) { case 'BTC': echo("\t{$kraken}BTC".print_from_int($posting['amount'] - $posting['fee'], 'BTC')." = BTC".print_from_int($posting['balance'], 'BTC')."\n"); echo("\t{$wallet}BTC".print_from_int(-$posting['amount'], 'BTC')."\n"); echo("\t{$wfee}BTC".print_from_int($posting['fee'], 'BTC')."\n"); break; default: die("unsupported asset for withdrawel {$posting['asset']}\n"); } break; case 'trade': if (count($postings) != 2) { //print_r($postings); echo("WARNING: unsupported amount of postings in trade\n"); continue; } foreach ($postings as $id => $posting) { echo("\t; $id\n"); switch ($posting['asset']) { case 'BTC': if ($posting['amount'] < 0) die("selling BTC not yet implemented\n"); if ($posting['fee'] > 0) die("nonzero fee on BTC side while buying BTC not supported"); echo("\t{$kraken}BTC".print_from_int($posting['amount'], 'BTC')." = BTC".print_from_int($posting['balance'], 'BTC')."\n"); break; case 'KEUR': if ($posting['amount'] > 0) die("buying KEUR not yet implemented\n"); echo("\t{$kraken}KEUR".print_from_int($posting['amount'] - $posting['fee'], 'KEUR')." = KEUR".print_from_int($posting['balance'], 'KEUR')."\n"); echo("\t{$tfee}KEUR".print_from_int($posting['fee'], 'KEUR')."\n"); break; } } //print_r($postings); break; default: die("unknown transaction type {$t['type']}\n"); } echo("\n"); } //print_r($transactions); ?>