Example #1
0
    def execution(self, tx: Exec):
        exec_pos = Position(pos=tx.delta, price=tx.price, side=tx.side)
        if tx.delta > 0:
            self.pos += exec_pos + exec_pos.fee_pos(tx.fee)

        if self.pos.position() == 0:
            self.closed_pnl += self.pos.balance
            self.pos = Position(0, 0)
            self.zero_position_time = time.time()
Example #2
0
    def take_pnl(self):
        if self.pos.position() == 0:
            return Decimal('0')
        exit_side = Side.opposite_side(self.position())
        take_price = self.nbbo.side(Side.side(self.position()))
        take_order = Position(pos=self.pos.abs_position(),
                              price=take_price,
                              side=exit_side)
        take_pos = self.pos + take_order + take_order.fee_pos(self.fee)

        return take_pos.balance
Example #3
0
def test_fee():
    def test_case(pos, order):
        fee_pos = order.fee_pos('0.18')
        print("pos " + str(pos))
        print("order " + str(order))
        print("fee " + str(fee_pos))
        wo_fee = pos + order
        w_fee = pos + order + fee_pos
        print("wo_fee " + str(wo_fee))
        print("w_fee " + str(w_fee))
        assert wo_fee.balance >= w_fee.balance, "pos " + str(
            pos) + " order" + str(order)
        assert (abs(fee_pos.balance) < abs(order.balance)) or (order.balance ==0 and fee_pos.balance==0)\
            , "fee " + fee_pos.balance+" order cost " + order.balance

    pos = Position(pos=Decimal('1'), balance=Decimal('1199'), side=Side.BID)
    order = Position(pos=Decimal('1'), balance=Decimal('1200'), side=Side.ASK)
    test_case(order, pos)
    print('---------')
    test_case(pos, order)
    print('---------')
    pos = Position(pos=Decimal('0.01'), balance=Decimal('1199'), side=Side.BID)
    order = Position(pos=Decimal('0.01'),
                     balance=Decimal('1200'),
                     side=Side.ASK)
    test_case(pos, order)
    print('---------')
    pos = Position(pos=Decimal('0.01'), balance=Decimal('12'), side=Side.BID)
    order = Position(pos=Decimal('0.01'), price=Decimal('1201'), side=Side.ASK)
    test_case(order, pos)
    print('---------')
    test_case(pos, order)
    print('---------')
    pos = Position(pos=Decimal('0'), balance=Decimal('0'), side=Side.BID)
    order = Position(pos=Decimal('0.01'), price=Decimal('1201'), side=Side.ASK)
    print("order fee " + str(order.fee_pos(Decimal('0.18')).balance))
    test_case(pos, order)
    print('---------')
    test_case(order, pos)
    assert pos + order == pos + order + order.fee_pos(0)