コード例 #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)
コード例 #2
0
ファイル: demo.py プロジェクト: khusrokarim/supersimplestocks
def demo():
    stocks = (
        Stock('TEA', 'common', 100, 0),
        Stock('POP', 'common', 100, 8),
        Stock('ALE', 'common', 60, 23),
        Stock('GIN', 'preferred', 100, 8, 0.02),
        Stock('JOE', 'common', 250, 13),
    )
    exchange = Exchange('GBCE')
    print('Created exchange {}'.format(exchange))
    for stock in stocks:
        exchange.add_stock(stock)
        print('Added {} to {}'.format(stock, exchange))
    print('Recording 10 random trades:')
    for _ in range(10):
        stock = random.choice(stocks)
        buy_or_sell = random.choice(('buy', 'sell'))
        quantity = random.randint(1, 1000)
        price = random.randint(1, 1000)
        stock.record_trade(quantity, buy_or_sell, price)
        print('   {}'.format(exchange.trades[stock.symbol][-1]))
    for stock in stocks:
        print('-----')
        print(stock.symbol)
        try:
            print('Dividend yield: {}'.format(stock.dividend_yield()))
        except InvalidOperation:
            pass
        try:
            print('P/E Ratio: {}'.format(stock.pe_ratio()))
        except InvalidOperation:
            pass
        print('Price: {}'.format(stock.price()))
    print('-----')
    print('GBCE All-Share Index: {}'.format(exchange.all_share_index()))
コード例 #3
0
 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)
コード例 #4
0
    def test_dividend(self):
        stock_common = Stock(self.symbol_1, StockType.COMMON, self.par_value_1,
                             self.last_dividend_1, self.fixed_dividend_0)
        stock_preferred = Stock(self.symbol_2, StockType.PREFERRED,
                                self.par_value_2, self.last_dividend_2,
                                self.fixed_dividend_1)

        self.assertEqual(stock_common.dividend, self.last_dividend_1)
        self.assertEqual(stock_preferred.dividend,
                         self.fixed_dividend_1 * self.par_value_2)
コード例 #5
0
 def test_create_stock(self):
     stock = Stock('TEST', 'common', 100)
     self.assertIsInstance(stock, Stock)
     stock = Stock('TEST',
                   'preferred',
                   100,
                   last_dividend=1,
                   fixed_dividend=Decimal('0.02'))
     self.assertIsInstance(stock, Stock)
     with self.assertRaises(ValueError):
         Stock('TEST', 'invalid type', 100)
     with self.assertRaises(ValueError):
         Stock('TEST', 'common', -5)
コード例 #6
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)
コード例 #7
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)
コード例 #8
0
 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'))
コード例 #9
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)
コード例 #10
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)
コード例 #11
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)
コード例 #12
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)
コード例 #13
0
    def test_add_stock(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)

        stocks = exchange.get_all_stocks()

        self.assertEqual(len(stocks), 2)

        self.assertEqual(stocks[stock_1.symbol_and_type()].symbol,
                         stock_1.symbol)
        self.assertEqual(stocks[stock_1.symbol_and_type()].stock_type,
                         stock_1.stock_type)

        self.assertEqual(stocks[stock_2.symbol_and_type()].symbol,
                         stock_2.symbol)
        self.assertEqual(stocks[stock_2.symbol_and_type()].stock_type,
                         stock_2.stock_type)
コード例 #14
0
 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)
コード例 #15
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)
コード例 #16
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)
コード例 #17
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)
コード例 #18
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)
コード例 #19
0
    def test_ticker_price_no_trades(self):
        stock = Stock(self.symbol_1, StockType.COMMON, self.par_value_1,
                      self.last_dividend_1, self.fixed_dividend_0)

        with self.assertRaises(AttributeError):
            stock.ticker_price
コード例 #20
0
 def test_add_stock(self):
     stock = Stock('TEST', 'common', 100)
     self.exchange.add_stock(stock)
     self.assertEqual(self.exchange.stocks[stock.symbol], stock)
コード例 #21
0
 def test_common_stock_with_fixed_dividend(self):
     with self.assertRaises(ValueError):
         stock = Stock(self.symbol_1, StockType.COMMON, self.par_value_1,
                       self.last_dividend_1, self.fixed_dividend_1)
コード例 #22
0
    def test_not_instantiable(self):

        with self.assertRaises(TypeError):
            stock = Stock(ticker_symbol=TickerSymbol.ALE, par_value=100.0)