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
def test_owned_transaction(self): ''' test_owned_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 owned_quantity = StockTransaction.get_owned_quantity("601398") self.assertEqual(owned_quantity, 100) return
def get_stock_value(self): stock_value = StockTransaction.get_owned_quantity(self.symbol) * \ self.current_price return stock_value