From 202b1d1f0bef02ad6f373babb9d995d774a1ab98 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 21 Oct 2018 09:21:32 +0200 Subject: [PATCH 1/2] fix #1289 - we should not modify decimal context --- freqtrade/persistence.py | 6 +---- freqtrade/tests/optimize/test_backtesting.py | 2 +- freqtrade/tests/rpc/test_rpc_telegram.py | 6 ++--- freqtrade/tests/test_freqtradebot.py | 10 ++++---- freqtrade/tests/test_persistence.py | 26 ++++++++++---------- 5 files changed, 23 insertions(+), 27 deletions(-) diff --git a/freqtrade/persistence.py b/freqtrade/persistence.py index c26d74015..02267ac21 100644 --- a/freqtrade/persistence.py +++ b/freqtrade/persistence.py @@ -4,7 +4,7 @@ This module contains the class to persist trades into SQLite import logging from datetime import datetime -from decimal import Decimal, getcontext +from decimal import Decimal from typing import Any, Dict, Optional import arrow @@ -241,7 +241,6 @@ class Trade(_DECL_BASE): logger.info('Updating trade (id=%d) ...', self.id) - getcontext().prec = 8 # Bittrex do not go above 8 decimal if order_type == 'limit' and order['side'] == 'buy': # Update open rate and actual amount self.open_rate = Decimal(order['price']) @@ -278,7 +277,6 @@ class Trade(_DECL_BASE): If rate is not set self.fee will be used :return: Price in BTC of the open trade """ - getcontext().prec = 8 buy_trade = (Decimal(self.amount) * Decimal(self.open_rate)) fees = buy_trade * Decimal(fee or self.fee_open) @@ -296,7 +294,6 @@ class Trade(_DECL_BASE): If rate is not set self.close_rate will be used :return: Price in BTC of the open trade """ - getcontext().prec = 8 if rate is None and not self.close_rate: return 0.0 @@ -336,7 +333,6 @@ class Trade(_DECL_BASE): :param fee: fee to use on the close rate (optional). :return: profit in percentage as float """ - getcontext().prec = 8 open_trade_price = self.calc_open_trade_price() close_trade_price = self.calc_close_trade_price( diff --git a/freqtrade/tests/optimize/test_backtesting.py b/freqtrade/tests/optimize/test_backtesting.py index a17867b3a..3ea0f240c 100644 --- a/freqtrade/tests/optimize/test_backtesting.py +++ b/freqtrade/tests/optimize/test_backtesting.py @@ -534,7 +534,7 @@ def test_backtest(default_conf, fee, mocker) -> None: expected = pd.DataFrame( {'pair': [pair, pair], - 'profit_percent': [0.00029975, 0.00056708], + 'profit_percent': [0.00029977, 0.00056716], 'profit_abs': [1.49e-06, 7.6e-07], 'open_time': [Arrow(2018, 1, 29, 18, 40, 0).datetime, Arrow(2018, 1, 30, 3, 30, 0).datetime], diff --git a/freqtrade/tests/rpc/test_rpc_telegram.py b/freqtrade/tests/rpc/test_rpc_telegram.py index 708ed4478..45e01aa57 100644 --- a/freqtrade/tests/rpc/test_rpc_telegram.py +++ b/freqtrade/tests/rpc/test_rpc_telegram.py @@ -725,7 +725,7 @@ def test_forcesell_handle(default_conf, update, ticker, fee, 'open_rate': 1.099e-05, 'current_rate': 1.172e-05, 'profit_amount': 6.126e-05, - 'profit_percent': 0.06110514, + 'profit_percent': 0.0611052, 'stake_currency': 'BTC', 'fiat_currency': 'USD', } == last_msg @@ -778,7 +778,7 @@ def test_forcesell_down_handle(default_conf, update, ticker, fee, 'open_rate': 1.099e-05, 'current_rate': 1.044e-05, 'profit_amount': -5.492e-05, - 'profit_percent': -0.05478343, + 'profit_percent': -0.05478342, 'stake_currency': 'BTC', 'fiat_currency': 'USD', } == last_msg @@ -823,7 +823,7 @@ def test_forcesell_all_handle(default_conf, update, ticker, fee, markets, mocker 'open_rate': 1.099e-05, 'current_rate': 1.098e-05, 'profit_amount': -5.91e-06, - 'profit_percent': -0.00589292, + 'profit_percent': -0.00589291, 'stake_currency': 'BTC', 'fiat_currency': 'USD', } == msg diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index cad2f654d..383b07864 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -804,7 +804,7 @@ def test_handle_trade(default_conf, limit_buy_order, limit_sell_order, trade.update(limit_sell_order) assert trade.close_rate == 0.00001173 - assert trade.close_profit == 0.06201057 + assert trade.close_profit == 0.06201058 assert trade.calc_profit() == 0.00006217 assert trade.close_date is not None @@ -1231,7 +1231,7 @@ def test_execute_sell_up(default_conf, ticker, fee, ticker_sell_up, markets, moc 'open_rate': 1.099e-05, 'current_rate': 1.172e-05, 'profit_amount': 6.126e-05, - 'profit_percent': 0.06110514, + 'profit_percent': 0.0611052, 'stake_currency': 'BTC', 'fiat_currency': 'USD', } == last_msg @@ -1277,7 +1277,7 @@ def test_execute_sell_down(default_conf, ticker, fee, ticker_sell_down, markets, 'open_rate': 1.099e-05, 'current_rate': 1.044e-05, 'profit_amount': -5.492e-05, - 'profit_percent': -0.05478343, + 'profit_percent': -0.05478342, 'stake_currency': 'BTC', 'fiat_currency': 'USD', } == last_msg @@ -1324,7 +1324,7 @@ def test_execute_sell_without_conf_sell_up(default_conf, ticker, fee, 'open_rate': 1.099e-05, 'current_rate': 1.172e-05, 'profit_amount': 6.126e-05, - 'profit_percent': 0.06110514, + 'profit_percent': 0.0611052, } == last_msg @@ -1370,7 +1370,7 @@ def test_execute_sell_without_conf_sell_down(default_conf, ticker, fee, 'open_rate': 1.099e-05, 'current_rate': 1.044e-05, 'profit_amount': -5.492e-05, - 'profit_percent': -0.05478343, + 'profit_percent': -0.05478342, } == last_msg diff --git a/freqtrade/tests/test_persistence.py b/freqtrade/tests/test_persistence.py index 7584537e2..5e0647dff 100644 --- a/freqtrade/tests/test_persistence.py +++ b/freqtrade/tests/test_persistence.py @@ -113,7 +113,7 @@ def test_update_with_bittrex(limit_buy_order, limit_sell_order, fee): trade.update(limit_sell_order) assert trade.open_order_id is None assert trade.close_rate == 0.00001173 - assert trade.close_profit == 0.06201057 + assert trade.close_profit == 0.06201058 assert trade.close_date is not None @@ -129,16 +129,16 @@ def test_calc_open_close_trade_price(limit_buy_order, limit_sell_order, fee): trade.open_order_id = 'something' trade.update(limit_buy_order) - assert trade.calc_open_trade_price() == 0.001002500 + assert trade.calc_open_trade_price() == 0.0010024999999225068 trade.update(limit_sell_order) - assert trade.calc_close_trade_price() == 0.0010646656 + assert trade.calc_close_trade_price() == 0.0010646656050132426 # Profit in BTC assert trade.calc_profit() == 0.00006217 # Profit in percent - assert trade.calc_profit_percent() == 0.06201057 + assert trade.calc_profit_percent() == 0.06201058 @pytest.mark.usefixtures("init_persistence") @@ -207,10 +207,10 @@ def test_calc_open_trade_price(limit_buy_order, fee): trade.update(limit_buy_order) # Buy @ 0.00001099 # Get the open rate price with the standard fee rate - assert trade.calc_open_trade_price() == 0.001002500 + assert trade.calc_open_trade_price() == 0.0010024999999225068 # Get the open rate price with a custom fee rate - assert trade.calc_open_trade_price(fee=0.003) == 0.001003000 + assert trade.calc_open_trade_price(fee=0.003) == 0.001002999999922468 @pytest.mark.usefixtures("init_persistence") @@ -226,14 +226,14 @@ def test_calc_close_trade_price(limit_buy_order, limit_sell_order, fee): trade.update(limit_buy_order) # Buy @ 0.00001099 # Get the close rate price with a custom close rate and a regular fee rate - assert trade.calc_close_trade_price(rate=0.00001234) == 0.0011200318 + assert trade.calc_close_trade_price(rate=0.00001234) == 0.0011200318470471794 # Get the close rate price with a custom close rate and a custom fee rate - assert trade.calc_close_trade_price(rate=0.00001234, fee=0.003) == 0.0011194704 + assert trade.calc_close_trade_price(rate=0.00001234, fee=0.003) == 0.0011194704275749754 # Test when we apply a Sell order, and ask price with a custom fee rate trade.update(limit_sell_order) - assert trade.calc_close_trade_price(fee=0.005) == 0.0010619972 + assert trade.calc_close_trade_price(fee=0.005) == 0.0010619972701635854 @pytest.mark.usefixtures("init_persistence") @@ -281,17 +281,17 @@ def test_calc_profit_percent(limit_buy_order, limit_sell_order, fee): trade.update(limit_buy_order) # Buy @ 0.00001099 # Get percent of profit with a custom rate (Higher than open rate) - assert trade.calc_profit_percent(rate=0.00001234) == 0.1172387 + assert trade.calc_profit_percent(rate=0.00001234) == 0.11723875 # Get percent of profit with a custom rate (Lower than open rate) - assert trade.calc_profit_percent(rate=0.00000123) == -0.88863827 + assert trade.calc_profit_percent(rate=0.00000123) == -0.88863828 # Test when we apply a Sell order. Sell higher than open rate @ 0.00001173 trade.update(limit_sell_order) - assert trade.calc_profit_percent() == 0.06201057 + assert trade.calc_profit_percent() == 0.06201058 # Test with a custom fee rate on the close trade - assert trade.calc_profit_percent(fee=0.003) == 0.0614782 + assert trade.calc_profit_percent(fee=0.003) == 0.06147824 def test_clean_dry_run_db(default_conf, fee): From 677a9e56afd83141f7e2a3a6cc5acc2737fdffaa Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 21 Oct 2018 09:23:07 +0200 Subject: [PATCH 2/2] remove skipped test (refresh_whitelist is tested in test_acl_pair) --- freqtrade/tests/test_freqtradebot.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index 383b07864..6b13da35f 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -167,11 +167,6 @@ def test_gen_pair_whitelist_not_supported(mocker, default_conf, tickers) -> None freqtrade._gen_pair_whitelist(base_currency='BTC') -@pytest.mark.skip(reason="Test not implemented") -def test_refresh_whitelist() -> None: - pass - - def test_get_trade_stake_amount(default_conf, ticker, limit_buy_order, fee, mocker) -> None: patch_RPCManager(mocker) patch_exchange(mocker)