示例#1
0
    def get_performance_analysis_with_updated_balance(
            self,  # type: HummingbotApplication
    ) -> PerformanceAnalysis:
        performance_analysis = PerformanceAnalysis()
        dedup_set: Set[Tuple[str, str, bool]] = set()

        for market_symbol_pair in self.market_symbol_pairs:
            for is_base in [True, False]:
                for is_starting in [True, False]:
                    market_name = market_symbol_pair.market.name
                    asset_name = market_symbol_pair.base_asset if is_base else market_symbol_pair.quote_asset

                    if len(self.assets) == 0 or len(self.markets) == 0:
                        # Prevent KeyError '***SYMBOL***'
                        amount = self.starting_balances[asset_name][
                            market_name]
                    else:
                        amount = self.starting_balances[asset_name][market_name] if is_starting \
                            else self.balance_snapshot()[asset_name][market_name]
                    amount = float(amount)

                    # Adding this check to prevent assets in the same market to be added multiple times
                    if (market_name, asset_name, is_starting) not in dedup_set:
                        dedup_set.add((market_name, asset_name, is_starting))
                        performance_analysis.add_balances(
                            asset_name, amount, is_base, is_starting)

        return performance_analysis
示例#2
0
    def get_performance_analysis_with_updated_balance(
            self,  # type: HummingbotApplication
    ) -> PerformanceAnalysis:
        performance_analysis = PerformanceAnalysis()

        for market_symbol_pair in self.market_symbol_pairs:
            for is_base in [True, False]:
                for is_starting in [True, False]:
                    market_name = market_symbol_pair.market.name
                    asset_name = market_symbol_pair.base_asset if is_base else market_symbol_pair.quote_asset
                    amount = self.starting_balances[asset_name][market_name] if is_starting \
                        else self.balance_snapshot()[asset_name][market_name]
                    amount = float(amount)
                    performance_analysis.add_balances(asset_name, amount,
                                                      is_base, is_starting)

        return performance_analysis
 def test_basic_one_ex(self):
     """ Test performance analysis on a one exchange balance. """
     performance_analysis = PerformanceAnalysis()
     performance_analysis.add_balances("WETH", 0.5, True, True)
     performance_analysis.add_balances("DAI", 60, False, True)
     performance_analysis.add_balances("WETH", 0.4, True, False)
     performance_analysis.add_balances("DAI", 70, False, False)
     calculated_percent = performance_analysis.compute_profitability(50)
     expected_percent = (((0.4 * 50) + 70)/((0.5 * 50) + 60) - 1) * 100
     self.assertEqual(calculated_percent, expected_percent, "Basic one ex test failed.")
 def test_nan_starting(self):
     """ Test the case where the starting balance is 0. """
     performance_analysis = PerformanceAnalysis()
     performance_analysis.add_balances("WETH", 0, True, True)
     performance_analysis.add_balances("DAI", 0, False, True)
     performance_analysis.add_balances("WETH", 0.3, True, False)
     performance_analysis.add_balances("DAI", 70, False, False)
     calculated_percent = performance_analysis.compute_profitability(50)
     self.assertTrue(math.isnan(calculated_percent), "Starting value of 0 test failed.")
    def test_basic_one_ex(self):
        """ Test performance analysis on a one exchange balance. """
        performance_analysis = PerformanceAnalysis()
        starting_weth = 0.5
        starting_dai = 60
        current_weth = 0.4
        current_dai = 70

        performance_analysis.add_balances("WETH", starting_weth, True, True)
        performance_analysis.add_balances("DAI", starting_dai, False, True)
        performance_analysis.add_balances("WETH", current_weth, True, False)
        performance_analysis.add_balances("DAI", current_dai, False, False)

        calculated_starting_token, calculated_starting_amount = performance_analysis.compute_starting(
            self._price)
        calculated_current_token, calculated_current_amount = performance_analysis.compute_current(
            self._price)
        calculated_delta_token, calculated_delta_amount = performance_analysis.compute_delta(
            self._price)
        calculated_return = performance_analysis.compute_return(self._price)

        expected_starting_amount = (starting_weth * self._price) + starting_dai
        expected_current_amount = (current_weth * self._price) + current_dai
        expected_delta_amount = expected_current_amount - expected_starting_amount
        expected_return = (
            (expected_current_amount / expected_starting_amount) - 1) * 100

        self.assertEqual(
            calculated_starting_token,
            "DAI",
            msg=
            "Basic one exchange test: expected starting token incorrectly determined."
        )
        self.assertAlmostEquals(
            calculated_starting_amount,
            expected_starting_amount,
            msg=
            "Basic one exchange test: expected starting amount incorrectly determined."
        )
        self.assertEqual(
            calculated_current_token,
            "DAI",
            msg=
            "Basic one exchange test: expected current token incorrectly determined."
        )
        self.assertAlmostEquals(
            calculated_current_amount,
            expected_current_amount,
            msg=
            "Basic one exchange test: expected current amount incorrectly determined."
        )
        self.assertEqual(
            calculated_delta_token,
            "DAI",
            msg=
            "Basic one exchange test: expected delta token incorrectly determined."
        )
        self.assertAlmostEquals(
            calculated_delta_amount,
            expected_delta_amount,
            msg=
            "Basic one exchange test: expected delta amount incorrectly determined."
        )
        self.assertAlmostEquals(
            calculated_return,
            expected_return,
            msg="Basic one exchange test: return incorrectly determined.")
    def test_nan_starting(self):
        """ Test the case where the starting balance is 0. """
        performance_analysis = PerformanceAnalysis()
        starting_weth = 0
        starting_dai = 0
        current_weth = 0.3
        current_dai = 70

        performance_analysis.add_balances("WETH", starting_weth, True, True)
        performance_analysis.add_balances("DAI", starting_dai, False, True)
        performance_analysis.add_balances("WETH", current_weth, True, False)
        performance_analysis.add_balances("DAI", current_dai, False, False)

        calculated_starting_token, calculated_starting_amount = performance_analysis.compute_starting(
            self._price)
        calculated_current_token, calculated_current_amount = performance_analysis.compute_current(
            self._price)
        calculated_delta_token, calculated_delta_amount = performance_analysis.compute_delta(
            self._price)
        calculated_return = performance_analysis.compute_return(self._price)

        expected_starting_amount = (starting_weth * self._price) + starting_dai
        expected_current_amount = (current_weth * self._price) + current_dai
        expected_delta_amount = expected_current_amount - expected_starting_amount

        self.assertEqual(
            calculated_starting_token,
            "DAI",
            msg=
            "Starting value of 0 test: expected starting token incorrectly determined."
        )
        self.assertAlmostEquals(
            calculated_starting_amount,
            expected_starting_amount,
            msg=
            "Starting value of 0 test: expected starting amount incorrectly determined."
        )
        self.assertEqual(
            calculated_current_token,
            "DAI",
            msg=
            "Starting value of 0 test: expected current token incorrectly determined."
        )
        self.assertAlmostEquals(
            calculated_current_amount,
            expected_current_amount,
            msg=
            "Starting value of 0 test: expected current amount incorrectly determined."
        )
        self.assertEqual(
            calculated_delta_token,
            "DAI",
            msg=
            "Starting value of 0 test: expected delta token incorrectly determined."
        )
        self.assertAlmostEquals(
            calculated_delta_amount,
            expected_delta_amount,
            msg=
            "Starting value of 0 test: expected delta amount incorrectly determined."
        )
        self.assertTrue(
            math.isnan(calculated_return),
            "Starting value of 0 test: return incorrectly determined.")
    def test_different_tokens_two_ex(self):
        """ Test performance analysis on a two exchange balance with different currencies trading. Note that this test
        will not work as the config file that contains the conversion has not been loaded."""
        performance_analysis = PerformanceAnalysis()
        starting_weth_1 = 0.5
        starting_dai_1 = 60
        starting_eth_2 = 0.7
        starting_usdc_2 = 50
        current_weth_1 = 0.4
        current_dai_1 = 70
        current_eth_2 = 0.3
        current_usdc_2 = 70

        performance_analysis.add_balances("WETH", starting_weth_1, True, True)
        performance_analysis.add_balances("DAI", starting_dai_1, False, True)
        performance_analysis.add_balances("ETH", starting_eth_2, True, True)
        performance_analysis.add_balances("USDC", starting_usdc_2, False, True)
        performance_analysis.add_balances("WETH", current_weth_1, True, False)
        performance_analysis.add_balances("DAI", current_dai_1, False, False)
        performance_analysis.add_balances("ETH", current_eth_2, True, False)
        performance_analysis.add_balances("USDC", current_usdc_2, False, False)

        calculated_starting_token, calculated_starting_amount = performance_analysis.compute_starting(
            self._price)
        calculated_current_token, calculated_current_amount = performance_analysis.compute_current(
            self._price)
        calculated_delta_token, calculated_delta_amount = performance_analysis.compute_delta(
            self._price)
        calculated_return = performance_analysis.compute_return(self._price)

        starting_weth = starting_weth_1 + starting_eth_2
        starting_dai = starting_dai_1 + (starting_usdc_2 * self._usdc_price *
                                         (1 / self._dai_price))
        current_weth = current_weth_1 + current_eth_2
        current_dai = current_dai_1 + (current_usdc_2 * self._usdc_price *
                                       (1 / self._dai_price))

        expected_starting_amount = (starting_weth * self._price) + starting_dai
        expected_current_amount = (current_weth * self._price) + current_dai
        expected_delta_amount = expected_current_amount - expected_starting_amount
        expected_return = (
            (expected_current_amount / expected_starting_amount) - 1) * 100

        self.assertEqual(
            calculated_starting_token,
            "DAI",
            msg=
            "Two exchange test w/ diff tokens: expected starting token incorrectly determined."
        )
        self.assertAlmostEquals(
            calculated_starting_amount,
            expected_starting_amount,
            msg="Two exchange test w/ diff tokens: "
            "expected starting amount incorrectly determined.")
        self.assertEqual(
            calculated_current_token,
            "DAI",
            msg=
            "Two exchange test w/ diff tokens: expected current token incorrectly determined."
        )
        self.assertAlmostEquals(
            calculated_current_amount,
            expected_current_amount,
            msg=
            "Two exchange test w/ diff tokens: expected current amount incorrectly determined."
        )
        self.assertEqual(
            calculated_delta_token,
            "DAI",
            msg=
            "Two exchange test w/ diff tokens: expected delta token incorrectly determined."
        )
        self.assertAlmostEquals(
            calculated_delta_amount,
            expected_delta_amount,
            msg=
            "Two exchange test w/ diff tokens: expected delta amount incorrectly determined."
        )
        self.assertAlmostEquals(
            calculated_return,
            expected_return,
            msg=
            "Two exchange test w/ diff tokens: return incorrectly determined.")
    def test_basic_two_ex(self):
        """ Test performance analysis on a two exchange balance with the same currencies trading in both exchanges. """
        performance_analysis = PerformanceAnalysis()
        starting_weth_1 = 0.5
        starting_dai_1 = 60
        starting_weth_2 = 0.7
        starting_dai_2 = 50
        current_weth_1 = 0.4
        current_dai_1 = 70
        current_weth_2 = 0.3
        current_dai_2 = 70

        performance_analysis.add_balances("WETH", starting_weth_1, True, True)
        performance_analysis.add_balances("DAI", starting_dai_1, False, True)
        performance_analysis.add_balances("WETH", starting_weth_2, True, True)
        performance_analysis.add_balances("DAI", starting_dai_2, False, True)
        performance_analysis.add_balances("WETH", current_weth_1, True, False)
        performance_analysis.add_balances("DAI", current_dai_1, False, False)
        performance_analysis.add_balances("WETH", current_weth_2, True, False)
        performance_analysis.add_balances("DAI", current_dai_2, False, False)

        calculated_starting_token, calculated_starting_amount = performance_analysis.compute_starting(
            self._price)
        calculated_current_token, calculated_current_amount = performance_analysis.compute_current(
            self._price)
        calculated_delta_token, calculated_delta_amount = performance_analysis.compute_delta(
            self._price)
        calculated_return = performance_analysis.compute_return(self._price)

        starting_weth = starting_weth_1 + starting_weth_2
        starting_dai = starting_dai_1 + starting_dai_2
        current_weth = current_weth_1 + current_weth_2
        current_dai = current_dai_1 + current_dai_2

        expected_starting_amount = (starting_weth * self._price) + starting_dai
        expected_current_amount = (current_weth * self._price) + current_dai
        expected_delta_amount = expected_current_amount - expected_starting_amount
        expected_return = (
            (expected_current_amount / expected_starting_amount) - 1) * 100

        self.assertEqual(
            calculated_starting_token,
            "DAI",
            msg=
            "Basic two exchange test: expected starting token incorrectly determined."
        )
        self.assertAlmostEquals(
            calculated_starting_amount,
            expected_starting_amount,
            msg=
            "Basic two exchange test: expected starting amount incorrectly determined."
        )
        self.assertEqual(
            calculated_current_token,
            "DAI",
            msg=
            "Basic two exchange test: expected current token incorrectly determined."
        )
        self.assertAlmostEquals(
            calculated_current_amount,
            expected_current_amount,
            msg=
            "Basic two exchange test: expected current amount incorrectly determined."
        )
        self.assertEqual(
            calculated_delta_token,
            "DAI",
            msg=
            "Basic two exchange test: expected delta token incorrectly determined."
        )
        self.assertAlmostEquals(
            calculated_delta_amount,
            expected_delta_amount,
            msg=
            "Basic two exchange test: expected delta amount incorrectly determined."
        )
        self.assertAlmostEquals(
            calculated_return,
            expected_return,
            msg="Basic two exchange test: return incorrectly determined.")
 def test_different_tokens_two_ex(self):
     """ Test performance analysis on a two exchange balance with different currencies trading. Note that this test
     will not work as the config file that contains the conversion has not been loaded."""
     performance_analysis = PerformanceAnalysis()
     performance_analysis.add_balances("WETH", 0.5, True, True)
     performance_analysis.add_balances("DAI", 60, False, True)
     performance_analysis.add_balances("ETH", 0.7, True, True)
     performance_analysis.add_balances("USD", 50, False, True)
     performance_analysis.add_balances("WETH", 0.4, True, False)
     performance_analysis.add_balances("DAI", 70, False, False)
     performance_analysis.add_balances("ETH", 0.3, True, False)
     performance_analysis.add_balances("USD", 70, False, False)
     calculated_percent = performance_analysis.compute_profitability(50)
     expected_percent = (((0.7 * 50) + 140)/((1.2 * 50) + 110) - 1) * 100
     self.assertAlmostEquals(calculated_percent, expected_percent, msg="Two diff token test failed.", delta=0.1)
 def test_basic_two_ex(self):
     """ Test performance analysis on a two exchange balance with the same currencies trading in both exchanges. """
     performance_analysis = PerformanceAnalysis()
     performance_analysis.add_balances("WETH", 0.5, True, True)
     performance_analysis.add_balances("DAI", 60, False, True)
     performance_analysis.add_balances("WETH", 0.7, True, True)
     performance_analysis.add_balances("DAI", 50, False, True)
     performance_analysis.add_balances("WETH", 0.4, True, False)
     performance_analysis.add_balances("DAI", 70, False, False)
     performance_analysis.add_balances("WETH", 0.3, True, False)
     performance_analysis.add_balances("DAI", 70, False, False)
     calculated_percent = performance_analysis.compute_profitability(50)
     expected_percent = (((0.7 * 50) + 140)/((1.2 * 50) + 110) - 1) * 100
     self.assertEqual(calculated_percent, expected_percent, "Basic one ex test failed.")