Bracket Order Strategy Take Profit and Stop Loss Combination Order Strategy

All successful trading involves 3 key elements:

  • Edge
  • Size
  • Variance

Trading edge relies on either trader’s talent or instinct; size is the amount you wish to invest in a trade; and variance is the market volatility measure. Simplest way (not ideal) of way of taking all three into one bucket is the “bucket order”.

“Bucket order” involves three simultaneous trades: center order, stop-loss (SL), and take-profit (TP), all in one shot. Here is how to realize this on BitMEX XBTUSD via ccxt:

A successful transaction usually has 3 elements:

  • Opportunity discovery
  • Transaction size
  • Market fluctuations

Discovering trading opportunities depends on the trader's technology and personal intuition; the size of the transaction is the funds we can use to trade; the volatility is the market behavior. One of the easiest ways to take care of all three elements at the same time is to use an order strategy with stop-profit and stop-loss.

This ordering strategy contains 3 orders, which are issued at the same time: central order, stop loss order, and take profit order. Some exchanges, including traditional financial and digital asset exchanges, provide this order type. The BitMEX exchange does not provide Bucket Order, but you can combine this order type by yourself. code show as below:

...
def _get_prices(p0,islong):
    sl_r = 0.01 # 1% SL
    tp_r = 0.05 # 5% TP
    
    side = 1 if islong else -1
    sl = p0 * (1 - side * sl_r)
    tp = p0 * (1 + side * tp_r)

    sl = _b(sl)
    tp = _b(tp)

    return p0, sl, tp
...
def _bracket(exchange,limitprice, price, stopprice, amount=1, direction='long'):
    """
    @param exchange (object): ccxt stub of bitmex api
    @param limitprice (float): price of the limit order for TP leg
    @param price (float):   price of the center order (limit order)
    @param stopprice (float):  price of the market order for SL leg
    @param amount (int): quantitiy of contracts in $
    """
        
    sym = 'BTC/USD' # don't change this for bitmex perp contract XBTUSD
    kwargs = dict(symbol=sym,type='Limit',side='buy' if direction == 'long' else 'sell',amount=amount,price=price)
    od = exchange.create_order(**kwargs)

    kwargs = dict(symbol=sym,type='Stop',side='sell' if direction == 'long' else 'buy',amount=amount,params=dict(stopPx=stopprice))
    sl = exchange.create_order(**kwargs)

    kwargs = dict(symbol=sym,type='Limit',side='sell' if direction == 'long' else 'buy',amount=amount,price=limitprice)
    tp = exchange.create_order(**kwargs)

    logging.info(f'order: {od}\nSL: {sl}\nTP: {tp}')
    
    return (od, sl, tp)

def _buy_bracket(exchange,limitprice, price, stopprice,amount=1):
    logging.info('  -- creating buy bracket')

    if stopprice > limitprice:
        raise ValueError('SL price should not be greater than TP.')

    _bracket(exchange,limitprice, price, stopprice,amount,'long')


def _sell_bracket(exchange,limitprice, price, stopprice,amount=1):
    logging.info('  -- creating sell bracket')
    if stopprice < limitprice:
        raise ValueError('SL price should not be less than TP.')

    _bracket(exchange,limitprice, price, stopprice,amount,'short')

Guess you like

Origin blog.csdn.net/weixin_47368014/article/details/108865317