コード例 #1
0
def calculate_p_e_ratio(created_stock=None):
    """
    A console wrapper for StockExchange.p_e_ratio_calculator
    :param created_stock: an optional argument for a stock object, for when the method is called from create_stock.
    :type created_stock: Stock
    """
    global stock_exchange
    # Creating a stock exchange, on the first run, to register the stock and access p_e_ratio_calculator()
    # Calling create_stock() to create the stock for which the p/e ratio will be calculated. Only one stock is needed.
    try:
        if not stock_exchange:
            stock_exchange = StockExchange('temp')
            create_stock('calculate_p_e_ratio')
        else:
            user_provided_args = raw_input(
                'Please provide the price for stock: {}, or \'q\'to quit\n:'.format(created_stock.stock_symbol))
            exit_program(user_provided_args)
            stock_exchange.add_new_stock(created_stock)
            p_e_ratio = stock_exchange.p_e_ratio_calculator(created_stock.stock_symbol, user_provided_args.strip())
            print 'P/E ratio for stock: {} and price: {} is: {}'.format(created_stock.stock_symbol,
                                                                        user_provided_args.strip(), p_e_ratio)
    except TypeError as error:
        print error
        # Retrying
        return calculate_p_e_ratio()
コード例 #2
0
 def test_vw_stock_price_calculator1(self):
     stock_exchange = StockExchange('test')
     Stock('GIN', 'preferred', '8', '100', '2%', stock_exchange)
     TradeRecord(stock_exchange, 'GIN', '300', 'buy', '130',
                 '2017-01-05 21:14:39')
     TradeRecord(stock_exchange, 'GIN', '500', 'sell', '150',
                 '2017-01-05 22:14:39')
     TradeRecord(stock_exchange, 'GIN', '200', 'sell', '120',
                 '2017-01-05 23:14:39')
     volume_weighted_price = stock_exchange.vw_stock_price_calculator('GIN')
     self.assertIsNone(volume_weighted_price)
コード例 #3
0
 def test_add_new_trade(self):
     stock_exchange = StockExchange('test')
     stock = Stock('GIN', 'preferred', '8', '100', '2%', stock_exchange)
     stock_exchange.add_new_trade(
         TradeRecord(stock_exchange, 'GIN', '500', 'sell', '150',
                     '2017-02-05 22:14:39'))
     stock_exchange.add_new_trade(
         TradeRecord(stock_exchange, 'GIN', '300', 'buy', '130',
                     '2017-02-05 23:14:39'))
     stock_exchange.remove_existing_stock(stock)
     stock_exchange.add_new_trade(
         TradeRecord(stock_exchange, 'GIN', '200', 'sell', '150',
                     '2017-02-05 23:15:39'))
コード例 #4
0
 def test_p_e_ratio_calculator(self):
     stock_exchange = StockExchange('test')
     Stock('POP',
           'common',
           '8',
           '100',
           current_stock_exchange=stock_exchange)
     stock_exchange.dividend_yield_calculator('POP', '150')
     p_e_ratio = stock_exchange.p_e_ratio_calculator('POP', '150')
     self.assertEqual(p_e_ratio, 18.75)
     p_e_ratio1 = stock_exchange.p_e_ratio_calculator('POP', '250')
     self.assertEqual(p_e_ratio1, 31.25)
     Stock('TEA',
           'common',
           '0',
           '100',
           current_stock_exchange=stock_exchange)
     p_e_ratio2 = stock_exchange.p_e_ratio_calculator('TEA', '150')
     self.assertIsNone(p_e_ratio2)
     Stock('ALE',
           'common',
           '23',
           '60',
           current_stock_exchange=stock_exchange)
     p_e_ratio3 = stock_exchange.p_e_ratio_calculator('ALE', '140')
     self.assertEqual(p_e_ratio3, 6.087)
コード例 #5
0
 def test_vw_stock_price_calculator(self):
     stock_exchange = StockExchange('test')
     Stock('GIN', 'preferred', '8', '100', '2%', stock_exchange)
     TradeRecord(stock_exchange, 'GIN', '300', 'buy', '130')
     TradeRecord(stock_exchange, 'GIN', '500', 'sell', '150')
     TradeRecord(stock_exchange, 'GIN', '200', 'sell', '120')
     stock_exchange.vw_stock_price_calculator('TEA')
     stock_exchange.vw_stock_price_calculator('GIN', '10')
     stock_exchange.vw_stock_price_calculator('GIN', 'test')
     volume_weighted_price = stock_exchange.vw_stock_price_calculator('GIN')
     self.assertEqual(volume_weighted_price, 138.0)
コード例 #6
0
 def test_remove_trade_by_symbol_date(self):
     stock_exchange = StockExchange('test')
     Stock('GIN', 'preferred', '8', '100', '2%', stock_exchange)
     TradeRecord(stock_exchange, 'GIN', '300', 'buy', '130',
                 '2017-02-05 23:14:39')
     TradeRecord(stock_exchange, 'GIN', '500', 'sell', '150',
                 '2017-02-05 22:14:39')
     stock_exchange.remove_trade_by_symbol_date('GIN',
                                                '2017-02-05 23:14:39')
     stock_exchange.remove_trade_by_symbol_date('GIN',
                                                '2017-02-05 23:14:39')
     stock_exchange.remove_trade_by_symbol_date('GIN',
                                                '2017-02-05 22:14:39')
     stock_exchange.remove_trade_by_symbol_date('GIN',
                                                '2017-02-05 22:14:39')
