def testCanCalculatePERatioForStock(self): stock = superSimpleStock.Stock('test/sample_data.csv') self.assertTrue( math.isnan(stock.get_pe_ratio(stock='TEA', ticker_price=2))) self.assertAlmostEqual(3 / 2.667, stock.get_pe_ratio(stock='POP', ticker_price=3), places=3)
def testCanGetAllShareIndex(self): stock = superSimpleStock.Stock('test/sample_data.csv') self.assertTrue(math.isnan(stock.get_all_share_index())) stock.record_trade(stock='POP', quantity=10, sold=True, price=1) stock.record_trade(stock='POP', quantity=15, sold=True, price=2) stock.record_trade(stock='TEA', quantity=100, sold=True, price=3) stock.record_trade(stock='TEA', quantity=150, sold=True, price=4) self.assertAlmostEqual(2.21, stock.get_all_share_index(), places=2)
def testCanRecordTrade(self): stock = superSimpleStock.Stock('test/sample_data.csv') stock.record_trade(stock='POP', quantity=10, sold=True, price=5) self.assertEqual(1, len(stock.trade)) stock.record_trade(stock='TEA', quantity=10, sold=True, price=5) self.assertEqual(2, len(stock.trade)) self.assertRaises(superSimpleStock.StockException, stock.record_trade, stock='FOOBAR', quantity=10, sold=True, price=5)
def testCanCalculateDividendYieldForStock(self): stock = superSimpleStock.Stock('test/sample_data.csv') self.assertAlmostEqual( 0, stock.get_dividend_yield(stock='TEA', ticker_price=2)) self.assertAlmostEqual(2.667, stock.get_dividend_yield(stock='POP', ticker_price=3), places=3) self.assertAlmostEqual(0.667, stock.get_dividend_yield(stock='GIN', ticker_price=3), places=3) self.assertRaises(superSimpleStock.StockException, stock.get_dividend_yield, stock='FOOBAR', ticker_price=3)
def testCalculateStockPrice(self): stock = superSimpleStock.Stock('sample_data.csv') self.assertTrue(math.isnan(stock.calculate_stock_price('POP'))) stock.record_trade(stock='POP', quantity=10, sold=True, price=5) stock.record_trade(stock='POP', quantity=15, sold=True, price=4) self.assertTrue(math.isnan(stock.calculate_stock_price('TEA'))) stock.record_trade(stock='TEA', quantity=100, sold=True, price=50) stock.record_trade(stock='TEA', quantity=150, sold=True, price=40) self.assertAlmostEqual(4.4, stock.calculate_stock_price('POP')) self.assertAlmostEqual(44.0, stock.calculate_stock_price('TEA')) time.sleep(2) self.assertTrue( math.isnan(stock.calculate_stock_price('TEA', last_minutes=0.01)))
import superSimpleStock stock= superSimpleStock.Stock('test/sample_data.csv') print('Cleaned dataFrame created from csv file : \n{} '.format(stock.gbce_data)) # calculate_dividend_yield print('i: Dividend for Stock: ') print('TEA : {} '.format(stock.calculate_dividend_yield(stock='TEA', ticker_price=3))) print('POP : {} '.format(stock.calculate_dividend_yield(stock='POP', ticker_price=3))) print('GIN : {} '.format(stock.calculate_dividend_yield(stock='GIN', ticker_price=3))) # calculate_pe_ratio print('ii: P/E Ratio for Stock: ') print('TEA : {} '.format(stock.calculate_pe_ratio(stock='TEA', ticker_price=2))) print('POP : {} '.format(stock.calculate_pe_ratio(stock='POP', ticker_price=3))) print('GIN : {} '.format(stock.calculate_pe_ratio(stock='GIN', ticker_price=3))) # record_trade stock.record_trade(stock='POP', quantity=10, sold=True, price=5) stock.record_trade(stock='POP', quantity=11, sold=True, price=6) stock.record_trade(stock='POP', quantity=12, sold=False, price=7) stock.record_trade(stock='TEA', quantity=1, sold=True, price=7) print('iii: Record Trades: \n{} '.format(stock.trade)) # calculate_stock_price print('iv: Stock Price: {} '.format(stock.calculate_stock_price('POP'))) # calculate_stock_price print('b: GBCE All Share Index: {} '.format(stock.calculate_all_share_index()))