Пример #1
0
def is_close_to_stop_loss(symbol, side, entrance_point, stop_loss, percent):
    if side == 'buy':
        sl_trigger = entrance_point - ((entrance_point - stop_loss) * percent / 100)
        # print(symbol, side, 'price', get_current_price(symbol, 'ask'), 'sl_trigger', sl_trigger)
        return True if get_current_price(symbol, 'ask') <= sl_trigger else False
    else:
        sl_trigger = entrance_point + ((stop_loss - entrance_point) * percent / 100)
        print(symbol, side, 'price', get_current_price(symbol, 'bid'), 'sl_trigger', sl_trigger)
        return True if get_current_price(symbol, 'bid') >= sl_trigger else False
Пример #2
0
def is_close_to_take_profit(symbol, side, entrance_point, take_profit, percent):
    if side == 'buy':
        tp_trigger = entrance_point + ((take_profit - entrance_point) * percent / 100)
        # print(symbol, side, 'price', get_current_price(symbol, 'ask'), 'tp_trigger', tp_trigger)
        return True if get_current_price(symbol, 'ask') >= tp_trigger else False
    else:
        tp_trigger = entrance_point - ((entrance_point - take_profit) * percent / 100)
        print(symbol, side, 'price', get_current_price(symbol, 'bid'), 'sl_trigger', tp_trigger)
        return True if get_current_price(symbol, 'bid') <= tp_trigger else False
Пример #3
0
def convert(asset, quantity, market):
    if quantity == 0:
        return 0
    if asset != market:
        for symbol in SYMBOLS:
            if symbol == asset + market:
                price = get_current_price(symbol, 'bid')
                return round(quantity * price, 8)
            elif symbol == market + asset:
                price = get_current_price(symbol, 'ask')
                return round(quantity / price, 8)
    else:
        return quantity
Пример #4
0
def is_entrance(symbol, side, price):
    if side == 'buy':
        return True if get_current_price(symbol, 'ask') >= price else False
    else:
        return True if get_current_price(symbol, 'bid') <= price else False
Пример #5
0
def is_stop_loss(symbol, side, stop_loss):
    if side == 'buy':
        return True if get_current_price(symbol, 'bid') <= stop_loss else False
    else:
        return True if get_current_price(symbol, 'ask') >= stop_loss else False
Пример #6
0
def is_take_profit(symbol, side, take_profit):
    if side == 'buy':
        return True if get_current_price(symbol, 'bid') >= take_profit else False
    else:
        return True if get_current_price(symbol, 'ask') <= take_profit else False
Пример #7
0
def place_pending_order(symbol, signal_side, precision, price_precision):
    try:
        global signal_number
        signal_id = '{}_{}'.format(symbol.lower(), signal_number)

        if signal_side == 'buy':
            entrance_point = get_current_price(symbol, 'ask')
            stop_loss = round(entrance_point * (1 - (PERCENT_SL / 100)), price_precision)
            take_profit = round(entrance_point * (1 + (PERCENT_TP / 100)), price_precision)
        else:
            entrance_point = get_current_price(symbol, 'bid')
            stop_loss = round(entrance_point * (1 + (PERCENT_SL / 100)), price_precision)
            take_profit = round(entrance_point * (1 - (PERCENT_TP / 100)), price_precision)

        new_signal_report(symbol, signal_side.upper(), entrance_point, stop_loss, take_profit)

        quantity = get_quantity(symbol, signal_side)
        if quantity is None:
            return
        open_order = place_market_order(symbol, signal_side, quantity, 'Entry')
        signal_number += 1
        save_trade('{}_SIGNAL'.format(signal_side.upper()), signal_id, open_order, 'Entry')
        cumulative_quote_qty = float(open_order['cummulativeQuoteQty'])
        order_side = get_opposite_side(signal_side)

        current_sl_order = 0
        current_tp_order = 0

        while True:

            # stop loss monitoring
            if is_close_to_stop_loss(symbol, signal_side, entrance_point, stop_loss, 80):
                if current_sl_order == 0:

                    # cancel existed TP order
                    if current_tp_order != 0:
                        cancel_limit_order(symbol, order_side, current_tp_order['orderId'], 'TakeProfit')

                    # place SL limit order
                    if execute_stop_loss(symbol, signal_side):
                        qty = get_qty(symbol, signal_side, quantity, cumulative_quote_qty, stop_loss, precision)
                        to_general_log(symbol, 'get_qty: qty {}'.format(qty))

                        current_sl_order = place_limit_order(symbol, order_side, qty, stop_loss, 'StopLoss')
                    # else:
                    #     to_general_log(symbol, 'Skip stop loss for buy signal')

                elif is_order_filled('Binance', current_sl_order['orderId']):
                    remove_orders_info('Binance', current_sl_order['orderId'])
                    to_general_log(symbol, '{} Limit order filled (StopLoss)'.format(order_side))
                    save_trade('{}_SIGNAL'.format(signal_side.upper()), signal_id, current_sl_order, 'SL')
                    break

                    # take profit monitoring
            if is_close_to_take_profit(symbol, signal_side, entrance_point, take_profit, 80):
                if current_tp_order == 0:

                    # cancel existed SL order
                    if current_sl_order != 0:
                        cancel_limit_order(symbol, order_side, current_sl_order['orderId'], 'StopLoss')

                    # place TP limit order
                    qty = get_qty(symbol, signal_side, quantity, cumulative_quote_qty, take_profit, precision)
                    to_general_log(symbol, 'get_qty: qty {}'.format(qty))

                    current_tp_order = place_limit_order(symbol, order_side, qty, take_profit, 'TakeProfit')

                elif is_order_filled('Binance', current_tp_order['orderId']):
                    remove_orders_info('Binance', current_tp_order['orderId'])
                    to_general_log(symbol, '{} Limit order filled (TakeProfit)'.format(order_side))
                    save_trade('{}_SIGNAL'.format(signal_side.upper()), signal_id, current_tp_order, 'TP')
                    break
            time.sleep(0.3)
    except:
        to_general_log(symbol, traceback.format_exc())