Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # python3.4
- ' Wrapper for Common PyBitshares DEX Algo Trading API Calls '
- # data is in easy to quant float / list of floats / dict of floats format
- # buy / sell / cancel
- # orders / balance / complete_balance
- # orderbook / last_price
- # if no price / amount specified executes market order buy/sell
- # if no expiration specified default is 3.2 years
- # cancels all outstanding orders in market
- ' BTS: litepresence1 '
- # http://docs.pybitshares.com
- from bitshares.market import Market
- from bitshares.account import Account
- from bitshares import BitShares
- import time
- BitCURRENCY = 'USD'
- BitASSET = 'BTS'
- ACCOUNT = Account("")
- PASS_PHRASE = ""
- MARKET_PAIR = BitASSET + ":" + BitCURRENCY
- MARKET = Market(MARKET_PAIR)
- SATOSHI = 0.00000001
- ANTISAT = 1 / SATOSHI
- def dex(command, amount=ANTISAT, price=None, depth=1, expiration=ANTISAT):
- MARKET.bitshares.wallet.unlock(PASS_PHRASE)
- ACCOUNT.refresh()
- if command == 'buy':
- # buy relentlessly until satisfied or currency exhausted
- print(('Bitshares API', command))
- if price is None:
- price = ANTISAT
- print(('buying', amount, 'at', price))
- attempt = 1
- currency = float(ACCOUNT.balance(BitCURRENCY))
- if amount > 0.998 * currency * price:
- amount = 0.998 * currency * price
- if amount > 0:
- while attempt:
- try:
- details = (MARKET.buy(price, amount, expiration))
- print (details)
- attempt = 0
- except:
- print(("buy attempt %s failed" % attempt))
- attempt += 1
- if attempt > 10:
- print ('buy aborted')
- return
- pass
- else:
- print('no currency to buy')
- if command == 'sell':
- # sell relentlessly until satisfied or assets exhausted
- expiration = 86400 * 7
- print(('Bitshares API', command))
- if price is None:
- price = SATOSHI
- print(('selling', amount, 'at', price))
- attempt = 1
- assets = float(ACCOUNT.balance(BitASSET))
- if amount > 0.998 * assets:
- amount = 0.998 * assets
- if amount > 0:
- while attempt:
- try:
- details = (MARKET.sell(price, amount, expiration))
- print (details)
- attempt = 0
- except:
- print(("sell attempt %s failed" % attempt))
- attempt += 1
- if attempt > 10:
- print ('sell aborted')
- return
- pass
- else:
- print('no assets to sell')
- if (command == 'cancel'):
- # cancel all orders in this MARKET relentlessly until satisfied
- print(('Bitshares API', command))
- orders = MARKET.accountopenorders()
- print((len(orders), 'open orders to cancel'))
- for order in orders:
- attempt = 1
- while attempt:
- print('API Bitshares Cancel')
- try:
- details = (MARKET.cancel(order['id']))
- print (details)
- attempt = 0
- except:
- print((attempt, 'cancel failed', order['id']))
- attempt += 1
- if attempt > 10:
- print ('cancel aborted')
- return
- pass
- if command == 'orders':
- # dictionary of open orders in traditional format:
- # orderNumber, orderType, market, amount, price
- print(('Bitshares API', command))
- orders = []
- for order in MARKET.accountopenorders():
- orderNumber = order['id']
- asset = order['base']['symbol']
- currency = order['quote']['symbol']
- amount = float(order['base'])
- price = float(order['price'])
- orderType = 'buy'
- if asset == BitASSET:
- orderType = 'sell'
- price = 1 / price
- orders.append({'orderNumber': orderNumber, 'orderType': orderType,
- 'market': MARKET_PAIR, 'amount': amount,
- 'price': price})
- for o in orders:
- print (o)
- if len(orders) == 0:
- print ('no open orders')
- return orders
- if command == 'market_balances':
- # dictionary of currency and assets in this MARKET
- print(('Bitshares API', command))
- currency = float(ACCOUNT.balance(BitCURRENCY))
- assets = float(ACCOUNT.balance(BitASSET))
- balances = {'currency': currency, 'assets': assets}
- print (balances)
- return balances
- if command == 'complete_balances':
- # dictionary of ALL account balances
- print(('Bitshares API', command))
- raw = list(ACCOUNT.balances)
- balances = {}
- for i in range(len(raw)):
- balances[raw[i]['symbol']] = float(raw[i]['amount'])
- print (balances)
- return balances
- if command == 'book':
- # dictionary of 4 lists containing bid/ask volume/price
- print(('Bitshares API', command))
- raw = MARKET.orderbook(limit=depth)
- bids = raw['bids']
- asks = raw['asks']
- bidp = [float(bids[i]['price']) for i in range(len(bids))]
- bidv = [float(bids[i]['quote']) for i in range(len(bids))]
- askp = [float(asks[i]['price']) for i in range(len(asks))]
- askv = [float(asks[i]['quote']) for i in range(len(asks))]
- book = {'bidp': bidp, 'bidv': bidv, 'askp': askp, 'askv': askv}
- print(book)
- # print(book['bidp'][0]) #highest bid price
- # print(book['askp'][0]) #lowest ask price
- # print(book['bidv'][0]) #highest bid volume
- # print(book['askv'][0]) #lowest ask volume
- return book
- if command == 'last':
- # the most recent transation in this MARKET
- print(('Bitshares API', command))
- raw = MARKET.ticker()['latest']
- price = float(raw)
- print (price)
- return price
- # buy/sell/cancel
- dex('buy', amount=1.111, price=0.17, expiration=86400)
- dex('sell', amount=0.979, price=0.25, expiration=86400)
- dex('cancel')
- # orders / balances / last / book
- orders = dex('orders')
- market_balances = dex('market_balances')
- comlete_balances = dex('complete_balances')
- last = dex('last')
- book = dex('book', depth=5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement