mirror of
https://github.com/freqtrade/freqtrade.git
synced 2024-11-10 10:21:59 +00:00
Refactor exchange to class
This commit is contained in:
parent
e3c91df081
commit
21edcbdc27
|
@ -10,7 +10,7 @@ import arrow
|
|||
from pandas import DataFrame, to_datetime
|
||||
|
||||
from freqtrade import constants
|
||||
from freqtrade.exchange import get_ticker_history
|
||||
from freqtrade.exchange import Exchange
|
||||
from freqtrade.persistence import Trade
|
||||
from freqtrade.strategy.resolver import StrategyResolver, IStrategy
|
||||
|
||||
|
@ -110,14 +110,14 @@ class Analyze(object):
|
|||
dataframe = self.populate_sell_trend(dataframe)
|
||||
return dataframe
|
||||
|
||||
def get_signal(self, pair: str, interval: str) -> Tuple[bool, bool]:
|
||||
def get_signal(self, exchange: Exchange, pair: str, interval: str) -> Tuple[bool, bool]:
|
||||
"""
|
||||
Calculates current signal based several technical analysis indicators
|
||||
:param pair: pair in format ANT/BTC
|
||||
:param interval: Interval to use (in min)
|
||||
:return: (Buy, Sell) A bool-tuple indicating buy/sell signal
|
||||
"""
|
||||
ticker_hist = get_ticker_history(pair, interval)
|
||||
ticker_hist = exchange.get_ticker_history(pair, interval)
|
||||
if not ticker_hist:
|
||||
logger.warning('Empty ticker history for pair %s', pair)
|
||||
return False, False
|
||||
|
|
|
@ -12,16 +12,8 @@ from freqtrade import constants, OperationalException, DependencyException, Temp
|
|||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Current selected exchange
|
||||
_API: ccxt.Exchange = None
|
||||
|
||||
_CONF: Dict = {}
|
||||
API_RETRY_COUNT = 4
|
||||
|
||||
_CACHED_TICKER: Dict[str, Any] = {}
|
||||
|
||||
# Holds all open sell orders for dry_run
|
||||
_DRY_RUN_OPEN_ORDERS: Dict[str, Any] = {}
|
||||
|
||||
# Urls to exchange markets, insert quote and base with .format()
|
||||
_EXCHANGE_URLS = {
|
||||
|
@ -74,7 +66,17 @@ def init_ccxt(exchange_config: dict) -> ccxt.Exchange:
|
|||
return api
|
||||
|
||||
|
||||
def init(config: dict) -> None:
|
||||
class Exchange(object):
|
||||
|
||||
# Current selected exchange
|
||||
_API: ccxt.Exchange = None
|
||||
_CONF: Dict = {}
|
||||
_CACHED_TICKER: Dict[str, Any] = {}
|
||||
|
||||
# Holds all open sell orders for dry_run
|
||||
_DRY_RUN_OPEN_ORDERS: Dict[str, Any] = {}
|
||||
|
||||
def __init__(self, config: dict) -> None:
|
||||
"""
|
||||
Initializes this module with the given config,
|
||||
it does basic validation whether the specified
|
||||
|
@ -82,23 +84,28 @@ def init(config: dict) -> None:
|
|||
:param config: config to use
|
||||
:return: None
|
||||
"""
|
||||
global _CONF, _API
|
||||
self._API
|
||||
|
||||
_CONF.update(config)
|
||||
self._CONF.update(config)
|
||||
|
||||
if config['dry_run']:
|
||||
logger.info('Instance is running with dry_run enabled')
|
||||
|
||||
exchange_config = config['exchange']
|
||||
_API = init_ccxt(exchange_config)
|
||||
self._API = init_ccxt(exchange_config)
|
||||
|
||||
logger.info('Using Exchange "%s"', get_name())
|
||||
logger.info('Using Exchange "%s"', self.get_name())
|
||||
|
||||
# Check if all pairs are available
|
||||
validate_pairs(config['exchange']['pair_whitelist'])
|
||||
self.validate_pairs(config['exchange']['pair_whitelist'])
|
||||
|
||||
def get_name(self) -> str:
|
||||
return self._API.name
|
||||
|
||||
def validate_pairs(pairs: List[str]) -> None:
|
||||
def get_id(self) -> str:
|
||||
return self._API.id
|
||||
|
||||
def validate_pairs(self, pairs: List[str]) -> None:
|
||||
"""
|
||||
Checks if all given pairs are tradable on the current exchange.
|
||||
Raises OperationalException if one pair is not available.
|
||||
|
@ -107,12 +114,12 @@ def validate_pairs(pairs: List[str]) -> None:
|
|||
"""
|
||||
|
||||
try:
|
||||
markets = _API.load_markets()
|
||||
markets = self._API.load_markets()
|
||||
except ccxt.BaseError as e:
|
||||
logger.warning('Unable to validate pairs (assuming they are correct). Reason: %s', e)
|
||||
return
|
||||
|
||||
stake_cur = _CONF['stake_currency']
|
||||
stake_cur = self._CONF['stake_currency']
|
||||
for pair in pairs:
|
||||
# Note: ccxt has BaseCurrency/QuoteCurrency format for pairs
|
||||
# TODO: add a support for having coins in BTC/USDT format
|
||||
|
@ -121,24 +128,21 @@ def validate_pairs(pairs: List[str]) -> None:
|
|||
f'Pair {pair} not compatible with stake_currency: {stake_cur}')
|
||||
if pair not in markets:
|
||||
raise OperationalException(
|
||||
f'Pair {pair} is not available at {get_name()}')
|
||||
f'Pair {pair} is not available at {self.get_name()}')
|
||||
|
||||
|
||||
def exchange_has(endpoint: str) -> bool:
|
||||
def exchange_has(self, endpoint: str) -> bool:
|
||||
"""
|
||||
Checks if exchange implements a specific API endpoint.
|
||||
Wrapper around ccxt 'has' attribute
|
||||
:param endpoint: Name of endpoint (e.g. 'fetchOHLCV', 'fetchTickers')
|
||||
:return: bool
|
||||
"""
|
||||
return endpoint in _API.has and _API.has[endpoint]
|
||||
return endpoint in self._API.has and self._API.has[endpoint]
|
||||
|
||||
|
||||
def buy(pair: str, rate: float, amount: float) -> Dict:
|
||||
if _CONF['dry_run']:
|
||||
global _DRY_RUN_OPEN_ORDERS
|
||||
def buy(self, pair: str, rate: float, amount: float) -> Dict:
|
||||
if self._CONF['dry_run']:
|
||||
order_id = f'dry_run_buy_{randint(0, 10**6)}'
|
||||
_DRY_RUN_OPEN_ORDERS[order_id] = {
|
||||
self._DRY_RUN_OPEN_ORDERS[order_id] = {
|
||||
'pair': pair,
|
||||
'price': rate,
|
||||
'amount': amount,
|
||||
|
@ -152,7 +156,7 @@ def buy(pair: str, rate: float, amount: float) -> Dict:
|
|||
return {'id': order_id}
|
||||
|
||||
try:
|
||||
return _API.create_limit_buy_order(pair, amount, rate)
|
||||
return self._API.create_limit_buy_order(pair, amount, rate)
|
||||
except ccxt.InsufficientFunds as e:
|
||||
raise DependencyException(
|
||||
f'Insufficient funds to create limit buy order on market {pair}.'
|
||||
|
@ -169,12 +173,10 @@ def buy(pair: str, rate: float, amount: float) -> Dict:
|
|||
except ccxt.BaseError as e:
|
||||
raise OperationalException(e)
|
||||
|
||||
|
||||
def sell(pair: str, rate: float, amount: float) -> Dict:
|
||||
if _CONF['dry_run']:
|
||||
global _DRY_RUN_OPEN_ORDERS
|
||||
def sell(self, pair: str, rate: float, amount: float) -> Dict:
|
||||
if self._CONF['dry_run']:
|
||||
order_id = f'dry_run_sell_{randint(0, 10**6)}'
|
||||
_DRY_RUN_OPEN_ORDERS[order_id] = {
|
||||
self._DRY_RUN_OPEN_ORDERS[order_id] = {
|
||||
'pair': pair,
|
||||
'price': rate,
|
||||
'amount': amount,
|
||||
|
@ -187,7 +189,7 @@ def sell(pair: str, rate: float, amount: float) -> Dict:
|
|||
return {'id': order_id}
|
||||
|
||||
try:
|
||||
return _API.create_limit_sell_order(pair, amount, rate)
|
||||
return self._API.create_limit_sell_order(pair, amount, rate)
|
||||
except ccxt.InsufficientFunds as e:
|
||||
raise DependencyException(
|
||||
f'Insufficient funds to create limit sell order on market {pair}.'
|
||||
|
@ -204,28 +206,26 @@ def sell(pair: str, rate: float, amount: float) -> Dict:
|
|||
except ccxt.BaseError as e:
|
||||
raise OperationalException(e)
|
||||
|
||||
|
||||
@retrier
|
||||
def get_balance(currency: str) -> float:
|
||||
if _CONF['dry_run']:
|
||||
@retrier
|
||||
def get_balance(self, currency: str) -> float:
|
||||
if self._CONF['dry_run']:
|
||||
return 999.9
|
||||
|
||||
# ccxt exception is already handled by get_balances
|
||||
balances = get_balances()
|
||||
balances = self.get_balances()
|
||||
balance = balances.get(currency)
|
||||
if balance is None:
|
||||
raise TemporaryError(
|
||||
f'Could not get {currency} balance due to malformed exchange response: {balances}')
|
||||
return balance['free']
|
||||
|
||||
|
||||
@retrier
|
||||
def get_balances() -> dict:
|
||||
if _CONF['dry_run']:
|
||||
@retrier
|
||||
def get_balances(self) -> dict:
|
||||
if self._CONF['dry_run']:
|
||||
return {}
|
||||
|
||||
try:
|
||||
balances = _API.fetch_balance()
|
||||
balances = self._API.fetch_balance()
|
||||
# Remove additional info from ccxt results
|
||||
balances.pop("info", None)
|
||||
balances.pop("free", None)
|
||||
|
@ -239,14 +239,13 @@ def get_balances() -> dict:
|
|||
except ccxt.BaseError as e:
|
||||
raise OperationalException(e)
|
||||
|
||||
|
||||
@retrier
|
||||
def get_tickers() -> Dict:
|
||||
@retrier
|
||||
def get_tickers(self) -> Dict:
|
||||
try:
|
||||
return _API.fetch_tickers()
|
||||
return self._API.fetch_tickers()
|
||||
except ccxt.NotSupported as e:
|
||||
raise OperationalException(
|
||||
f'Exchange {_API.name} does not support fetching tickers in batch.'
|
||||
f'Exchange {self._API.name} does not support fetching tickers in batch.'
|
||||
f'Message: {e}')
|
||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
||||
raise TemporaryError(
|
||||
|
@ -254,15 +253,13 @@ def get_tickers() -> Dict:
|
|||
except ccxt.BaseError as e:
|
||||
raise OperationalException(e)
|
||||
|
||||
|
||||
@retrier
|
||||
def get_ticker(pair: str, refresh: Optional[bool] = True) -> dict:
|
||||
global _CACHED_TICKER
|
||||
if refresh or pair not in _CACHED_TICKER.keys():
|
||||
@retrier
|
||||
def get_ticker(self, pair: str, refresh: Optional[bool] = True) -> dict:
|
||||
if refresh or pair not in self._CACHED_TICKER.keys():
|
||||
try:
|
||||
data = _API.fetch_ticker(pair)
|
||||
data = self._API.fetch_ticker(pair)
|
||||
try:
|
||||
_CACHED_TICKER[pair] = {
|
||||
self._CACHED_TICKER[pair] = {
|
||||
'bid': float(data['bid']),
|
||||
'ask': float(data['ask']),
|
||||
}
|
||||
|
@ -276,11 +273,11 @@ def get_ticker(pair: str, refresh: Optional[bool] = True) -> dict:
|
|||
raise OperationalException(e)
|
||||
else:
|
||||
logger.info("returning cached ticker-data for %s", pair)
|
||||
return _CACHED_TICKER[pair]
|
||||
return self._CACHED_TICKER[pair]
|
||||
|
||||
|
||||
@retrier
|
||||
def get_ticker_history(pair: str, tick_interval: str, since_ms: Optional[int] = None) -> List[Dict]:
|
||||
@retrier
|
||||
def get_ticker_history(self, pair: str, tick_interval: str,
|
||||
since_ms: Optional[int] = None) -> List[Dict]:
|
||||
try:
|
||||
# last item should be in the time interval [now - tick_interval, now]
|
||||
till_time_ms = arrow.utcnow().shift(
|
||||
|
@ -294,7 +291,7 @@ def get_ticker_history(pair: str, tick_interval: str, since_ms: Optional[int] =
|
|||
|
||||
data: List[Dict[Any, Any]] = []
|
||||
while not since_ms or since_ms < till_time_ms:
|
||||
data_part = _API.fetch_ohlcv(pair, timeframe=tick_interval, since=since_ms)
|
||||
data_part = self._API.fetch_ohlcv(pair, timeframe=tick_interval, since=since_ms)
|
||||
|
||||
# Because some exchange sort Tickers ASC and other DESC.
|
||||
# Ex: Bittrex returns a list of tickers ASC (oldest first, newest last)
|
||||
|
@ -315,7 +312,7 @@ def get_ticker_history(pair: str, tick_interval: str, since_ms: Optional[int] =
|
|||
return data
|
||||
except ccxt.NotSupported as e:
|
||||
raise OperationalException(
|
||||
f'Exchange {_API.name} does not support fetching historical candlestick data.'
|
||||
f'Exchange {self._API.name} does not support fetching historical candlestick data.'
|
||||
f'Message: {e}')
|
||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
||||
raise TemporaryError(
|
||||
|
@ -323,14 +320,13 @@ def get_ticker_history(pair: str, tick_interval: str, since_ms: Optional[int] =
|
|||
except ccxt.BaseError as e:
|
||||
raise OperationalException(f'Could not fetch ticker data. Msg: {e}')
|
||||
|
||||
|
||||
@retrier
|
||||
def cancel_order(order_id: str, pair: str) -> None:
|
||||
if _CONF['dry_run']:
|
||||
@retrier
|
||||
def cancel_order(self, order_id: str, pair: str) -> None:
|
||||
if self._CONF['dry_run']:
|
||||
return
|
||||
|
||||
try:
|
||||
return _API.cancel_order(order_id, pair)
|
||||
return self._API.cancel_order(order_id, pair)
|
||||
except ccxt.InvalidOrder as e:
|
||||
raise DependencyException(
|
||||
f'Could not cancel order. Message: {e}')
|
||||
|
@ -340,17 +336,16 @@ def cancel_order(order_id: str, pair: str) -> None:
|
|||
except ccxt.BaseError as e:
|
||||
raise OperationalException(e)
|
||||
|
||||
|
||||
@retrier
|
||||
def get_order(order_id: str, pair: str) -> Dict:
|
||||
if _CONF['dry_run']:
|
||||
order = _DRY_RUN_OPEN_ORDERS[order_id]
|
||||
@retrier
|
||||
def get_order(self, order_id: str, pair: str) -> Dict:
|
||||
if self._CONF['dry_run']:
|
||||
order = self._DRY_RUN_OPEN_ORDERS[order_id]
|
||||
order.update({
|
||||
'id': order_id
|
||||
})
|
||||
return order
|
||||
try:
|
||||
return _API.fetch_order(order_id, pair)
|
||||
return self._API.fetch_order(order_id, pair)
|
||||
except ccxt.InvalidOrder as e:
|
||||
raise DependencyException(
|
||||
f'Could not get order. Message: {e}')
|
||||
|
@ -360,15 +355,14 @@ def get_order(order_id: str, pair: str) -> Dict:
|
|||
except ccxt.BaseError as e:
|
||||
raise OperationalException(e)
|
||||
|
||||
|
||||
@retrier
|
||||
def get_trades_for_order(order_id: str, pair: str, since: datetime) -> List:
|
||||
if _CONF['dry_run']:
|
||||
@retrier
|
||||
def get_trades_for_order(self, order_id: str, pair: str, since: datetime) -> List:
|
||||
if self._CONF['dry_run']:
|
||||
return []
|
||||
if not exchange_has('fetchMyTrades'):
|
||||
if not self.exchange_has('fetchMyTrades'):
|
||||
return []
|
||||
try:
|
||||
my_trades = _API.fetch_my_trades(pair, since.timestamp())
|
||||
my_trades = self._API.fetch_my_trades(pair, since.timestamp())
|
||||
matched_trades = [trade for trade in my_trades if trade['order'] == order_id]
|
||||
|
||||
return matched_trades
|
||||
|
@ -379,46 +373,35 @@ def get_trades_for_order(order_id: str, pair: str, since: datetime) -> List:
|
|||
except ccxt.BaseError as e:
|
||||
raise OperationalException(e)
|
||||
|
||||
|
||||
def get_pair_detail_url(pair: str) -> str:
|
||||
def get_pair_detail_url(self, pair: str) -> str:
|
||||
try:
|
||||
url_base = _API.urls.get('www')
|
||||
url_base = self._API.urls.get('www')
|
||||
base, quote = pair.split('/')
|
||||
|
||||
return url_base + _EXCHANGE_URLS[_API.id].format(base=base, quote=quote)
|
||||
return url_base + _EXCHANGE_URLS[self._API.id].format(base=base, quote=quote)
|
||||
except KeyError:
|
||||
logger.warning('Could not get exchange url for %s', get_name())
|
||||
logger.warning('Could not get exchange url for %s', self.get_name())
|
||||
return ""
|
||||
|
||||
|
||||
@retrier
|
||||
def get_markets() -> List[dict]:
|
||||
@retrier
|
||||
def get_markets(self) -> List[dict]:
|
||||
try:
|
||||
return _API.fetch_markets()
|
||||
return self._API.fetch_markets()
|
||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
||||
raise TemporaryError(
|
||||
f'Could not load markets due to {e.__class__.__name__}. Message: {e}')
|
||||
except ccxt.BaseError as e:
|
||||
raise OperationalException(e)
|
||||
|
||||
|
||||
def get_name() -> str:
|
||||
return _API.name
|
||||
|
||||
|
||||
def get_id() -> str:
|
||||
return _API.id
|
||||
|
||||
|
||||
@retrier
|
||||
def get_fee(symbol='ETH/BTC', type='', side='', amount=1,
|
||||
@retrier
|
||||
def get_fee(self, symbol='ETH/BTC', type='', side='', amount=1,
|
||||
price=1, taker_or_maker='maker') -> float:
|
||||
try:
|
||||
# validate that markets are loaded before trying to get fee
|
||||
if _API.markets is None or len(_API.markets) == 0:
|
||||
_API.load_markets()
|
||||
if self._API.markets is None or len(self._API.markets) == 0:
|
||||
self._API.load_markets()
|
||||
|
||||
return _API.calculate_fee(symbol=symbol, type=type, side=side, amount=amount,
|
||||
return self._API.calculate_fee(symbol=symbol, type=type, side=side, amount=amount,
|
||||
price=price, takerOrMaker=taker_or_maker)['rate']
|
||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
||||
raise TemporaryError(
|
||||
|
@ -426,12 +409,11 @@ def get_fee(symbol='ETH/BTC', type='', side='', amount=1,
|
|||
except ccxt.BaseError as e:
|
||||
raise OperationalException(e)
|
||||
|
||||
|
||||
def get_amount_lots(pair: str, amount: float) -> float:
|
||||
def get_amount_lots(self, pair: str, amount: float) -> float:
|
||||
"""
|
||||
get buyable amount rounding, ..
|
||||
"""
|
||||
# validate that markets are loaded before trying to get fee
|
||||
if not _API.markets:
|
||||
_API.load_markets()
|
||||
return _API.amount_to_lots(pair, amount)
|
||||
if not self._API.markets:
|
||||
self._API.load_markets()
|
||||
return self._API.amount_to_lots(pair, amount)
|
||||
|
|
|
@ -14,11 +14,11 @@ import requests
|
|||
from cachetools import TTLCache, cached
|
||||
|
||||
from freqtrade import (
|
||||
DependencyException, OperationalException, TemporaryError,
|
||||
exchange, persistence, __version__,
|
||||
DependencyException, OperationalException, TemporaryError, persistence, __version__,
|
||||
)
|
||||
from freqtrade import constants
|
||||
from freqtrade.analyze import Analyze
|
||||
from freqtrade.exchange import Exchange
|
||||
from freqtrade.fiat_convert import CryptoToFiatConverter
|
||||
from freqtrade.persistence import Trade
|
||||
from freqtrade.rpc.rpc_manager import RPCManager
|
||||
|
@ -66,7 +66,7 @@ class FreqtradeBot(object):
|
|||
# Initialize all modules
|
||||
|
||||
persistence.init(self.config)
|
||||
exchange.init(self.config)
|
||||
self.exchange = Exchange(self.config)
|
||||
|
||||
# Set initial application state
|
||||
initial_state = self.config.get('initial_state')
|
||||
|
@ -186,13 +186,13 @@ class FreqtradeBot(object):
|
|||
:return: List of pairs
|
||||
"""
|
||||
|
||||
if not exchange.exchange_has('fetchTickers'):
|
||||
if not self.exchange.exchange_has('fetchTickers'):
|
||||
raise OperationalException(
|
||||
'Exchange does not support dynamic whitelist.'
|
||||
'Please edit your config and restart the bot'
|
||||
)
|
||||
|
||||
tickers = exchange.get_tickers()
|
||||
tickers = self.exchange.get_tickers()
|
||||
# check length so that we make sure that '/' is actually in the string
|
||||
tickers = [v for k, v in tickers.items()
|
||||
if len(k.split('/')) == 2 and k.split('/')[1] == base_currency]
|
||||
|
@ -210,7 +210,7 @@ class FreqtradeBot(object):
|
|||
black_listed
|
||||
"""
|
||||
sanitized_whitelist = whitelist
|
||||
markets = exchange.get_markets()
|
||||
markets = self.exchange.get_markets()
|
||||
|
||||
markets = [m for m in markets if m['quote'] == self.config['stake_currency']]
|
||||
known_pairs = set()
|
||||
|
@ -255,7 +255,7 @@ class FreqtradeBot(object):
|
|||
interval = self.analyze.get_ticker_interval()
|
||||
stake_currency = self.config['stake_currency']
|
||||
fiat_currency = self.config['fiat_display_currency']
|
||||
exc_name = exchange.get_name()
|
||||
exc_name = self.exchange.get_name()
|
||||
|
||||
logger.info(
|
||||
'Checking buy signals to create a new trade with stake_amount: %f ...',
|
||||
|
@ -263,7 +263,7 @@ class FreqtradeBot(object):
|
|||
)
|
||||
whitelist = copy.deepcopy(self.config['exchange']['pair_whitelist'])
|
||||
# Check if stake_amount is fulfilled
|
||||
if exchange.get_balance(stake_currency) < stake_amount:
|
||||
if self.exchange.get_balance(stake_currency) < stake_amount:
|
||||
raise DependencyException(
|
||||
f'stake amount is not fulfilled (currency={stake_currency})')
|
||||
|
||||
|
@ -278,19 +278,19 @@ class FreqtradeBot(object):
|
|||
|
||||
# Pick pair based on buy signals
|
||||
for _pair in whitelist:
|
||||
(buy, sell) = self.analyze.get_signal(_pair, interval)
|
||||
(buy, sell) = self.analyze.get_signal(self.exchange, _pair, interval)
|
||||
if buy and not sell:
|
||||
pair = _pair
|
||||
break
|
||||
else:
|
||||
return False
|
||||
pair_s = pair.replace('_', '/')
|
||||
pair_url = exchange.get_pair_detail_url(pair)
|
||||
pair_url = self.exchange.get_pair_detail_url(pair)
|
||||
# Calculate amount
|
||||
buy_limit = self.get_target_bid(exchange.get_ticker(pair))
|
||||
buy_limit = self.get_target_bid(self.exchange.get_ticker(pair))
|
||||
amount = stake_amount / buy_limit
|
||||
|
||||
order_id = exchange.buy(pair, buy_limit, amount)['id']
|
||||
order_id = self.exchange.buy(pair, buy_limit, amount)['id']
|
||||
|
||||
stake_amount_fiat = self.fiat_converter.convert_amount(
|
||||
stake_amount,
|
||||
|
@ -305,7 +305,7 @@ with limit `{buy_limit:.8f} ({stake_amount:.6f} \
|
|||
{stake_currency}, {stake_amount_fiat:.3f} {fiat_currency})`"""
|
||||
)
|
||||
# Fee is applied twice because we make a LIMIT_BUY and LIMIT_SELL
|
||||
fee = exchange.get_fee(symbol=pair, taker_or_maker='maker')
|
||||
fee = self.exchange.get_fee(symbol=pair, taker_or_maker='maker')
|
||||
trade = Trade(
|
||||
pair=pair,
|
||||
stake_amount=stake_amount,
|
||||
|
@ -315,7 +315,7 @@ with limit `{buy_limit:.8f} ({stake_amount:.6f} \
|
|||
open_rate=buy_limit,
|
||||
open_rate_requested=buy_limit,
|
||||
open_date=datetime.utcnow(),
|
||||
exchange=exchange.get_id(),
|
||||
exchange=self.exchange.get_id(),
|
||||
open_order_id=order_id
|
||||
)
|
||||
Trade.session.add(trade)
|
||||
|
@ -348,7 +348,7 @@ with limit `{buy_limit:.8f} ({stake_amount:.6f} \
|
|||
if trade.open_order_id:
|
||||
# Update trade with order values
|
||||
logger.info('Found open order for %s', trade)
|
||||
order = exchange.get_order(trade.open_order_id, trade.pair)
|
||||
order = self.exchange.get_order(trade.open_order_id, trade.pair)
|
||||
# Try update amount (binance-fix)
|
||||
try:
|
||||
new_amount = self.get_real_amount(trade, order)
|
||||
|
@ -372,7 +372,7 @@ with limit `{buy_limit:.8f} ({stake_amount:.6f} \
|
|||
def get_real_amount(self, trade: Trade, order: Dict) -> float:
|
||||
"""
|
||||
Get real amount for the trade
|
||||
Necessary for exchanges which charge fees in base currency (e.g. binance)
|
||||
Necessary for self.exchanges which charge fees in base currency (e.g. binance)
|
||||
"""
|
||||
order_amount = order['amount']
|
||||
# Only run for closed orders
|
||||
|
@ -388,7 +388,8 @@ with limit `{buy_limit:.8f} ({stake_amount:.6f} \
|
|||
return new_amount
|
||||
|
||||
# Fallback to Trades
|
||||
trades = exchange.get_trades_for_order(trade.open_order_id, trade.pair, trade.open_date)
|
||||
trades = self.exchange.get_trades_for_order(trade.open_order_id, trade.pair,
|
||||
trade.open_date)
|
||||
|
||||
if len(trades) == 0:
|
||||
logger.info("Applying fee on amount for %s failed: myTrade-Dict empty found", trade)
|
||||
|
@ -420,7 +421,7 @@ with limit `{buy_limit:.8f} ({stake_amount:.6f} \
|
|||
raise ValueError(f'attempt to handle closed trade: {trade}')
|
||||
|
||||
logger.debug('Handling %s ...', trade)
|
||||
current_rate = exchange.get_ticker(trade.pair)['bid']
|
||||
current_rate = self.exchange.get_ticker(trade.pair)['bid']
|
||||
|
||||
(buy, sell) = (False, False)
|
||||
|
||||
|
@ -449,7 +450,7 @@ with limit `{buy_limit:.8f} ({stake_amount:.6f} \
|
|||
# updated via /forcesell in a different thread.
|
||||
if not trade.open_order_id:
|
||||
continue
|
||||
order = exchange.get_order(trade.open_order_id, trade.pair)
|
||||
order = self.exchange.get_order(trade.open_order_id, trade.pair)
|
||||
except requests.exceptions.RequestException:
|
||||
logger.info(
|
||||
'Cannot query order for %s due to %s',
|
||||
|
@ -475,7 +476,7 @@ with limit `{buy_limit:.8f} ({stake_amount:.6f} \
|
|||
:return: True if order was fully cancelled
|
||||
"""
|
||||
pair_s = trade.pair.replace('_', '/')
|
||||
exchange.cancel_order(trade.open_order_id, trade.pair)
|
||||
self.exchange.cancel_order(trade.open_order_id, trade.pair)
|
||||
if order['remaining'] == order['amount']:
|
||||
# if trade is not partially completed, just delete the trade
|
||||
Trade.session.delete(trade)
|
||||
|
@ -502,7 +503,7 @@ with limit `{buy_limit:.8f} ({stake_amount:.6f} \
|
|||
pair_s = trade.pair.replace('_', '/')
|
||||
if order['remaining'] == order['amount']:
|
||||
# if trade is not partially completed, just cancel the trade
|
||||
exchange.cancel_order(trade.open_order_id, trade.pair)
|
||||
self.exchange.cancel_order(trade.open_order_id, trade.pair)
|
||||
trade.close_rate = None
|
||||
trade.close_profit = None
|
||||
trade.close_date = None
|
||||
|
@ -525,15 +526,15 @@ with limit `{buy_limit:.8f} ({stake_amount:.6f} \
|
|||
exc = trade.exchange
|
||||
pair = trade.pair
|
||||
# Execute sell and update trade record
|
||||
order_id = exchange.sell(str(trade.pair), limit, trade.amount)['id']
|
||||
order_id = self.exchange.sell(str(trade.pair), limit, trade.amount)['id']
|
||||
trade.open_order_id = order_id
|
||||
trade.close_rate_requested = limit
|
||||
|
||||
fmt_exp_profit = round(trade.calc_profit_percent(rate=limit) * 100, 2)
|
||||
profit_trade = trade.calc_profit(rate=limit)
|
||||
current_rate = exchange.get_ticker(trade.pair)['bid']
|
||||
current_rate = self.exchange.get_ticker(trade.pair)['bid']
|
||||
profit = trade.calc_profit_percent(limit)
|
||||
pair_url = exchange.get_pair_detail_url(trade.pair)
|
||||
pair_url = self.exchange.get_pair_detail_url(trade.pair)
|
||||
gain = "profit" if fmt_exp_profit > 0 else "loss"
|
||||
|
||||
message = f"*{exc}:* Selling\n" \
|
||||
|
|
|
@ -8,7 +8,7 @@ from typing import Optional, List, Dict, Tuple, Any
|
|||
import arrow
|
||||
|
||||
from freqtrade import misc, constants
|
||||
from freqtrade.exchange import get_ticker_history
|
||||
from freqtrade.exchange import Exchange
|
||||
from freqtrade.arguments import TimeRange
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
@ -183,6 +183,7 @@ def load_cached_data_for_updating(filename: str,
|
|||
|
||||
|
||||
def download_backtesting_testdata(datadir: str,
|
||||
exchange: Exchange,
|
||||
pair: str,
|
||||
tick_interval: str = '5m',
|
||||
timerange: Optional[TimeRange] = None) -> None:
|
||||
|
@ -216,7 +217,8 @@ def download_backtesting_testdata(datadir: str,
|
|||
logger.debug("Current Start: %s", misc.format_ms_time(data[1][0]) if data else 'None')
|
||||
logger.debug("Current End: %s", misc.format_ms_time(data[-1][0]) if data else 'None')
|
||||
|
||||
new_data = get_ticker_history(pair=pair, tick_interval=tick_interval, since_ms=since_ms)
|
||||
new_data = exchange.get_ticker_history(pair=pair, tick_interval=tick_interval,
|
||||
since_ms=since_ms)
|
||||
data.extend(new_data)
|
||||
|
||||
logger.debug("New Start: %s", misc.format_ms_time(data[0][0]))
|
||||
|
|
|
@ -14,7 +14,7 @@ from pandas import DataFrame
|
|||
from tabulate import tabulate
|
||||
|
||||
import freqtrade.optimize as optimize
|
||||
from freqtrade import exchange
|
||||
from freqtrade.exchange import Exchange
|
||||
from freqtrade.analyze import Analyze
|
||||
from freqtrade.arguments import Arguments
|
||||
from freqtrade.configuration import Configuration
|
||||
|
@ -61,7 +61,7 @@ class Backtesting(object):
|
|||
self.config['exchange']['password'] = ''
|
||||
self.config['exchange']['uid'] = ''
|
||||
self.config['dry_run'] = True
|
||||
exchange.init(self.config)
|
||||
self.exchange = Exchange(self.config)
|
||||
|
||||
@staticmethod
|
||||
def get_timeframe(data: Dict[str, DataFrame]) -> Tuple[arrow.Arrow, arrow.Arrow]:
|
||||
|
@ -130,7 +130,7 @@ class Backtesting(object):
|
|||
|
||||
stake_amount = args['stake_amount']
|
||||
max_open_trades = args.get('max_open_trades', 0)
|
||||
fee = exchange.get_fee()
|
||||
fee = self.exchange.get_fee()
|
||||
trade = Trade(
|
||||
open_rate=buy_row.close,
|
||||
open_date=buy_row.date,
|
||||
|
@ -256,7 +256,7 @@ class Backtesting(object):
|
|||
if self.config.get('live'):
|
||||
logger.info('Downloading data for all pairs in whitelist ...')
|
||||
for pair in pairs:
|
||||
data[pair] = exchange.get_ticker_history(pair, self.ticker_interval)
|
||||
data[pair] = self.exchange.get_ticker_history(pair, self.ticker_interval)
|
||||
else:
|
||||
logger.info('Using local backtesting data (using whitelist in given config) ...')
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user