Exemple #1
0
 def refresh_list_box(self):
     self.lstboxStockCash.delete(0, END)
     stock_cash_table = StockCashTable()
     stock_cash_list = stock_cash_table.get_all_stock_cash()
     for stock_cash in stock_cash_list:
         self.lstboxStockCash.insert(END, stock_cash.get_symbol())
     return
Exemple #2
0
 def add_stock_cash(self):
     stock_cash = StockCash()
     stock_cash.set_symbol(self.entrySymbol.get())
     stock_cash.set_amount(self.entryAmount.get())
     stock_cash_table = StockCashTable()
     stock_cash_table.add_stock_cash(stock_cash)
     self.set_status("Added")
 def get_cash_pool_text(self):
     result = ""
     stock_cash_table = StockCashTable()
     stock_cash_list = stock_cash_table.get_all_stock_cash()
     for stock_cash in stock_cash_list:
         result = result + "{0}".format(stock_cash) + "\n"
     return result
 def get_cash_value(self):
     stock_cash_table = StockCashTable(self.conn)
     stock_cash = stock_cash_table.get_stock_cash_by_symbol(self.symbol)
     # minus the transaction service fee,
     # the database operation need to decouple from the algorithm
     amount = stock_cash.get_amount() - 20
     return amount
 def update_cash_table(self, symbol, amount_offset):
     stock_cash_table = StockCashTable()
     stock_cash = stock_cash_table.get_stock_cash_by_symbol(symbol)
     new_amount = stock_cash.get_amount() + amount_offset
     stock_cash.set_amount(new_amount)
     stock_cash_table.update_stock_cash(stock_cash)
     return
Exemple #6
0
 def fill_symbol_combobox(self):
     stock_cash_table = StockCashTable()
     stock_cash_list = stock_cash_table.get_all_stock_cash()
     symbol_list = []
     for stock_cash in stock_cash_list:
         symbol_list.append(stock_cash.get_symbol())
     self.cbbSymbol["values"] = symbol_list
     return
Exemple #7
0
    def delete_stock_cash(self):
        index = self.lstboxStockCash.curselection()
        if len(index) == 0:
            return

        symbol = self.lstboxStockCash.get(index[0])

        stock_cash = StockCash(symbol)
        stock_cash_table = StockCashTable()
        stock_cash_table.delete_stock_cash(stock_cash)

        self.refresh_list_box()

        return
Exemple #8
0
    def update_stock_cash(self):
        index = self.lstboxStockCash.curselection()
        if len(index) == 0:
            return
        
        symbol = self.lstboxStockCash.get(index[0])
        amount = float(self.entryAmount.get())
        
        stock_cash = StockCash(symbol, amount)
        stock_cash_table = StockCashTable()
        stock_cash_table.update_stock_cash(stock_cash)

        self.refresh_list_box()
        
        return
 def get_stock_current_value(self, symbol):
     stock_cash_table = StockCashTable()
     stock_cash = stock_cash_table.get_stock_cash_by_symbol(symbol)
     cash_amount = stock_cash.get_amount()
     
     stock_transaction_table = StockTransactionTable()
     stock_transaction_list = \
         stock_transaction_table.get_stock_transaction_list_by_symbol(symbol)
     quantity = 0
     for stock_transaction in stock_transaction_list:
         buy_or_sell = stock_transaction.get_buy_or_sell()
         if (buy_or_sell == "Buy"):
             quantity = quantity + stock_transaction.get_quantity()
         elif (buy_or_sell == "Sell"):
             quantity = quantity - stock_transaction.get_quantity()
         else:
             # Need to raise an error
             return None
     stock_price = 8.09
     
     total = cash_amount + quantity*stock_price
     
     return total
    def test_stock_cash_sanity(self):
        stock_db_connection = get_default_db_connection()
        reset_table(stock_db_connection)
        stock_cash_table = StockCashTable(stock_db_connection)
        stock_cash = StockCash("601398", 1000)
        stock_cash_table.add_stock_cash(stock_cash)

        # test the new created line
        stock_cash = stock_cash_table.get_stock_cash_by_symbol("601398")
        self.assertEqual(stock_cash.get_symbol(), "601398")
        self.assertEqual(stock_cash.get_amount(), 1000)

        # test update stock cash
        stock_cash.set_amount(23.456)
        stock_cash_table.update_stock_cash(stock_cash)
        stock_cash = stock_cash_table.get_stock_cash_by_symbol("601398")
        self.assertEqual(stock_cash.get_amount(), 23.456)

        # test an unavailable line
        stock_cash = stock_cash_table.get_stock_cash_by_symbol("XXXXXX")
        self.assertEqual(stock_cash, None)

        # insert a new line, and test get_all function
        stock_cash = StockCash("601857", 5000)
        stock_cash_table.add_stock_cash(stock_cash)
        stock_cash_list = stock_cash_table.get_all_stock_cash()
        self.assertEqual(len(stock_cash_list), 2)
        stock_cash = stock_cash_list[0]
        self.assertEqual(stock_cash.get_symbol(), "601398")
        self.assertEqual(stock_cash.get_amount(), 23.456)
        stock_cash = stock_cash_list[1]
        self.assertEqual(stock_cash.get_symbol(), "601857")
        self.assertEqual(stock_cash.get_amount(), 5000)

        # delete a line
        stock_cash = StockCash("601398", 0)
        stock_cash_table.delete_stock_cash(stock_cash)
        stock_cash = stock_cash_table.get_stock_cash_by_symbol("601398")
        self.assertEqual(stock_cash, None)
