How to use the qstrader.broker.position.Position 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 / broker / test_position.py View on Github external
def test_position_short_twice(self):
        """
        Tests that the quantity and book cost are
        correctly calculated for an initial short
        position with an additional short transaction
        in the same asset.
        """
        asset = EquityMock(1, exchange='NYSE')
        position = Position(
            asset, quantity=-100, book_cost_ps=950.0,
            current_trade_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)
        self.assertEqual(position.quantity, -200)
        self.assertEqual(position.book_cost_ps, 955.0)
        self.assertEqual(position.direction, -1.0)
        self.assertEqual(position.current_trade_price, 960.0)
        self.assertEqual(position.market_value, -192000.0)
        self.assertEqual(position.unr_gain, -1000.0)
        self.assertEqual(position.unr_perc_gain, -0.5235602094240838)
github mhallsmoore / qstrader / tests / broker / test_position.py View on Github external
def test_position_short_goes_to_zero(self):
        """
        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 = EquityMock(1, exchange='NYSE')
        position = Position(
            asset, quantity=-100, book_cost_ps=50.0,
            current_trade_price=50.0
        )
        dt = pd.Timestamp('2015-05-06')
        position.current_trade_price = 0.0
        position.current_trade_date = dt
        self.assertEqual(position.quantity, -100)
        self.assertEqual(position.book_cost_ps, 50.0)
        self.assertEqual(position.direction, -1.0)
        self.assertEqual(position.current_trade_price, 0.0)
        self.assertEqual(position.market_value, 0.0)
        self.assertEqual(position.unr_gain, 5000.0)
        self.assertEqual(position.unr_perc_gain, 100.0)
github mhallsmoore / qstrader / tests / broker / test_position.py View on Github external
def test_position_short_long_excess_cover(self):
        """
        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 = EquityMock(1, exchange='NYSE')
        position = Position(
            asset, quantity=-100, book_cost_ps=700.0,
            current_trade_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)
        self.assertEqual(position.quantity, 75)
        self.assertEqual(position.book_cost_ps, 873.0)
        self.assertEqual(position.direction, 1.0)
        self.assertEqual(position.current_trade_price, 873.0)
        self.assertEqual(position.market_value, 65475.0)
        self.assertEqual(position.unr_gain, 0.0)
        self.assertEqual(position.unr_perc_gain, 0.0)
github mhallsmoore / qstrader / tests / broker / test_position.py View on Github external
def test_position_short_close(self):
        """
        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 = EquityMock(1, exchange='NYSE')
        position = Position(
            asset, quantity=-100, book_cost_ps=950.0,
            current_trade_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)
        self.assertEqual(position.quantity, 0)
        self.assertEqual(position.book_cost_ps, 0.0)
        self.assertEqual(position.direction, 1.0)
        self.assertEqual(position.current_trade_price, 960.0)
        self.assertEqual(position.market_value, 0.0)
        self.assertEqual(position.unr_gain, 0.0)
        self.assertEqual(position.unr_perc_gain, 0.0)
github mhallsmoore / qstrader / tests / broker / test_position.py View on Github external
def test_update_book_cost_for_commission_for_incorrect_asset(self):
        """
        Tests that the 'update_book_cost_for_commission'
        method, when provided with a transaction with an
        asset that does not match the position's asset,
        raises an Exception.
        """
        asset1 = EquityMock(1, exchange='NYSE')
        asset2 = EquityMock(2, exchange='NYSE')
        position = Position(
            asset1, quantity=100, book_cost_ps=950.0,
            current_trade_price=950.0
        )
        with self.assertRaises(Exception):
            position.update_book_cost_for_commission(asset2, 23.00)
github mhallsmoore / qstrader / tests / broker / test_position.py View on Github external
def test_position_long_short_positive_gain(self):
        """
        Tests that the quantity and book cost are
        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 gain.
        """
        asset = EquityMock(1, exchange='NYSE')
        position = Position(
            asset, quantity=100, book_cost_ps=950.0,
            current_trade_price=950.0
        )
        dt = pd.Timestamp('2015-05-06')
        transaction = Transaction(
            asset, quantity=-50, dt=dt, price=960.0,
            order_id=123, commission=None
        )
        position.update(transaction)
        self.assertEqual(position.quantity, 50)
        self.assertEqual(position.book_cost_ps, 950.0)
        self.assertEqual(position.direction, 1.0)
        self.assertEqual(position.current_trade_price, 960.0)
        self.assertEqual(position.market_value, 48000.0)
        self.assertEqual(position.unr_gain, 500.0)
        self.assertEqual(position.unr_perc_gain, 1.0526315789473684)
github mhallsmoore / qstrader / tests / broker / test_position.py View on Github external
def test_position_representation(self):
        """
        Tests that the Position representation
        correctly recreates the object.
        """
        asset = EquityMock(1, exchange='NYSE')
        position = Position(
            asset, quantity=153, book_cost_ps=950.0,
            current_trade_price=950.0
        )
        exp_repr = (
            "Position(asset=Equity(1), quantity=153, "
            "book_cost_ps=950.0, current_trade_price=950.0)"
        )
        self.assertEqual(repr(position), exp_repr)
github mhallsmoore / qstrader / tests / broker / test_position.py View on Github external
def test_update_for_incorrect_asset(self):
        """
        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 = EquityMock(1, exchange='NYSE')
        asset2 = EquityMock(2, exchange='NYSE')
        position = Position(
            asset1, quantity=100, book_cost_ps=950.0,
            current_trade_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 self.assertRaises(Exception):
            position.update(transaction)
github mhallsmoore / qstrader / qstrader / broker / position_handler.py View on Github external
def _check_set_position(self, asset):
        """
        Checks if a position exists in the positions list
        and if not creates a new key, and Position instance
        for this particular asset.
        """
        if asset in self.positions:
            position = self.positions[asset]
        else:
            position = Position(asset)
            self.positions[asset] = position
        return position