Пример #1
0
def enter_hedge(pnl: PNL, book: Book, side, cfg: HedgeConfig, vc: VenueConfig):
    quote = book.quote(side)
    side = quote.side
    pos = pnl.pos
    order_size = cfg.order_size.side(side)
    theo = (book.quote(Side.BID).price + book.quote(Side.ASK).price) / 2
    if pos.abs_position() < vc.min_order_size:
        # depth or ema
        order_size = adjusted_size(order_size, side, pos.abs_position())
        method, price = depth_ema_price(cfg, order_size, pnl, quote, side, vc)
        return Position(pos=order_size, side=side, price=price), method
    elif Side.opposite(
            pos.side()) == side and pos.abs_position() >= vc.min_order_size:
        # exit order
        # depth or zero
        method, price = depth_ema_price(cfg, order_size, pnl, quote, side, vc)

        add_pos = pos.oppoiste_with_price(price)
        min_margin = pos.opposite_with_margin(vc.tick_size)
        min_margin = bound_pos_to_lower_quote(quote, min_margin, vc.tick_size)

        if (pos + add_pos).balance > 0:
            return add_pos, "QUOTE"
        else:
            return min_margin, "MIN PROFIT"
    elif pos.side(
    ) == side and cfg.max_pos - pos.abs_position() >= vc.min_order_size:
        #depth
        #hedge
        #order_size = adjusted_size(order_size, side, pos.abs_position())
        method, price_depth = depth_ema_price(cfg, order_size, pnl, quote,
                                              side, vc)

        order_size = min(order_size, cfg.max_pos - pos.abs_position())
        depth_pos = Position(pos=order_size, side=side, price=price_depth)
        sign = Side.sign(pos.side())

        target_price = theo - Side.sign(pos.side()) * theo * cfg.hedge_perc
        hedge_pos = hedge_positon_size(pos, Decimal(target_price), order_size)
        hedge_pos = bound_pos_to_lower_quote(quote, hedge_pos, vc.tick_size)
        #return hedge_pos, "HEDGE HEDGE"
        if (side == Side.BID and hedge_pos.price() < depth_pos.price()) \
                or (side == Side.ASK and hedge_pos.price() > depth_pos.price()):
            return hedge_pos, "HEDGE HEDGE"
        else:
            return depth_pos, "HEDGE DEPTH"

    else:
        #zero
        return Position(side=side, pos=0, balance=0), "CANCEL"
Пример #2
0
 def hedge_workflow(prior_pos: Position, target_price, enter_price):
     # define current state
     pandl.execution(prior_pos.side(), prior_pos.abs_position(),
                     prior_pos.price())
     exit_order = remove_exit_price_strategy(book, prior_pos, None)
     #place exit order
     broker.request(0, exit_order.side(), exit_order.price(),
                    exit_order.abs_position())
     # calc hedge order
     hedge_pos = hedge_position(prior_pos, target_price, enter_price)
     #place hedge order
     broker.request(1, hedge_pos.side(), hedge_pos.price(),
                    hedge_pos.abs_position())
     # exec hedge order
     pandl.execution(hedge_pos.side(), hedge_pos.abs_position(),
                     hedge_pos.price())
     # look at new exit
     exit_order = remove_exit_price_strategy(
         book, Position(pandl.pos(), pandl.balance()), None)
Пример #3
0
 def volume_behind_order(min_pos: Position):
     sign = Side.sign(min_pos.side())
     return sum([
         level.volume() for level in book.quote(min_pos.side())
         if sign * level.price > sign * min_pos.price()
     ])
Пример #4
0
def bound_pos_to_lower_quote(quote: Level, pos: Position, tick_size):
    lower_quote_price = bound_price_to_lower_quote(quote, pos.price(),
                                                   tick_size)
    return Position(side=pos.side(),
                    price=lower_quote_price,
                    pos=pos.abs_position())
Пример #5
0
def calc_target_price(theo: Decimal, pos: Position, hedge_perc: Decimal):
    #exit_side = Side.opposite(pos.side())
    sign = Side.sign(pos.side())
    theo_target = theo - Side.sign(pos.side()) * theo * hedge_perc
    pos_target = pos.price() - sign * theo * hedge_perc
    return theo_target