Giter Club home page Giter Club logo

mexbtcapi's Introduction

Python-MtGox

This is an interface to MtGox's API version 1.

All numbers are returned as integers by default, divide by the currency's multiplier to get a human readable value. Just use the multiplier dict, mtgox.multiplier[three-letter-currency-symbol].

INSTALL

$ python setup.py install

EXAMPLE

import mtgox

mtgox.ticker("USD")
mtgox.multiplier["EUR"]

account = mtgox.Private(key, secret)
account.info()


import mtgox.arrays

numpy_array_of_trades = mtgox.arrays.trades()

Donations: 174fzBxiW2Eiqmdd64CGTdJwLbjU2fxHEY

mexbtcapi's People

Contributors

ehmry avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mexbtcapi's Issues

Please provide __str__(self) function for Market.Trade class

It would be easier to understand how the Trade class should be used, and also easier to handle it if it provided a str(self) function in mexbtcapi/concepts/market.py .

Perhaps something like this?
def str(self):
return "%s %s -> %s %s" % (self.from_amount, self.from_entity, self.to_amount, self.to_entity)

Rationale behind Trade from_amount, to_amount

Can you explain the Rationale behind Trade 'from_amount', 'to_amount'? I think it should be changed to 'price' and 'amount'.

An example: Somebody buys 0.5 BTC for 11 dollars. I'm assuming that from_amount would the have value '5.50 dollar' and to_amount have the value '0.5 BTC'. To get the price the trade actually represents, one would have to divide the 5.50 dollar by 0.5 BTC, resulting in 11 dollar/BTC. To build a chart like http://bitcoincharts.com/charts/mtgoxUSD, this calculation has to be done for every trade, resulting in a lot of overhead.

In the trading domain, for a trade, usually two attributes are given:

  • price (e.g., 11 dollar / BTC)
  • amount (e.g., 0.5 BTC)

MtGox, for example, also does it this way: https://en.bitcoin.it/wiki/MtGox/API/HTTP/v1#Multi_currency_trades
Yahoo Finance does it this way: http://finance.yahoo.com/q?s=MSFT&ql=1 (notice 'price')

Draft implementation for Bitstamp support

Hi. I tried to implement Bitstamp support, but do not quite know how to do it, and the getTicket()
API do not seem to match the available API from Bitstamp very well. When I run the example code from the README, I get this output.