コード例 #7
0
def create_stock_exchange():
    """
    A POST rest wrapper for StockExchange
    Example usage: {
                    "name": "New_Stock_exchange"
                    }
    """
    global stock_exchange

    json_request = request.json

    if json_request:
        # Checking if all required params were in the request
        if 'name' in json_request:
            name = json_request['name']
            # Initializing the stock exchange, only the first time. All other times the stock exchange is returned.
            if stock_exchange:
                return jsonify(stock_exchange.__dict__), 200
            else:
                # Testing if the underlying code in stock_exchange.StockExchange() executed successfully.
                try:
                    stock_exchange = StockExchange(name)
                    return jsonify({'Stock Exchange': stock_exchange.name}), 200
                except TypeError as error:
                    abort(500, error)
        else:
            abort(400, 'Parameter "name" needs to be in the request')
    else:
        abort(400)
コード例 #8
0
 def test_dividend_yield_calculator(self):
     stock_exchange = StockExchange('test')
     Stock('TEA',
           'common',
           '0',
           '100',
           current_stock_exchange=stock_exchange)
     dividend = stock_exchange.dividend_yield_calculator('TEA', '200')
     self.assertEqual(dividend, 0.0)
     Stock('POP',
           'common',
           '8',
           '100',
           current_stock_exchange=stock_exchange)
     dividend = stock_exchange.dividend_yield_calculator('POP', '150')
     self.assertEqual(dividend, 0.053)
     Stock('GIN', 'preferred', '8', '100', '2%', stock_exchange)
     dividend1 = stock_exchange.dividend_yield_calculator('GIN', '350')
     self.assertEqual(dividend1, 0.006)
コード例 #9
0
 def test_fixed_dividend_exchange(self):
     stock_exchange = StockExchange('test')
     stock = Stock('GIN', 'preferred', '8', '100', '0.02', stock_exchange)
     self.assertEqual(stock.stock_symbol, 'GIN')
     self.assertEqual(stock.stock_type, 'preferred')
     self.assertEqual(stock.last_dividend, 8.0)
     self.assertEqual(stock.fixed_dividend, 0.02)
     self.assertEqual(stock.par_value, 100.0)
     self.assertEqual(stock.current_stock_exchange, stock_exchange)
     self.assertTrue(stock.created_successfully)
コード例 #10
0
 def test_time_stamp(self):
     trade = TradeRecord(StockExchange('test'), 'TEA', '500', 'sell', '150',
                         '2017-02-05 22:14:39')
     self.assertEqual(trade.stock_symbol, 'TEA')
     self.assertEqual(trade.quantity, 500.0)
     self.assertEqual(trade.trade_type, 'sell')
     self.assertEqual(trade.traded_price, 150)
     self.assertEqual(
         trade.time_stamp,
         datetime.strptime('2017-02-05 22:14:39', '%Y-%m-%d %H:%M:%S'))
     self.assertTrue(trade.created_successfully)
コード例 #11
0
 def test_simple(self):
     trade = TradeRecord(StockExchange('test'), 'TEA', '500', 'sell', '150')
     time_stamp = datetime.strptime(
         str(datetime.now())[:-10], '%Y-%m-%d %H:%M')
     self.assertEqual(trade.stock_symbol, 'TEA')
     self.assertEqual(trade.quantity, 500.0)
     self.assertEqual(trade.trade_type, 'sell')
     self.assertEqual(trade.traded_price, 150)
     self.assertEqual(str(trade.time_stamp)[:-3],
                      str(time_stamp)[:-3])  # removing seconds
     self.assertTrue(trade.created_successfully)
コード例 #12
0
def create_exchange_market():
    """
    A console wrapper for StockExchange
    :return: Recursion
    """
    global stock_exchange
    if not stock_exchange:
        user_provided_args = raw_input('Please provide an exchange market name, or \'q\'to quit\n:')
        exit_program(user_provided_args)
        try:
            stock_exchange = StockExchange(user_provided_args.strip())
        except TypeError as error:
            print error
        # Retrying
        return create_exchange_market()
コード例 #13
0
 def test_is_stock_price_valid(self):
     StockExchange.is_stock_price_valid('400.35')
     StockExchange.is_stock_price_valid('test')
     StockExchange.is_stock_price_valid('0.0')
コード例 #14
0
 def test_remove_stock_by_symbol(self):
     stock_exchange = StockExchange('test')
     stock = Stock('GIN', 'preferred', '8', '100', '2%')
     stock_exchange.add_new_stock(stock)
     stock_exchange.remove_existing_stock_by_symbol('GIN')
     stock_exchange.remove_existing_stock_by_symbol('TEA')
コード例 #15
0
 def test_add_new_stock(self):
     stock_exchange = StockExchange('test')
     stock = Stock('GIN', 'preferred', '8', '100', '2%')
     stock_exchange.add_new_stock(stock)
     stock_exchange.add_new_stock(stock)
コード例 #16
0
 def test_all_share_index_calculator(self):
     stock_exchange = StockExchange('test')
     stock_exchange.all_share_index_calculator()
     stock = Stock('POP', 'common', '8', '100')
     stock.current_price = 150
     stock_exchange.add_new_stock(stock)
     stock1 = Stock('TEA', 'common', '0', '100')
     stock1.current_price = 250
     stock_exchange.add_new_stock(stock1)
     stock2 = Stock('GIN', 'preferred', '8', '100', '2%')
     stock_exchange.add_new_stock(stock2)
     all_share_index = stock_exchange.all_share_index_calculator()
     self.assertEqual(all_share_index, 155.362)