当前位置: 首页>>代码示例>>Python>>正文


Python Position.update_market_value方法代码示例

本文整理汇总了Python中position.Position.update_market_value方法的典型用法代码示例。如果您正苦于以下问题:Python Position.update_market_value方法的具体用法?Python Position.update_market_value怎么用?Python Position.update_market_value使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在position.Position的用法示例。


在下文中一共展示了Position.update_market_value方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestRoundTripXOPosition

# 需要导入模块: from position import Position [as 别名]
# 或者: from position.Position import update_market_value [as 别名]
class TestRoundTripXOPosition(unittest.TestCase):
    """
    Trade a round-trip trade in Exxon-MObil where the initial
    trade is a buy/long of 100 shares of XOM, at a price of
    $74.78, with $1.00 commission.
    """
    def setUp(self):
        """
        Set up the Position object that wil store the PnL.
        """
        self.position = Position('BOT', 'XOM', Decimal('100'), 
                                Decimal('74.78'), Decimal('1.00'), 
                                Decimal('74.78'), Decimal('74.78'))

    def test_calculate_round_trip(self):
            
        """
        After the subsequent purchase, carry outtwo more buys/longs
        and then close the position out with two additional sells/shorts.

        The following prices have been tested against those calculated
        via Interactive Brokers' Trader Workstation (TWS).
        """

        # set the data in the object
        self.position.transact_shares('BOT', Decimal('100'), Decimal('74.63'), Decimal('1.00'))

        self.position.transact_shares('BOT', Decimal('250'), Decimal('74.620'), Decimal('1.25'))
        
        self.position.transact_shares('SLD', Decimal('200'), Decimal('74.58'), Decimal('1.00'))

        self.position.transact_shares('SLD', Decimal('250'), Decimal('75.26'), Decimal('1.25'))

        self.position.update_market_value(Decimal('77.75'), Decimal('77.77'))

        # start testing the values inside the object
        # the assertEqual method is derived by the unittest.TestCase from which the class derive from 
        self.assertEqual(self.position.action, 'BOT')
        self.assertEqual(self.position.ticker, 'XOM')
        self.assertEqual(self.position.quantity, Decimal('0'))

        self.assertEqual(self.position.buys, Decimal('450'))
        self.assertEqual(self.position.sells, Decimal('450'))
        self.assertEqual(self.position.net, Decimal('0'))
        self.assertEqual(self.position.avg_bot, Decimal('74.65778'))
        self.assertEqual(self.position.avg_sld, Decimal('74.95778'))
        self.assertEqual(self.position.total_bot, Decimal('33596.00'))
        self.assertEqual(self.position.total_sld, Decimal('33731.00'))
        self.assertEqual(self.position.net_total, Decimal('135.00'))
        self.assertEqual(self.position.total_commission, Decimal('5.50'))
        self.assertEqual(self.position.net_incl_comm, Decimal('129.50'))
            
        self.assertEqual(self.position.avg_price, Decimal('74.665'))
        self.assertEqual(self.position.cost_basis, Decimal('0.00'))
        self.assertEqual(self.position.market_value, Decimal('0.00'))
        self.assertEqual(self.position.unrealised_pnl, Decimal('0.00'))
        self.assertEqual(self.position.realised_pnl, Decimal('129.50'))
开发者ID:LoShan17,项目名称:TradingInfrastructure,代码行数:59,代码来源:position_test.py

示例2: TestRoundTripPGPosition

# 需要导入模块: from position import Position [as 别名]
# 或者: from position.Position import update_market_value [as 别名]
class TestRoundTripPGPosition(unittest.TestCase):
    """
    Test a round-trip trade in Proctor & Gamble where the initial
    trade is a sell/short of 100 shares of PG, at a price of $77.69, with $1.00 commission.
    """

    def setUp(self):
        self.position = Position('SLD', 'PG', Decimal('100'), 
                                Decimal('77.69'), Decimal('1.00'), 
                                Decimal('77.68'), Decimal('77.70'))

    def test_calculate_round_trip(self):
        """
        After the subsequent sale, carry out two more sells/shorts
        and then close the position out with two additional buys/longs.

        The following prices have been tested against those calculated
        via Interactive Brokers' Trader Workstation (TWS).
        """

        self.position.transact_shares('SLD', Decimal('100'), Decimal('77.68'), Decimal('1.00'))

        self.position.transact_shares('SLD', Decimal('50'), Decimal('77.70'), Decimal('1.00'))

        self.position.transact_shares('BOT', Decimal('100'), Decimal('77.77'), Decimal('1.00'))

        self.position.transact_shares('BOT', Decimal('150'), Decimal('77.73'), Decimal('1.00'))

        self.position.update_market_value(Decimal('77.72'), Decimal('77.72'))

        self.assertEqual(self.position.action, 'SLD')
        self.assertEqual(self.position.ticker, 'PG')
        self.assertEqual(self.position.quantity, Decimal('0'))

        self.assertEqual(self.position.buys, Decimal('250'))
        self.assertEqual(self.position.sells, Decimal('250'))
        self.assertEqual(self.position.net, Decimal('0'))
        self.assertEqual(self.position.avg_bot, Decimal('77.746'))
        self.assertEqual(self.position.avg_sld, Decimal('77.688'))
        self.assertEqual(self.position.total_bot, Decimal('19436.50'))
        self.assertEqual(self.position.total_sld, Decimal('19422.00'))
        self.assertEqual(self.position.net_total, Decimal('-14.50'))
        self.assertEqual(self.position.total_commission, Decimal('5.00'))
        self.assertEqual(self.position.net_incl_comm, Decimal('-19.50'))
            
        self.assertEqual(self.position.avg_price, Decimal('77.67600'))
        self.assertEqual(self.position.cost_basis, Decimal('0.00'))
        self.assertEqual(self.position.market_value, Decimal('0.00'))
        self.assertEqual(self.position.unrealised_pnl, Decimal('0.00'))
        self.assertEqual(self.position.realised_pnl, Decimal('-19.50'))
开发者ID:LoShan17,项目名称:TradingInfrastructure,代码行数:52,代码来源:position_test.py


注:本文中的position.Position.update_market_value方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。