Skip to content

Commit 402d925

Browse files
authored
Merge pull request #21 from cryptomkt/develop
Upgrade to API v3
2 parents 87e6021 + 32b81ae commit 402d925

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+5559
-3812
lines changed

README.md

Lines changed: 137 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
# CryptoMarket-Python
2-
[main page](https://www.cryptomkt.com/)
32

3+
[main page](https://www.cryptomkt.com/)
44

55
[sign up in CryptoMarket](https://www.cryptomkt.com/account/register).
66

77
# Installation
8+
89
To install Cryptomarket use pip
10+
911
```
1012
pip install cryptomarket
1113
```
12-
# Documentation
1314

14-
This sdk makes use of the [api version 2](https://api.exchange.cryptomkt.com/v2) of cryptomarket
15+
# Documentation
1516

17+
This sdk makes use of the [api version 3](https://api.exchange.cryptomkt.com) of cryptomarket
1618

1719
# Quick Start
1820

1921
## rest client
22+
2023
```python
2124
from cryptomarket.client import Client
25+
from cryptomarket.args import Account, Side, OrderType
2226
from cryptomarket.exceptions import CryptomarketSDKException
2327

2428
# instance a client
@@ -30,92 +34,141 @@ client = Client(api_key, api_secret)
3034
currencies = client.get_currencies()
3135

3236
# get order books
33-
order_book = client.get_order_book('EOSETH')
37+
order_book = client.get_order_book_of_symbol('EOSETH')
3438

35-
# get your account balances
36-
account_balance = client.get_account_balance()
39+
# get your wallet balances
40+
wallet_balance = client.get_wallet_balances()
3741

38-
# get your trading balances
39-
trading_balance = client.get_trading_balance()
42+
# get your spot trading balances
43+
trading_balance = client.get_spot_trading_balances()
4044

41-
# move balance from account bank to account trading
42-
result = client.transfer_money_from_bank_balance_to_trading_balance('ETH', '3.2')
45+
# move balance from wallet account to trading account
46+
transfer = client.transfer_between_wallet_and_exchange('ETH', '3.2', source=Account.WALLET, destination=Account.SPOT)
4347

44-
# get your active orders
45-
orders = client.get_active_orders('EOSETH')
48+
# get your active spot orders
49+
orders = client.get_all_active_spot_orders('EOSETH')
4650

47-
# create a new order
48-
order = client.create_order('EOSETH', 'buy', '10', order_type=args.ORDER_TYPE.MARKET)
51+
# create a new spot order
52+
order = client.create_spot_order('EOSETH', Side.BUY, '10', type=OrderType.MARKET)
4953
```
5054

51-
## websocket client
52-
53-
All websocket calls work with callbacks, subscriptions use a callback with one argument for the subscription feed. All the other callbacks takes two arguments, err and result: callback(err, result). If the transaction is successful err is None and the result is in result. If the transaction fails, result is None and the error is in err.
54-
55-
There are three websocket clients, the PublicClient, the TradingClient and the AccountClient.
56-
57-
```python
58-
from cryptomarket.websocket import PublicClient, TradingClient, AccountClient
59-
60-
# THE PUBLIC CLIENT
61-
62-
wsclient = PublicClient()
63-
64-
wsclient.connect() # blocks until connected
65-
66-
def my_callback(err, data):
67-
if err is not None: # deal with error
68-
print(data)
69-
70-
# get currencies
71-
wsclient.get_currencies(my_callback)
72-
73-
74-
# get an order book feed,
75-
# feed_callback is for the subscription feed, with one argument
76-
# result_callback is for the subscription result (success or failure)
77-
def feed_callback(feed):
78-
print(feed)
55+
## Websocket Clients
7956

80-
wsclient.subscribe_to_order_book('EOSETH', callback=feed_callback, result_calback=my_callback)
57+
there are three websocket clients, `MarketDataClient`, the `TradingClient` and the `WalletClient`. The `MarketDataClient` is public, while the others require authentication to be used.
8158

82-
# THE TRADING CLIENT
59+
Some subscription callbacks take a second argument, indicating the type of notification, either 'snapshsot' or 'update'.
8360

84-
wsclient = TradingClient(api_key, api_secret)
61+
### MarketDataClient
8562

86-
wsclient.connect() # blocks until connected and authenticated.
63+
There are no unsubscriptions methods for the `MarketDataClient`. To stop recieving messages is recomended to close the `MarketDataClient`.
8764

88-
# get your trading balances
89-
wsclient.get_trading_balance(my_callback)
90-
91-
# get your active orders
92-
wsclient.get_active_orders(my_callback)
65+
```python
66+
# instance a client
67+
client = MarketDataClient()
68+
client.connect()
69+
70+
# subscribe to public trades
71+
def trades_callback(trades_by_symbol: Dict[str, List[WSTrade]], notification_type):
72+
for symbol in trades_by_symbol:
73+
trade_list = trades_by_symbol[symbol]
74+
for trade in trade_list:
75+
print(trade)
76+
client.subscribe_to_trades(
77+
callback=trades_callback,
78+
symbols=['ETHBTC'],
79+
limit=5,
80+
)
81+
82+
# subscribe to symbol tickers
83+
def ticker_callback(tikers_of_symbol: Dict[str, WSTicker]):
84+
for symbol in tikers_of_symbol:
85+
ticker = tikers_of_symbol[symbol]
86+
print(ticker)
87+
client.subscribe_to_ticker(
88+
callback=ticker_callback,
89+
speed=TickerSpeed._3_SECONDS,
90+
result_callback=lambda err, result: print(f'err:{err}, result:{result}')
91+
)
92+
93+
# run for some time
94+
time.sleep(10)
95+
96+
# close the client
97+
client.close()
98+
```
9399

94-
# create a new order
95-
clientOrderId = '123123123'
96-
wsclient.create_order('EOSETH', 'buy', '3', callback=my_callback)
100+
### TradingClient
97101

98-
# THE ACCONUT CLIENT
102+
```python
103+
# instance a client with a 15 seconds window
104+
client = TradingClient(api_key, api_secret, window=15_000)
105+
client.connect()
106+
# close the client
107+
client.close()
108+
109+
# subscribe to order reports
110+
def print_feed(feed, feed_type):
111+
for report in feed:
112+
print(report)
113+
client.subscribe_to_reports(callback)
114+
# unsubscribe from order reports
115+
client.unsubscribe_to_reports()
116+
117+
client_order_id = str(int(time.time()*1000))
118+
119+
# create an order
120+
client.create_spot_order(
121+
client_order_id=client_order_id,
122+
symbol='EOSETH',
123+
side='sell',
124+
quantity='0.01',
125+
price='10000',
126+
)
127+
128+
# candel an order
129+
client.cancel_spot_order(client_order_id)
99130

100-
wsclient = AccountClient(api_key, api_secret)
131+
```
101132

102-
wsclient.connect() # blocks until connected
133+
### WalletClient
103134

104-
wsclient.get_account_balance(my_callback)
135+
```python
136+
# instance a client
137+
client = WalletClient(api_key, api_secret)
138+
client.connect()
139+
140+
# close the client
141+
client.close()
142+
143+
# subscribe to wallet transactions
144+
def callback(transaction):
145+
print(transaction)
146+
client.subscribe_to_transactions(callback)
147+
148+
# unsubscribe from wallet transactions
149+
err = client.unsubscribe_to_transactions()
150+
151+
# get wallet balances
152+
def callback(err, balances):
153+
if err:
154+
print(err)
155+
return
156+
print(balances)
157+
client.get_wallet_balances(callback)
105158
```
106159

107-
108160
## exception handling
161+
109162
```python
110163
from cryptomarket.client import Client
111164
from cryptomarket.exceptions import CryptomarketSDKException
112165

113166
client = Client(api_key, secret_key)
114167

115-
# catch a wrong argument
168+
# catch a wrong argument
116169
try:
117170
order = client.create_order(
118-
symbol='EOSETH',
171+
symbol='EOSETH',
119172
side='selllll', # wrong
120173
quantity='3'
121174
)
@@ -127,72 +180,40 @@ try:
127180
order = client.create_order(
128181
symbol='eosehtt', # non existant symbol
129182
side='sell',
130-
quantity='10',
183+
quantity='10',
131184
)
132185
except CryptomarketSDKException as e:
133186
print(f'exception catched {e}')
134187

135188

136-
wsclient = TradingClient(api_key, api_secret)
137-
138-
# websocket errors are passed as the first argument to the callback
139-
def callback(err, result):
140-
if err is not None:
141-
print('an error ocurred')
142-
print(err)
143-
else:
144-
print('successful transaction')
145-
print(result)
189+
client = WalletClient(api_key, api_secret)
190+
try:
191+
client.connect()
192+
except Exception as e:
193+
# here we are catching connection and authentication errors
194+
print(e)
195+
```
146196

147-
wsclient.authenticate(callback=callback)
197+
websocket methods take callbacks with two parameters, the first is the possible error, the second is the result of response from the server, for example:
148198

149-
# catch authorization error
150-
# to catch an authorization error on client connection, a on_error function must be passed to the client
151-
wsclient = TradingClient(apiKey, apiSecret, on_error=lambda err: print(err))
199+
```python
200+
def callback(err, balances):
201+
if err:
202+
print(err)
203+
return
204+
print(balances)
205+
client.get_wallet_balances(callback)
152206
```
153207

154-
# Constants of interest
208+
websocket subscriptions also have this type of callback, but is called **result_callback** instead
209+
210+
## Constants of interest
155211

156212
All constants required for calls are in the `cryptomarket.args` module.
157-
each enum has the name of the argument that needs it.
158-
Here is the full list
159-
```python
160-
import cryptomarket.args as args
161-
162-
args.SORT.ASCENDING = 'ASC'
163-
args.SORT.DESCENDING = 'DESC'
164-
165-
args.BY.TIMESTAMP = 'timestamp'
166-
args.BY.ID = 'id'
167-
168-
args.PERIOD._1_MINS = 'M1'
169-
args.PERIOD._3_MINS = 'M3'
170-
args.PERIOD._5_MINS = 'M5'
171-
args.PERIOD._15_MINS = 'M15'
172-
args.PERIOD._30_MINS = 'M30'
173-
args.PERIOD._1_HOURS = 'H1'
174-
args.PERIOD._4_HOURS = 'H4'
175-
args.PERIOD._1_DAYS = 'D1'
176-
args.PERIOD._7_DAYS = 'D7'
177-
args.PERIOD._1_MONTH = '1M'
178-
179-
args.SIDE.BUY = 'buy'
180-
args.SIDE.SELL = 'sell'
181-
182-
args.ORDER_TYPE.LIMIT = 'limit'
183-
args.ORDER_TYPE.MARKET = 'market'
184-
args.ORDER_TYPE.STOPLIMIT = 'stopLimit'
185-
args.ORDER_TYPE.STOPMARKET = 'stopMarket'
186-
187-
args.TIME_IN_FORCE.GTC = 'GTC' # Good till canceled
188-
args.TIME_IN_FORCE.IOC = 'IOC' # Immediate or cancell
189-
args.TIME_IN_FORCE.FOK = 'FOK' # Fill or kill
190-
args.TIME_IN_FORCE.DAY = 'Day' # Good for the day
191-
args.TIME_IN_FORCE.GTD = 'GDT' # Good till date
192-
193-
args.TRANSFER_BY.USERNAME = 'username',
194-
args.TRANSFER_BY.EMAIL = 'email'
195-
```
213+
214+
## Dataclasses
215+
216+
All classes returned by the client are in the `cryptomarket.dataclasses` module
196217

197218
# Checkout our other SDKs
198219

0 commit comments

Comments
 (0)