How to use the qstrader.asset.equity.Equity function in qstrader

To help you get started, we’ve selected a few qstrader examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_position.py View on Github external
def test_update_book_cost_for_commission_some_commission():
    """
    Tests that 'update_book_cost_for_commission' calculates
    book cost correctly for the position when a positive
    commission is supplied.
    """
    asset = Equity('Apple, Inc.', 'AAPL')
    position = Position(
        asset, quantity=100, book_cost_pu=50.0,
        current_price=50.0
    )
    position.update_book_cost_for_commission(asset, 15.0)

    assert position.book_cost_pu == 50.15
    assert position.book_cost == 5015.0
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_position.py View on Github external
def test_position_short_long_excess_cover():
    """
    Tests that the quantity and book cost are
    correctly calculated for an initial short
    position with an additional long transaction
    in the same asset, where the long position
    is in excess of the short position.
    """
    asset = Equity('Apple, Inc.', 'AAPL')
    position = Position(
        asset, quantity=-100, book_cost_pu=700.0,
        current_price=700.0
    )
    dt = pd.Timestamp('2015-05-06')
    transaction = Transaction(
        asset, quantity=175, dt=dt, price=873.0,
        order_id=123, commission=None
    )
    position.update(transaction)

    assert position.quantity == 75
    assert position.book_cost_pu == 873.0
    assert position.direction == 1.0
    assert position.current_price == 873.0
    assert position.market_value == 65475.0
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_position_handler.py View on Github external
def test_transact_position_quantity_zero():
    """
    Tests the 'transact_position' method for a transaction
    with net zero quantity after the transaction to ensure
    deletion of the position.
    """
    # Create the PositionHandler, Transaction and
    # carry out a transaction
    ph = PositionHandler()
    asset = Equity('Amazon, Inc.', 'AMZN')
    dt = pd.Timestamp('2015-05-06')
    transaction_long = Transaction(
        asset, quantity=100, dt=dt, price=960.0,
        order_id=123, commission=26.83
    )
    transaction_close = Transaction(
        asset, quantity=-100, dt=dt, price=980.0,
        order_id=234, commission=18.53
    )

    # Go long and then close, then check that the
    # positions OrderedDict is empty
    ph.transact_position(transaction_long)
    ph.transact_position(transaction_close)
    od = OrderedDict()
    assert ph.positions == od
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_position.py View on Github external
Tests that the quantity and book cost are
    correctly calculated for three long transactions,
    followed by a partial closing position, followed
    by a new long position, all in the same asset.

    Buy 100 qty at £1.00 -> £100
    Buy 100 qty at £2.00 -> £200
    Buy 200 qty at £3.00 -> £600
    Total qty after 3 longs is 400, with book cost £900 (£2.25 p/u)

    Sell 100 qty -> Book cost now £675 (25% holdings reduced),
    still at £2.25 p/u
    Buy 100 at £4.00 -> 400
    Final qty is 400, but book cost is now £1,075 (£2.6875 p/u).
    """
    asset = Equity('Apple, Inc.', 'AAPL')

    # Initial long
    position = Position(
        asset,
        quantity=100,
        book_cost_pu=1.0,
        current_price=1.0
    )

    # Second long
    dt = pd.Timestamp('2015-05-06')
    transaction = Transaction(
        asset,
        quantity=100,
        dt=dt,
        price=2.0,
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_position_handler.py View on Github external
def test_total_values_for_two_separate_transactions():
    """
    Tests 'total_book_cost', 'total_market_value',
    'total_gain' and 'total_perc_gain' for single
    transactions in two separate assets.
    """
    ph = PositionHandler()

    # Asset 1
    asset1 = Equity('Amazon, Inc.', 'AMZN')
    dt1 = pd.Timestamp('2015-05-06')
    trans_pos_1 = Transaction(
        asset1.symbol, quantity=75, dt=dt1, price=483.45,
        order_id=1, commission=15.97
    )
    ph.transact_position(trans_pos_1)

    # Asset 2
    asset2 = Equity('Microsoft, Inc.', 'MSFT')
    dt2 = pd.Timestamp('2015-05-07')
    trans_pos_2 = Transaction(
        asset2.symbol, quantity=250, dt=dt2, price=142.58,
        order_id=2, commission=8.35
    )
    ph.transact_position(trans_pos_2)
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_position.py View on Github external
def test_position_short_goes_to_zero():
    """
    Tests that the quantity and book cost are
    correctly calculated for an initial short
    position, where the share value goes to zero.
    This should be a percentage gain of 100%.
    """
    asset = Equity('Apple, Inc.', 'AAPL')
    position = Position(
        asset, quantity=-100, book_cost_pu=50.0,
        current_price=50.0
    )
    dt = pd.Timestamp('2015-05-06')
    position.current_price = 0.0
    position.current_trade_date = dt

    assert position.quantity == -100
    assert position.book_cost_pu == 50.0
    assert position.direction == -1.0
    assert position.current_price == 0.0
    assert position.market_value == 0.0
    assert position.unrealised_gain == 5000.0
    assert position.unrealised_percentage_gain == 100.0
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_position_handler.py View on Github external
def test_check_set_position_new_asset():
    """
    Checks the _check_set_position method when
    a new asset is added to the PositionHandler
    and when it is checked subsequently.
    """
    # Create PositionHandler, Asset and OrderedDict
    # positions list
    ph = PositionHandler()
    asset = Equity('Amazon, Inc.', 'AMZN')
    od = OrderedDict()
    assert ph.positions == od

    # Check that the position is set for new asset
    pos = ph._check_set_position(asset)
    assert pos.asset == asset

    # Check that the OrderedDict is correctly set
    # for new asset
    od[asset] = pos
    assert ph.positions == od

    # Check that it works for a current asset
    pos = ph._check_set_position(asset)
    assert pos.asset == asset
    assert ph.positions == od
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_position.py View on Github external
def test_update_book_cost_for_commission_zero_position():
    """
    Tests that 'update_book_cost_for_commission' returns None
    when some positive commission is provided, given that the
    Position itself has zero quantity.
    """
    asset = Equity('Apple, Inc.', 'AAPL')
    position = Position(
        asset, quantity=0, book_cost_pu=0.0, current_price=0.0
    )
    assert position.update_book_cost_for_commission(asset, 15.0) is None
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_position.py View on Github external
def test_position_short_close():
    """
    Tests that the quantity and book cost are
    correctly calculated for an initial short
    position with an additional long transaction
    in the same asset, where the long closes
    the position.
    """
    asset = Equity('Apple, Inc.', 'AAPL')
    position = Position(
        asset, quantity=-100, book_cost_pu=950.0,
        current_price=950.0
    )
    dt = pd.Timestamp('2015-05-06')
    transaction = Transaction(
        asset, quantity=100, dt=dt, price=960.0,
        order_id=123, commission=None
    )
    position.update(transaction)
    assert position.quantity == 0
    assert position.book_cost_pu == 0.0
    assert position.direction == 1.0
    assert position.current_price == 960.0
    assert position.market_value == 0.0
    assert position.unrealised_gain == 0.0
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_position_handler.py View on Github external
def test_update_commission():
    """
    Tests the 'update_commission' method to ensure
    commission is correctly set on the Position entities.
    """
    ph = PositionHandler()

    # Asset 1
    asset1 = Equity('Amazon, Inc.', 'AMZN')
    dt1 = pd.Timestamp('2015-05-06')
    trans_pos_1 = Transaction(
        asset1.symbol, quantity=75, dt=dt1, price=483.45,
        order_id=1, commission=0.0
    )
    ph.transact_position(trans_pos_1)
    ph.update_commission(asset1.symbol, 15.97)

    # Asset 2
    asset2 = Equity('Microsoft, Inc.', 'MSFT')
    dt2 = pd.Timestamp('2015-05-07')
    trans_pos_2 = Transaction(
        asset2.symbol, quantity=250, dt=dt2, price=142.58,
        order_id=2, commission=0.0
    )
    ph.transact_position(trans_pos_2)