Author

Topic: [code available] PHP & Perl Scripts to read mtgox .csv files! (Read 2997 times)

newbie
Activity: 41
Merit: 0
Can someone explain how to use those scripts?

I'm using windows 7, but if linux is required I can install debian for instance on Vbox
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank


if someone was willing to host this (it's a perl script), i'd be awesomely happy.


Code:
#!/usr/bin/perl
use strict;
use CGI;
open BTC, "history_BTC.csv";
my %array;
#my @type = ("deposit", "withdraw", "in", "out", "fee");
if ($ENV{'REQUEST_METHOD'} eq "GET") {
        print "Content-Type: text/html\n\n
Choose File To Upload (Total Max: 2M):
BTC:
USD:
EUR:
";
}
else{
$CGI::POST_MAX = 1024 * 2048;
my $query = new CGI;
my $btc = $query->upload("btc");
my $usd = $query->upload("usd");
my $eur = $query->upload("eur");
print "Content-Type: text/html\n\nBTC:
";
while (<$btc>) {
        my ($date, $type, $info, $value, $balance) = ($_ =~ /^\d+,\"(\d{4}-\d{2}-\d{2}) .+?\",(.+?),(.+?),(.+?),(.+)/);
        $date || next;
        if (my ($x, $y) = $value =~ /(.*)E(.*)/) {
                $value = $x * (10 ** $y);
        }
        $array{$date}{$type} += $value;
        $array{$date}{balance} = $balance;
        if ($type eq "in") {
                my ($price) = ($info =~ /at \$?(\d+\.\d+)/);
                if ($info =~ /\$/) {
                        $array{$date}{totalspentusd} += ($price * $value);
                        $array{$date}{totalboughtwithusd} += $value;
                }
                else {
                        $array{$date}{totalspenteur} += ($price * $value);
                        $array{$date}{totalboughtwitheur} += $value;
                }
        }
        elsif ($type eq "out") {
                my ($price) = ($info =~ /at \$?(\d+\.\d+)/);
                if ($info =~ /\$/) {
                        $array{$date}{totalreceiveusd} += ($price * $value);
                        $array{$date}{totalsoldforusd} += $value;
                }
                else {
                        $array{$date}{totalreceiveeur} += ($price * $value);
                        $array{$date}{totalsoldforeur} += $value;
                }
        }
}


foreach (sort keys %array) {
        if ($array{$_}{deposit} > 0) {
                print "\"$_\",\"DEPOSIT\",\"". sprintf("%.8f", $array{$_}{deposit})."\"
\n";
        }
        if ($array{$_}{withdraw} > 0) {
                print "\"$_\",\"WITHDRAW\",\"". sprintf("%.8f", $array{$_}{withdraw})."\"
\n";
        }
        if ($array{$_}{fee} > 0) {
                print "\"$_\",\"FEES\",\"". sprintf("%.8f", $array{$_}{fee})."\"
\n";
        }
        if ($array{$_}{totalboughtwithusd} > 0) {
                print "\"$_\",\"BUY\",\"". sprintf("%.8f", $array{$_}{totalboughtwithusd})."\",\"". $array{$_}{totalspentusd}/$array{$_}{totalboughtwithusd} ." USD\"
\n";
        }
        if ($array{$_}{totalboughtwitheur} > 0) {
                print "\"$_\",\"BUY\",\"". sprintf("%.8f", $array{$_}{totalboughtwitheur})."\",\"". $array{$_}{totalspenteur}/$array{$_}{totalboughtwitheur} ." EUR\"
\n";
        }
        if ($array{$_}{totalsoldforusd} > 0) {
                print "\"$_\",\"SELL\",\"". sprintf("%.8f", $array{$_}{totalsoldforusd})."\",\"". $array{$_}{totalreceiveusd}/$array{$_}{totalsoldforusd} ." USD\"
\n";
        }
        if ($array{$_}{totalsoldforeur} > 0) {
                print "\"$_\",\"SELL\",\"". sprintf("%.8f", $array{$_}{totalsoldforeur})."\",\"". $array{$_}{totalreceiveeur}/$array{$_}{totalsoldforeur} ." EUR\"
\n";
        }
}
for (keys %array)
    {
        delete $array{$_};
    }
print "

USD:
";
while (<$usd>) {
        my ($date, $type, $info, $value, $balance) = ($_ =~ /^\d+,\"(\d{4}-\d{2}-\d{2}) .+?\",(.+?),(.+?),(.+?),(.+)/);
        $date || next;
        if (my ($x, $y) = $value =~ /(.*)E(.*)/) {
                $value = $x * (10 ** $y);
        }
        $array{$date}{$type} += $value;
        $array{$date}{balance} = $balance;
        if ($type eq "spent") {
                my ($btc, $price) = ($info =~ /(\d+\.\d+).*?BTC at \$?(\d+\.\d+)/);
                $array{$date}{totalspent} += $value;
                $array{$date}{totalboughtbtc} += $btc;
        }
        elsif ($type eq "earned") {
                my ($btc, $price) = ($info =~ /(\d+\.\d+).*?BTC at \$?(\d+\.\d+)/);
                $array{$date}{totalreceive} += $value;
                $array{$date}{totalsoldbtc} += $btc;
        }
}
foreach (sort keys %array) {

        if ($array{$_}{deposit} > 0) {
                print "\"$_\",\"DEPOSIT\",\"". sprintf("%.8f", $array{$_}{deposit}) . " USD\"
\n";
        }
        if ($array{$_}{withdraw} > 0) {
                print "\"$_\",\"WITHDRAW\",\"". sprintf("%.8f", $array{$_}{withdraw}) . " USD\"
\n";
        }
        if ($array{$_}{fee} > 0) {
                print "\"$_\",\"FEES\",\"". sprintf("%.8f", $array{$_}{fee}) . " USD\"
\n";
        }
        if ($array{$_}{totalboughtbtc} > 0) {
                print "\"$_\",\"BUY\",\"". sprintf("%.8f", $array{$_}{totalboughtbtc}) . " BTC\",\"". $array{$_}{totalspent}/$array{$_}{totalboughtbtc} ." USD\"
\n";
        }
        if ($array{$_}{totalsoldbtc} > 0) {
                print "\"$_\",\"SELL\",\"". sprintf("%.8f", $array{$_}{totalsoldbtc}) . " BTC\",\"". $array{$_}{totalreceive}/$array{$_}{totalsoldbtc} ." USD\"
\n";
        }
}
for (keys %array)
    {
        delete $array{$_};
    }
print "

EUR:
";
while (<$eur>) {
        my ($date, $type, $info, $value, $balance) = ($_ =~ /^\d+,\"(\d{4}-\d{2}-\d{2}) .+?\",(.+?),(.+?),(.+?),(.+)/);
        $date || next;
        if (my ($x, $y) = $value =~ /(.*)E(.*)/) {
                $value = $x * (10 ** $y);
        }
        $array{$date}{$type} += $value;
        $array{$date}{balance} = $balance;
        if ($type eq "spent") {
                my ($btc, $price) = ($info =~ /(\d+\.\d+).*?BTC at \$?(\d+\.\d+)/);
                $array{$date}{totalspent} += $value;
                $array{$date}{totalboughtbtc} += $btc;
        }
        elsif ($type eq "earned") {
                my ($btc, $price) = ($info =~ /(\d+\.\d+).*?BTC at \$?(\d+\.\d+)/);
                $array{$date}{totalreceive} += $value;
                $array{$date}{totalsoldbtc} += $btc;
        }
}
foreach (sort keys %array) {

        if ($array{$_}{deposit} > 0) {
                print "\"$_\",\"DEPOSIT\",\"". sprintf("%.8f", $array{$_}{deposit}) . " EUR\"
\n";
        }
        if ($array{$_}{withdraw} > 0) {
                print "\"$_\",\"WITHDRAW\",\"". sprintf("%.8f", $array{$_}{withdraw}) . " EUR\"
\n";
        }
        if ($array{$_}{fee} > 0) {
                print "\"$_\",\"FEES\",\"". sprintf("%.8f", $array{$_}{fee}) . " EUR\"
\n";
        }
        if ($array{$_}{totalboughtbtc} > 0) {
                print "\"$_\",\"BUY\",\"". sprintf("%.8f", $array{$_}{totalboughtbtc}) . " BTC\",\"". $array{$_}{totalspent}/$array{$_}{totalboughtbtc} ." EUR\"
\n";
        }
        if ($array{$_}{totalsoldbtc} > 0) {
                print "\"$_\",\"SELL\",\"". sprintf("%.8f", $array{$_}{totalsoldbtc}) . " BTC\",\"". $array{$_}{totalreceive}/$array{$_}{totalsoldbtc} ." EUR\"
\n";
        }
}


print "";
}
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
Bounty received. Thanks! I'll continue to develop the web interface for it

how's that goin' ? :-D
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
legendary
Activity: 924
Merit: 1004
Firstbits: 1pirata
Bounty received. Thanks! I'll continue to develop the web interface for it

Quoted for posterity...
legendary
Activity: 1792
Merit: 1111
Bounty received. Thanks! I'll continue to develop the web interface for it
legendary
Activity: 924
Merit: 1004
Firstbits: 1pirata
Bounty received.
Nice guy to work with and he paid promptly too.

Witnessed
hero member
Activity: 576
Merit: 514
Bounty received.
Nice guy to work with and he paid promptly too.
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
task done, bounty awarded.
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
two people pretty much nailed it.
i'd say that some modifications pending both are eligible for the reward.


i'd like to host both in a html file on my server so i can make them publicly available.
if both of you could provide upload and export functionality so i everyone can use this?

aside: i dunno if my webspace (bitcoinsinberlin.com) runs perl, actually.
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
obv the above makes no sense when working with headers, so here:

DATE;DEPOSIT;amountdepositCurrency
DATE;BUY;boughtBTC;avgPriceCurrency
DATE;SELL;soldBTC;avgPriceCurrency
DATE;FEES;feespaid
DATE;WITHDRAW;amountwithdrawnCurrency


legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
after much deliberation, i need the file for each currency to read


DATE; depositedCurreny
DATE; boughtBTC; avgPrice
DATE; soldBTC; avgPrice
DATE; feesPAID
DATE; withdrawnCurrency

whereas
"boughtBTC" should be positive
"soldBTC" is a negative number

legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
here's a script user gweedo kindly sent to me. he allowed me to post it - if it leads to the solution, he says, he'd like to have half the bounty :-D


Code:
#!/usr/bin/php
$file_handle fopen($argv[1], "r");
$array= array();
$replace = array("$",".",",");
while(!
feof($file_handle)) {
$line_of_text fgetcsv($file_handle1024);
if(
$line_of_text[0]!="Index"){
$temparray=explode(' ',$line_of_text[1]);
$date=$temparray[0];
if(!in_array($date$temparray)){
$array[$date]=array();
}
if($array[$date][$line_of_text[2]]!==null){
$temp=$array[$date][$line_of_text[2]];
$array[$date][$line_of_text[2]]=$temp+floatval(str_replace($replace,"",trim($line_of_text[4])));
}else{
$array[$date][$line_of_text[2]]=floatval(str_replace($replace,"",trim($line_of_text[4])));
}
}
}
fclose($file_handle);
foreach (
$array as $key=>$value){
if($key==null){
break;
}
$total=0.00;
$total=+$value['despoit']-$value['withdraw']+$value['in']-$value['out'];
echo $key.' Total: '.$total."\n";
if($value['despoit']!==null){
echo $key.' Despoit '.$value['despoit']."\n";
}
if($value['withdraw']!==null){
echo $key.' Withdrew '.$value['withdraw']."\n";
}
}
?>
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
So far I have gotten a number of responses.
None have gotten "it" right so far, so there is still a chance :-D
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
i did get a first reply, will test the result now.
if i like it, i will publish the script for anyone to work on it.
full member
Activity: 182
Merit: 100
the files that i can export from my mtgox account that are supposed to give me a clear image of what I have traded, deposited, withdrawn and paid as fees are close to unusable to me. not only do i use a different format for my 1000 and decimals (1.200,75 instead of 1,200.75), but also there's no way I am manually adding all those millibitcoins that i traded to make total for the day




i am looking for the following:

a script that takes the .csv as-is and automatically

- detects the currency
- makes a total for EACH DAY displaying
-- bought @ average price
-- sold @ average price
-- fees paid
-- deposited
-- withdrawn
-- 24avg bitcoin price


Code:
01/01/2012, +5000, 7,25€
01/01/2012, withdraw, 1000  BTC
01/01/2012, deposit, 10000 €
02/05/2012, -2000, 8,58€
....

...this is the best i can describe it. any questions?
also I am undecided as to how high the bounty should be.

if its a php script and you would include an upload and export feature, i would gladly host it.

Hi, you can try making a new bounty on Rugatu and see what you get.
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
I can do a perl script for you. You can host it like a php, with upload and export feature you want

so i can up a .csv and it will export a .csv for me to download?
how much time (BTC in bounty) will this take?


kinda need it to calculate profit/losses for tax reasons.

oh, and the fees paid need to be in it.
legendary
Activity: 1792
Merit: 1111
I can do a perl script for you. You can host it like a php, with upload and export feature you want
legendary
Activity: 2058
Merit: 1005
this space intentionally left blank
I was looking for a script that would make it easy for my and my accountant to figure out what I had bought/sold/deposited/withdrawn/paidfees for a given time.

20BTC was bountied.

12.5 BTC went to the PHP script
7.5 BTC went to the Pearl scripts.

Please feel free to use them under the given license and consider tipping to contribute to my expenses.

Cheers
2weiX

The php code
Code:
/*
This code is available to you under CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/).

If you use this tool to analyze your mtgox trades or use this code to build your own project, consider
donating to 12weixK5ttMdjeoQgdGca9mKMKxkCxZkBG and consider adding your work to this thread:
https://bitcointalk.org/index.php?topic=94769

Thank you!
2weiX, Bitsky and jl2012
*/

if (isset($_FILES['csv'])) {
if ($_FILES['csv']['size']>1024000) { die('file too big'); }
if ($_FILES['csv']['error']!==0) { die('upload error'); }
if ($_FILES['csv']['name']=='history_EUR.csv') { $currency='€'; }
elseif ($_FILES['csv']['name']=='history_USD.csv') { $currency='$'; }
else { $currency=''; }
$csv=file($_FILES['csv']['tmp_name'], FILE_IGNORE_NEW_LINES);
$out=parsecsv($csv);
$tmp=makecsv($out$currency);
header('Content-type: text/plain');
header('Content-disposition: attachment; filename="converted_'.$_FILES['csv']['name'].'"');
echo $tmp;
}
else {
echo "\n";
echo "\n";
echo "\n";
echo "\n";
echo "\n";
echo "\n";
echo "\n";
echo "\n";
echo "\n";
}

exit;

function 
parsecsv($csv) {
global $currency;
$regex=array();
$regex['$']='/\] ([\d\.]+).+BTC at \$([\d\.]+)/';
$regex['€']='/\] ([\d\.]+).+BTC at ([\d\.]+)/';
$out=array();
foreach ($csv as $tmp) {
$line=explode(','$tmp);
if (!is_numeric($line[0])) { continue; }
$line[1]=preg_replace('/"(.+) .*/''$1'$line[1]);
if (!isset($out[$line[1]])) {
$out[$line[1]]=array();
$out[$line[1]]['day']=$line[1];
$out[$line[1]]['deposit']=0;
$out[$line[1]]['withdraw']=0;
$out[$line[1]]['earned']=0;
$out[$line[1]]['earnedbtc']=0;
$out[$line[1]]['earnedcur']=0;
$out[$line[1]]['earnedrate']=0;
$out[$line[1]]['fee']=0;
$out[$line[1]]['spent']=0;
$out[$line[1]]['spentbtc']=0;
$out[$line[1]]['spentcur']=0;
$out[$line[1]]['spentrate']=0;
}
if ($line[2]=='deposit') { $out[$line[1]]['deposit']+=$line[4]; }
elseif ($line[2]=='withdraw') { $out[$line[1]]['withdraw']+=$line[4]; }
elseif ($line[2]=='fee') { $out[$line[1]]['fee']+=$line[4]; }
elseif ($line[2]=='earned') {
$out[$line[1]]['earned']+=$line[4];
if (preg_match($regex[$currency], $line[3], $tmp)) {
$out[$line[1]]['earnedbtc']+=$tmp[1];
$out[$line[1]]['earnedcur']+=$line[4];
}
}
elseif ($line[2]=='spent') {
$out[$line[1]]['spent']+=$line[4];
if (preg_match($regex[$currency], $line[3], $tmp)) {
$out[$line[1]]['spentbtc']+=$tmp[1];
$out[$line[1]]['spentcur']+=$line[4];
}
}
}
return($out);
}

function 
makecsv($out$currency) {
$tmp='';
global $currency;
foreach ($out as $i) {
if ($i['spentcur']>0) { $i['spentrate']=$i['spentcur']/$i['spentbtc']; }
if ($i['earnedbtc']>0) { $i['earnedrate']=$i['earnedcur']/$i['earnedbtc']; }
$i['day']=preg_replace('/(\d\d\d\d)\-(\d\d)\-(\d\d)/''$3.$2.$1'$i['day']);
$tmp.=$i['day'].";DEPOSIT;".number_format($i['deposit'], 2',''').$currency."\n";
$tmp.=$i['day'].";BUY;".number_format($i['spentbtc'], 8',''')."BTC;".number_format($i['spentrate'], 8',''').$currency."/BTC\n";
$tmp.=$i['day'].";SELL;".number_format($i['earnedbtc'], 8',''')."BTC;".number_format($i['earnedrate'], 8',''').$currency."/BTC\n";
$tmp.=$i['day'].";FEES;".number_format($i['fee'], 2',''').$currency."\n";
$tmp.=$i['day'].";WITHDRAW;-".number_format($i['withdraw'], 2',''').$currency."\n";
$tmp.="\n\n";
}
$tmp.="This code is available to you under CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/).\n\n";
$tmp.="If you use this tool to analyze your mtgox trades or use this code to build your own project, consider\n";
$tmp.="donating to 12weixK5ttMdjeoQgdGca9mKMKxkCxZkBG and consider adding your work to this thread:\n";
$tmp.="https://bitcointalk.org/index.php?topic=94769\n\n";
$tmp.="Thank you!\n";
$tmp.="2weiX, Bitsky and jl2012\n";
return($tmp);
}

?>


Pearl code for EUR
Code:
#!/usr/bin/perl
use strict;
open USD, "history_EUR.csv";
open OUTCSV, ">goxeur.csv";
open OUTCSVE, ">goxeur_e.csv";
my %array;
#my @type = ("deposit", "withdraw", "earned", "spent", "fee");
while () {
my ($date, $type, $info, $value, $balance) = ($_ =~ /^\d+,\"(\d{4}-\d{2}-\d{2}) .+?\",(.+?),(.+?),(.+?),(.+)/);
$date || next;
if (my ($x, $y) = $value =~ /(.*)E(.*)/) {
$value = $x * (10 ** $y);
}
$array{$date}{$type} += $value;
$array{$date}{balance} = $balance;
if ($type eq "spent") {
my ($btc, $price) = ($info =~ /(\d+\.\d+).*?BTC at \$?(\d+\.\d+)/);
$array{$date}{totalspent} += $value;
$array{$date}{totalboughtbtc} += $btc;
}
elsif ($type eq "earned") {
my ($btc, $price) = ($info =~ /(\d+\.\d+).*?BTC at \$?(\d+\.\d+)/);
                $array{$date}{totalreceive} += $value;
$array{$date}{totalsoldbtc} += $btc;
        }
}
foreach (sort keys %array) {

if ($array{$_}{deposit} > 0) {
print OUTCSV "\"$_\",\"DEPOSIT\",\"". sprintf("%.8f", $array{$_}{deposit}) . " EUR\"\n";
}
        if ($array{$_}{withdraw} > 0) {
                print OUTCSV "\"$_\",\"WITHDRAW\",\"". sprintf("%.8f", $array{$_}{withdraw}) . " EUR\"\n";
        }
        if ($array{$_}{fee} > 0) {
                print OUTCSV "\"$_\",\"FEES\",\"". sprintf("%.8f", $array{$_}{fee}) . " EUR\"\n";
        }
        if ($array{$_}{totalboughtbtc} > 0) {
                print OUTCSV "\"$_\",\"BUY\",\"". sprintf("%.8f", $array{$_}{totalboughtbtc}) . " BTC\",\"". $array{$_}{totalspent}/$array{$_}{totalboughtbtc} ." EUR\"\n";
        }
        if ($array{$_}{totalsoldbtc} > 0) {
                print OUTCSV "\"$_\",\"SELL\",\"". sprintf("%.8f", $array{$_}{totalsoldbtc}) . " BTC\",\"". $array{$_}{totalreceive}/$array{$_}{totalsoldbtc} ." EUR\"\n";
        }
}
close OUTCSV;
open OUTCSV, "goxeur.csv";
while () {
s/\./,/g;
print OUTCSVE;
}

Parl code for USD
Code:
#!/usr/bin/perl
use strict;
open USD, "history_USD.csv";
open OUTCSV, ">goxusd.csv";
open OUTCSVE, ">goxusd_e.csv";
my %array;
#my @type = ("deposit", "withdraw", "earned", "spent", "fee");
while () {
my ($date, $type, $info, $value, $balance) = ($_ =~ /^\d+,\"(\d{4}-\d{2}-\d{2}) .+?\",(.+?),(.+?),(.+?),(.+)/);
$date || next;
if (my ($x, $y) = $value =~ /(.*)E(.*)/) {
$value = $x * (10 ** $y);
}
$array{$date}{$type} += $value;
$array{$date}{balance} = $balance;
if ($type eq "spent") {
my ($btc, $price) = ($info =~ /(\d+\.\d+).*?BTC at \$?(\d+\.\d+)/);
$array{$date}{totalspent} += $value;
$array{$date}{totalboughtbtc} += $btc;
}
elsif ($type eq "earned") {
my ($btc, $price) = ($info =~ /(\d+\.\d+).*?BTC at \$?(\d+\.\d+)/);
                $array{$date}{totalreceive} += $value;
$array{$date}{totalsoldbtc} += $btc;
        }
}
foreach (sort keys %array) {

if ($array{$_}{deposit} > 0) {
print OUTCSV "\"$_\",\"DEPOSIT\",\"". sprintf("%.8f", $array{$_}{deposit}) . " USD\"\n";
}
        if ($array{$_}{withdraw} > 0) {
                print OUTCSV "\"$_\",\"WITHDRAW\",\"". sprintf("%.8f", $array{$_}{withdraw}) . " USD\"\n";
        }
        if ($array{$_}{fee} > 0) {
                print OUTCSV "\"$_\",\"FEES\",\"". sprintf("%.8f", $array{$_}{fee}) . " USD\"\n";
        }
        if ($array{$_}{totalboughtbtc} > 0) {
                print OUTCSV "\"$_\",\"BUY\",\"". sprintf("%.8f", $array{$_}{totalboughtbtc}) . " BTC\",\"". $array{$_}{totalspent}/$array{$_}{totalboughtbtc} ." USD\"\n";
        }
        if ($array{$_}{totalsoldbtc} > 0) {
                print OUTCSV "\"$_\",\"SELL\",\"". sprintf("%.8f", $array{$_}{totalsoldbtc}) . " BTC\",\"". $array{$_}{totalreceive}/$array{$_}{totalsoldbtc} ." USD\"\n";
        }
}
close OUTCSV;
open OUTCSV, "goxusd.csv";
while () {
s/\./,/g;
print OUTCSVE;
}
Jump to: