Beispiel #1
0
    def print_gains(self, format_str, gain, timespan):
        positive_gain = gain >= 0
        gain_symbol = "+" if positive_gain else "-"
        gain_verboge = "Gained" if positive_gain else "Lost"

        print("{:25}".format("Value " + gain_verboge + " " + timespan + ": "),
              end="")
        print(Fore.GREEN if positive_gain else Fore.RED, end="")

        # This prevents a runtime warning by making sure we are never dividing by zero
        if self.portfolio.cost_value == 0:
            gain_val = 0
        else:
            gain_val = gain / self.portfolio.cost_value * 100
        print(
            format_str.format(gain_symbol + "$" + str(
                abs(utils.round_value(gain, self.mode, 2)))) +
            format_str.format(gain_symbol + str(
                abs(utils.round_value(gain_val, self.mode, 2))) + "%"))
        print(Style.RESET_ALL, end="")
        return
Beispiel #2
0
    def print_table(self, config):
        # table format:
        #   ticker    owned   last    change  change% low high    avg
        # each row will also get a bonus boolean at the end denoting what color to print the line:
        #   None = don't color (headers)
        #   True = green
        #   False = red
        # additional things to print: portfolio total value, portfolio change (and change %)
        mode = config["General"]["rounding_mode"]
        cell_width = 11  # buffer space between columns
        table = [
            [
                "Ticker",
                "Last",
                "Change",
                "Change%",
                "Low",
                "High",
                "Avg",
                "Owned",
                "Aggregate Value",
                None,
            ]
        ]
        table.append(
            ["-" * cell_width for _ in range(len(table[0]))]
        )  # this is the solid line under the header
        table[-1].append(None)  # make sure that solid line is not colored
        self.current_value = 0
        self.opening_value = 0
        for stock in self.stocks:
            line = []
            change_d = utils.round_value(
                stock.get_curr() - stock.get_open(), mode, 2
            )  # change
            change_p = utils.round_value(
                (stock.get_curr() - stock.get_open()) / stock.get_curr() * 100, mode, 2
            )  # change %
            line.append(stock.symbol)  # symbol
            line.append(
                "$" + str(utils.round_value(stock.get_curr(), mode, 2))
            )  # current value
            if change_d >= 0:  # insert the changes into the array
                line.append("+$" + str(change_d))
                line.append("+" + str(change_p) + "%")
            else:
                line.append(
                    "-$" + str(change_d)[1:]
                )  # string stripping here is to remove the native '-' sign
                line.append("-" + str(change_p)[1:] + "%")
            line.append(
                "$" + str(utils.round_value(min(stock.get_data()), mode, 2))
            )  # low
            line.append(
                "$" + str(utils.round_value(max(stock.get_data()), mode, 2))
            )  # high
            line.append(
                "$"
                + str(
                    utils.round_value(
                        sum(stock.get_data()) / len(stock.get_data()), mode, 2
                    )
                )
            )  # avg
            line.append(
                str(round(self.stocks_metadata[stock.symbol][0], 3))
            )  # number of stocks owned
            line.append(
                "$"
                + str(
                    utils.round_value(
                        stock.calc_value(self.stocks_metadata[stock.symbol][0]), mode, 2
                    )
                )
            )
            line.append(True if change_d >= 0 else False)
            table.append(line)

            # just add in the total value seeing as we're iterating stocks anyways
            self.current_value += stock.calc_value(
                self.stocks_metadata[stock.symbol][0]
            )
            # and the opening value of all the tracked stocks
            self.opening_value += (
                stock.get_open() * self.stocks_metadata[stock.symbol][0]
            )

        print("\nPortfolio Summary:\n")
        format_str = "{:" + str(cell_width) + "}"
        for line in table:
            if line[-1] is None:
                pass
            elif line[-1]:
                print(Fore.GREEN, end="")
            else:
                print(Fore.RED, end="")
            print("\t" + "".join([format_str.format(item) for item in line[:-1]]))
            print(Style.RESET_ALL, end="")
        print(
            "\n"
            + "{:25}".format("Total Value: ")
            + format_str.format("$" + str(round(self.current_value, 2)))
        )
        value_gained_day = self.current_value - self.opening_value
        if value_gained_day >= 0:
            print("{:25}".format("Value Gained Today: "), end="")
            print(Fore.GREEN, end="")
            print(
                format_str.format(
                    "+$" + str(utils.round_value(value_gained_day, mode, 2))
                )
                + format_str.format(
                    "+"
                    + str(
                        utils.round_value(
                            value_gained_day / self.current_value * 100, mode, 2
                        )
                    )
                    + "%"
                )
            )
        else:
            print("{:25}".format("Value Gained Today: "), end="")
            print(Fore.RED, end="")
            print(
                format_str.format(
                    "-$" + str(utils.round_value(value_gained_day, mode, 2))[1:]
                )
                + format_str.format(
                    str(
                        utils.round_value(
                            value_gained_day / self.current_value * 100, mode, 2
                        )
                    )
                    + "%"
                )
            )
        print(Style.RESET_ALL, end="")

        value_gained_all = self.current_value - self.initial_value
        if value_gained_all >= 0:
            print("{:25}".format("Value Gained Overall: "), end="")
            print(Fore.GREEN, end="")
            print(
                format_str.format(
                    "+$" + str(utils.round_value(value_gained_all, mode, 2))
                )
                + format_str.format(
                    "+"
                    + str(
                        utils.round_value(
                            value_gained_all / self.current_value * 100, mode, 2
                        )
                    )
                    + "%"
                )
            )
        else:
            print("{:25}".format("Value Gained Overall: "), end="")
            print(Fore.RED, end="")
            print(
                format_str.format(
                    "-$" + str(utils.round_value(value_gained_all, mode, 2))[1:]
                )
                + format_str.format(
                    str(
                        utils.round_value(
                            value_gained_all / self.current_value * 100, mode, 2
                        )
                    )
                    + "%"
                )
            )
        print(Style.RESET_ALL, end="")
Beispiel #3
0
def format_number(value) -> str:
    return str(abs(utils.round_value(value, "math", 2)))
 def test_zero_decimals_down(self):
     assert round_value(1.456543, "down", 0) == 1
 def test_negative_decimals(self):
     assert round_value(1.456543, "math", -3) == 0
 def test_zero_decimals_math(self):
     assert round_value(1.456543, "math", 0) == 1
 def test_invalid_decimals(self):
     with pytest.raises(TypeError):
         round_value(1.456543, "math", "Hello World")
 def test_nan(self):
     assert round_value(float("NaN"), "math", 2) == 0
 def test_zero(self):
     assert round_value(0, "math", 2) == 0
Beispiel #10
0
 def test_down(self):
     assert round_value(1.456543, "down", 2) == 1.45
Beispiel #11
0
 def test_math(self):
     assert round_value(1.456543, "math", 2) == 1.46