trading program using JSON and APIs // update 7/13 help please

YoungGun21

Platinum Member
Aug 17, 2006
2,546
1
81
Ok I have no clue how to do this, but basically I want to make a program that I can use to trade with. I want to be able to say "place order to sell x amount at $x price each." The available data I have to use is in JSON format.

Data:
Ticker Data http://mtgox.com/code/data/ticker.php
Market Depth http://mtgox.com/code/data/getDepth.php
Recent Trades http://mtgox.com/code/data/getTrades.php

Here is the rest of the info I am given:
Get your current balance https://mtgox.com/code/getFunds.php?name=blah&pass=blah

Place an order to Buy BTC https://mtgox.com/code/buyBTC.php?name=blah&pass=blah&amount=#&price=#
returns a list of your open orders

Place an order to Sell BTC https://mtgox.com/code/sellBTC.php?name=blah&pass=blah&amount=#&price=#
returns a list of your open orders

Fetch a list of your open Orders https://mtgox.com/code/getOrders.php?name=blah&pass=blah
oid: Order ID
type: 1 for sell order or 2 for buy order
status: 1 for active, 2 for not enough funds

Cancel an order https://mtgox.com/code/cancelOrder.php?name=blah&pass=blah&oid=#&type=#
oid: Order ID
type: 1 for sell order or 2 for buy order

Send BTC https://mtgox.com/code/withdraw.php...=BTC&btca=bitcoin_address_to_send_to&amount=#

It can be very simple, doesn't need to have an interface of any sort. I have very basic programming experience with C++, but that's about it. I'm just looking for a place to start and some help from somebody that might know what to do.

EDIT: Please help me in post 15 -> http://forums.anandtech.com/showpost.php?p=32014307&postcount=15. I'm having some troubles now.
 
Last edited:

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,708
4,665
75
Playing the BitCoin market, eh?

Language depends on your preference, but C++ is the last language I'd use. My personal preference is Perl, LWP::Simple or pipes from wget, and I'd look into the JSON module; not sure whether I'd use it or not.
 

YoungGun21

Platinum Member
Aug 17, 2006
2,546
1
81
At first we are just going to use it to easily sell the BTC that we mine, but eventually we may start actively trading. I have no experience with JSON data or any real projects like this, so I'm really just looking to see where I am supposed to start.
 

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
Hell, from the sound of it you could do the whole thing in JavaScript. I'd use jquery or Cappuccino
 

hans007

Lifer
Feb 1, 2000
20,212
18
81
C++ is a harder language tod o things like http calls in..

but if you do end up using it go to json.org and download json_spirit which is a open source json toolkit. its well it works, i have used it and its fairly easy to understand. but i'd probably suggest using any language besides c++ to do this.
 

YoungGun21

Platinum Member
Aug 17, 2006
2,546
1
81
I'm using Perl to do it. It hasn't been too bad thus far, but I'll be posting a thread with some questions soon if I can't figure out the two parts I'm having trouble with.
 

YoungGun21

Platinum Member
Aug 17, 2006
2,546
1
81
Ok so here are my troubles:

1. Using I'm outputting the telnet steam of live trades to a file. Each trade is a JSON object, but I do not decode anything before I put it in the file. I am now trying to decode each trade, then input them into a MySQL db, but am having trouble. I see two ways I can try this, either decode real-time and then put them in, or continue putting them into a file and decoding them after they are placed in the file. Whichever way I can get it to work is the way I will consider best for right now.
When I tried to do it real-time, it would decode 2 trades and then stop which I found very odd. Now when I am trying it from the file, it won't do any.
Code:
#!/usr/bin/perl
use strict;
use warnings;
use JSON;

#format data from telnet feed
my $json_tl;
my $json;

