Пример #1
0
def test_run(main=False):
    position = Position()
    comminfo = FakeCommInfo()
    order = bt.BuyOrder(data=FakeData(),
                        size=100,
                        price=1.0,
                        exectype=bt.Order.Market,
                        simulated=True)
    order.addcomminfo(comminfo)

    ### Test that partially updating order will maintain correct iterpending sequence
    ### (Orders are cloned for each notification. The pending bits should be reported
    ###  related to the previous notification (clone))

    # Add two bits and validate we have two pending bits
    _execute(position, order, 10, 1.0, True)
    _execute(position, order, 20, 1.1, True)

    clone = order.clone()
    pending = clone.executed.getpending()
    assert len(pending) == 2
    assert pending[0].size == 10
    assert pending[0].price == 1.0
    assert pending[1].size == 20
    assert pending[1].price == 1.1

    # Add additional two bits and validate we still have two pending bits after clone
    _execute(position, order, 30, 1.2, True)
    _execute(position, order, 40, 1.3, False)

    clone = order.clone()
    pending = clone.executed.getpending()
    assert len(pending) == 2
    assert pending[0].size == 30
    assert pending[0].price == 1.2
    assert pending[1].size == 40
    assert pending[1].price == 1.3
def test_run(main=False):
    tr = trade.Trade(data=FakeData())

    order = bt.BuyOrder(data=FakeData(),
                        size=0,
                        price=1.0,
                        exectype=bt.Order.Market,
                        simulated=True)

    commrate = 0.025
    size = 10
    price = 10.0
    value = size * price
    commission = value * commrate

    tr.update(order=order,
              size=size,
              price=price,
              value=value,
              commission=commission,
              pnl=0.0,
              comminfo=FakeCommInfo())

    assert not tr.isclosed
    assert tr.size == size
    assert tr.price == price
    # assert tr.value == value
    assert tr.commission == commission
    assert not tr.pnl
    assert not tr.pnlcomm

    upsize = -5
    upprice = 12.5
    upvalue = upsize * upprice
    upcomm = abs(value) * commrate

    tr.update(order=order,
              size=upsize,
              price=upprice,
              value=upvalue,
              commission=upcomm,
              pnl=0.0,
              comminfo=FakeCommInfo())

    assert not tr.isclosed
    assert tr.size == size + upsize
    assert tr.price == price  # size is being reduced, price must not change
    # assert tr.value == upvalue
    assert tr.commission == commission + upcomm

    size = tr.size
    price = tr.price
    commission = tr.commission

    upsize = 7
    upprice = 14.5
    upvalue = upsize * upprice
    upcomm = abs(value) * commrate

    tr.update(order=order,
              size=upsize,
              price=upprice,
              value=upvalue,
              commission=upcomm,
              pnl=0.0,
              comminfo=FakeCommInfo())

    assert not tr.isclosed
    assert tr.size == size + upsize
    assert tr.price == ((size * price) + (upsize * upprice)) / (size + upsize)
    # assert tr.value == upvalue
    assert tr.commission == commission + upcomm

    size = tr.size
    price = tr.price
    commission = tr.commission

    upsize = -size
    upprice = 12.5
    upvalue = upsize * upprice
    upcomm = abs(value) * commrate

    tr.update(order=order,
              size=upsize,
              price=upprice,
              value=upvalue,
              commission=upcomm,
              pnl=0.0,
              comminfo=FakeCommInfo())

    assert tr.isclosed
    assert tr.size == size + upsize
    assert tr.price == price  # no change ... we simple closed the operation
    # assert tr.value == upvalue
    assert tr.commission == commission + upcomm