def test_pos(): bid = Position(pos=Decimal('1'), price=100, side=Side.ASK) + Position(pos=Decimal('0.1'), price=101, side=Side.BID) assert bid.position() == Decimal('-0.9') assert bid.side() == Side.ASK bid = Position(pos=Decimal('1'), price=100, side=Side.BID) + Position(pos=Decimal('0.1'), price=101, side=Side.ASK) assert bid.position() == Decimal('0.9') assert bid.side() == Side.BID print(bid)
def remove_exit_price_strategy(book: Book, pos: Position, config: MarketmakerConfig, fee=Decimal(0.3)): # pos, last_price = remove_price(book.quote(pos.side()), pos) # if pos.balance > 0: # remove_pos = pos.oppoiste_with_price(last_price) # print("fee " + str(pos * Decimal('0.3'))) remove_pos = pos.oppoiste_with_price(book.quote(pos.side()).price) remove_pos_wfee = Position(pos=remove_pos.position(), balance=remove_pos.balance + (remove_pos.balance / 100) * fee) add_pos = pos.oppoiste_with_price( book.quote(Side.opposite(pos.side())).price) fee_ = remove_pos * Decimal(fee / 100) fin_pos = pos + remove_pos if (pos + remove_pos_wfee).balance > 0: return remove_pos elif (pos + add_pos).balance > 0: return add_pos else: return pos.opposite_with_margin(config.min_profit)
def test_eneter_hedge(pos: Position): print("pos" + str(pos)) book = gen_book() pnl = PNL(Decimal('0.16')) pnl.quote_changed(book.quote(Side.BID)) pnl.quote_changed(book.quote(Side.ASK)) pnl.pos = pos config: AppConfig = load_config('config.json') hedge = enter_hedge(pnl, book, pos.side(), config.algo, config.venue) print("hedge " + str(hedge[0]) + " method " + hedge[1]) print("after hedge " + str(hedge[0] + pos)) #print("after hedge " + str(hedge[0]+pos)) print('-----------') pass
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)
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() ])
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())
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