├── .gitignore
├── LICENSE
├── README.md
├── examples
├── LICENSE
├── conditional-close.py
└── kraken.key
├── krakenex
├── __init__.py
├── api.py
└── connection.py
└── setup.py
/.gitignore:
--------------------------------------------------------------------------------
1 | *.py[cod]
2 |
3 | # C extensions
4 | *.so
5 |
6 | # Packages
7 | *.egg
8 | *.egg-info
9 | dist
10 | build
11 | eggs
12 | parts
13 | bin
14 | var
15 | sdist
16 | develop-eggs
17 | .installed.cfg
18 | lib
19 | lib64
20 | __pycache__
21 |
22 | # Installer logs
23 | pip-log.txt
24 |
25 | # Unit test / coverage reports
26 | .coverage
27 | .tox
28 | nosetests.xml
29 |
30 | # Translations
31 | *.mo
32 |
33 | # Mr Developer
34 | .mr.developer.cfg
35 | .project
36 | .pydevproject
37 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU LESSER GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 |
9 | This version of the GNU Lesser General Public License incorporates
10 | the terms and conditions of version 3 of the GNU General Public
11 | License, supplemented by the additional permissions listed below.
12 |
13 | 0. Additional Definitions.
14 |
15 | As used herein, "this License" refers to version 3 of the GNU Lesser
16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU
17 | General Public License.
18 |
19 | "The Library" refers to a covered work governed by this License,
20 | other than an Application or a Combined Work as defined below.
21 |
22 | An "Application" is any work that makes use of an interface provided
23 | by the Library, but which is not otherwise based on the Library.
24 | Defining a subclass of a class defined by the Library is deemed a mode
25 | of using an interface provided by the Library.
26 |
27 | A "Combined Work" is a work produced by combining or linking an
28 | Application with the Library. The particular version of the Library
29 | with which the Combined Work was made is also called the "Linked
30 | Version".
31 |
32 | The "Minimal Corresponding Source" for a Combined Work means the
33 | Corresponding Source for the Combined Work, excluding any source code
34 | for portions of the Combined Work that, considered in isolation, are
35 | based on the Application, and not on the Linked Version.
36 |
37 | The "Corresponding Application Code" for a Combined Work means the
38 | object code and/or source code for the Application, including any data
39 | and utility programs needed for reproducing the Combined Work from the
40 | Application, but excluding the System Libraries of the Combined Work.
41 |
42 | 1. Exception to Section 3 of the GNU GPL.
43 |
44 | You may convey a covered work under sections 3 and 4 of this License
45 | without being bound by section 3 of the GNU GPL.
46 |
47 | 2. Conveying Modified Versions.
48 |
49 | If you modify a copy of the Library, and, in your modifications, a
50 | facility refers to a function or data to be supplied by an Application
51 | that uses the facility (other than as an argument passed when the
52 | facility is invoked), then you may convey a copy of the modified
53 | version:
54 |
55 | a) under this License, provided that you make a good faith effort to
56 | ensure that, in the event an Application does not supply the
57 | function or data, the facility still operates, and performs
58 | whatever part of its purpose remains meaningful, or
59 |
60 | b) under the GNU GPL, with none of the additional permissions of
61 | this License applicable to that copy.
62 |
63 | 3. Object Code Incorporating Material from Library Header Files.
64 |
65 | The object code form of an Application may incorporate material from
66 | a header file that is part of the Library. You may convey such object
67 | code under terms of your choice, provided that, if the incorporated
68 | material is not limited to numerical parameters, data structure
69 | layouts and accessors, or small macros, inline functions and templates
70 | (ten or fewer lines in length), you do both of the following:
71 |
72 | a) Give prominent notice with each copy of the object code that the
73 | Library is used in it and that the Library and its use are
74 | covered by this License.
75 |
76 | b) Accompany the object code with a copy of the GNU GPL and this license
77 | document.
78 |
79 | 4. Combined Works.
80 |
81 | You may convey a Combined Work under terms of your choice that,
82 | taken together, effectively do not restrict modification of the
83 | portions of the Library contained in the Combined Work and reverse
84 | engineering for debugging such modifications, if you also do each of
85 | the following:
86 |
87 | a) Give prominent notice with each copy of the Combined Work that
88 | the Library is used in it and that the Library and its use are
89 | covered by this License.
90 |
91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license
92 | document.
93 |
94 | c) For a Combined Work that displays copyright notices during
95 | execution, include the copyright notice for the Library among
96 | these notices, as well as a reference directing the user to the
97 | copies of the GNU GPL and this license document.
98 |
99 | d) Do one of the following:
100 |
101 | 0) Convey the Minimal Corresponding Source under the terms of this
102 | License, and the Corresponding Application Code in a form
103 | suitable for, and under terms that permit, the user to
104 | recombine or relink the Application with a modified version of
105 | the Linked Version to produce a modified Combined Work, in the
106 | manner specified by section 6 of the GNU GPL for conveying
107 | Corresponding Source.
108 |
109 | 1) Use a suitable shared library mechanism for linking with the
110 | Library. A suitable mechanism is one that (a) uses at run time
111 | a copy of the Library already present on the user's computer
112 | system, and (b) will operate properly with a modified version
113 | of the Library that is interface-compatible with the Linked
114 | Version.
115 |
116 | e) Provide Installation Information, but only if you would otherwise
117 | be required to provide such information under section 6 of the
118 | GNU GPL, and only to the extent that such information is
119 | necessary to install and execute a modified version of the
120 | Combined Work produced by recombining or relinking the
121 | Application with a modified version of the Linked Version. (If
122 | you use option 4d0, the Installation Information must accompany
123 | the Minimal Corresponding Source and Corresponding Application
124 | Code. If you use option 4d1, you must provide the Installation
125 | Information in the manner specified by section 6 of the GNU GPL
126 | for conveying Corresponding Source.)
127 |
128 | 5. Combined Libraries.
129 |
130 | You may place library facilities that are a work based on the
131 | Library side by side in a single library together with other library
132 | facilities that are not Applications and are not covered by this
133 | License, and convey such a combined library under terms of your
134 | choice, if you do both of the following:
135 |
136 | a) Accompany the combined library with a copy of the same work based
137 | on the Library, uncombined with any other library facilities,
138 | conveyed under the terms of this License.
139 |
140 | b) Give prominent notice with the combined library that part of it
141 | is a work based on the Library, and explaining where to find the
142 | accompanying uncombined form of the same work.
143 |
144 | 6. Revised Versions of the GNU Lesser General Public License.
145 |
146 | The Free Software Foundation may publish revised and/or new versions
147 | of the GNU Lesser General Public License from time to time. Such new
148 | versions will be similar in spirit to the present version, but may
149 | differ in detail to address new problems or concerns.
150 |
151 | Each version is given a distinguishing version number. If the
152 | Library as you received it specifies that a certain numbered version
153 | of the GNU Lesser General Public License "or any later version"
154 | applies to it, you have the option of following the terms and
155 | conditions either of that published version or of any later version
156 | published by the Free Software Foundation. If the Library as you
157 | received it does not specify a version number of the GNU Lesser
158 | General Public License, you may choose any version of the GNU Lesser
159 | General Public License ever published by the Free Software Foundation.
160 |
161 | If the Library as you received it specifies that a proxy can decide
162 | whether future versions of the GNU Lesser General Public License shall
163 | apply, that proxy's public statement of acceptance of any version is
164 | permanent authorization for you to choose that version for the
165 | Library.
166 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | krakenex
2 | ========
3 |
4 | Kraken.com exchange API, Python 2 package.
5 |
6 |
7 | Installation
8 | -----------
9 |
10 | Run `python ./setup.py install`. The module will be called `krakenex`.
11 |
12 |
13 | Documentation
14 | -------------
15 |
16 | The code is simple and documented in docstrings.
17 |
18 | For a list of public/private API methods, see
19 | [Kraken API documentation][krakenapidoc].
20 |
21 |
22 | Attribution
23 | -----------
24 |
25 | This code is licensed under LGPLv3. It should be available in
26 | `LICENSE`. If not, see [here][license].
27 |
28 | Examples are licensed under the Simplified BSD license. See
29 | `examples/LICENSE`.
30 |
31 | Payward's [PHP API][krakenphpapi], Alan McIntyre's [BTC-e API][btceapi],
32 | and ScriptProdigy's [Cryptsy Python API][cryptsypyapi] were used as
33 | examples.
34 |
35 |
36 | [krakenapidoc]: https://www.kraken.com/help/api
37 | [license]: https://www.gnu.org/licenses/lgpl-3.0.txt
38 | [krakenphpapi]: https://github.com/payward/kraken-api-client
39 | [btceapi]: https://github.com/alanmcintyre/btce-api
40 | [cryptsypyapi]: https://github.com/ScriptProdigy/CryptsyPythonAPI
41 |
--------------------------------------------------------------------------------
/examples/LICENSE:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2014, Noel Maersk.
2 | # All rights reserved.
3 | #
4 | # Redistribution and use in source and binary forms, with or without
5 | # modification, are permitted provided that the following conditions are
6 | # met:
7 | #
8 | # 1. Redistributions of source code must retain the above copyright
9 | # notice, this list of conditions and the following disclaimer.
10 | # 2. Redistributions in binary form must reproduce the above copyright
11 | # notice, this list of conditions and the following disclaimer in the
12 | # documentation and/or other materials provided with the distribution.
13 | #
14 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 | #
26 | # The views and conclusions contained in the software and documentation
27 | # are those of the authors and should not be interpreted as representing
28 | # official policies, either expressed or implied, of krakenex
29 | # developers.
30 |
--------------------------------------------------------------------------------
/examples/conditional-close.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python2
2 |
3 | # This file is part of krakenex.
4 | # Licensed under the Simplified BSD license. See examples/LICENSE.
5 |
6 | # Demonstrates how to use the conditional close functionality; that is,
7 | # placing an order that, once filled, will place another order.
8 | #
9 | # This can be useful for very simple automation, where a bot is not
10 | # needed to constantly monitor execution.
11 |
12 | import krakenex
13 |
14 | k = krakenex.API()
15 | k.load_key('kraken.key')
16 |
17 | k.query_private('AddOrder', {'pair': 'XXBTZEUR',
18 | 'type': 'buy',
19 | 'ordertype': 'limit',
20 | 'price': '1',
21 | 'volume': '1',
22 | 'close[pair]': 'XXBTZEUR',
23 | 'close[type]': 'sell',
24 | 'close[ordertype]': 'limit',
25 | 'close[price]': '9001',
26 | 'close[volume]': '1'})
27 |
--------------------------------------------------------------------------------
/examples/kraken.key:
--------------------------------------------------------------------------------
1 | keykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykey
2 | secretsecretsecretsecretsecretsecretsecretsecretsecretsecretsecretsecretsecretsecretsecret
3 |
--------------------------------------------------------------------------------
/krakenex/__init__.py:
--------------------------------------------------------------------------------
1 | # This file is part of krakenex.
2 | #
3 | # krakenex is free software: you can redistribute it and/or modify it
4 | # under the terms of the GNU Lesser General Public License as published
5 | # by the Free Software Foundation, either version 3 of the License, or
6 | # (at your option) any later version.
7 | #
8 | # krakenex is distributed in the hope that it will be useful,
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 | # Lesser General Public License for more details.
12 | #
13 | # You should have received a copy of the GNU Lesser
14 | # General Public LICENSE along with krakenex. If not, see
15 | # .
16 |
17 |
18 | from connection import Connection
19 | from api import API
20 |
--------------------------------------------------------------------------------
/krakenex/api.py:
--------------------------------------------------------------------------------
1 | # This file is part of krakenex.
2 | #
3 | # krakenex is free software: you can redistribute it and/or modify it
4 | # under the terms of the GNU Lesser General Public License as published
5 | # by the Free Software Foundation, either version 3 of the License, or
6 | # (at your option) any later version.
7 | #
8 | # krakenex is distributed in the hope that it will be useful,
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 | # Lesser General Public License for more details.
12 | #
13 | # You should have received a copy of the GNU Lesser
14 | # General Public LICENSE along with krakenex. If not, see
15 | # .
16 |
17 |
18 | import json
19 | import urllib
20 |
21 | # private query nonce
22 | import time
23 |
24 | # private query signing
25 | import hashlib
26 | import hmac
27 | import base64
28 |
29 | from krakenex import connection
30 |
31 |
32 | class API(object):
33 | """Kraken.com cryptocurrency Exchange API.
34 |
35 | Public methods:
36 | load_key
37 | query_public
38 | query_private
39 |
40 | """
41 | def __init__(self, key = '', secret = ''):
42 | """Create an object with authentication information.
43 |
44 | Arguments:
45 | key -- key required to make queries to the API (default: '')
46 | secret -- private key used to sign API messages (default: '')
47 |
48 | """
49 | self.key = key
50 | self.secret = secret
51 | self.uri = 'https://api.kraken.com'
52 | self.apiversion = '0'
53 |
54 |
55 | def load_key(self, path):
56 | """Load key and secret from file.
57 |
58 | Argument:
59 | path -- path to file (string, no default)
60 |
61 | """
62 | with open(path, "r") as f:
63 | self.key = f.readline().strip()
64 | self.secret = f.readline().strip()
65 |
66 |
67 | def _query(self, urlpath, req = {}, conn = None, headers = {}):
68 | """Low-level query handling.
69 |
70 | Arguments:
71 | urlpath -- API URL path sans host (string, no default)
72 | req -- additional API request parameters (default: {})
73 | conn -- kraken.Connection object (default: None)
74 | headers -- HTTPS headers (default: {})
75 |
76 | """
77 | url = self.uri + urlpath
78 |
79 | if conn is None:
80 | conn = connection.Connection()
81 |
82 | ret = conn._request(url, req, headers)
83 | return json.loads(ret)
84 |
85 |
86 | def query_public(self, method, req = {}, conn = None):
87 | """API queries that do not require a valid key/secret pair.
88 |
89 | Arguments:
90 | method -- API method name (string, no default)
91 | req -- additional API request parameters (default: {})
92 | conn -- connection object to reuse (default: None)
93 |
94 | """
95 | urlpath = '/' + self.apiversion + '/public/' + method
96 |
97 | return self._query(urlpath, req, conn)
98 |
99 |
100 | def query_private(self, method, req={}, conn = None):
101 | """API queries that require a valid key/secret pair.
102 |
103 | Arguments:
104 | method -- API method name (string, no default)
105 | req -- additional API request parameters (default: {})
106 | conn -- connection object to reuse (default: None)
107 |
108 | """
109 | urlpath = '/' + self.apiversion + '/private/' + method
110 |
111 | req['nonce'] = int(1000*time.time())
112 | postdata = urllib.urlencode(req)
113 | message = urlpath + hashlib.sha256(str(req['nonce']) +
114 | postdata).digest()
115 | signature = hmac.new(base64.b64decode(self.secret),
116 | message, hashlib.sha512)
117 | headers = {
118 | 'API-Key': self.key,
119 | 'API-Sign': base64.b64encode(signature.digest())
120 | }
121 |
122 | return self._query(urlpath, req, conn, headers)
123 |
--------------------------------------------------------------------------------
/krakenex/connection.py:
--------------------------------------------------------------------------------
1 | # This file is part of krakenex.
2 | #
3 | # krakenex is free software: you can redistribute it and/or modify it
4 | # under the terms of the GNU Lesser General Public License as published
5 | # by the Free Software Foundation, either version 3 of the License, or
6 | # (at your option) any later version.
7 | #
8 | # krakenex is distributed in the hope that it will be useful,
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 | # Lesser General Public License for more details.
12 | #
13 | # You should have received a copy of the GNU Lesser
14 | # General Public LICENSE along with krakenex. If not, see
15 | # .
16 |
17 |
18 | import httplib
19 | import urllib
20 |
21 |
22 | class Connection:
23 | """Kraken.com connection handler.
24 |
25 | Public methods:
26 | close
27 | """
28 |
29 |
30 | def __init__(self, uri = 'api.kraken.com', timeout = 30):
31 | """ Create an object for reusable connections.
32 |
33 | Arguments:
34 | uri -- URI to connect to (default: 'https://api.kraken.com')
35 | timeout -- blocking operations' timeout in seconds (default: 30)
36 | """
37 | self.headers = {
38 | 'User-Agent': 'krakenex/0.0.5 (+https://github.com/veox/python2-krakenex)'
39 | }
40 |
41 | self.conn = httplib.HTTPSConnection(uri, timeout = timeout)
42 |
43 |
44 | def close(self):
45 | """ Close the connection.
46 |
47 | No arguments.
48 | """
49 | self.conn.close()
50 |
51 |
52 | def _request(self, url, req = {}, headers = {}):
53 | """ Send POST request to API server.
54 |
55 | url -- Fully-qualified URL with all necessary urlencoded
56 | information (string, no default)
57 | req -- additional API request parameters (default: {})
58 | headers -- additional HTTPS headers, such as API-Key and API-Sign
59 | (default: {})
60 | """
61 | data = urllib.urlencode(req)
62 | headers.update(self.headers)
63 |
64 | self.conn.request("POST", url, data, headers)
65 | response = self.conn.getresponse()
66 |
67 | return response.read()
68 |
--------------------------------------------------------------------------------
/setup.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python2
2 |
3 | from distutils.core import setup
4 |
5 | setup(name='krakenex',
6 | version='0.0.5',
7 | description='kraken.com cryptocurrency exchange API',
8 | author='Noel Maersk',
9 | author_email='veox@wemakethings.net',
10 | url='https://github.com/veox/python2-krakenex',
11 | packages=['krakenex'],
12 | )
13 |
--------------------------------------------------------------------------------