Convert amount to contracts before comparing for close

closes #7279
This commit is contained in:
Matthias 2022-08-22 20:48:02 +02:00
parent 78b161e14c
commit fe7108ae75
5 changed files with 22 additions and 11 deletions

View File

@ -9,7 +9,8 @@ from freqtrade.exchange.bitpanda import Bitpanda
from freqtrade.exchange.bittrex import Bittrex
from freqtrade.exchange.bybit import Bybit
from freqtrade.exchange.coinbasepro import Coinbasepro
from freqtrade.exchange.exchange import (amount_to_precision, available_exchanges, ccxt_exchanges,
from freqtrade.exchange.exchange import (amount_to_contracts, amount_to_precision,
available_exchanges, ccxt_exchanges, contracts_to_amount,
date_minus_candles, is_exchange_known_ccxt,
is_exchange_officially_supported, market_is_active,
price_to_precision, timeframe_to_minutes,

View File

@ -2892,7 +2892,7 @@ def market_is_active(market: Dict) -> bool:
return market.get('active', True) is not False
def amount_to_contracts(amount: float, contract_size: float) -> float:
def amount_to_contracts(amount: float, contract_size: Optional[float]) -> float:
"""
Convert amount to contracts.
:param amount: amount to convert
@ -2905,7 +2905,7 @@ def amount_to_contracts(amount: float, contract_size: float) -> float:
return amount
def contracts_to_amount(num_contracts: float, contract_size: float) -> float:
def contracts_to_amount(num_contracts: float, contract_size: Optional[float]) -> float:
"""
Takes num-contracts and converts it to contract size
:param num_contracts: number of contracts

View File

@ -756,6 +756,7 @@ class FreqtradeBot(LoggingMixin):
amount_precision=self.exchange.get_precision_amount(pair),
price_precision=self.exchange.get_precision_price(pair),
precision_mode=self.exchange.precisionMode,
contract_size=self.exchange.get_contract_size(pair),
)
else:
# This is additional buy, we reset fee_open_currency so timeout checking can work

View File

@ -24,7 +24,8 @@ from freqtrade.enums import (BacktestState, CandleType, ExitCheckTuple, ExitType
TradingMode)
from freqtrade.exceptions import DependencyException, OperationalException
from freqtrade.exchange import timeframe_to_minutes, timeframe_to_seconds
from freqtrade.exchange.exchange import amount_to_precision
from freqtrade.exchange.exchange import (amount_to_contracts, amount_to_precision,
contracts_to_amount)
from freqtrade.mixins import LoggingMixin
from freqtrade.optimize.backtest_caching import get_strategy_run_id
from freqtrade.optimize.bt_progress import BTProgress
@ -823,11 +824,13 @@ class Backtesting:
self.order_id_counter += 1
base_currency = self.exchange.get_pair_base_currency(pair)
amount_p = (stake_amount / propose_rate) * leverage
amount = self.exchange._contracts_to_amount(
pair, amount_to_precision(
self.exchange._amount_to_contracts(pair, amount_p),
self.exchange.get_precision_amount(pair), self.precision_mode)
)
contract_size = self.exchange.get_contract_size(pair)
precision_amount = self.exchange.get_precision_amount(pair)
amount = contracts_to_amount(
amount_to_precision(
amount_to_contracts(amount_p, contract_size),
precision_amount, self.precision_mode),
contract_size)
# Backcalculate actual stake amount.
stake_amount = amount * propose_rate / leverage
@ -859,9 +862,10 @@ class Backtesting:
trading_mode=self.trading_mode,
leverage=leverage,
# interest_rate=interest_rate,
amount_precision=self.exchange.get_precision_amount(pair),
amount_precision=precision_amount,
price_precision=self.exchange.get_precision_price(pair),
precision_mode=self.precision_mode,
contract_size=contract_size,
orders=[],
)

View File

@ -15,6 +15,7 @@ from freqtrade.constants import (DATETIME_PRINT_FORMAT, MATH_CLOSE_PREC, NON_OPE
from freqtrade.enums import ExitType, TradingMode
from freqtrade.exceptions import DependencyException, OperationalException
from freqtrade.exchange import amount_to_precision, price_to_precision
from freqtrade.exchange.exchange import amount_to_contracts, contracts_to_amount
from freqtrade.leverage import interest
from freqtrade.persistence.base import _DECL_BASE
from freqtrade.util import FtPrecise
@ -624,7 +625,11 @@ class LocalTrade():
else:
logger.warning(
f'Got different open_order_id {self.open_order_id} != {order.order_id}')
amount_tr = amount_to_precision(self.amount, self.amount_precision, self.precision_mode)
amount_tr = contracts_to_amount(
amount_to_precision(
amount_to_contracts(self.amount, self.contract_size),
self.amount_precision, self.precision_mode),
self.contract_size)
if isclose(order.safe_amount_after_fee, amount_tr, abs_tol=MATH_CLOSE_PREC):
self.close(order.safe_price)
else: