|
| 1 | +import urllib |
| 2 | +import urllib2 |
| 3 | +import json |
| 4 | +import time |
| 5 | +import hmac,hashlib |
| 6 | + |
| 7 | +class Cryptsy: |
| 8 | + def __init__(self, APIKey, Secret): |
| 9 | + self.APIKey = APIKey |
| 10 | + self.Secret = Secret |
| 11 | + |
| 12 | + def api_query(self, method, req={}): |
| 13 | + if(method=="marketdata" or method=="orderdata"): |
| 14 | + ret = urllib2.urlopen(urllib2.Request('https://www.cryptsy.com/api.php?method=' + method)) |
| 15 | + return json.loads(ret.read()) |
| 16 | + else: |
| 17 | + req['method'] = method |
| 18 | + req['nonce'] = int(time.time()) |
| 19 | + post_data = urllib.urlencode(req) |
| 20 | + |
| 21 | + sign = hmac.new(self.Secret, post_data, hashlib.sha512).hexdigest() |
| 22 | + headers = { |
| 23 | + 'Sign': sign, |
| 24 | + 'Key': self.APIKey |
| 25 | + } |
| 26 | + |
| 27 | + ret = urllib2.urlopen(urllib2.Request('https://www.cryptsy.com/api', post_data, headers)) |
| 28 | + return json.loads(ret.read()) |
| 29 | + |
| 30 | + def getMarketData(self): |
| 31 | + return self.api_query("marketdata") |
| 32 | + |
| 33 | + def getOrderData(self): |
| 34 | + return self.api_query("orderdata") |
| 35 | + |
| 36 | + # Outputs: |
| 37 | + # balances_available Array of currencies and the balances availalbe for each |
| 38 | + # balances_hold Array of currencies and the amounts currently on hold for open orders |
| 39 | + # servertimestamp Current server timestamp |
| 40 | + # servertimezone Current timezone for the server |
| 41 | + # serverdatetime Current date/time on the server |
| 42 | + # openordercount Count of open orders on your account |
| 43 | + def getInfo(self): |
| 44 | + return self.api_query('getinfo') |
| 45 | + |
| 46 | + |
| 47 | + # Outputs: Array of Active Markets |
| 48 | + # marketid Integer value representing a market |
| 49 | + # label Name for this market, for example: AMC/BTC |
| 50 | + # primary_currency_code Primary currency code, for example: AMC |
| 51 | + # primary_currency_name Primary currency name, for example: AmericanCoin |
| 52 | + # secondary_currency_code Secondary currency code, for example: BTC |
| 53 | + # secondary_currency_name Secondary currency name, for example: BitCoin |
| 54 | + # current_volume 24 hour trading volume in this market |
| 55 | + # last_trade Last trade price for this market |
| 56 | + # high_trade 24 hour highest trade price in this market |
| 57 | + # low_trade 24 hour lowest trade price in this market |
| 58 | + def getMarkets(self): |
| 59 | + return self.api_query('getmarkets') |
| 60 | + |
| 61 | + |
| 62 | + # Outputs: Array of Deposits and Withdrawals on your account |
| 63 | + # currency Name of currency account |
| 64 | + # timestamp The timestamp the activity posted |
| 65 | + # datetime The datetime the activity posted |
| 66 | + # timezone Server timezone |
| 67 | + # type Type of activity. (Deposit / Withdrawal) |
| 68 | + # address Address to which the deposit posted or Withdrawal was sent |
| 69 | + # amount Amount of transaction |
| 70 | + def myTransactions(self): |
| 71 | + return self.api_query('mytransactions') |
| 72 | + |
| 73 | + |
| 74 | + # Inputs: |
| 75 | + # marketid Market ID for which you are querying |
| 76 | + ## |
| 77 | + # Outputs: Array of last 1000 Trades for this Market, in Date Decending Order |
| 78 | + # datetime Server datetime trade occurred |
| 79 | + # tradeprice The price the trade occurred at |
| 80 | + # quantity Quantity traded |
| 81 | + # total Total value of trade (tradeprice * quantity) |
| 82 | + def marketTrades(self, marketid): |
| 83 | + return self.api_query('markettrades', {'marketid': marketid}) |
| 84 | + |
| 85 | + |
| 86 | + # Inputs: |
| 87 | + # marketid Market ID for which you are querying |
| 88 | + ## |
| 89 | + # Outputs: 2 Arrays. First array is sellorders listing current open sell orders ordered price ascending. Second array is buyorders listing current open buy orders ordered price descending. |
| 90 | + # sellprice If a sell order, price which order is selling at |
| 91 | + # buyprice If a buy order, price the order is buying at |
| 92 | + # quantity Quantity on order |
| 93 | + # total Total value of order (price * quantity) |
| 94 | + def marketOrders(self, marketid): |
| 95 | + return self.api_query('marketorders', {'marketid': marketid}) |
| 96 | + |
| 97 | + |
| 98 | + # Inputs: |
| 99 | + # marketid Market ID for which you are querying |
| 100 | + # limit (optional) Limit the number of results. Default: 200 |
| 101 | + ## |
| 102 | + # Outputs: Array your Trades for this Market, in Date Decending Order |
| 103 | + # tradeid An integer identifier for this trade |
| 104 | + # tradetype Type of trade (Buy/Sell) |
| 105 | + # datetime Server datetime trade occurred |
| 106 | + # tradeprice The price the trade occurred at |
| 107 | + # quantity Quantity traded |
| 108 | + # total Total value of trade (tradeprice * quantity) |
| 109 | + def myTrades(self, marketid, limit=200): |
| 110 | + return self.api_query('mytrades', {'marketid': marketid, 'limit': limit}) |
| 111 | + |
| 112 | + |
| 113 | + # Outputs: Array your Trades for all Markets, in Date Decending Order |
| 114 | + # tradeid An integer identifier for this trade |
| 115 | + # tradetype Type of trade (Buy/Sell) |
| 116 | + # datetime Server datetime trade occurred |
| 117 | + # marketid The market in which the trade occurred |
| 118 | + # tradeprice The price the trade occurred at |
| 119 | + # quantity Quantity traded |
| 120 | + # total Total value of trade (tradeprice * quantity) |
| 121 | + def allMyTrades(self): |
| 122 | + return self.api_query('allmytrades') |
| 123 | + |
| 124 | + |
| 125 | + # Inputs: |
| 126 | + # marketid Market ID for which you are querying |
| 127 | + ## |
| 128 | + # Outputs: Array of your orders for this market listing your current open sell and buy orders. |
| 129 | + # orderid Order ID for this order |
| 130 | + # created Datetime the order was created |
| 131 | + # ordertype Type of order (Buy/Sell) |
| 132 | + # price The price per unit for this order |
| 133 | + # quantity Quantity for this order |
| 134 | + # total Total value of order (price * quantity) |
| 135 | + def myOrders(self, marketid): |
| 136 | + return self.api_query('myorders', {'marketid': marketid}) |
| 137 | + |
| 138 | + |
| 139 | + # Outputs: Array of all open orders for your account. |
| 140 | + # orderid Order ID for this order |
| 141 | + # marketid The Market ID this order was created for |
| 142 | + # created Datetime the order was created |
| 143 | + # ordertype Type of order (Buy/Sell) |
| 144 | + # price The price per unit for this order |
| 145 | + # quantity Quantity for this order |
| 146 | + # total Total value of order (price * quantity) |
| 147 | + def allMyOrders(self): |
| 148 | + return self.api_query('allmyorders') |
| 149 | + |
| 150 | + |
| 151 | + # Inputs: |
| 152 | + # marketid Market ID for which you are creating an order for |
| 153 | + # ordertype Order type you are creating (Buy/Sell) |
| 154 | + # quantity Amount of units you are buying/selling in this order |
| 155 | + # price Price per unit you are buying/selling at |
| 156 | + ## |
| 157 | + # Outputs: |
| 158 | + # orderid If successful, the Order ID for the order which was created |
| 159 | + def createOrder(self, marketid, ordertype, quantity, price): |
| 160 | + return self.api_query('createorder', {'marketid': marketid, 'ordertype': ordertype, 'quantity': quantity, 'price': price}) |
| 161 | + |
| 162 | + |
| 163 | + # Inputs: |
| 164 | + # orderid Order ID for which you would like to cancel |
| 165 | + ## |
| 166 | + # Outputs: If successful, it will return a success code. |
| 167 | + def cancelOrder(self, orderid): |
| 168 | + return self.api_query('cancelorder', {'orderid': orderid}) |
| 169 | + |
| 170 | + |
| 171 | + # Inputs: |
| 172 | + # ordertype Order type you are calculating for (Buy/Sell) |
| 173 | + # quantity Amount of units you are buying/selling |
| 174 | + # price Price per unit you are buying/selling at |
| 175 | + ## |
| 176 | + # Outputs: |
| 177 | + # fee The that would be charged for provided inputs |
| 178 | + # net The net total with fees |
| 179 | + def calculateFees(self, ordertype, quantity, price): |
| 180 | + return self.api_query('calculatefees', {'ordertype': ordertype, 'quantity': quantity, 'price': price}) |
0 commit comments