def check_safe_stop_loss(low, open): diff = open - low trade_risk = (diff / low) * 100 is_safe = MAX_STOP_LOSS_RISK > trade_risk print( yellow.bold('\n\t⚠ Position risk is: {}%'.format(round(trade_risk, 2)))) if not is_safe: print( red.bold( '\n\tTrade is too risky ({}), aborting!.'.format(trade_risk))) exit(1) return is_safe
from simple_chalk import chalk, yellow, red TEXTS = { 'ducky': [ yellow.bold(' _.._'), yellow.bold(' / ') + chalk.bold('a a') + yellow.bold('\\') + red.bold('__,'), yellow.bold(' \\ ') + red.bold('-.___/'), yellow.bold(' \\ \\'), yellow.bold('(\\__,-----,_) \\'), yellow.bold('( (_ )'), yellow.bold(' \\_ (__ /'), yellow.bold(' \\___________/'), '' ], } def ducky(): return TEXTS.get('ducky')
def check_best_trade(interval=Intervals.DAY): request_client = RequestClient(api_key=API_KEY, secret_key=SECRET_KEY) # Request info of all symbols to retrieve precision response = requests.get(BINANCE_FUTURES_BASE_URL + BINANCE_FUTURES_EXCHANGE_INFO_ENDPOINT) exchange_info = response.json() price_precision = 0 best_bullish_wicks = [] best_bearish_wicks = [] print('Number of pairs to check approx: ', len(exchange_info['symbols'])) for item in exchange_info['symbols']: if (not item['contractType'] == 'PERPETUAL'): continue print('\t * Checking: {}'.format(item['symbol'])) candles = get_last_binance_candles(item['symbol'], interval, Markets.FUTURES) if (not len(candles) > 1): continue current_candle = candles[1] cc_open = float(current_candle[1]) cc_high = float(current_candle[2]) cc_low = float(current_candle[3]) cc_close = float(current_candle[4]) # Candle is green if (cc_open < cc_close): diff = cc_high - cc_close cc_wick = round((diff / cc_close) * 100, 2) best_bullish_wicks.append({ 'wick': cc_wick, 'symbol': item['symbol'] }) else: # Candle is red diff = cc_close - cc_low cc_wick = -round((diff / cc_low) * 100, 2) best_bearish_wicks.append({ 'wick': cc_wick, 'symbol': item['symbol'] }) bullish_result = sorted(best_bullish_wicks, key=lambda k: k['wick'], reverse=True) bearish_result = sorted(best_bearish_wicks, key=lambda k: k['wick'], reverse=False) print(white.bold('Best bullish wicks to trade found are:')) for item in bullish_result[0:10]: print( green.bold('\t{} -> {} % wick.'.format(item['symbol'], item['wick']))) print(white.bold('Best bearish wicks to trade found are:')) for item in bearish_result[0:10]: print( red.bold('\t{} -> {} % wick.'.format(item['symbol'], item['wick'])))
def open_position_binance_futures(pair, take_profit, stop_loss, pair_change, quantity, leverage, side): global STOP_LOSS global TARGET global STOP_LOSS_REACHED global STOP_LOSS_ORDER_ID global TAKE_PROFIT_ORDER_ID request_client = RequestClient(api_key=API_KEY, secret_key=SECRET_KEY) # Cancel previous take profit and stop loss orders try: if (TAKE_PROFIT_ORDER_ID): request_client.cancel_order(symbol=pair, orderId=TAKE_PROFIT_ORDER_ID) except: print( red.bold('Take profit order id {} could not be cancelled'.format( TAKE_PROFIT_ORDER_ID))) try: if (STOP_LOSS_ORDER_ID): request_client.cancel_order(symbol=pair, orderId=STOP_LOSS_ORDER_ID) except: print( red.bold( 'Stop loss profit order id {} could not be cancelled'.format( STOP_LOSS_ORDER_ID))) STOP_LOSS_ORDER_ID = None TAKE_PROFIT_ORDER_ID = None # Change leverage try: request_client.change_initial_leverage(pair, leverage) except: print(red.bold('error changing leverage')) try: margin_type = request_client.change_margin_type( symbol=pair, marginType=FuturesMarginType.ISOLATED) except: print(red.bold('error changing margin type')) # Request info of all symbols to retrieve precision exchange_info = request_client.get_exchange_information() price_precision = 0 for item in exchange_info.symbols: if (item.symbol == pair): precision = item.quantityPrecision price_precision = item.pricePrecision # Create order quantity_rounded = float(quantity * leverage) / float(pair_change) quantity_with_precision = "{:0.0{}f}".format(quantity_rounded, precision) stop_loss = "{:0.0{}f}".format(stop_loss, price_precision) take_profit = "{:0.0{}f}".format(take_profit, price_precision) STOP_LOSS = stop_loss STOP_LOSS_REACHED = False TARGET = take_profit print( white.bold( '\n\tOpening future position {} at market ({}) with quantity: {} {} with take profit on: {} and stop loss: {}' .format(side, pair_change, quantity_with_precision, pair, take_profit, stop_loss))) order_side = OrderSide.BUY if (side == MarketSide.SHORT): order_side = OrderSide.SELL result = request_client.post_order(symbol=pair, side=order_side, quantity=quantity_with_precision, ordertype=OrderType.MARKET, positionSide="BOTH") orderId = result.orderId print(green.bold('\n\t\t✓ Market order created.')) # Set take profit and stop loss orders try: order_side = OrderSide.SELL if (side == MarketSide.SHORT): order_side = OrderSide.BUY result = request_client.post_order(symbol=pair, side=order_side, stopPrice=stop_loss, closePosition=True, ordertype=OrderType.STOP_MARKET, positionSide="BOTH", timeInForce="GTC") STOP_LOSS_ORDER_ID = result.orderId print( green.bold( '\n\t\t✓ Stop market order at: {} created.'.format(stop_loss))) result = request_client.post_order( symbol=pair, side=order_side, stopPrice=take_profit, closePosition=True, ordertype=OrderType.TAKE_PROFIT_MARKET, positionSide="BOTH", timeInForce="GTC") TAKE_PROFIT_ORDER_ID = result.orderId print( green.bold('\n\t\t✓ Take profit market at: {} creted.'.format( take_profit))) except: # Cancel order if something did not work as expected request_client.cancel_order(symbol=pair, orderId=orderId) print( red.bold( '\n\t\t x Something did not work as expected. Cancelling order' ))
from gegen import run from simple_chalk import chalk, green, red while True: text = input(green.bold('gegen $ ')) result, error = run('<stdin>', text) if error: print(red.bold(error.as_string())) else: print(result)