Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.61 KB | None | 0 0
  1. # python3.4
  2.  
  3. ' Wrapper for Common PyBitshares DEX Algo Trading API Calls '
  4.  
  5. # data is in easy to quant float / list of floats / dict of floats format
  6.  
  7.     # buy / sell / cancel
  8.     # orders / balance / complete_balance
  9.     # orderbook / last_price
  10.  
  11. # if no price / amount specified executes market order buy/sell
  12. # if no expiration specified default is 3.2 years
  13. # cancels all outstanding orders in market
  14.  
  15. ' BTS: litepresence1 '
  16.  
  17. # http://docs.pybitshares.com
  18. from bitshares.market import Market
  19. from bitshares.account import Account
  20. from bitshares import BitShares
  21. import time
  22.  
  23. BitCURRENCY = 'USD'
  24. BitASSET = 'BTS'
  25. ACCOUNT = Account("")
  26. PASS_PHRASE = ""
  27.  
  28.  
  29. MARKET_PAIR = BitASSET + ":" + BitCURRENCY
  30. MARKET = Market(MARKET_PAIR)
  31. SATOSHI = 0.00000001
  32. ANTISAT = 1 / SATOSHI
  33.  
  34.  
  35. def dex(command, amount=ANTISAT, price=None, depth=1, expiration=ANTISAT):
  36.  
  37.     MARKET.bitshares.wallet.unlock(PASS_PHRASE)
  38.     ACCOUNT.refresh()
  39.  
  40.     if command == 'buy':
  41.  
  42.         # buy relentlessly until satisfied or currency exhausted
  43.         print(('Bitshares API', command))
  44.         if price is None:
  45.             price = ANTISAT
  46.         print(('buying', amount, 'at', price))
  47.         attempt = 1
  48.         currency = float(ACCOUNT.balance(BitCURRENCY))
  49.         if amount > 0.998 * currency * price:
  50.             amount = 0.998 * currency * price
  51.         if amount > 0:
  52.             while attempt:
  53.                 try:
  54.                     details = (MARKET.buy(price, amount, expiration))
  55.                     print (details)
  56.                     attempt = 0
  57.                 except:
  58.                     print(("buy attempt %s failed" % attempt))
  59.                     attempt += 1
  60.                     if attempt > 10:
  61.                         print ('buy aborted')
  62.                         return
  63.                     pass
  64.         else:
  65.             print('no currency to buy')
  66.  
  67.     if command == 'sell':
  68.  
  69.         # sell relentlessly until satisfied or assets exhausted
  70.         expiration = 86400 * 7
  71.         print(('Bitshares API', command))
  72.         if price is None:
  73.             price = SATOSHI
  74.         print(('selling', amount, 'at', price))
  75.         attempt = 1
  76.         assets = float(ACCOUNT.balance(BitASSET))
  77.         if amount > 0.998 * assets:
  78.             amount = 0.998 * assets
  79.         if amount > 0:
  80.             while attempt:
  81.                 try:
  82.                     details = (MARKET.sell(price, amount, expiration))
  83.                     print (details)
  84.                     attempt = 0
  85.                 except:
  86.                     print(("sell attempt %s failed" % attempt))
  87.                     attempt += 1
  88.                     if attempt > 10:
  89.                         print ('sell aborted')
  90.                         return
  91.                     pass
  92.         else:
  93.             print('no assets to sell')
  94.  
  95.     if (command == 'cancel'):
  96.  
  97.         # cancel all orders in this MARKET relentlessly until satisfied
  98.         print(('Bitshares API', command))
  99.         orders = MARKET.accountopenorders()
  100.         print((len(orders), 'open orders to cancel'))
  101.         for order in orders:
  102.             attempt = 1
  103.             while attempt:
  104.                 print('API Bitshares Cancel')
  105.                 try:
  106.                     details = (MARKET.cancel(order['id']))
  107.                     print (details)
  108.                     attempt = 0
  109.                 except:
  110.                     print((attempt, 'cancel failed', order['id']))
  111.                     attempt += 1
  112.                     if attempt > 10:
  113.                         print ('cancel aborted')
  114.                         return
  115.                     pass
  116.  
  117.     if command == 'orders':
  118.  
  119.         # dictionary of open orders in traditional format:
  120.         # orderNumber, orderType, market, amount, price
  121.         print(('Bitshares API', command))
  122.         orders = []
  123.         for order in MARKET.accountopenorders():
  124.             orderNumber = order['id']
  125.             asset = order['base']['symbol']
  126.             currency = order['quote']['symbol']
  127.             amount = float(order['base'])
  128.             price = float(order['price'])
  129.             orderType = 'buy'
  130.             if asset == BitASSET:
  131.                 orderType = 'sell'
  132.                 price = 1 / price
  133.             orders.append({'orderNumber': orderNumber, 'orderType': orderType,
  134.                            'market': MARKET_PAIR, 'amount': amount,
  135.                            'price': price})
  136.         for o in orders:
  137.             print (o)
  138.         if len(orders) == 0:
  139.             print ('no open orders')
  140.         return orders
  141.  
  142.     if command == 'market_balances':
  143.  
  144.         # dictionary of currency and assets in this MARKET
  145.         print(('Bitshares API', command))
  146.         currency = float(ACCOUNT.balance(BitCURRENCY))
  147.         assets = float(ACCOUNT.balance(BitASSET))
  148.         balances = {'currency': currency, 'assets': assets}
  149.         print (balances)
  150.         return balances
  151.  
  152.     if command == 'complete_balances':
  153.  
  154.         # dictionary of ALL account balances
  155.         print(('Bitshares API', command))
  156.         raw = list(ACCOUNT.balances)
  157.         balances = {}
  158.         for i in range(len(raw)):
  159.             balances[raw[i]['symbol']] = float(raw[i]['amount'])
  160.         print (balances)
  161.         return balances
  162.  
  163.     if command == 'book':
  164.  
  165.         # dictionary of 4 lists containing bid/ask volume/price
  166.         print(('Bitshares API', command))
  167.         raw = MARKET.orderbook(limit=depth)
  168.         bids = raw['bids']
  169.         asks = raw['asks']
  170.         bidp = [float(bids[i]['price']) for i in range(len(bids))]
  171.         bidv = [float(bids[i]['quote']) for i in range(len(bids))]
  172.         askp = [float(asks[i]['price']) for i in range(len(asks))]
  173.         askv = [float(asks[i]['quote']) for i in range(len(asks))]
  174.         book = {'bidp': bidp, 'bidv': bidv, 'askp': askp, 'askv': askv}
  175.         print(book)
  176.         # print(book['bidp'][0]) #highest bid price
  177.         # print(book['askp'][0]) #lowest ask price
  178.         # print(book['bidv'][0]) #highest bid volume
  179.         # print(book['askv'][0]) #lowest ask volume
  180.         return book
  181.  
  182.     if command == 'last':
  183.  
  184.         # the most recent transation in this MARKET
  185.         print(('Bitshares API', command))
  186.         raw = MARKET.ticker()['latest']
  187.         price = float(raw)
  188.         print (price)
  189.         return price
  190.  
  191.  
  192. # buy/sell/cancel
  193.  
  194. dex('buy', amount=1.111, price=0.17, expiration=86400)
  195. dex('sell', amount=0.979, price=0.25, expiration=86400)
  196. dex('cancel')
  197.  
  198. # orders / balances / last / book
  199.  
  200. orders = dex('orders')
  201. market_balances = dex('market_balances')
  202. comlete_balances = dex('complete_balances')
  203. last = dex('last')
  204. book = dex('book', depth=5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement