Beispiel #1
0
 def test_delete_transaction(self):
     self.testDatabase.create_new_stock("testdelete")
     self.testDatabase.save_transaction(
         Transaction(
             "testdelete",
             datetime.strptime("2020-5-20 0:0:0", "%Y-%m-%d %H:%M:%S"),
             BuyTransaction(), 12, 5))
     self.testDatabase.delete_transactions(
         Transaction(
             "testdelete",
             datetime.strptime("2020-5-20 0:0:0", "%Y-%m-%d %H:%M:%S"),
             BuyTransaction(), 12, 5))
     with self.assertRaises(nr):
         item = self.testDatabase.get_transactions("testdelete")
Beispiel #2
0
    def calculate_current_shares(self):
        """
        Calculates current owned shares
        :return: Integer of number of shares of stock owned
        """
        if len(self.transactions) == 0:
            return 0
        total = 0

        buyType = BuyTransaction()
        sellType = SellTransaction()

        for x in self.transactions:
            if x.get_text_of_tran_type() == buyType.tran_type():
                total += x.number_of_shares
            if x.get_text_of_tran_type() == sellType.tran_type():
                total -= x.number_of_shares
        return total
Beispiel #3
0
 def test_get_transaction(self):
     self.testDatabase.create_new_stock("testtran")
     self.testDatabase.save_transaction(
         Transaction(
             "testtran",
             datetime.strptime("2020-5-20 0:0:0", "%Y-%m-%d %H:%M:%S"),
             BuyTransaction(), 12, 5))
     item = self.testDatabase.get_transactions("testtran")
     self.assertEqual(item[0].symbol, "testtran")
Beispiel #4
0
 def test_delete_individual_transaction_using_primary_key(self):
     self.testDatabase.create_new_stock("testpk")
     self.testDatabase.save_transaction(
         Transaction(
             "testpk",
             datetime.strptime("2020-5-20 0:0:0", "%Y-%m-%d %H:%M:%S"),
             BuyTransaction(), 12, 5))
     item = self.testDatabase.get_transactions("testpk")
     self.testDatabase.delete_individual_transaction_using_primary_key(
         item[0].tran_id)
     with self.assertRaises(nr):
         self.testDatabase.get_transactions("testpk")
Beispiel #5
0
    def test_calculate_buy_and_price(self):
        tranItem1 = Transaction("Google", datetime.now(), self.buy, 20.00, 20)
        tranItem2 = Transaction("IBM", datetime.now(), self.buy, 80.00, 20)
        tranItem3 = Transaction("FaceBook", datetime.now(), self.sell, 10.00, 10)
        tranItem4 = Transaction("Apple", datetime.now(), self.sell, 40.00, 10)

        listOfTrans = []
        listOfTrans.append(tranItem1)
        listOfTrans.append(tranItem2)
        listOfTrans.append(tranItem3)
        listOfTrans.append(tranItem4)

        testObj = Stock("Test", listOfTrans)

        self.assertEqual(testObj.calculate_buy_or_sell_total_price(BuyTransaction()), 100)
        self.assertEqual(testObj.calculate_buy_or_sell_total_price(SellTransaction()), 50)
Beispiel #6
0
    def process_transaction_entry(self):
        """
        Processes a transaction entry. Method called by button on window
        :return: None
        """
        action_object = None
        stock_name = self.enter_transaction_stock_radio_value.get()
        price = self.enter_transaction_window_price_entry.get()
        stock_quanity = self.enter_transaction_window_share_entry.get()
        date_to_save = datetime.strptime(self.enter_transaction_window_date_entry.get(), "%m-%d-%Y")

        if self.action_for_transaction_stringVar_object.get() == 'buy':
            action_object = BuyTransaction()
        else:
            action_object = SellTransaction()

        transactions_to_save = Transaction(stock_name, date_to_save, action_object, price, stock_quanity)
        database = Database()
        database.save_transaction(transactions_to_save)
        self.enter_transaction_window_object.destroy()
        self.adjust_status("New Transaction Added")
