forked from ScriptProdigy/CryptsyPythonV2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCryptsy.py
183 lines (147 loc) · 6.73 KB
/
Cryptsy.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
import requests
import time
import hmac,hashlib
import logging
import urllib
logging.getLogger("requests").setLevel(logging.NOTSET)
class Cryptsy:
def __init__(self, PublicKey, PrivateKey, domain="api.cryptsy.com"):
self.domain = domain
self.PublicKey = PublicKey
self.PrivateKey = PrivateKey
def _query(self, method, id=None, action=None, query=[], get_method="GET"):
route = "/api/v2/" + method
if(id != None):
route = route + "/" + str(id)
if(action != None):
route = route + "/" + str(action)
query.append(('nonce', time.time()))
queryStr = urllib.urlencode(query)
link = 'https://' + self.domain + route
sign = hmac.new(self.PrivateKey.encode('utf-8'),
queryStr,
hashlib.sha512).hexdigest()
headers = {'Sign': sign, 'Key': self.PublicKey.encode('utf-8')}
if(get_method == "PUT"):
ret = requests.put(link,
params=query,
headers=headers,
verify=False)
elif(get_method == "DELETE"):
ret = requests.delete(link,
params=query,
headers=headers,
verify=False)
elif(get_method == "POST"):
ret = requests.post(link,
params=query,
headers=headers,
verify=False)
else:
ret = requests.get(link,
params=query,
headers=headers,
verify=False)
print ret.text
try:
jsonRet = ret.json()
return jsonRet
except ValueError:
return {"success": False,
"error": {"ValueError": ["Could not load json string."]}}
# Markets
def markets(self):
return self._query(method="markets")
def market(self, id):
return self._query(method="markets", id=id)
def market_orderbook(self, id, limit=100, otype="both", mine=False):
return self._query(method="markets", id=id, action="orderbook",
query=[("limit", limit), ("type", otype), ("mine", mine)])
def market_tradehistory(self, id, limit=100, mine=False):
return self._query(method="markets", id=id, action="tradehistory",
query=[("limit", limit), ("mine", mine)])
def market_triggers(self, id, limit=100):
return self._query(method="markets", id=id, action="triggers",
query=[("limit", limit)])
def market_ohlc(self, id, start=0, stop=time.time(),
interval="minute", limit=100):
intervals = ["minute", "hour", "day"]
if(interval not in intervals):
interval = intervals[0]
return self._query(method="markets",
id=id,
action="ohlc",
query=[("start", start),
("stop", stop),
("interval", interval),
("limit", limit)])
# Currencies
def currencies(self):
return self._query(method="currencies")
def currency(self, id):
return self._query(method="currencies", id=id)
def currency_markets(self, id):
return self._query(method="currencies", id=id, action="markets")
# User
def balances(self, btype="all"):
return self._query(method="balances", query=[("type", btype)])
def balance(self, id, btype="all"):
return self._query(method="balances", id=id, query=[("type", btype)])
def deposits(self, id=0, limit=100):
if(id != 0):
return self._query(method="deposits",
id=id,
query=[("limit", limit)])
return self._query(method="deposits", query=[("limit", limit)])
def withdrawals(self, id=0, limit=100):
if(id != 0):
return self._query(method="withdrawals",
id=id,
query=[("limit", limit)])
return self._query(method="withdrawals", query=[("limit", limit)])
def addresses(self):
return self._query(method="addresses")
def address(self, id):
return self._query(method="addresses", id=id)
def transfers(self, limit=100):
return self._query(method="transfers", query=[("limit", limit)])
# Orders
def order(self, id):
return self._query(method="order", id=id)
def order_create(self, marketid, quantity, ordertype, price):
return self._query(method="order", query=[("quantity", quantity),
("ordertype", ordertype),
("price", price),
("marketid", marketid)],
get_method="POST")
def order_remove(self, id):
return self._query(method="order", id=id, get_method="DELETE")
# Triggers
def trigger(self, id):
return self._query(method="trigger", id=id)
def trigger_create(self, marketid, ordertype, quantity,
comparison, price, orderprice, expires=''):
return self._query(method="trigger",
query=[("marketid", marketid),
("type", ordertype),
("quantity", quantity),
("comparison", comparison),
("price", price),
("orderprice", orderprice),
("expires", expires)],
get_method="POST")
def trigger_remove(self, id):
return self._query(method="trigger", id=id, get_method="DELETE")
# Converter
def convert(self, id):
return self._query(method="converter", id=id)
def convert_create(self, fromcurrency, tocurrency, sendingamount=0.0,
receivingamount=0.0, tradekey="", feepercent=0.0):
return self._query(method="converter",
query=[("fromcurrency", fromcurrency),
("tocurrency", tocurrency),
("sendingamount", sendingamount),
("receivingamount", receivingamount),
("tradekey", tradekey),
("feepercent", feepercent)],
get_method="POST")