Exemple #11
0
 def get_cash_value(self):
     stock_cash_table = StockCashTable(self.conn)
     stock_cash = stock_cash_table.get_stock_cash_by_symbol(self.symbol)
     amount = stock_cash.get_amount()
     return amount
    def process_stock(self, stock_symbol):
        """
            process the stock
        """
        # get remaining cash for the stock
        stock_cash_table = StockCashTable()
        stock_cash = stock_cash_table.get_stock_cash_by_symbol(stock_symbol)
        # get current price of the stock
        stock_price = self.trade.get_stock_price(stock_symbol)

        print("process stock: " + stock_symbol)
        print("remaining_cash={0}".format(stock_cash.amount))
        print("stock_price={0}".format(stock_price))

        if abs(stock_price) < 0.005:
            return

        stock_price_range_table = StockPriceRangeTable()
        stock_price_range = \
            stock_price_range_table.get_stock_stock_price_range_by_symbol(
                stock_symbol)
        price_low = stock_price_range.get_price_low()
        price_high = stock_price_range.get_price_high()

        simple_algorithm = SimpleAlgorithm(stock_symbol, price_low, price_high,
                                           stock_price)
        simple_algorithm.calculate()

        buy_or_sell = simple_algorithm.get_suggested_buy_or_sell()
        suggested_amount = simple_algorithm.get_suggested_amount()

        result = "Symbol: {0}\nBuy or Sell: {1}\nAmount: {2}".format(
            stock_symbol,
            buy_or_sell,
            suggested_amount)
        print(result)

        amount = int(suggested_amount/100) * 100

        if amount >= 100:
            debug_msg = "stock_symbol: {0}\nbuy_or_sell: {1}\n" + \
                        "amount: {2}\nstock_price: {3}"
            debug_msg = debug_msg.format(
                stock_symbol,
                buy_or_sell,
                amount,
                stock_price)
            self.logger.debug(debug_msg)

            if buy_or_sell == "Buy":
                commission_id = self.trade.buy_stock(
                    stock_symbol,
                    stock_price,
                    amount)
                time.sleep(3)
                commission_state = self.trade.get_commission_state(
                    commission_id)
                if commission_state != "已成":
                    result = self.trade.cancel_commission(commission_id)
                    if result != 1:
                        commission_state = self.trade.get_commission_state(
                            commission_id)
                        if commission_state == "已成":
                            complete_buy_transaction(stock_symbol,
                                                     stock_price,
                                                     amount)
                        else:
                            print("Unknown error in canceling transaction")
                            self.logger.debug(
                                "Unknown error in canceling transaction.")
                else:
                    complete_buy_transaction(stock_symbol,
                                             stock_price,
                                             amount)
            elif buy_or_sell == "Sell":
                lowest_buy_price = StockTransaction.\
                                   get_lowest_buy_price(stock_symbol)
                lowest_buy_price_quantity = StockTransaction.\
                                            get_lowest_buy_price_quantity(
                                                stock_symbol)
                lowest_gain = get_lowest_gain(stock_symbol)
                if lowest_gain is None:
                    lowest_gain = 0.3
                if stock_price - lowest_buy_price < lowest_gain:
                    debug_msg = "stock_price is not high enough. {0} vs {1}"
                    debug_msg = debug_msg.format(
                        stock_price,
                        lowest_buy_price)
                    self.logger.debug(debug_msg)
                    return
                if amount > lowest_buy_price_quantity:
                    amount = lowest_buy_price_quantity
                commission_id = self.trade.sell_stock(
                    stock_symbol,
                    stock_price,
                    amount)
                time.sleep(3)
                commission_state = self.trade.get_commission_state(
                    commission_id)
                if commission_state != "已成":
                    result = self.trade.cancel_commission(commission_id)
                    if result != 1:
                        commission_state = self.trade.get_commission_state(
                            commission_id)
                        if commission_state == "已成":
                            complete_sell_transaction(stock_symbol,
                                                      stock_price,
                                                      amount)
                        else:
                            print("Unknown error in canceling transaction")
                            self.logger.debug(
                                "Unknown error in canceling transaction.")
                else:
                    complete_sell_transaction(stock_symbol,
                                              stock_price,
                                              amount)
            else:
                print("Error!")

        return
    def process_stock(self, stock_symbol):
        # get remaining cash for the stock
        stock_cash_table = StockCashTable()
        stock_cash = stock_cash_table.get_stock_cash_by_symbol(stock_symbol)
        # get current price of the stock
        stock_price = self.trade.get_stock_price(stock_symbol)
        
        print("process stock: " + stock_symbol)
        print("remaining_cash={0}".format(stock_cash.amount))
        print("stock_price={0}".format(stock_price))
        
        if(abs(stock_price) < 0.005):
            return
        
        stock_price_range_table = StockPriceRangeTable()
        stock_price_range = stock_price_range_table.get_stock_stock_price_range_by_symbol(stock_symbol)
        price_low = stock_price_range.get_price_low()
        price_high = stock_price_range.get_price_high()
                
        simple_algorithm = SimpleAlgorithm(stock_symbol, price_low, price_high,
                                           stock_price)
        simple_algorithm.calculate()

        buy_or_sell = simple_algorithm.get_suggested_buy_or_sell()
        suggested_amount = simple_algorithm.get_suggested_amount()

        result = "Symbol: {0}\nBuy or Sell: {1}\nAmount: {2}".format(stock_symbol, buy_or_sell,
                                                        suggested_amount)
        print(result)
        
        amount = int(suggested_amount/100) * 100
        
        # we suppost we need 10 for transaction service fee, which is a big enough number
        # for normal transaction
        if (buy_or_sell == "Buy"):
            cash_offset = -1 * (amount * stock_price + 10)
        else:
            cash_offset = amount * stock_price - 10
             
        if (amount >= 100):
            debug_msg = "stock_symbol: {0}\nbuy_or_sell: {1}\namount: {2}\nstock_price: {3}".format(stock_symbol,
                                                                                                    buy_or_sell,
                                                                                                    amount,
                                                                                                    stock_price)
            self.logger.debug(debug_msg)
            debug_msg = "cash_offset: {0}".format(cash_offset)
            self.logger.debug(debug_msg)
            
            
            if (buy_or_sell == "Buy"):
                commission_id = self.trade.buy_stock(stock_symbol, stock_price, amount)
                time.sleep(3)
                commission_state = self.trade.get_commission_state(commission_id)
                if (commission_state != "已成"):
                    result = self.trade.cancel_commission(commission_id)
                    if (result != 1):
                        commission_state = self.trade.get_commission_state(commission_id)
                        if (commission_state == "已成"):
                            self.add_transaction(stock_symbol, buy_or_sell, amount, stock_price)
                            self.update_cash_table(stock_symbol, cash_offset)
                        else:
                            print("Unknown error in canceling transaction")
                            self.logger.debug("Unknown error in canceling transaction.")
                else:
                    self.add_transaction(stock_symbol, buy_or_sell, amount, stock_price)
                    self.update_cash_table(stock_symbol, cash_offset)
            elif (buy_or_sell == "Sell"):
                commission_id = self.trade.sell_stock(stock_symbol, stock_price, amount)
                time.sleep(3)
                commission_state = self.trade.get_commission_state(commission_id)
                if (commission_state != "已成"):
                    result = self.trade.cancel_commission(commission_id)
                    if (result != 1):
                        commission_state = self.trade.get_commission_state(commission_id)
                        if (commission_state == "已成"):
                            self.add_transaction(stock_symbol, buy_or_sell, amount, stock_price)
                            self.update_cash_table(stock_symbol, cash_offset)
                        else:
                            print("Unknown error in canceling transaction")
                            self.logger.debug("Unknown error in canceling transaction.")
                else:
                    self.add_transaction(stock_symbol, buy_or_sell, amount, stock_price)
                    self.update_cash_table(stock_symbol, cash_offset)
            else:
                print("Error!")
             

        return