Skip to content

Commit 931f58c

Browse files
committed
Added buy and sell methods and made create_order "private".
1 parent e5b7c30 commit 931f58c

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

Cryptsy.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,31 @@ def depth(self, marketid):
170170
##
171171
# Outputs:
172172
# orderid If successful, the Order ID for the order which was created
173-
def create_order(self, marketid, ordertype, quantity, price):
173+
def _create_order(self, marketid, ordertype, quantity, price):
174+
""" Creates an order for buying or selling coins.
175+
176+
It is preferable to buy and sell coins using the Api.buy and Api.sell
177+
methods.
178+
179+
:param marketid: Market to buy from.
180+
:param ordertype: Either Buy or Sell.
181+
:param quantity: Number of coins to buy.
182+
:param price: At this price.
183+
"""
174184
return self._api_query('createorder',
175185
request_data={'marketid': marketid,
176186
'ordertype': ordertype,
177187
'quantity': quantity,
178188
'price': price})
179189

190+
def buy(self, marketid, quantity, price):
191+
""" Buy a specified number of coins on the given market. """
192+
return self._create_order(marketid, 'Buy', quantity, price)
193+
194+
def sell(self, marketid, quantity, price):
195+
""" Sell a specified number of coins on the given market. """
196+
return self._create_order(marketid, 'Sell', quantity, price)
197+
180198
# Inputs:
181199
# orderid Order ID for which you would like to cancel
182200
##

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ Example Usage
1414
-------------
1515
Create buy order for dgc, market id 26, then cancels all orders you have for dgc
1616
```python
17-
from Cryptsy import API
18-
exchange = API('KEY HERE', 'SECRET HERE')
19-
print(exchange.create_order(26, "Buy", 100, 0.00000001)) # Buy 100 dgc at .00000001 each
20-
print(exchange.cancel_all_market_orders(26)) # Cancels all orders in market 26, dgc
17+
from Cryptsy import Api
18+
exchange = Api('KEY HERE', 'SECRET HERE')
19+
print(exchange.buy(26, 100, 0.00000001)) # Buy 100 dgc at .00000001 each
20+
print(exchange.cancel_all_market_orders(26)) # Cancels all orders in market 26, dgc
2121
```
2222

2323
Running the tests
@@ -46,7 +46,6 @@ inherently better than programming without)!
4646

4747
[![Support via Gittip](https://rawgithub.com/chris---/Donation-Badges/master/gittip.jpeg)](https://www.gittip.com/jaapz)
4848

49-
5049
License
5150
-------
5251
This piece of software is licensed under the GPL2 license, see `license`.

test_cryptsy.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,27 @@ def test_marketid_should_be_added_as_get_parameter(self, mock_urlopen,
4242
api):
4343
rv = api._public_api_query('testmethod', marketid=10)
4444
assert rv['url'] == 'http://pubapi.cryptsy.com/api.php?method=testmethod&marketid=10'
45+
46+
47+
@pytest.fixture
48+
def mock_create_order(monkeypatch):
49+
""" Mock the create order so we can check if the correct ordertype is
50+
used. """
51+
def _mock_create_order(self, marketid, ordertype, quantity, price):
52+
return ordertype
53+
54+
monkeypatch.setattr(Api, '_create_order', _mock_create_order)
55+
56+
57+
def test_buy(mock_create_order, api):
58+
""" The buy method should call the _create_order method with the ordertyp
59+
as 'Buy'. """
60+
rv = api.buy(26, 10, 0.0000001)
61+
assert rv == 'Buy'
62+
63+
64+
def test_sell(mock_create_order, api):
65+
""" The sell method should call the _create_order method with the ordertyp
66+
as 'Sell'. """
67+
rv = api.sell(26, 10, 0.0000001)
68+
assert rv == 'Sell'

0 commit comments

Comments
 (0)