Exemplo n.º 1
0
    def test_all_share_index(self):
        exchange = GlobalBeverageCorporationExchange(dict())
        stock_1 = Stock(self.symbol_1, StockType.COMMON, self.par_value_1,
                        self.last_dividend_1, self.fixed_dividend_0)
        stock_2 = Stock(self.symbol_1, StockType.PREFERRED, self.par_value_1,
                        self.last_dividend_0, self.fixed_dividend_1)
        stock_3 = Stock(self.symbol_2, StockType.PREFERRED, self.par_value_1,
                        self.last_dividend_0, self.fixed_dividend_1)

        exchange.add_stock(stock_1)
        exchange.add_stock(stock_2)
        exchange.add_stock(stock_3)

        trade_1 = Trade(self.symbol_1, StockType.COMMON, self.timestamp_now,
                        self.quantity_1, 90, BuySellIndicator.SELL)
        trade_2 = Trade(self.symbol_1, StockType.PREFERRED, self.timestamp_now,
                        self.quantity_2, 100, BuySellIndicator.SELL)
        trade_3 = Trade(self.symbol_2, StockType.PREFERRED, self.timestamp_now,
                        self.quantity_2, 30, BuySellIndicator.SELL)

        exchange.record_trade(trade_1)
        exchange.record_trade(trade_2)
        exchange.record_trade(trade_3)

        self.assertEqual(int(exchange.all_share_index()), 64)
Exemplo n.º 2
0
 def test_raises_value_error_on_negative_price_per_share(self):
     with self.assertRaises(ValueError):
         bad_trade = Trade(ticker_symbol=self.trade.ticker_symbol,
                           timestamp=self.trade.timestamp,
                           quantity=self.trade.quantity,
                           price_per_share=-25.0,
                           buy_sell_indicator=self.trade.buy_sell_indicator)
Exemplo n.º 3
0
 def test_raises_value_error_on_non_positive_qty(self):
     with self.assertRaises(ValueError):
         bad_trade = Trade(ticker_symbol=self.trade.ticker_symbol,
                           timestamp=self.trade.timestamp,
                           quantity=0,
                           price_per_share=self.trade.price_per_share,
                           buy_sell_indicator=self.trade.buy_sell_indicator)
Exemplo n.º 4
0
 def from_tuple(trade_data: tuple) -> Trade:
     datetime_str_format = '%Y-%m-%dT%H:%M:%S'
     return Trade(ticker_symbol=trade_data[0],
                  timestamp=datetime.strptime(trade_data[1],
                                              datetime_str_format),
                  quantity=trade_data[2],
                  price_per_share=trade_data[3],
                  buy_sell_indicator=trade_data[4])
Exemplo n.º 5
0
    def test_ticker_price(self):
        stock = Stock(self.symbol_1, StockType.COMMON, self.par_value_1,
                      self.last_dividend_1, self.fixed_dividend_0)
        trade = Trade(self.symbol_1, StockType.COMMON, self.timestamp_now,
                      self.quantity_1, self.price_per_share_1,
                      BuySellIndicator.SELL)

        stock.record_trade(trade)

        self.assertEqual(stock.ticker_price, self.price_per_share_1)
Exemplo n.º 6
0
    def test_price_earning_ratio_dividend_zero(self):
        stock = Stock(self.symbol_1, StockType.COMMON, self.par_value_1,
                      self.last_dividend_0, self.fixed_dividend_0)
        trade = Trade(self.symbol_1, StockType.COMMON, self.timestamp_now,
                      self.quantity_1, self.price_per_share_1,
                      BuySellIndicator.SELL)

        stock.record_trade(trade)

        self.assertEqual(stock.price_earnings_ratio, None)
Exemplo n.º 7
0
    def test_price_no_recent_trades(self):
        stock = Stock(self.symbol_1, StockType.COMMON, self.par_value_1,
                      self.last_dividend_1, self.fixed_dividend_0)
        trade = Trade(self.symbol_1, StockType.COMMON,
                      self.timestamp_now - timedelta(minutes=20),
                      self.quantity_1, self.price_per_share_1,
                      BuySellIndicator.SELL)

        stock.record_trade(trade)

        self.assertEqual(stock.price(self.timestamp_now), None)
Exemplo n.º 8
0
    def test_record_trade_sorting(self):
        stock = Stock(self.symbol_1, StockType.COMMON, self.par_value_1,
                      self.last_dividend_1, self.fixed_dividend_0)

        trade_1 = Trade(self.symbol_1, StockType.COMMON, self.timestamp_now,
                        self.quantity_1, self.price_per_share_1,
                        BuySellIndicator.SELL)
        trade_2 = Trade(self.symbol_1, StockType.COMMON,
                        self.timestamp_now - timedelta(minutes=10),
                        self.quantity_1, self.price_per_share_1,
                        BuySellIndicator.SELL)
        trade_3 = Trade(self.symbol_1, StockType.COMMON,
                        self.timestamp_now - timedelta(minutes=20),
                        self.quantity_1, self.price_per_share_1,
                        BuySellIndicator.SELL)

        stock.record_trade(trade_1)
        stock.record_trade(trade_2)
        stock.record_trade(trade_3)

        self.assertTrue(stock.trades[-1].timestamp, self.timestamp_now)
