def test_get_mid_price_orderbooks(self): binance = Binance() products = ['BTC_USDT', 'ETH_USDT'] orderbooks = binance.get_mid_price_orderbooks(products) self.assertEqual(len(orderbooks), len(products)) self.assertIn(orderbooks[0].product, products) self.assertIn(orderbooks[1].product, products)
def test_get_orderbooks(self): binance = Binance() products = ['BTC_USDT', 'ETH_USDT'] orderbooks = binance.get_orderbooks(products) self.assertEqual(len(orderbooks), len(products)) self.assertIn(orderbooks[0].product, products) self.assertIn(orderbooks[1].product, products) self.assertLess(orderbooks[0].wall_bid, orderbooks[0].wall_ask) self.assertLess(orderbooks[1].wall_bid, orderbooks[1].wall_ask)
def main(args): with open(args.credentials_path, 'r') as rfile: credentials = json.load(rfile) binance = Binance(api_key=credentials['BINANCE_API_KEY'], secret_key=credentials['BINANCE_API_SECRET']) if args.q == 'get_open_orders': print(*binance.client.get_open_orders(symbol=args.symbol), sep='\n') return if args.q == 'cancel': print(binance.client.cancel_order( symbol=args.symbol, orderId=args.order_id)) return assert args.q == 'create' exchange_info = binance.client.get_exchange_info() for _symbol in exchange_info['symbols']: if _symbol['symbol'] == args.symbol: filt = _symbol['filters'][0] if args.action == 'SELL': price = filt['maxPrice'] else: price = filt['minPrice'] order_response = binance.client.create_order( symbol=args.symbol, side=args.action, quantity=Decimal(args.quantity), price=price, type='LIMIT_MAKER') print(f'create order response {order_response}') orderId = order_response['orderId'] order_response = binance.client.get_order( symbol=args.symbol, orderId=orderId) print(f'get order response {order_response}')
def test_parse_market_order_response(self): resp = { "symbol": "BTCUSDT", "orderId": 28, "clientOrderId": "6gCrw2kRUAF9CvJDGP16IP", "transactTime": 1507725176595, "price": "0.00000000", "origQty": "10.00000000", "executedQty": "10.00000000", "status": "FILLED", "timeInForce": "GTC", "type": "MARKET", "side": "SELL", "fills": [ { "price": "4000.00000000", "qty": "1.00000000", "commission": "4.00000000", "commissionAsset": "BNB" }, { "price": "3999.00000000", "qty": "5.00000000", "commission": "19.99500000", "commissionAsset": "BNB" }, { "price": "3998.00000000", "qty": "2.00000000", "commission": "7.99600000", "commissionAsset": "USDT" }, { "price": "3997.00000000", "qty": "1.00000000", "commission": "3.99700000", "commissionAsset": "USDT" }, { "price": "3995.00000000", "qty": "1.00000000", "commission": "3.99500000", "commissionAsset": "USDT" } ] } ret = Binance.parse_market_order_response(None, resp) correct_parsed_response = { "orderId": 28, "clientOrderId": "6gCrw2kRUAF9CvJDGP16IP", "executed_quantity": Decimal("10.00000000"), "mean_price": Decimal("3998.30000000"), "commission_USDT": Decimal("15.98800000"), "commission_BNB": Decimal("23.99500000"), "symbol": "BTCUSDT", "side": "SELL" } self.assertDictEqual(correct_parsed_response, ret)
def __init__(self): self.exchange = Binance() self.chart = chart() self.param = [{ "col_name": "4_ema", "color": 'green', "name": "4_ema" }, { "col_name": "9_ema", "color": 'yellow', "name": "9_ema" }, { "col_name": "18_ema", "color": "red", "name": "18_ema" }] self.buy_signals = [] self.sell_signals = []
def test_parse_params(self): correct_params = { 'symbol': 'BTCUSDT', 'orderId': 10, 'origClientOrderId': 'myOrder0' } product_symbol = {'product': 'BTC_USDT', 'symbol': 'BTCUSDT'} order_id = ['order_id', 'orderId'] client_order_id = ['orig_client_order_id', 'origClientOrderId'] params = [{ 'other_params': [1, 2, None, [4], {5: 6}], k: v, _id: 10, c_id: 'myOrder0' } for k, v in product_symbol.items() for _id in order_id for c_id in client_order_id] for param in params: self.assertDictEqual(Binance._parse_params(None, param), correct_params)
def get_exchange(self, type: ExchangeType) -> Exchange: if type == ExchangeType.Binance: return Binance() else: return Others()
class tradeSigns(): """ docstring for tradeSigns """ def __init__(self): self.exchange = Binance() self.chart = chart() self.param = [{ "col_name": "4_ema", "color": 'green', "name": "4_ema" }, { "col_name": "9_ema", "color": 'yellow', "name": "9_ema" }, { "col_name": "18_ema", "color": "red", "name": "18_ema" }] self.buy_signals = [] self.sell_signals = [] def sign(self, symbol: str, timeframe: str): """ """ df = self.exchange.GetSymbolKlines(symbol=symbol, interval=timeframe) #df = self.exchange.technicalA(df) df['3_ema'] = TA.EMA(df, 3) df['25_sma'] = TA.SMA(df, 25) #df['10_ema'] = TA.EMA(df, 10) #df['55_ema'] = TA.EMA(df, 55) #df2= TA.MACD(df = df, period_fast = 30, period_slow = 20, signal = 30) df2 = TA.MACD(df, period_fast=30, period_slow=20, signal=30) df2["HIST"] = df2["MACD"] - df2["SIGNAL"] df["MACD"] = df2["MACD"] df["SIGNAL"] = df2["SIGNAL"] df["HIST"] = df2["HIST"] df['HIST_ESMA'] = df['3_ema'] - df['25_sma'] #ADX df["ADX"] = TA.ADX(df) df["ADX"] = df["ADX"].fillna(0) df["HIST_ESMA"] = df["HIST_ESMA"].fillna(0) self.slopCalculator(df) df.to_csv('./export.csv', sep='\t') print(df) exit() df["sign"] = "" entrypoint = 'off' for i in range(0, len(df['close']) - 1): #strategy_result = Strategies.marginTrade(df = df, step = i) strategy_result = Strategies.tlStrategy(df=df, step=i) if strategy_result['signal'] == 'BUY' and entrypoint == 'off': #print('buy: ',i) self.buy_signals.append([df['time'][i], df['low'][i]]) df.loc[i, 'sign'] = 'buy' entrypoint = 'on' elif strategy_result['signal'] == 'SELL' and entrypoint == 'on': #print('sell: ',i) df.loc[i, 'sign'] = 'sell' self.sell_signals.append([df['time'][i], df['low'][i]]) entrypoint = 'off' #print(self.sell_signals) self.chart.plotData(df, symbol, timeframe, self.param, self.buy_signals, self.sell_signals) def slopCalculator(self, df): i = 3 for i in range(2, len(df['close']) - 1): adxSlope = 0 histSlope = 0 adxStatus = 0 if df['ADX'][i] < 23: adxStatus = 0 elif df['ADX'][i] > 23: adxStatus = 1 if df['ADX'][i] < df['ADX'][i - 1]: adxSlope = -1 elif df['ADX'][i] > df['ADX'][i - 1]: adxSlope = 1 if df['HIST_ESMA'][i] < df['HIST_ESMA'][i - 1]: histSlope = -1 elif df['HIST_ESMA'][i] > df['HIST_ESMA'][i - 1]: histSlope = 1 print( str(adxStatus) + " " + str(adxSlope) + " " + str(histSlope))