-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyRock.py
220 lines (181 loc) · 8.74 KB
/
PyRock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import json
import requests
import hmac
import hashlib
import time
StdUrl = 'https://api.therocktrading.com/v1/'
class PyRock:
# credentials
def __init__(self, ApiKey='apikey', Secret='apisecret'):
self.key = ApiKey
self.secret = Secret
# create headers
@classmethod
def getheaders(self, url, secret, key):
nonce = str(int(time.time() * 1e6))
message = str(nonce)+url
signature = hmac.new(secret.encode(), msg=message.encode(), digestmod=hashlib.sha512).hexdigest()
headers = {'User-Agent': 'PyRock v1', 'content-type': 'application/json', 'X-TRT-KEY': key, 'X-TRT-SIGN': signature, 'X-TRT-NONCE': nonce}
return headers
# BASIC API https://api.therocktrading.com/doc/v1/#api-Basic_API
# Balance of a certain fund
def Balance(self, fund):
url = StdUrl+'balances/'+fund.upper()
r = requests.get(url, headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
# All Balances
def AllBalances(self):
url = StdUrl+'balances'
r = requests.get(url, headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
# discount levels of one fund
def DiscountLevel(self, fund):
url = StdUrl+'discounts/'+fund.upper()
r = requests.get(url, headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
# discount levels of all funds
def AllDiscountLevels(self):
url = StdUrl+'discounts'
r = requests.get(url, headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
#Withdraw limits of one fund
def WithdrawLimit(self, fund):
url = StdUrl+'withdraw_limits/'+fund.upper()
r = requests.get(url, headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
#Withdraw limits of all funds
def AllWithdrawLimits(self):
url = StdUrl+'withdraw_limits'
r = requests.get(url, headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
# MARKET API https://api.therocktrading.com/doc/v1/#api-Market_API
# get all funds at once
def Funds(self):
url = StdUrl+'funds'
r = requests.get(url, headers = {'User-Agent': 'PyRock v1', 'content-type': 'application/json'}, timeout=30)
return r.json()
# get the orderbook for a certain fund
def OrderBook(self, fund):
url = StdUrl+'funds/'+fund.upper()+'/orderbook'
r = requests.get(url, headers = {'User-Agent': 'PyRock v1', 'content-type': 'application/json'}, timeout=30)
return r.json()
# get the ticker for a certain fund
def Ticker(self, fund):
url = StdUrl+'funds/'+fund.upper()+'/ticker'
r = requests.get(url, headers = {'User-Agent': 'PyRock v1', 'content-type': 'application/json'}, timeout=30)
return r.json()
# get all tickers
def AllTickers(self):
url = StdUrl+'funds/tickers'
r = requests.get(url, headers = {'User-Agent': 'PyRock v1', 'content-type': 'application/json'}, timeout=30)
return r.json()
# get the last trades for a certain fund, with pagination.
def Trades(self, fund):
url = StdUrl+'funds/'+fund.upper()+'/trades'
r = requests.get(url, headers = {'User-Agent': 'PyRock v1', 'content-type': 'application/json'}, timeout=30)
return r.json()
# get the last trades for a certain fund, after x date and before x date, with pagination
def TradesTime(self, fund, after, before):
url = StdUrl+'funds/'+fund.upper()+'/trades?after='+after+'&before='+before
r = requests.get(url, headers = {'User-Agent': 'PyRock v1', 'content-type': 'application/json'}, timeout=30)
return r.json()
# TRADING API https://api.therocktrading.com/doc/v1/#api-Trading_API
# Show the list of orders for a certain fund
def ListAllOrders(self, fund):
url = StdUrl+'funds/'+fund.upper()+'/orders'
r = requests.get(url, headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
# Show the details of one order
def ListOrder(self, fund, orderId):
url = StdUrl+'funds/'+fund.upper()+'/orders/'+str(orderId)
r = requests.get(url, headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
# Cancell all open orders for a certain fund
def CancelAllOrders(self, fund):
url = StdUrl+'funds/'+fund.upper()+'/orders/remove_all'
r = requests.delete(url, headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r
# Show the details of one order
def CancelOrder(self, fund, orderId):
url = StdUrl+'funds/'+fund.upper()+'/orders/'+str(orderId)
r = requests.delete(url, headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
def PlaceBuyOrder(self, fund, amount, price):
url = StdUrl+'funds/'+fund.upper()+'/orders'
values = {
'fund_id' : fund.upper(),
'side' : 'buy',
'amount' : str(amount),
'price' : str(price)
}
r = requests.post(url, data = json.dumps(values), headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
def PlaceSellOrder(self, fund, amount, price):
url = StdUrl+'funds/'+fund.upper()+'/orders'
values = {
'fund_id' : fund.upper(),
'side' : 'sell',
'amount' : amount,
'price' : price
}
r = requests.post(url, data = json.dumps(values), headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
# Get user's transactions, with pagination
def Transactions(self):
url = StdUrl+'transactions'
r = requests.get(url, headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
# Get user's transactions by FUND_ID, with pagination
def TransactionsByFund(self, fund):
url = StdUrl+'transactions?fund_id='+fund.upper()
r = requests.get(url, headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
# Get user's transactions by CURRENCY, with pagination
def TransactionsByCurrency(self, currency):
url = StdUrl+'transactions?currency='+currency.upper()
r = requests.get(url, headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
# Get user's transactions by TIME, with pagination
def TransactionsByTime(self, after, before):
url = StdUrl+'transactions?after='+after+'&before='+before
r = requests.get(url, headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
# get user's trades only, with pagination
def UserTrades(self, fund):
url = StdUrl+'funds/'+fund.upper()+'/trades'
r = requests.get(url, headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
# get user's trades only, with pagination, after a certain date and before another one
def UserTradesTime(self, fund, after, before):
url = StdUrl+'funds/'+fund.upper()+'/trades?after='+after+'&before='+before
r = requests.get(url, headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
# WITHDRAWAL API https://api.therocktrading.com/doc/v1/#api-Withdrawal_API
# withdraw a certain currency to an address
def Withdraw(self, currency, address, amount):
url = StdUrl+'atms/withdraw'
values = {
'currency' : currency,
'destination_address' : address,
'amount' : amount,
}
r = requests.post(url, data = json.dumps(values), headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
def WithdrawRipple(self, currency, address, amount):
url = StdUrl+'atms/withdraw'
values = {
'currency' : currency.upper(),
'withdraw_method' : 'RIPPLE',
'destination_address' : address,
'amount' : amount
}
r = requests.post(url, data = json.dumps(values), headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
# authenticated GET request to one url, used in example_pagination.py
def paginateSig(self, url):
r = requests.get(url, headers = PyRock.getheaders(url, self.secret, self.key), timeout=30)
return r.json()
# unauthenticated GET request to one url, used in example_pagination.py
def paginate(self, url):
r = requests.get(url, headers = {'User-Agent': 'PyRock v1', 'content-type': 'application/json'}, timeout=30)
return r.json()