Skip to content

Commit

Permalink
Merge pull request #28 from lancechua/master
Browse files Browse the repository at this point in the history
Added option for encryted API key/secret and get_market_summary
  • Loading branch information
ericsomdahl authored Aug 6, 2017
2 parents bcdc790 + 990af2d commit 6d9e55a
Showing 1 changed file with 34 additions and 63 deletions.
97 changes: 34 additions & 63 deletions bittrex/bittrex.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
from urllib.parse import urljoin
import requests

try:
from Crypto.Cipher import AES
import getpass, ast, json
encrypted = True
except ImportError:
encrypted = False

BUY_ORDERBOOK = 'buy'
SELL_ORDERBOOK = 'sell'
BOTH_ORDERBOOK = 'both'
Expand All @@ -24,6 +31,17 @@
ACCOUNT_SET = {'getbalances', 'getbalance', 'getdepositaddress', 'withdraw', 'getorderhistory', 'getorder'}


def encrypt(api_key, api_secret, export=True, export_fn='secrets.json'):
cipher = AES.new(getpass.getpass('Input encryption password (string will not show)'))
api_key_n = cipher.encrypt(api_key)
api_secret_n = cipher.encrypt(api_secret)
api = {'key': str(api_key_n), 'secret':str(api_secret_n)}
if export:
with open(export_fn, 'w') as outfile:
json.dump(api, outfile)
return api


