Example #1
0
    def calculate(self):
        print("expected percentage: {0}".\
              format(self.get_expected_percentage()))
        expected_quantity = self.get_expected_percentage() * \
                            self.get_total_value() / 100 / \
                            self.current_price
        owned_quantity = StockTransaction.get_owned_quantity(self.symbol)
        print("expected quantity: \t{0}".format(expected_quantity))
        print("owned quantity: \t{0}".format(owned_quantity))
        lowest_buy_price = StockTransaction.get_lowest_buy_price(self.symbol)

        if expected_quantity > owned_quantity:
            buy_quantity = expected_quantity - owned_quantity
            self.suggested_buy_or_sell = "Buy"
            self.suggested_amount = buy_quantity
        elif expected_quantity == owned_quantity:
            self.suggested_buy_or_sell = None
        elif expected_quantity < owned_quantity:
            # expected_quantity < owned_quantity
            sell_quantity = owned_quantity - expected_quantity
            if self.current_price <= lowest_buy_price:
                self.suggested_buy_or_sell = None
            else:
                self.suggested_buy_or_sell = "Sell"
                self.suggested_amount = sell_quantity
        else:
            self.suggested_buy_or_sell = None
        return
Example #2
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
Example #3
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
Example #4
0
    def test_lowest_buy_price_quantity2(self):
        '''
            test lowest buy price quantity
        '''
        stock_db_connection = get_default_db_connection()
        reset_table(stock_db_connection)
        StockTransactionTable(stock_db_connection)

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

        return
Example #5
0
    def test_lowest_buy_price2(self):
        '''
            test lowest buy price
        '''
        stock_db_connection = get_default_db_connection()
        reset_table(stock_db_connection)
        StockTransactionTable(stock_db_connection)

        lowest_price = StockTransaction.get_lowest_buy_price("601398")
        self.assertEqual(lowest_price, 9999.00)

        return
Example #6
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
    def test_stock_transaction_sanity(self):
        stock_db_connection = get_default_db_connection()
        reset_table(stock_db_connection)
        stock_transaction_table = StockTransactionTable(stock_db_connection)
        stock_transaction = StockTransaction()
        stock_transaction.set_symbol("601398")
        stock_transaction.set_buy_or_sell("buy")
        stock_transaction.set_quantity(100)
        stock_transaction.set_price(4.51)
        stock_transaction.set_date(date(2015, 11, 10))
        stock_transaction_table.add_stock_transaction(stock_transaction)

        stock_transaction = StockTransaction()
        stock_transaction.set_symbol("601857")
        stock_transaction.set_buy_or_sell("buy")
        stock_transaction.set_quantity(100)
        stock_transaction.set_price(4.51)
        stock_transaction.set_date(date(2015, 11, 10))
        stock_transaction_table.add_stock_transaction(stock_transaction)

        stock_transaction = \
            stock_transaction_table.get_stock_transaction_by_trans_id(1)
        stock_transaction.set_quantity("500")
        stock_transaction_table.update_stock_transaction(stock_transaction)

        stock_transaction = \
            stock_transaction_table.get_stock_transaction_by_trans_id(1)
        stock_transaction_table.delete_stock_transaction(stock_transaction)

        return
Example #8
0
 def get_stock_value(self):
     stock_value = StockTransaction.get_owned_quantity(self.symbol) * \
                   self.current_price
     return stock_value
Example #9
0
 def add_transaction(self, symbol, buy_or_sell, quantity, price):
     stock_transaction = StockTransaction()
     stock_transaction.set_symbol(symbol)
     stock_transaction.set_buy_or_sell(buy_or_sell)
     stock_transaction.set_quantity(quantity)
     stock_transaction.set_price(price)
     stock_transaction.set_date(datetime.datetime.now())
     
     stock_transaction_table = StockTransactionTable()
     stock_transaction_table.add_stock_transaction(stock_transaction)
     return
Example #10
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
Example #11
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
Example #12
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
Example #13
0
    def add_transaction(self):
        symbol = self.cbbSymbol.get()
        if (not symbol):
            self.set_status("Symbol is empty.")
            return
        buy_or_sell = self.cbbBuyOrSell.get()
        if (not buy_or_sell):
            self.set_status("Buy/Sell is empty.")
            return
        quantity = self.entryQuantity.get()
        if (not quantity):
            self.set_status("Quantity is empty.")
            return
        price = self.entryPrice.get()
        if (not price):
            self.set_status("Price is empty.")
            return
#         date = self.entryDate.get()
#         if (not date):
#             self.set_status("Date is empty.")
#             return

        stock_transaction = StockTransaction()
        stock_transaction.set_symbol(symbol)
        stock_transaction.set_buy_or_sell(buy_or_sell)
        stock_transaction.set_quantity(quantity)
        stock_transaction.set_price(price)
#        stock_transaction.set_date(date)
        stock_transaction.set_date(datetime.datetime.now())
        
        stock_transaction_table = StockTransactionTable()
        stock_transaction_table.add_stock_transaction(stock_transaction)
        
        self.set_status("Added")
        return