Exemplo n.º 1
0
    def test_option_expiration(self):
        call = Option('call', TSLA, 500, datetime(2020, 12, 28))
        port = Portfolio(0, {TSLA: 100})
        te = TradingEngine([port])

        te.sell_contract(port, call, 10)
        te.eval({TSLA: 400}, datetime(2020, 12, 28))

        self.assertFalse(port.contracts())
        self.assertEqual(0, port.collateral[TSLA])
Exemplo n.º 2
0
    def test_exercise_put(self):
        put = Option('put', TSLA, 500, datetime(2020, 12, 28))
        port = Portfolio(2000, {TSLA: 100})
        te = TradingEngine([port])

        te.buy_contract(port, put, 10)
        self.assertEqual(2000 - 1000, port.cash)
        self.assertEqual(1, port.contracts()[put])

        te.eval({TSLA: 400}, datetime(2020, 12, 28))

        self.assertEqual(2000 - 1000 + 50000, port.cash)
        self.assertEqual(0, port.securities.get(TSLA, 0))
        self.assertEqual(0, port.securities[put])
Exemplo n.º 3
0
    def test_exercise_call(self):
        call = Option('call', TSLA, 500, datetime(2020, 12, 28))
        port = Portfolio(51000, {})
        te = TradingEngine([port])

        te.buy_contract(port, call, 10)
        self.assertEqual(51000 - 1000, port.cash)
        self.assertEqual(1, port.contracts()[call])

        te.eval({TSLA: 600}, datetime(2020, 12, 28))

        self.assertEqual(0, port.cash)
        self.assertEqual(100, port.securities[TSLA])
        self.assertEqual(0, port.securities[call])
Exemplo n.º 4
0
    def test_expire_and_assign(self):
        call = Option('call', TSLA, 500, datetime(2020, 12, 28))
        port = Portfolio(0, {TSLA: 100})
        te = TradingEngine([port])

        te.sell_contract(port, call, 10)
        te.eval({TSLA: 400}, datetime(2020, 12, 28))

        call = Option('call', TSLA, 500, datetime(2021, 1, 15))
        te.sell_contract(port, call, 5)
        te.eval({TSLA: 600}, datetime(2021, 1, 15))

        self.assertEqual(1000 + 500 + 50000, port.cash)
        self.assertEqual(0, port.securities[TSLA])
        self.assertEqual(0, port.contracts()[call])
Exemplo n.º 5
0
    def test_contracts(self):
        call = Option('call', TSLA, 500, datetime(2020, 12, 28))
        p = Portfolio(0, {call: 1})

        self.assertEqual(1, p.contracts()[call])