% python x.py
At MtGox I can get 1.34 BTC for my 10.00 USD (that's 13.40 USD/BTC)
{u'volume': u'2665.99756783', u'last': u'13.00', u'bid': u'13.00', u'high': u'13.25', u'low': u'12.98', u'ask': u'13.13'}
MEXBTCAPI: You are using a number (13.25) that is not suitable to convert to Decimal!
MEXBTCAPI: You are using a number (12.98) that is not suitable to convert to Decimal!
MEXBTCAPI: You are using a number (13.00) that is not suitable to convert to Decimal!
MEXBTCAPI: You are using a number (13.13) that is not suitable to convert to Decimal!
MEXBTCAPI: You are using a number (13.00) that is not suitable to convert to Decimal!
At Bitstamp I can get 1.31 BTC for my 10.00 USD (that's 13.13 USD/BTC)
%

Here is the patch so far:

diff --git a/mexbtcapi/init.py b/mexbtcapi/init.py
index f308d06..4ffe5ed 100644
--- a/mexbtcapi/init.py
+++ b/mexbtcapi/init.py
@@ -1,5 +1,6 @@
from logger import log
from api import mtgox
+from api import bitstamp

-apis = [mtgox]
+apis = [mtgox, bitstamp]

--- /dev/null 2012-12-21 05:01:59.399416992 +0100
+++ mexbtcapi/api/bitstamp/init.py 2012-12-27 21:48:35.000000000 +0100
@@ -0,0 +1,29 @@
+# -- coding: utf-8 --
+
+# Copyright © 2012 Petter Reinholdtsen [email protected]
+#
+# Permission is hereby granted, free of charge, to any person
+# obtaining a copy of this software and associated documentation
+# files (the "Software"), to deal in the Software without
+# restriction, including without limitation the rights to use,
+# copy, modify, merge, publish, distribute, sublicense, and/or
+# sell copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following
+# conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+# OTHER DEALINGS IN THE SOFTWARE.
+
+from mexbtcapi.api.bitstamp.rest.high_level import MARKET_NAME, Market
+
+name = MARKET_NAME
+market = Market
--- /dev/null 2012-12-21 05:01:59.399416992 +0100
+++ mexbtcapi/api/bitstamp/rest/high_level.py 2012-12-27 21:48:45.000000000 +0100
@@ -0,0 +1,82 @@
+# -- coding: utf-8 --
+
+# Copyright © 2012 Petter Reinholdtsen [email protected]
+#
+# Permission is hereby granted, free of charge, to any person
+# obtaining a copy of this software and associated documentation
+# files (the "Software"), to deal in the Software without
+# restriction, including without limitation the rights to use,
+# copy, modify, merge, publish, distribute, sublicense, and/or
+# sell copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following
+# conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+# OTHER DEALINGS IN THE SOFTWARE.
+
+from decimal import Decimal
+import functools
+import datetime
+
+import mexbtcapi
+from mexbtcapi import concepts
+from mexbtcapi.concepts.currencies import BTC, USD
+from mexbtcapi.concepts.market import Market as BaseMarket
+
+import urllib
+import urllib2
+import json
+
+MARKET_NAME= "Bitstamp"
+_URL = "https://www.bitstamp.net/api/"
+
+class BitStampTicker( concepts.market.Ticker):

  • TIME_PERIOD= 24_60_60

+class Market(BaseMarket):

  • def init( self, currency ):
  •    mexbtcapi.concepts.market.Market.**init**(self, MARKET_NAME, BTC, currency)
    
  • def json_request(self, url, data=None):
  •    if data is not None:
    
  •        data = urllib.urlencode(data)
    
  •        req = urllib2.Request(url, data)
    
  •    else:
    
  •        req = urllib2.Request(url)
    
  •    f = urllib2.urlopen(req)
    
  •    jdata = json.load(f)
    
  •    print jdata
    
  •    return jdata
    
  • def getTicker(self):
  •    curname = self.c2.name
    
  •    if "USD" != curname:
    
  •        raise BitstampError("Unknown currency: " + currency)
    
    +#GET https://www.bitstamp.net/api/ticker/
    +# Returns JSON dictionary:
    +#last - last BTC price
    +#high - last 24 hours price high
    +#low - last 24 hours price low
    +#volume - last 24 hours volume
    +#bid - highest buy order
    +#ask - lowest sell order
  •    url = _URL + "ticker"
    
  •    data = self.json_request(url)
    
  •    data['avg'] = 0 # FIXME - not available from API
    
  •    data2 = {}
    
  •    for name in ('high', 'low', 'avg', 'last', 'ask', 'bid'):
    
  •        data2[name] = concepts.currency.ExchangeRate(BTC, USD, data[name])
    
  •    time= datetime.datetime.now()
    
  •    return BitStampTicker( market=self, time=time, high=data2['high'],
    
  •                           low=data2['low'], average=data2['avg'],
    
  •                           last=data2['last'], sell=data2['ask'],
    
  •                           buy=data2['bid'] ) 
    

Example from README do not work on Debian Squeeze

I tried to use this module on Debian Squeeze with the example code found in the readme, but
get this exception:

pere@minerva:~/src/bitcoin/mexbtcapi$ cat x.py
import mexbtcapi
from mexbtcapi.concepts.currencies import USD
from mexbtcapi.concepts.currency import Amount

ten_dollars= Amount(10, USD)
for api in mexbtcapi.apis:
exchange_rate= api.market(USD).getTicker().sell
print "At %s I can get %s for my %s (that's %s)"%(api.name, exchange_rate.convert( ten_dollars ), ten_dollars, exchange_rate)
pere@minerva:/src/bitcoin/mexbtcapi$
pere@minerva:
/src/bitcoin/mexbtcapi$ python x.py
Traceback (most recent call last):
File "x.py", line 7, in
exchange_rate= api.market(USD).getTicker().sell
File "/home/pere/src/bitcoin/mexbtcapi/mexbtcapi/api/mtgox/http_v1/high_level.py", line 20, in init
self.multiplier= low_level.multiplier[ currency.name ] #to convert low level data
File "/home/pere/src/bitcoin/mexbtcapi/mexbtcapi/api/mtgox/http_v1/mtgox.py", line 51, in getitem
info = currency(currency_)
File "/home/pere/src/bitcoin/mexbtcapi/mexbtcapi/api/mtgox/http_v1/mtgox.py", line 119, in currency
return _generic('currency', data=data)
File "/home/pere/src/bitcoin/mexbtcapi/mexbtcapi/api/mtgox/http_v1/mtgox.py", line 96, in _generic
return _json_request(url, data)
File "/home/pere/src/bitcoin/mexbtcapi/mexbtcapi/api/mtgox/http_v1/mtgox.py", line 109, in _json_request
jdata = json.load(f, object_pairs_hook=_pairs_hook)
File "/usr/lib/python2.6/json/init.py", line 267, in load
parse_constant=parse_constant, *_kw)
File "/usr/lib/python2.6/json/init.py", line 318, in loads
return cls(encoding=encoding, *_kw).decode(s)
TypeError: init() got an unexpected keyword argument 'object_pairs_hook'
pere@minerva:~/src/bitcoin/mexbtcapi$

Is it a bug in the library or something else wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.