Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from LBank.new_v2_inter.MarketMan import MarketMan
- from LBank.new_v2_inter.OrderMan import OrderMan
- from LBank.AccountMan import AccountMan
- from datetime import datetime
- import time
- class Lbank:
- def __init__(self, args, symbol, use_rsa=True):
- if args['memo'] == 'HmacSHA256':
- sign_method = "HmacSHA256"
- else:
- sign_method = "RSA" if use_rsa else "HmacSHA256"
- self.market = "lbank"
- self.market_client = MarketMan(args['api'], args['secret'], 0, sign_method)
- self.order_client = OrderMan(args['api'], args['secret'], 0, sign_method)
- self.account_client = AccountMan(args['api'], args['secret'], 0, sign_method)
- self.symbol = (symbol[0].lower(), symbol[1].lower(), symbol[2].lower()) + symbol[3:]
- self.last_public_trades = set()
- self.last_private_trades = set()
- def get_info(self):
- book = self.market_client.getBookTicker(symbol=self.symbol[0])
- book = book['data'] if 'data' in book else {}
- best = {'ask_price': float(book['askPrice']) if 'askPrice' in book else 0, 'bid_price': float(book['bidPrice']) if 'bidPrice' in book else 0}
- acc = self.account_client.getUserInfo()['data']
- bal = {'base': 0, 'quote': 0}
- for asset in acc['free'].keys():
- if asset == self.symbol[1]:
- bal['base'] += float(acc['free'][asset])
- elif asset == self.symbol[2]:
- bal['quote'] += float(acc['free'][asset])
- for asset in acc['freeze'].keys():
- if asset == self.symbol[1]:
- bal['base'] += float(acc['freeze'][asset])
- elif asset == self.symbol[2]:
- bal['quote'] += float(acc['freeze'][asset])
- print('finish')
- return {'book': best, 'balance': bal}
- def cancel(self, order_id):
- self.order_client.getCancel_orderV2(symbol=self.symbol[0], orderId=order_id)
- def proper_round(self, x, precision):
- return f"%.{precision}f" % round(x, precision)
- def new_limit_maker(self, price, quantity, is_bid):
- type = 'buy_maker' if is_bid else 'sell_maker'
- try:
- self.order_client.getCreate_order(symbol=self.symbol[0], type=type, price=self.proper_round(price, self.symbol[3]), amount=self.proper_round(quantity, self.symbol[4]))
- except Exception as e:
- print(e)
- def new_limit(self, price, quantity, is_bid):
- type = 'buy' if is_bid else 'sell'
- self.order_client.getCreate_order(symbol=self.symbol[0], type=type, price=self.proper_round(price, self.symbol[3]), amount=self.proper_round(quantity, self.symbol[4]))
- def get_orders(self):
- try:
- orders = self.order_client.getOrders_info_no_deal(symbol=self.symbol[0], page_length=200, current_page=1)['data']['orders']
- except Exception as e:
- print(e)
- orders = {}
- #print(orders)
- res = {'ask': [], 'bid': []}
- for order in orders:
- if order['type'] == 'sell_maker':
- res['ask'].append({'id': order['orderId'], 'quantity': float(order['origQty']), 'price': float(order['price'])})
- elif order['type'] == 'buy_maker':
- res['bid'].append({'id': order['orderId'], 'quantity': float(order['origQty']), 'price': float(order['price'])})
- return res
- def get_depth(self):
- depth = self.market_client.getIncrDepth(symbol=self.symbol[0])
- print(depth)
- return dict(zip(depth.keys(), map(lambda z: map(lambda v: list(map(float, v)), z), depth.values())))
- def get_new_public_trades(self):
- trades = []
- new_trades = set()
- try:
- response = self.market_client.getTradesV2(symbol=self.symbol[0], size=50)['data']
- except Exception as e:
- print(e)
- return []
- for order in response:
- new_trades.add(order['id'])
- if order['id'] in self.last_public_trades:
- continue
- trade = {'price': float(order['price']), 'quantity': float(order['qty']), 'side': 'sell' if order['isBuyerMaker'] else 'buy', 'timestamp': int(order['time'])}
- trades.append(trade)
- if len(self.last_public_trades) == 0:
- trades = []
- if len(new_trades) > 0:
- self.last_public_trades = new_trades
- return trades
- def get_new_private_trades(self):
- trades = []
- new_trades = set()
- try:
- response = self.order_client.getTransaction_history(symbol=self.symbol[0], startTime=str(datetime.fromtimestamp(int(time.time())-60+8*60*60)))['data']
- except Exception as e:
- print(e)
- return []
- for order in response:
- new_trades.add(order['id'])
- if order['id'] in self.last_private_trades:
- continue
- trade = {'price': float(order['price']), 'quantity': float(order['qty']), 'side': 'sell' if (order['isBuyer'] == order['isMaker']) else 'buy', 'timestamp': int(order['time'])}
- trades.append(trade)
- if len(self.last_private_trades) == 0:
- trades = []
- if len(new_trades) > 0:
- self.last_private_trades = new_trades
- return trades
- def cancel_open_orders(self):
- self.order_client.getCancel_order_by_symbol(symbol=self.symbol[0])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement