示例#1
0
    def test_record_trade_wrong_type(self):
        stock = Stock(self.symbol_1, StockType.COMMON, self.par_value_1,
                      self.last_dividend_1, self.fixed_dividend_0)
        false_trade = "a new trade"

        with self.assertRaises(TypeError):
            stock.record_trade(false_trade)
 def test_all_share_index(self):
     stock1 = Stock('TEST1', 'common', 100)
     stock2 = Stock('TEST2', 'common', 100)
     self.exchange.add_stock(stock1)
     self.exchange.add_stock(stock2)
     stock1.record_trade(1, 'buy', 8)
     stock2.record_trade(1, 'sell', 2)
     self.assertEqual(self.exchange.all_share_index(), 4)
示例#3
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)
示例#4
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)
示例#5
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)
 def test_record_trade(self):
     stock = Stock('TEST', 'common', 100)
     with self.assertRaises(AttributeError):
         stock.record_trade(10, 'buy', 300)
     self.exchange.add_stock(stock)
     stock.record_trade(10, 'buy', 300)
     trade = self.exchange.trades[stock.symbol][0]
     self.assertEqual(trade.symbol, stock.symbol)
     self.assertEqual(trade.quantity, 10)
     self.assertEqual(trade.price, 300)
     with self.assertRaises(ValueError):
         stock.record_trade(-4, 'buy', 300)
     with self.assertRaises(ValueError):
         stock.record_trade(10, 'discard', 300)
     with self.assertRaises(ValueError):
         stock.record_trade(10, 'buy', -1)
示例#7
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)
示例#8
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)
 def test_pe_ratio(self):
     exchange = Exchange('Test Exchange')
     stock = Stock('TEST', 'common', 100, 10)
     exchange.add_stock(stock)
     stock.record_trade(10, 'buy', 2)
     stock.record_trade(20, 'sell', 2)
     stock.record_trade(30, 'buy', 3)
     self.assertEqual(stock.pe_ratio(), Decimal('0.25'))
示例#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)
示例#11
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)
示例#12
0
 def test_dividend_yield(self):
     exchange = Exchange('Test Exchange')
     stock = Stock('TEST', 'common', 100, 10)
     exchange.add_stock(stock)
     stock.record_trade(10, 'buy', 2)
     stock.record_trade(20, 'sell', 2)
     stock.record_trade(30, 'buy', 3)
     self.assertEqual(stock.dividend_yield(), 4)
     stock = Stock('TESTPREFERRED', 'preferred', 100, 10, Decimal('0.25'))
     exchange.add_stock(stock)
     stock.record_trade(10, 'buy', 2)
     stock.record_trade(20, 'sell', 2)
     stock.record_trade(30, 'buy', 3)
     self.assertEqual(stock.dividend_yield(), 10)