#telnet feed
       open FILE, "/home/tyler/begperl/telnet_mtg"
       || die "Can't open\n";




       while(<FILE> ){
                my $line = <FILE>;

          $json_tl = decode_json $line;
          my %trades = %{$json_tl};
          print "Price: $trades{price}\n";

This is what I have, error given when run is "malformed json string at line 20, <FILE> 2." I know that the file telnet_mtg is full of several hundred trades waiting to be decoded and put in my database. If you are curious, the telnet stream of trades is located at host: bitcoincharts.com port: 27007.




2. Separate question here, this is for a different program. If I assign decoded json info to a hash, but I'm inside a subroutine (using Perl here), how can I call values from that hash later on? Example:
Code:
 sub fetch_json_page {
.....
stuff gets assigned to my %info
....
}

my $tlow = $info{low};

It says that there is an uninitialized value when I do this. This is probably a simple syntax error that I don't know how to correct, but I am learning Perl as I go. BTW, I know that it decodes and assigns the data correctly because I print it several times from within the subroutine.


If I need to provide more code from either program, let me know.
Any help is appreciated, thanks.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,708
4,665
75
1. I'm unable to get any data from bitcoincharts.com:27007. :confused:
2. Return %info out of fetch_json_page, which is better practice and seems more likely to be what you meant to do. Or define %info earlier, in a wider scope.

Code:
my %info; # This is a global variable when defined here.
...
sub fetch_json_page {
# Don't use "my" before %info in here anywhere.
...
}

my $tlow = $info{"low"}; # is this what you meant?
 

YoungGun21

Platinum Member
Aug 17, 2006
2,546
1
81
If you have a telnet program, use bitcoincharts.com as the host and port # 27007. If you are on linux and have telnet installed, just type "telnet bitcoincharts.com 27007" without quotes obviously. It is real-time, so you may actually have to wait for a trade to happen to actually see what I am dealing with.

As for the &#37;info, I did what you said and still got an error that it is uninitialized when I try to use it after the subroutine. I'll explain more so you know exactly what I am trying to do. I have a subroutine that will fetch the json data, decode it, and assign it to a hash %info. After that, I am using Perl/Tk module to produce a table of each value in the hash.

EDIT: ALL CAPS HERE BECAUSE YOU FREAKING ROCK. I ACTUALLY HAD TWO MISTAKES CAUSING THE PROBLEM, BUT YOU FIXING THE HARDER ONE ALLOWED ME TO SEE THE SMALLER ONE. THANK YOU *MILLION.
 

YoungGun21

Platinum Member
Aug 17, 2006
2,546
1
81
Code:
#!/usr/bin/perl
use strict;
use warnings;
use Net::Telnet;
use JSON;

my $secs;

#connect to bitcoin telnet
my $t = new Net::Telnet(Host=>"bitcoincharts.com", Port=>27007, Timeout=>999 );
my $line;
my $json;
my $json_tl;
my %trades;


        while(1){
                     $line = $t->getline();

            #$line = '{"timestamp": 1310085231, "price": 14.82991, "volume": 8, "currency": "USD", "tid": 1310085231758895, "symbol": "mtgoxUSD"}';

            if($line =~ /mtgoxUSD/){

                $json_tl = decode_json $line;
                   %trades = %{$json_tl};
                  print "$json_tl\n";
                  print "$line\n";
                  print $trades{price};
                  print "\n\n";

                  undef $line;
                  undef %trades;
                  undef $json_tl;
                 }

        };

Here is the beginning of the first program, which is decoding the trades and then will eventually place them in the SQL database. If you run it, it will complete 2 passes through the loop (1 pass = 1 trade), and will fail upon starting the 3rd. Always finishes 2, never even starts the 3rd. Says I have a malformed JSON string at that point. The commented out $line is what the JSON strings actually look like, and when we used that to run through as a test, it will run forever and ever, as many passes as we care to watch.

Help please! Thanks.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Instead of only printing the lines when you have a successful parse try printing every line you try to parse. Since you can only see the output when you have a successful parse you really don't know what the cause of the error is yet.

I'm willing to bet you've got 2+ JSON objects crammed into one string without a proper array structure.
 

YoungGun21

Platinum Member
Aug 17, 2006
2,546
1
81
I used to have it print $line before the if loop, which then prints all of the trades regardless of if they contain my search string mtgoxUSD or not. That worked fine, but it only ever printed normally, but that would print as many non-mtgoxUSD trades that took place. I think you are right in that it is a problem with the way I decode the JSON, but this is the only way I know how to do it. The data itself is a hash reference, so I put it into a hash.

Error it gives me is:
malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "\x{0}{"timestamp": 1...") at dbins.pl line 24

The line it always points to is:
$json_tl = decode_json $line;
 
Last edited:

YoungGun21

Platinum Member
Aug 17, 2006
2,546
1
81
I got it working! Finished the script, combined it with a couple others, so now it reads the trades real-time, decodes them, and places them into a database.

15 minutes after we finished it and got it running, the computer our MySQL database was on crashed. Not cool.
 

YoungGun21

Platinum Member
Aug 17, 2006
2,546
1
81
Ok I need to know how to send data using a POST method using Perl. I am trying to use WWW::Mechanize, but for some reason my POSTs don't work?

Code:
...
my $mech = WWW::Mechanize->new();
$mech->post(
           $url,
           name => 'blah',
           pass => 'blahhhh');
...

I get "{error: must be logged in}". There must be something I'm missing.