예제 #1
0
 def test_add_stocks(self):
     self.portfolio.add_stocks(self.stocks)
     self.assertSetEqual(set(self.portfolio.get_current_stocks()),
                         {'IBM', 'AAPL'})
     stocks = [Stock('SAP'), Stock('GOOGL')]
     self.portfolio.add_stocks(stocks)
     self.assertSetEqual(set(self.portfolio.get_current_stocks()),
                         {'IBM', 'AAPL', 'SAP', 'GOOGL'})
예제 #2
0
 def test_add_stock(self):
     self.portfolio.add_stocks(self.stocks)
     self.assertSetEqual(set(self.portfolio.get_current_stocks()),
                         {'IBM', 'AAPL'})
     stock = Stock('SAP')
     self.portfolio.add_stock(stock)
     self.assertSetEqual(set(self.portfolio.get_current_stocks()),
                         {'IBM', 'AAPL', 'SAP'})
예제 #3
0
    def test_get_weights_value(self):
        ibm = Stock('IBM')
        ibm.add_transaction('2014-10-12', 100)
        ibm.add_transaction('2015-10-12', 150)
        ibm.add_transaction('2016-10-12', -100)

        aapl = Stock('AAPL')
        aapl.add_transaction('2014-10-6', 10)
        aapl.add_transaction('2015-10-6', 15)
        aapl.add_transaction('2016-10-6', 30)

        self.portfolio = Portfolio(stocks=[ibm, aapl])

        prices = {'AAPL': 100.32, 'IBM': 171.03}

        weights = self.portfolio.get_weights(weight_by='value', prices=prices)

        self.assertAlmostEqual(weights.sum(axis=1)[0], 1.00)

        weight_ibm = 0.82
        weight_aapl = 0.18

        self.assertAlmostEqual(weights['IBM'].iloc[0], weight_ibm, 2)
        self.assertAlmostEqual(weights['AAPL'].iloc[0], weight_aapl, 2)
예제 #4
0
 def setUp(self):
     self.stocks = [Stock('IBM'), Stock('AAPL')]
     self.portfolio = Portfolio()
예제 #5
0
    def test_get_weights_number(self):
        ibm = Stock('IBM')
        ibm.add_transaction('2014-10-12', 100)
        ibm.add_transaction('2015-10-12', 150)
        ibm.add_transaction('2016-10-12', -100)

        aapl = Stock('AAPL')
        aapl.add_transaction('2014-10-6', 10)
        aapl.add_transaction('2015-10-6', 15)
        aapl.add_transaction('2016-10-6', 30)

        self.portfolio = Portfolio(stocks=[ibm, aapl])

        weight_ibm = 0.73
        weight_aapl = 0.27

        weights = self.portfolio.get_weights()
        self.assertAlmostEqual(weights.sum(), 1.00)
        self.assertAlmostEqual(weights['IBM'], weight_ibm, 2)
        self.assertAlmostEqual(weights['AAPL'], weight_aapl, 2)
예제 #6
0
    def test_get_dataframe(self):
        ibm = Stock('IBM')
        ibm.add_transaction('2014-10-12', 100)
        ibm.add_transaction('2015-10-12', 150)
        ibm.add_transaction('2016-10-12', -100)

        aapl = Stock('AAPL')
        aapl.add_transaction('2014-10-6', 10)
        aapl.add_transaction('2015-10-6', 15)
        aapl.add_transaction('2016-10-6', 30)

        self.portfolio = Portfolio(stocks=[ibm, aapl])
        df = self.portfolio.get_dataframe()

        # sum - vertical
        shares_sum = df.sum(axis=0)
        self.assertEqual(shares_sum['IBM'], 150)
        self.assertEqual(shares_sum['AAPL'], 55)

        # total shares
        shares_total = shares_sum.sum()
        self.assertEqual(shares_total, 205)
예제 #7
0
 def test_get_current_stocks(self):
     stocks = [Stock('SAP'), Stock('GOOGL')]
     self.portfolio = Portfolio(stocks=stocks)
     self.assertSetEqual(set(self.portfolio.get_current_stocks()),
                         {'SAP', 'GOOGL'})
예제 #8
0
    def test_get_prices(self, mock_get_prices):
        ibm = Stock('IBM')
        ibm.add_transaction('2014-10-12', 100)
        ibm.add_transaction('2015-10-12', 150)
        ibm.add_transaction('2016-10-12', -100)

        aapl = Stock('AAPL')
        aapl.add_transaction('2014-10-06', 10)
        aapl.add_transaction('2015-10-06', 15)
        aapl.add_transaction('2016-10-06', 30)

        self.portfolio = Portfolio(stocks=[ibm, aapl])

        # today's date (default)
        dt_end = dt.date.today()
        dt_start = dt_end - dt.timedelta(days=1)

        self.portfolio.get_prices()
        mock_get_prices.assert_called_with(symbols=ANY,
                                           dt_start=dt_start,
                                           dt_end=dt_end)

        # specific date
        date_str = '2016-07-13'
        date = dt.datetime.strptime(date_str, '%Y-%m-%d').date()
        dt_end = dt.datetime.strptime(date_str, '%Y-%m-%d').date()
        dt_start = dt_end - dt.timedelta(days=1)

        self.portfolio.get_prices(date=date, range='date')
        mock_get_prices.assert_called_with(symbols=ANY,
                                           dt_start=dt_start,
                                           dt_end=dt_end)

        # validate the date input field
        with self.assertRaises(AssertionError):
            self.portfolio.get_prices(date=date_str, range='date')

        # date range of all transactions
        dt_start = dt.datetime.strptime('2014-10-06', '%Y-%m-%d').date()
        dt_end = dt.datetime.strptime('2016-10-12', '%Y-%m-%d').date()

        self.portfolio.get_prices(range='range')
        mock_get_prices.assert_called_with(symbols=ANY,
                                           dt_start=dt_start,
                                           dt_end=dt_end)

        # full range - first transaction data to today
        dt_end = dt.date.today()

        self.portfolio.get_prices(range='full')
        mock_get_prices.assert_called_with(symbols=ANY,
                                           dt_start=dt_start,
                                           dt_end=dt_end)
