How to use the qstrader.broker.transaction.transaction.Transaction 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_handler.py View on Github external
def test_transact_position_current_position():
    """
    Tests the 'transact_position' method for a transaction
    with a current asset and checks that all objects are
    set correctly.
    """
    # 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_long_again = Transaction(
        asset, quantity=200, dt=dt, price=990.0,
        order_id=234, commission=18.53
    )
    ph.transact_position(transaction_long)
    ph.transact_position(transaction_long_again)

    # Check that the position object is set correctly
    pos = ph.positions[asset]
    assert pos.quantity == 300
    assert pos.direction == 1.0
    assert pos.book_cost_pu == 980.1512000000001
    assert pos.book_cost == 294045.36000000004
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_position_handler.py View on Github external
def test_transact_position_new_position():
    """
    Tests the 'transact_position' method for a transaction
    with a brand new asset and checks that all objects are
    set correctly.
    """
    # Create the PositionHandler, Transaction and
    # carry out a transaction
    ph = PositionHandler()
    asset = Equity('Amazon, Inc.', 'AMZN')
    dt = pd.Timestamp('2015-05-06')
    transaction = Transaction(
        asset, quantity=100, dt=dt, price=960.0,
        order_id=123, commission=26.83
    )
    ph.transact_position(transaction)

    # Check that the position object is set correctly
    pos = ph.positions[asset]
    assert pos.quantity == 100
    assert pos.direction == 1.0
    assert pos.book_cost_pu == 960.2683000000001
    assert pos.book_cost == 96026.83
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_position.py View on Github external
correctly calculated for an initial long
    position with an additional short transaction
    in the same asset, where the short does not
    completely eliminate the position and the
    result is a loss.
    """
    asset = Equity('Apple, Inc.', 'AAPL')
    position = Position(
        asset,
        quantity=100,
        book_cost_pu=960.0,
        current_price=950.0
    )

    dt = pd.Timestamp('2015-05-06')
    transaction = Transaction(
        asset,
        quantity=-50,
        dt=dt,
        price=950.0,
        order_id=123,
        commission=None
    )
    position.update(transaction)

    assert position.quantity == 50
    assert position.book_cost_pu == 960.0
    assert position.direction == 1.0
    assert position.current_price == 950.0
    assert position.market_value == 47500.0
    assert position.unrealised_gain == -500.0
    assert position.unrealised_percentage_gain == -1.0416666666666665
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_position_handler.py View on Github external
def test_transact_position_current_position():
    """
    Tests the 'transact_position' method for a transaction
    with a current asset and checks that all objects are
    set correctly.
    """
    # 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_long_again = Transaction(
        asset, quantity=200, dt=dt, price=990.0,
        order_id=234, commission=18.53
    )
    ph.transact_position(transaction_long)
    ph.transact_position(transaction_long_again)

    # Check that the position object is set correctly
    pos = ph.positions[asset]
    assert pos.quantity == 300
    assert pos.direction == 1.0
    assert pos.book_cost_pu == 980.1512000000001
    assert pos.book_cost == 294045.36000000004
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_portfolio.py View on Github external
def test_portfolio_to_dict_for_two_holdings():
    """
    Test portfolio_to_dict for two holdings.
    """
    start_dt = pd.Timestamp('2017-10-05 08:00:00', tz=pytz.UTC)
    asset1_dt = pd.Timestamp('2017-10-06 08:00:00', tz=pytz.UTC)
    asset2_dt = pd.Timestamp('2017-10-07 08:00:00', tz=pytz.UTC)
    update_dt = pd.Timestamp('2017-10-08 08:00:00', tz=pytz.UTC)
    asset1 = Equity("AAA Inc.", "EQ:AAA", tax_exempt=False)
    asset2 = Equity("BBB Inc.", "EQ:BBB", tax_exempt=False)

    port = Portfolio(start_dt, portfolio_id='1234')
    port.subscribe_funds(start_dt, 100000.0)
    tn_asset1 = Transaction(
        asset=asset1.symbol, quantity=100, dt=asset1_dt,
        price=567.0, order_id=1, commission=15.78
    )
    port.transact_asset(tn_asset1)

    tn_asset2 = Transaction(
        asset=asset2.symbol, quantity=100, dt=asset2_dt,
        price=123.0, order_id=2, commission=7.64
    )
    port.transact_asset(tn_asset2)

    port.update_market_value_of_asset(asset2.symbol, 134.0, update_dt)
    test_holdings = {
        asset1.symbol: {
            "quantity": 100,
            "book_cost": 56715.78,
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
    assert position.unrealised_percentage_gain == 0.0
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 an initial long
    position with an additional long transaction
    in the same asset.
    """
    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 == 200
    assert position.book_cost_pu == 955.0
    assert position.direction == 1.0
    assert position.current_price == 960.0
    assert position.market_value == 192000.0
    assert position.unrealised_gain == 1000.0
    assert position.unrealised_percentage_gain == 0.5235602094240838
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_portfolio.py View on Github external
Test transact_asset raises for incorrect time
    Test transact_asset raises for transaction total
    cost exceeding total cash
    Test correct total_cash and total_securities_value
    for correct transaction (commission etc), correct
    portfolio event and correct time update
    """
    start_dt = pd.Timestamp('2017-10-05 08:00:00', tz=pytz.UTC)
    earlier_dt = pd.Timestamp('2017-10-04 08:00:00', tz=pytz.UTC)
    later_dt = pd.Timestamp('2017-10-06 08:00:00', tz=pytz.UTC)
    even_later_dt = pd.Timestamp('2017-10-07 08:00:00', tz=pytz.UTC)
    port = Portfolio(start_dt)
    asset = Equity("Acme Inc.", "EQ:AAA", tax_exempt=False)

    # Test transact_asset raises for incorrect time
    tn_early = Transaction(
        asset=asset.symbol,
        quantity=100,
        dt=earlier_dt,
        price=567.0,
        order_id=1,
        commission=0.0
    )
    with pytest.raises(ValueError):
        port.transact_asset(tn_early)

    # Test transact_asset raises for transaction total
    # cost exceeding total cash
    port.subscribe_funds(later_dt, 1000.0)

    assert port.total_cash == 1000.0
    assert port.total_non_cash_equity == 0.0
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_position_handler.py View on Github external
def test_update_position_for_non_none_values():
    """
    Tests the 'update_position' method for non-None
    values when updating a Position entity.
    """
    ph = PositionHandler()

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

    # Update values manually
    quantity = 100
    current_price = 504.32
    current_dt = pd.Timestamp('2015-05-07')
    book_cost_pu = 23.65
    ph.update_position(
        asset1,
        quantity=quantity,
        current_price=current_price,
        current_dt=current_dt,
        book_cost_pu=book_cost_pu
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_position.py View on Github external
def test_update_for_incorrect_asset():
    """
    Tests that the 'update' method, when provided
    with a transaction with an asset that does not
    match the position's asset, raises an Exception.
    """
    asset1 = Equity('Apple, Inc.', 'AAPL')
    asset2 = Equity('Amazon, Inc.', 'AMZN')

    position = Position(
        asset1, quantity=100, book_cost_pu=950.0,
        current_price=950.0
    )
    dt = pd.Timestamp('2015-05-06')
    transaction = Transaction(
        asset2, quantity=50, dt=dt, price=960.0,
        order_id=123, commission=None
    )

    with pytest.raises(Exception):
        position.update(transaction)