Beispiel #7
0
 def get_transactions(self, stock):
     """
     Returns a list of all transactions for a given stock
     :param stock: String of stock ticker symbol
     :return: All transactions for a given stock in a list
     """
     if not isinstance(stock, str):
         raise ValueError("Invalid input parameter. Must be string")
     try:
         stock_id_to_lookup = self.get_stock_id(stock)
         sql_statement = """select p.stock, transactions.id, date, number_of_shares, price_per_share,
                         transaction_type from transactions  inner join portfolio p on p.id = transactions.stock_id 
                         where stock_id = ?"""
         argument_list = []
         argument_list.append(stock_id_to_lookup)
         conn = self.__init_connection()
         dataset = conn.cursor()
         dataset.execute(sql_statement, argument_list)
         rows = dataset.fetchall()
         if len(rows) == 0:
             raise NoResultsException.NoResultsException
         output = []
         for x in rows:
             #time_in_datetime = datetime.datetime.strptime(x[2], "%Y-%m-%d %H:%M:%S.%f")
             #time_in_datetime = datetime.datetime.strptime(x[2], "%Y-%m-%d")
             time_in_datetime = datetime.datetime.strptime(
                 x[2], "%Y-%m-%d %H:%M:%S")
             if x[5] == SellTransaction().tran_type():
                 t_type = SellTransaction()
             else:
                 t_type = BuyTransaction()
             tran_item = Transaction(x[0], time_in_datetime, t_type, x[4],
                                     x[3])
             tran_item.tran_id = x[1]
             output.append(tran_item)
         return output
     except Error:
         print(Error)
     finally:
         conn.close()
Beispiel #8
0
 def setUp(self):
     self.testDatabase = Database()
     self.testTransaction = Transaction("test", datetime.now(),
                                        BuyTransaction(), 35.00, 10)
Beispiel #9
0
    def profit_loss_window(self):
        """
        Generates profit/loss data and window
        :return: None
        """
        def get_data():
            """
            Gets data from profit loss window
            :return:
            """
            database = Database()
            complete_list_of_stocks = []
            list_of_bare_stocks = database.get_all_stocks()
            ready_for_display_stocks = []

            for x in list_of_bare_stocks:
                item = database.get_stock(x.symbol)
                complete_list_of_stocks.append(item)

            api = Api()

            for z in complete_list_of_stocks:
                item = api.get_stock_quote(z)
                ready_for_display_stocks.append(item)
            return ready_for_display_stocks

        display_stocks = get_data()
        root = Toplevel()
        label_frame = Frame(root)
        data_frame = Frame(root)
        button_frame = Frame(root)

        label_frame.grid(row=0)
        data_frame.grid(row=1)
        button_frame.grid(row=2)

        label = Label(label_frame, text='Stock   Total Buy Price     Total Sell Price   current holding  Profit')

        exit_button = Button(button_frame, text='Exit', command=root.destroy)

        data = Listbox(data_frame, width=50, height=25)
        data.grid_propagate(0)
        label.grid(row=0)
        data.grid(row=0)
        exit_button.grid(row=0)

        if len(display_stocks) == 0:
            data.insert(END, "No stocks saved")
        else:
            insert_string = ""
            total_buy = 0
            total_Sell = 0
            for x in display_stocks:
                insert_string += x.symbol
                insert_string += "   "
                insert_string += "$" + str(x.calculate_buy_or_sell_total_price(BuyTransaction()))
                total_buy = x.calculate_buy_or_sell_total_price(BuyTransaction())
                insert_string += "   "
                insert_string += "  " + "$" + str(x.calculate_buy_or_sell_total_price(SellTransaction()))
                total_Sell = x.calculate_buy_or_sell_total_price(SellTransaction())
                insert_string += "   "
                insert_string += "  " + "$" + str(x.last_price * x.calculate_current_shares())
                total_Sell += x.last_price * x.calculate_current_shares()

                insert_string += "   " + "$" + str(total_Sell - total_buy)
                data.insert(END, insert_string)
                insert_string = ""
                total_Sell = 0
                total_buy = 0
        root.mainloop()
Beispiel #10
0
 def test_zero_calculate_buy_or_sell_price(self):
     testObj = Stock("test")
     self.assertEqual(testObj.calculate_buy_or_sell_total_price(BuyTransaction()), 0)
     self.assertEqual(testObj.calculate_buy_or_sell_total_price(SellTransaction()), 0)
Beispiel #11
0
 def setUp(self):
     self.now = datetime.now()
     self.buy = BuyTransaction()
     self.sell = SellTransaction()
     self.testTran = Transaction("IBM", self.now, self.buy, 115.00, 10)
Beispiel #12
0
 def test_invalid_price(self):
     with self.assertRaises(ValueError):
         Transaction("test", datetime.now(), BuyTransaction(), "invalid",
                     10)
Beispiel #13
0
 def test_invalid_shares(self):
     with self.assertRaises(ValueError):
         Transaction("Test", datetime.now(), BuyTransaction(), 25.36,
                     "Invalid")
Beispiel #14
0
 def test_invalid_date(self):
     with self.assertRaises(ValueError):
         Transaction("test", "invalid", BuyTransaction(), 36.01, 25)
Beispiel #15
0
 def test_get_text_of_tran_type(self):
     self.assertEqual(self.test.get_text_of_tran_type(),
                      BuyTransaction().tran_type())
Beispiel #16
0
 def setUp(self):
     self.test = Transaction("test", datetime.now(), BuyTransaction(),
                             35.00, 10)