Exemplo n.º 9
0
    def test_price_use_only_recent_trades(self):
        stock = Stock(self.symbol_1, StockType.COMMON, self.par_value_1,
                      self.last_dividend_1, self.fixed_dividend_0)

        trade_1 = Trade(self.symbol_1, StockType.COMMON, self.timestamp_now,
                        self.quantity_1, self.price_per_share_1,
                        BuySellIndicator.SELL)
        trade_2 = Trade(self.symbol_1, StockType.COMMON,
                        self.timestamp_now - timedelta(minutes=10),
                        self.quantity_1, self.price_per_share_2,
                        BuySellIndicator.SELL)
        trade_3 = Trade(self.symbol_1, StockType.COMMON,
                        self.timestamp_now - timedelta(minutes=30),
                        self.quantity_2, self.price_per_share_2,
                        BuySellIndicator.SELL)

        stock.record_trade(trade_1)
        stock.record_trade(trade_2)
        stock.record_trade(trade_3)

        self.assertEqual(stock.price(), 175)
Exemplo n.º 10
0
    def test_record_trade_wrong_symbol_and_stock_type(self):
        stock = Stock(self.symbol_1, StockType.COMMON, self.par_value_1,
                      self.last_dividend_1, self.fixed_dividend_0)

        trade_1 = Trade(self.symbol_2, StockType.COMMON, datetime.now(),
                        self.quantity_1, self.price_per_share_1,
                        BuySellIndicator.SELL)
        trade_2 = Trade(self.symbol_1, StockType.PREFERRED, datetime.now(),
                        self.quantity_1, self.price_per_share_1,
                        BuySellIndicator.SELL)
        trade_3 = Trade(self.symbol_2, StockType.PREFERRED, datetime.now(),
                        self.quantity_1, self.price_per_share_1,
                        BuySellIndicator.SELL)

        with self.assertRaises(ValueError):
            stock.record_trade(trade_1)

        with self.assertRaises(ValueError):
            stock.record_trade(trade_2)

        with self.assertRaises(ValueError):
            stock.record_trade(trade_3)
Exemplo n.º 11
0
    def test_record_trade(self):
        exchange = GlobalBeverageCorporationExchange(dict())
        stock_1 = Stock(self.symbol_1, StockType.COMMON, self.par_value_1,
                        self.last_dividend_1, self.fixed_dividend_0)
        stock_2 = Stock(self.symbol_1, StockType.PREFERRED, self.par_value_1,
                        self.last_dividend_0, self.fixed_dividend_1)

        exchange.add_stock(stock_1)
        exchange.add_stock(stock_2)

        trade_1 = Trade(self.symbol_1, StockType.COMMON, self.timestamp_now,
                        self.quantity_1, self.price_per_share_1,
                        BuySellIndicator.SELL)
        trade_2 = Trade(self.symbol_1, StockType.PREFERRED, self.timestamp_now,
                        self.quantity_2, self.price_per_share_2,
                        BuySellIndicator.SELL)

        exchange.record_trade(trade_1)
        exchange.record_trade(trade_2)

        stocks = exchange.get_all_stocks()

        self.assertEqual(len(stocks[stock_1.symbol_and_type()].trades), 1)
        self.assertEqual(len(stocks[stock_2.symbol_and_type()].trades), 1)
Exemplo n.º 12
0
 def test_price(self):
     exchange = Exchange('Test Exchange')
     stock = Stock('TEST', 'common', 100)
     exchange.add_stock(stock)
     self.assertIsNone(stock.price())
     stock.record_trade(10, 'buy', 2)
     stock.record_trade(20, 'sell', 2)
     stock.record_trade(30, 'buy', 3)
     self.assertEqual(stock.price(), Decimal('2.5'))
     new_trades = []
     for trade in exchange.trades[stock.symbol]:
         six_minutes_ago = datetime.utcnow() - timedelta(minutes=6)
         new_trades.append(
             Trade(
                 date_time=six_minutes_ago,
                 symbol=trade.symbol,
                 quantity=trade.quantity,
                 buy_or_sell=trade.buy_or_sell,
                 price=trade.price,
             ))
     exchange.trades[stock.symbol] = new_trades
     self.assertEqual(stock.price(), 3)
Exemplo n.º 13
0
 def test_price_per_share_below_zero(self):
     with self.assertRaises(ValueError):
         Trade(self.symbol, StockType.COMMON, datetime.now(),
               self.quantity_1, self.price_per_share_0, BuySellIndicator.SELL)
Exemplo n.º 14
0
 def test_wrong_type(self):
     with self.assertRaises(ValueError):
         Trade(self.symbol, "COMMON", datetime.now(),
               self.quantity_1, self.price_per_share_1, BuySellIndicator.SELL)
Exemplo n.º 15
0
    def test_stock_and_type(self):
        trade = Trade(self.symbol, StockType.COMMON, datetime.now(),
                      self.quantity_1, self.price_per_share_1, BuySellIndicator.SELL)

        self.assertEquals(trade.symbol_and_type(), trade.symbol + "_" + str(trade.stock_type.name))
Exemplo n.º 16
0
    def test_total_price(self):
        trade = Trade(self.symbol, StockType.COMMON, datetime.now(),
                      self.quantity_1, self.price_per_share_1, BuySellIndicator.SELL)

        self.assertEquals(trade.total_price, 100*150)