Example #1
0
class Shop(object):
    def __init__(self, name):
        self.name = name
        self.stock = Stock()

    def add_stock(self, item, quantity):
        self.stock.update_stock(item, quantity)
Example #2
0
 def test_get_all_stocks(self):
     th = TradeHandler()
     stock1 = Stock('ABC', 10, 100)
     stock2 = Stock('ABD', 10, 100)
     stock3 = Stock('ABC', 10, 100, is_preferred=True, fixed_dividend=3)
     th.buy_stock(stock2, 2, 10)
     th.buy_stock(stock2, 2, 10)
     th.buy_stock(stock3, 4, 2)
     self.assertEquals(th.get_all_stocks(), {stock2, stock3})
 def test_get_all_share_index(self):
     th = TradeHandler()
     stock1 = Stock('ABC', 10, 100)
     stock2 = Stock('ABD', 10, 100)
     th.buy_stock(stock1, 10, 10)
     th.buy_stock(stock1, 10, 10)
     th.buy_stock(stock2, 10, 10)
     self.assertAlmostEqual(10, get_all_share_index(th))
     th.buy_stock(stock2, 50, 1)
     self.assertAlmostEqual(5, get_all_share_index(th))
 def test_get_all_share_index_based_on_last_price(self):
     th = TradeHandler()
     stock1 = Stock('ABC', 10, 100)
     stock2 = Stock('ABD', 10, 100)
     stock3 = Stock('ABE', 10, 100)
     th.buy_stock(stock1, 1, 9)
     th.buy_stock(stock1, 10, 10)
     th.buy_stock(stock2, 64, 4)
     th.buy_stock(stock2, 50, 10)
     th.buy_stock(stock3, 1, 346)
     th.buy_stock(stock3, 1, 10)
     self.assertAlmostEqual(10, get_all_share_index_based_on_last_price(th))
Example #5
0
class TestStock(unittest.TestCase):
    def setUp(self):
        self.stock = Stock()

    def test_stock_adds_new_stock_items(self):
        item = StockItem('Fresh Diary Milk', 1000)
        self.stock.update_stock(item, 20)
        self.assertEqual(
            self.stock.stock_items, {
                'Fresh Diary Milk': {
                    'name': 'Fresh Diary Milk',
                    'quantity': 20,
                    'unit_price': 1000
                }
            })

    def test_stock_adds_multiple_stock_items(self):
        items = [
            StockItem('Fresh Diary Milk', 1000),
            StockItem('Bic Pen', 2000)
        ]
        for item in items:
            self.stock.update_stock(item, 20)
        self.assertEqual(
            self.stock.stock_items, {
                'Fresh Diary Milk': {
                    'name': 'Fresh Diary Milk',
                    'quantity': 20,
                    'unit_price': 1000
                },
                'Bic Pen': {
                    'name': 'Bic Pen',
                    'quantity': 20,
                    'unit_price': 2000
                }
            })

    def test_stock_updates_existing_stock_item(self):
        item = StockItem('Fresh Diary Milk', 1000)
        self.stock.update_stock(item, 20)
        self.stock.update_stock(item, 40)
        self.assertEqual(
            self.stock.stock_items, {
                'Fresh Diary Milk': {
                    'name': 'Fresh Diary Milk',
                    'quantity': 60,
                    'unit_price': 1000
                }
            })

    def test_stock_get_sub_total_return_the_cost_of_a_line_item(self):
        item = StockItem('Fresh Diary Milk', 1000)
        self.stock.update_stock(item, 20)
        line_item = LineItem('Fresh Diary Milk', 10)

        sub_total = self.stock.get_sub_total(line_item)
        self.assertEqual(sub_total, 10000)
Example #6
0
 def test_get_all_trades_for_stock(self):
     th = TradeHandler()
     stock1 = Stock('ABC', 10, 100)
     stock2 = Stock('ABD', 10, 100)
     stock3 = Stock('ABC', 10, 100, is_preferred=True, fixed_dividend=3)
     trade_stock2_1 = th.buy_stock(stock2, 2, 10)
     trade_stock2_2 = th.buy_stock(stock2, 2, 10)
     trade_stock3_1 = th.buy_stock(stock3, 4, 2)
     self.assertEquals([], th.get_all_trades_for_stock(stock1))
     self.assertEquals(th.get_all_trades_for_stock(stock2),
                       [trade_stock2_1, trade_stock2_2])
     self.assertEquals(th.get_all_trades_for_stock(stock3),
                       [trade_stock3_1])
Example #7
0
 def test_multiple_transactions(self):
     th = TradeHandler()
     stock1 = Stock('ABC', 10, 100)
     stock2 = Stock('ABD', 10, 100)
     stock3 = Stock('ABC', 10, 100, is_preferred=True, fixed_dividend=3)
     trade_stock1_1 = th.sell_stock(stock1, 2, 10)
     trade_stock1_2 = th.sell_stock(stock1, 3, 10)
     trade_stock1_3 = th.buy_stock(stock1, 4, 10)
     trade_stock2_1 = th.sell_stock(stock2, 2, 10)
     trade_stock3_1 = th.sell_stock(stock3, 2, 10)
     self.assertEquals(th._trades['ABC']['common'],
                       [trade_stock1_1, trade_stock1_2, trade_stock1_3])
     self.assertEquals(th._trades['ABD']['common'], [trade_stock2_1])
     self.assertEquals(th._trades['ABC']['preferred'], [trade_stock3_1])
 def test_get_volume_wighted_stock_price_with_only_old_trades(self):
     th = TradeHandler()
     stock = Stock('ABC', 10, 100)
     minus_16_minutes = datetime.datetime.now() - datetime.timedelta(
         minutes=16)
     th.buy_stock(stock, 2, 10, timestamp=minus_16_minutes)
     with self.assertRaises(Exception) as context:
         get_volume_weighted_stock_price(th, stock)
     self.assertEquals(context.exception.message,
                       'No Trades found for the given Stock')