class Bittrex(object):
"""
Used for requesting Bittrex with API key and API secret
Expand All @@ -32,16 +50,27 @@ def __init__(self, api_key, api_secret):
self.api_key = str(api_key) if api_key is not None else ''
self.api_secret = str(api_secret) if api_secret is not None else ''

def decrypt(self):
if encrypted:
cipher = AES.new(getpass.getpass('Input decryption password (string will not show)'))
try:
self.api_key = ast.literal_eval(self.api_key) if type(self.api_key) == str else self.api_key
self.api_secret = ast.literal_eval(self.api_secret) if type(self.api_secret) == str else self.api_secret
except:
pass
self.api_key = cipher.decrypt(self.api_key).decode()
self.api_secret = cipher.decrypt(self.api_secret).decode()
else:
raise ImportError('"pycrypto" module has to be installed')


def api_query(self, method, options=None):
"""
Queries Bittrex with given method and options
:param method: Query method for getting info
:type method: str
:param options: Extra options for query
:type options: dict
:return: JSON response from Bittrex
:rtype : dict
"""
Expand Down Expand Up @@ -71,7 +100,6 @@ def get_markets(self):
"""
Used to get the open and available trading markets
at Bittrex along with other meta data.
:return: Available market info in JSON
:rtype : dict
"""
Expand All @@ -81,7 +109,6 @@ def get_currencies(self):
"""
Used to get all supported currencies at Bittrex
along with other meta data.
:return: Supported currencies info in JSON
:rtype : dict
"""
Expand All @@ -90,10 +117,8 @@ def get_currencies(self):
def get_ticker(self, market):
"""
Used to get the current tick values for a market.
:param market: String literal for the market (ex: BTC-LTC)
:type market: str
:return: Current values for given market in JSON
:rtype : dict
"""
Expand All @@ -102,7 +127,6 @@ def get_ticker(self, market):
def get_market_summaries(self):
"""
Used to get the last 24 hour summary of all active exchanges
:return: Summaries of active exchanges in JSON
:rtype : dict
"""
Expand All @@ -123,17 +147,13 @@ def get_marketsummary(self, market):
def get_orderbook(self, market, depth_type, depth=20):
"""
Used to get retrieve the orderbook for a given market
:param market: String literal for the market (ex: BTC-LTC)
:type market: str
:param depth_type: buy, sell or both to identify the type of orderbook to return.
Use constants BUY_ORDERBOOK, SELL_ORDERBOOK, BOTH_ORDERBOOK
:type depth_type: str
:param depth: how deep of an order book to retrieve. Max is 100, default is 20
:type depth: int
:return: Orderbook of market in JSON
:rtype : dict
"""
Expand All @@ -143,15 +163,11 @@ def get_market_history(self, market, count):
"""
Used to retrieve the latest trades that have occurred for a
specific market.
/market/getmarkethistory
:param market: String literal for the market (ex: BTC-LTC)
:type market: str
:param count: Number between 1-100 for the number of entries to return (default = 20)
:type count: int
:return: Market history in JSON
:rtype : dict
"""
Expand All @@ -162,19 +178,14 @@ def buy_market(self, market, quantity):
Used to place a buy order in a specific market. Use buymarket to
place market orders. Make sure you have the proper permissions
set on your API keys for this call to work
/market/buymarket
:param market: String literal for the market (ex: BTC-LTC)
:type market: str
:param quantity: The amount to purchase
:type quantity: float
:param rate: The rate at which to place the order.
This is not needed for market orders
:type rate: float
:return:
:rtype : dict
"""
Expand All @@ -185,19 +196,14 @@ def buy_limit(self, market, quantity, rate):
Used to place a buy order in a specific market. Use buylimit to place
limit orders Make sure you have the proper permissions set on your
API keys for this call to work
/market/buylimit
:param market: String literal for the market (ex: BTC-LTC)
:type market: str
:param quantity: The amount to purchase
:type quantity: float
:param rate: The rate at which to place the order.
This is not needed for market orders
:type rate: float
:return:
:rtype : dict
"""
Expand All @@ -208,19 +214,14 @@ def sell_market(self, market, quantity):
Used to place a sell order in a specific market. Use sellmarket to place
market orders. Make sure you have the proper permissions set on your
API keys for this call to work
/market/sellmarket
:param market: String literal for the market (ex: BTC-LTC)
:type market: str
:param quantity: The amount to purchase
:type quantity: float
:param rate: The rate at which to place the order.
This is not needed for market orders
:type rate: float
:return:
:rtype : dict
"""
Expand All @@ -231,19 +232,14 @@ def sell_limit(self, market, quantity, rate):
Used to place a sell order in a specific market. Use selllimit to place
limit orders Make sure you have the proper permissions set on your
API keys for this call to work
/market/selllimit
:param market: String literal for the market (ex: BTC-LTC)
:type market: str
:param quantity: The amount to purchase
:type quantity: float
:param rate: The rate at which to place the order.
This is not needed for market orders
:type rate: float
:return:
:rtype : dict
"""
Expand All @@ -252,12 +248,9 @@ def sell_limit(self, market, quantity, rate):
def cancel(self, uuid):
"""
Used to cancel a buy or sell order
/market/cancel
:param uuid: uuid of buy or sell order
:type uuid: str
:return:
:rtype : dict
"""
Expand All @@ -266,12 +259,9 @@ def cancel(self, uuid):
def get_open_orders(self, market):
"""
Get all orders that you currently have opened. A specific market can be requested
/market/getopenorders
:param market: String literal for the market (ie. BTC-LTC)
:type market: str
:return: Open orders info in JSON
:rtype : dict
"""
Expand All @@ -280,9 +270,7 @@ def get_open_orders(self, market):
def get_balances(self):
"""
Used to retrieve all balances from your account
/account/getbalances
:return: Balances info in JSON
:rtype : dict
"""
Expand All @@ -293,12 +281,9 @@ def get_balances(self):
def get_balance(self, currency):
"""
Used to retrieve the balance from your account for a specific currency
/account/getbalance
:param currency: String literal for the currency (ex: LTC)
:type currency: str
:return: Balance info in JSON
:rtype : dict
"""
Expand All @@ -307,12 +292,9 @@ def get_balance(self, currency):
def get_deposit_address(self, currency):
"""
Used to generate or retrieve an address for a specific currency
/account/getdepositaddress
:param currency: String literal for the currency (ie. BTC)
:type currency: str
:return: Address info in JSON
:rtype : dict
"""
Expand All @@ -321,18 +303,13 @@ def get_deposit_address(self, currency):
def withdraw(self, currency, quantity, address):
"""
Used to withdraw funds from your account
/account/withdraw
:param currency: String literal for the currency (ie. BTC)
:type currency: str
:param quantity: The quantity of coins to withdraw
:type quantity: float
:param address: The address where to send the funds.
:type address: str
:return:
:rtype : dict
"""
Expand All @@ -341,25 +318,19 @@ def withdraw(self, currency, quantity, address):
def get_order_history(self, market, count):
"""
Used to reterieve order trade history of account
/account/getorderhistory
:param market: optional a string literal for the market (ie. BTC-LTC). If ommited, will return for all markets
:type market: str
:param count: optional the number of records to return
:param count: optional the number of records to return
:type count: int
:return: order history in JSON
:rtype : dict
"""
return self.api_query('getorderhistory', {'market':market, 'count': count})

def get_order(self, uuid):
"""
Used to get details of buy or sell order
/account/getorder
:param uuid: uuid of buy or sell order
Expand All @@ -368,4 +339,4 @@ def get_order(self, uuid):
:return:
:rtype : dict
"""
return self.api_query('getorder', {'uuid': uuid})
return self.api_query('getorder', {'uuid': uuid})

0 comments on commit 6d9e55a

Please sign in to comment.