示例#1
0
    def test_lowest_buy_price_quantity3(self):
        '''
            test lowest buy price quantity with sell transaction
        '''
        stock_db_connection = get_default_db_connection()
        reset_table(stock_db_connection)
        stock_transaction_table = StockTransactionTable(stock_db_connection)

        # init transaction 1
        stock_transaction_1 = StockTransaction()
        stock_transaction_1.symbol = "601398"
        stock_transaction_1.buy_or_sell = StockTransaction.BUY_FLAG
        stock_transaction_1.date = date(2016, 5, 15)
        stock_transaction_1.quantity = 200
        stock_transaction_1.price = 4.51
        stock_transaction_table.add_stock_transaction(stock_transaction_1)
        stock_transaction_1.trans_id

        # init transaction 2
        stock_transaction_2 = StockTransaction()
        stock_transaction_2.symbol = "601398"
        stock_transaction_2.buy_or_sell = StockTransaction.SELL_FLAG
        stock_transaction_2.date = date(2016, 5, 16)
        stock_transaction_2.quantity = 100
        stock_transaction_2.price = 4.81
        stock_transaction_table.add_stock_transaction(stock_transaction_2)
        stock_transaction_2.trans_id

        with self.assertRaises(Exception):
            StockTransaction.get_lowest_buy_price_quantity("601398")

        return
示例#2
0
    def test_lowest_buy_price_quantity(self):
        '''
            test lowest buy price quantity
        '''
        stock_db_connection = get_default_db_connection()
        reset_table(stock_db_connection)
        stock_transaction_table = StockTransactionTable(stock_db_connection)

        # init transaction 1
        stock_transaction_1 = StockTransaction()
        stock_transaction_1.symbol = "601398"
        stock_transaction_1.buy_or_sell = StockTransaction.BUY_FLAG
        stock_transaction_1.date = date(2016, 5, 15)
        stock_transaction_1.quantity = 200
        stock_transaction_1.price = 4.9
        stock_transaction_table.add_stock_transaction(stock_transaction_1)
        stock_transaction_1.trans_id

        # init transaction 2
        stock_transaction_2 = StockTransaction()
        stock_transaction_2.symbol = "601398"
        stock_transaction_2.buy_or_sell = StockTransaction.BUY_FLAG
        stock_transaction_2.date = date(2016, 5, 16)
        stock_transaction_2.quantity = 100
        stock_transaction_2.price = 4.81
        stock_transaction_table.add_stock_transaction(stock_transaction_2)
        stock_transaction_2.trans_id

        quantity = StockTransaction.get_lowest_buy_price_quantity("601398")
        self.assertEqual(quantity, 100)

        return
示例#3
0
    def test_close_stock_transaction(self):
        '''
            test_close_stock_transaction
        '''
        stock_db_connection = get_default_db_connection()
        reset_table(stock_db_connection)
        stock_transaction_table = StockTransactionTable(stock_db_connection)

        # init transaction 1
        stock_transaction_1 = StockTransaction()
        stock_transaction_1.symbol = "601398"
        stock_transaction_1.buy_or_sell = StockTransaction.BUY_FLAG
        stock_transaction_1.date = datetime(2016, 5, 15, 0, 0, 0)
        stock_transaction_1.quantity = 200
        stock_transaction_1.price = 4.51
        stock_transaction_table.add_stock_transaction(stock_transaction_1)
        trans_id_1 = stock_transaction_1.trans_id

        # init transaction 2
        stock_transaction_2 = StockTransaction()
        stock_transaction_2.symbol = "601398"
        stock_transaction_2.buy_or_sell = StockTransaction.SELL_FLAG
        stock_transaction_2.date = datetime(2016, 5, 16, 0, 0, 0)
        stock_transaction_2.quantity = 200
        stock_transaction_2.price = 4.81
        stock_transaction_table.add_stock_transaction(stock_transaction_2)
        trans_id_2 = stock_transaction_2.trans_id

        stock_closed_transaction = \
            StockClosedTransactionTable.close_transaction(stock_transaction_1,
                                                          stock_transaction_2)

        self.assertEqual(stock_closed_transaction.symbol, "601398")
        self.assertEqual(stock_closed_transaction.buy_price, 4.51)
        self.assertEqual(stock_closed_transaction.sell_price, 4.81)
        self.assertEqual(stock_closed_transaction.buy_date,
                         datetime(2016, 5, 15, 0, 0, 0))
        self.assertEqual(stock_closed_transaction.sell_date,
                         datetime(2016, 5, 16, 0, 0, 0))
        self.assertEqual(stock_closed_transaction.quantity, 200)

        stock_transaction = \
            stock_transaction_table.get_stock_transaction_by_trans_id(
                trans_id_1)
        self.assertIsNone(stock_transaction,
                          "stock_transaction_1 is not deleted")
        stock_transaction = \
            stock_transaction_table.get_stock_transaction_by_trans_id(
                trans_id_2)
        self.assertIsNone(stock_transaction,
                          "stock_transaction_2 is not deleted")

        return
示例#4
0
def complete_buy_transaction(symbol, price, quantity, conn=None):
    '''
        update DB after buy transaction
    '''
    if conn is None:
        conn = get_default_db_connection()
    session = conn.get_sessionmake()()

    total_amount = price * quantity

    stock_cash = session.query(StockCash).\
                 filter(StockCash.symbol == symbol).\
                 one_or_none()
    if stock_cash is None:
        session.close()
        raise Exception("Save buy transaction failed")

    stock_cash.amount = stock_cash.amount - total_amount

    stock_transaction = StockTransaction()
    stock_transaction.symbol = symbol
    stock_transaction.buy_or_sell = "Buy"
    stock_transaction.quantity = quantity
    stock_transaction.price = price
    stock_transaction.date = datetime.utcnow()

    session.add(stock_transaction)
    session.commit()

    session.close()
    return
示例#5
0
def complete_buy_transaction(symbol, price, quantity, conn=None):
    '''
        update DB after buy transaction
    '''
    if conn is None:
        conn = get_default_db_connection()
    session = conn.get_sessionmake()()

    stock_cash = session.query(StockCash).\
                 filter(StockCash.symbol == symbol).\
                 one_or_none()
    if stock_cash is None:
        session.close()
        raise Exception("Save buy transaction failed")

    LOGGER.debug("Amount before: {0}".format(stock_cash.amount))
    stock_cash.amount = stock_cash.amount - \
                        price * quantity - \
                        buy_fee(symbol, price, quantity)
    LOGGER.debug("Amount after: {0}".format(stock_cash.amount))

    stock_transaction = StockTransaction()
    stock_transaction.symbol = symbol
    stock_transaction.buy_or_sell = "Buy"
    stock_transaction.quantity = quantity
    stock_transaction.price = price
    stock_transaction.date = datetime.utcnow()

    session.add(stock_transaction)
    session.commit()

    session.close()
    return
示例#6
0
def split_transaction(conn=None):
    '''
        split_transaction
    '''
    if conn is None:
        db_connection = get_default_db_connection()
    else:
        db_connection = conn
    session = db_connection.create_session()

    query = session.query(StockTransaction).\
            filter(StockTransaction.quantity > 100)
    transactions = query.all()
    for transaction in transactions:
        quantity = transaction.quantity
        i = 100
        while i <= quantity:
            trans = StockTransaction()
            trans.buy_or_sell = transaction.buy_or_sell
            trans.date = transaction.date
            trans.price = transaction.price
            trans.symbol = transaction.symbol
            trans.quantity = 100
            session.add(trans)
            print("ADD")
            i = i + 100
        session.delete(transaction)
        session.commit()
        break
    session.close()
    return