Example #1
0
    def process_noble_metal(self, noble_metal_name):
        '''
            process the noble metal
        '''

        print("process {0}".format(noble_metal_name))

        try:
            self.trade.main_page()
            self.trade.select_noble_metal()
        except StaleElementReferenceException as stale_exception:
            error_msg = "select noble metal error. " + \
                        "StaleElementReferenceException{0}"
            self.logger.error(error_msg)
            print(error_msg.format(stale_exception))
            self.error_counter += 1
            if self.error_counter > 10:
                raise NobleMetalProcessorException("Stale Element Exception")
            time.sleep(10)
            return
        except WebDriverException as web_driver_exception:
            error_msg = "select noble metal error." + \
                        "WebDriverException{0}"
            error_msg = error_msg.format(web_driver_exception)
            self.logger.error(error_msg)
            print(error_msg)
            self.error_counter += 1
            if self.error_counter > 10:
                raise NobleMetalProcessorException("WebDriver Exception")
            time.sleep(10)
            return
        except Exception as exc:
            error_msg = "Exception {0}"
            error_msg = error_msg.format(exc)
            self.logger.error(error_msg)
            print(error_msg)
            self.error_counter += 1
            if self.error_counter > 10:
                raise NobleMetalProcessorException("Unknown Exception")
            time.sleep(10)
            return

        # reset counter
        self.error_counter = 0

        try:
            noble_metal_price = self.trade.\
                                get_noble_metal_price(noble_metal_name)
            print("price: {0}".format(noble_metal_price))
        except Exception as exception:
            self.logger.debug("get price exception")
            print("get price exception: {0}".format(exception))
            return

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

        # buy
        buy_price = noble_metal_price.selling_price
        algorithm = SimpleAlgorithm(symbol=noble_metal_name,
                                    start_price=price_low,
                                    stop_price=price_high,
                                    current_price=buy_price)

        algorithm.calculate()

        buy_or_sell = algorithm.get_suggested_buy_or_sell()
        print("Buy_or_sell: {0}".format(buy_or_sell))
        if (buy_or_sell is not None) and (buy_or_sell == "Buy"):
            suggested_amount = algorithm.get_suggested_amount()
            print("buy {0}, price: {1}, suggested_amount: {2}".\
                  format(noble_metal_name, buy_price, suggested_amount))

            stock_lowest_unit_table = StockLowestUnitTable()
            stock_lowest_unit = stock_lowest_unit_table.\
                                get_lowest_unit(noble_metal_name)
            if stock_lowest_unit is not None:
                lowest_unit = stock_lowest_unit.lowest_unit
                is_integer = stock_lowest_unit.is_integer
            else:
                lowest_unit = 1
                is_integer = True

            if is_integer:
                amount = int(suggested_amount / lowest_unit) * int(lowest_unit)
            else:
                amount = int(suggested_amount / lowest_unit) * lowest_unit

            print(amount)
            if amount < lowest_unit:
                return

            result = self.trade.buy_noble_metal(noble_metal_name, amount,
                                                buy_price)
            print(result)
            if result:
                print("update db")
                complete_buy_transaction(noble_metal_name, buy_price, amount)
            return

        # sell
        sell_price = noble_metal_price.buying_price
        algorithm = SimpleAlgorithm(symbol=noble_metal_name,
                                    start_price=price_low,
                                    stop_price=price_high,
                                    current_price=sell_price)

        algorithm.calculate()

        buy_or_sell = algorithm.get_suggested_buy_or_sell()
        print("Buy_or_sell: {0}".format(buy_or_sell))
        if (buy_or_sell is not None) and (buy_or_sell == "Sell"):
            lowest_price = StockTransaction.\
                           get_lowest_buy_price(noble_metal_name)
            lowest_gain = get_lowest_gain(noble_metal_name)
            if lowest_gain is None:
                lowest_gain = 0.04
            print(lowest_gain)
            if sell_price - lowest_price < lowest_gain:
                return
            suggested_amount = algorithm.get_suggested_amount()

            stock_lowest_unit_table = StockLowestUnitTable()
            stock_lowest_unit = stock_lowest_unit_table.\
                                get_lowest_unit(noble_metal_name)
            if stock_lowest_unit is not None:
                lowest_unit = stock_lowest_unit.lowest_unit
                is_integer = stock_lowest_unit.is_integer
            else:
                lowest_unit = 1
                is_integer = True
            if is_integer:
                amount = int(suggested_amount / lowest_unit) * int(lowest_unit)
            else:
                amount = int(suggested_amount / lowest_unit) * lowest_unit

            print(suggested_amount)
            print(amount)
            quantity = StockTransaction.\
                       get_lowest_buy_price_quantity(noble_metal_name)
            if amount < lowest_unit:
                return
            if amount >= quantity:
                amount = quantity

            print("sell {0}, price: {1}, amount: {2}".\
                  format(noble_metal_name, sell_price, amount))
            result = self.trade.sell_noble_metal(noble_metal_name, amount,
                                                 sell_price)
            print(result)
            if result:
                complete_sell_transaction(noble_metal_name, sell_price, amount)
            return

        return
Example #2
0
    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
Example #3
0
    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