Example #9
0
 def test_buy_one_stock(self):
     th = TradeHandler()
     stock = Stock('ABC', 10, 100)
     trade = th.buy_stock(stock, 2, 10)
     trade_list = th._trades['ABC']['common']
     self.assertEquals(1, len(trade_list))
     self.assertEquals(trade_list[0], trade)
     self.assertEquals(trade_list[0].quantity, 2)
     self.assertEquals(trade_list[0].price, 10)
     self.assertEquals(trade_list[0].indicator, TRADE_TYPE.BUY)
 def test_get_volume_wighted_stock_price(self):
     th = TradeHandler()
     stock = Stock('ABC', 10, 100)
     th.buy_stock(stock, 2, 10)
     th.buy_stock(stock, 3, 10)
     self.assertAlmostEqual(10.0,
                            get_volume_weighted_stock_price(th, stock))
     th.sell_stock(stock, 10, 20)
     self.assertAlmostEqual(16.66666,
                            get_volume_weighted_stock_price(th, stock),
                            delta=0.00001)
 def test_get_all_share_index_with_no_valid_trades(self):
     th = TradeHandler()
     stock = Stock('ABC', 10, 100)
     th.buy_stock(stock,
                  2,
                  10,
                  timestamp=datetime.datetime.now() -
                  datetime.timedelta(minutes=16))
     with self.assertRaises(Exception) as context:
         get_all_share_index(th)
     self.assertEquals(context.exception.message,
                       'No Trades found for the given Stock')
 def test_get_volume_wighted_stock_price_with_older_trades(self):
     th = TradeHandler()
     stock = Stock('ABC', 10, 100)
     too_old = datetime.datetime.now() - datetime.timedelta(minutes=16)
     should_be_okay = datetime.datetime.now() - datetime.timedelta(
         minutes=14)
     th.buy_stock(stock, 2, 10, timestamp=too_old)
     th.buy_stock(stock, 2, 10, timestamp=should_be_okay)
     th.buy_stock(stock, 3, 30, timestamp=should_be_okay)
     th.buy_stock(stock, 1, 50)
     self.assertAlmostEqual(26.66666,
                            get_volume_weighted_stock_price(th, stock),
                            delta=0.00001)
Example #13
0
 def test_get_all_trades_for_untraded_stock(self):
     stock1 = Stock('ABC', 10, 100)
     self.assertEquals(TradeHandler().get_all_trades_for_stock(stock1), [])
Example #14
0
 def test_invalid_preferred_stock(self):
     with self.assertRaises(Exception) as context:
         Stock('ABC', 12, 100, is_preferred=True)
     self.assertEquals(
         context.exception.message,
         'You need to provide fixed_dividend for preferred stocks')
Example #15
0
 def test_get_pe_ratio(self):
     stock = Stock('ABC', 12, 100)
     self.assertAlmostEqual(0.75, stock.get_pe_ratio(3))
     self.assertAlmostEqual(1.33333, stock.get_pe_ratio(4), delta=0.00001)
Example #16
0
 def test_get_preferred_dividend_yield_floats(self):
     stock = Stock('ABC', 12, 100, is_preferred=True, fixed_dividend=2)
     self.assertAlmostEqual(2.0, stock.get_dividend_yield(100))
     self.assertAlmostEqual(6.66666,
                            stock.get_dividend_yield(30),
                            delta=0.00001)
Example #17
0
 def test_get_common_dividend_yield_floats(self):
     stock = Stock('ABC', 10, 100)
     self.assertAlmostEqual(0.1, stock.get_dividend_yield(100))
     self.assertAlmostEqual(0.33333,
                            stock.get_dividend_yield(30),
                            delta=0.00001)
Example #18
0
 def setUp(self):
     self.stock = Stock()
Example #19
0
 def test_get_dividend_yield_with_zero(self):
     stock = Stock('ABC', 12, 100)
     with self.assertRaises(Exception) as context:
         stock.get_dividend_yield(0)
     self.assertEquals(context.exception.message, 'Price cant be zero')
Example #20
0
 def __init__(self, name):
     self.name = name
     self.stock = Stock()
Example #21
0
 def test_get_common_dividend_yield_ints(self):
     stock = Stock('ABC', 12, 100)
     self.assertEquals(1, stock.get_dividend_yield(12))
     self.assertEquals(2, stock.get_dividend_yield(6))
     self.assertEquals(3, stock.get_dividend_yield(4))
Example #22
0
 def test_get_preferred_dividend_yield_ints(self):
     stock = Stock('ABC', 12, 100, is_preferred=True, fixed_dividend=2)
     self.assertEquals(1, stock.get_dividend_yield(200))
     self.assertEquals(4, stock.get_dividend_yield(50))
     self.assertEquals(10, stock.get_dividend_yield(20))