fix #1289 - we should not modify decimal context

This commit is contained in:
Matthias 2018-10-21 09:21:32 +02:00
parent 2f81dc8ff4
commit 202b1d1f0b
5 changed files with 23 additions and 27 deletions

View File

@ -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(

View File

@ -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],

View File

@ -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

View File

@ -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

View File

@ -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):