예제 #9
0
    def test_get_current_value(self):
        ibm = Stock('IBM')
        ibm.add_transaction('2014-10-12', 100)
        ibm.add_transaction('2015-10-12', 150)
        ibm.add_transaction('2016-10-12', -100)

        aapl = Stock('AAPL')
        aapl.add_transaction('2014-10-6', 10)
        aapl.add_transaction('2015-10-6', 15)
        aapl.add_transaction('2016-10-6', 30)

        self.portfolio = Portfolio(stocks=[ibm, aapl])

        prices = {'AAPL': 100.32, 'IBM': 171.03}

        value_ibm = 25654.5
        value_aapl = 5517.6
        value_total = 31172.1

        values = self.portfolio.get_current_value(prices)

        self.assertAlmostEqual(values['IBM'].iloc[0], value_ibm, 2)
        self.assertAlmostEqual(values['AAPL'].iloc[0], value_aapl, 2)
        self.assertAlmostEqual(values['TOTAL'].iloc[0], value_total, 2)
예제 #10
0
    def test_get_prices(self, mock_get_prices):
        ibm = Stock('IBM')
        ibm.add_transaction('2014-10-12', 100)
        ibm.add_transaction('2015-10-12', 150)
        ibm.add_transaction('2016-10-12', -100)

        aapl = Stock('AAPL')
        aapl.add_transaction('2014-10-06', 10)
        aapl.add_transaction('2015-10-06', 15)
        aapl.add_transaction('2016-10-06', 30)

        self.portfolio = Portfolio(stocks=[ibm, aapl])

        symbols = ['IBM', 'AAPL']

        # today's date (default)
        dt_end = dt.date.today()
        dt_start = dt_end - dt.timedelta(days=1)

        self.portfolio.get_prices()
        mock_get_prices.assert_called_with(symbols=symbols,
                                           dt_start=dt_start,
                                           dt_end=dt_end)
예제 #11
0
def main():

    # define your stocks
    ibm = Stock('IBM')
    ibm.add_transaction('2014-10-12', 100)
    ibm.add_transaction('2015-10-12', 150)
    ibm.add_transaction('2016-10-12', -100)

    appl = Stock('APPL')
    appl.add_transaction('2014-10-6', 10)
    appl.add_transaction('2015-10-6', 15)
    appl.add_transaction('2016-10-6', 30)

    # create your portfolio and add the stocks to it
    portfolio = Portfolio(stocks=[ibm, appl])

    # define the prices you want to use to get the
    prices = {'APPL': 100.32, 'IBM': 171.03}

    weights = portfolio.get_weights(weight_by='value', prices=prices)
    print('weights', weights)

    values = portfolio.get_current_value(prices)
    print('values', values)
예제 #12
0
 def setUp(self):
     self.symbol = 'IBM'
     self.name = 'International Business Machines'
     self.stock = Stock(self.symbol, self.name)
예제 #13
0
class TestStock(TestCase):
    def setUp(self):
        self.symbol = 'IBM'
        self.name = 'International Business Machines'
        self.stock = Stock(self.symbol, self.name)

    def test_add_initial_transaction(self):
        self.assertEqual(self.stock.symbol, self.symbol)
        self.assertEqual(self.stock.name, self.name)
        self.assertEqual(self.stock.get_total_amount(), 0)
        self.stock.add_transaction('2015-12-11', 123, 166.32)
        self.assertEqual(self.stock.get_total_amount(), 123)

    def test_get_total_amount(self):
        self.assertEqual(self.stock.symbol, self.symbol)
        self.stock.add_transaction('2016-01-12', 50)
        self.stock.add_transaction('2016-02-17', -10)
        self.stock.add_transaction('2016-03-17', 70)
        self.stock.add_transaction('2016-04-17', 30)
        self.stock.add_transaction('2016-05-17', -5)
        self.assertEqual(self.stock.get_total_amount(), 135)

    def test_ensure_transaction_order(self):
        self.assertEqual(self.stock.symbol, self.symbol)
        self.stock.add_transaction('2015-01-12', 50)
        self.stock.add_transaction('2016-02-17', -10)
        self.stock.add_transaction('2015-03-17', 70)
        self.stock.add_transaction('2015-04-17', 30)
        self.stock.add_transaction('2014-05-17', -5)
        self.assertEqual(self.stock.holdings[-1].amount, -10)
예제 #14
0
    def test_save_portfolio_to_file(self):
        ibm = Stock('IBM')
        ibm.add_transaction('2014-10-12', 100)
        ibm.add_transaction('2015-10-12', 150)
        ibm.add_transaction('2016-10-12', -100)

        aapl = Stock('AAPL')
        aapl.add_transaction('2014-10-6', 10)
        aapl.add_transaction('2015-10-6', 15)
        aapl.add_transaction('2016-10-6', 30)

        self.portfolio = Portfolio(stocks=[ibm, aapl])
        _ = self.portfolio.get_dataframe()

        self.portfolio.save_to_file('/'.join([self.path_tmp, self.path_file]))
예제 #15
0
 def setUp(self):
     self.stocks = [Stock('IBM'), Stock('AAPL')]
     self.portfolio = Portfolio()
     self.path_tmp = PATH_TMP
     self.path_file = PATH_FILE
     self.path_test_files = PATH_TEST_FILES