Refactor Freqtradebot

This commit is contained in:
hroff-1902 2019-10-02 03:27:17 +03:00
parent 8c5b299449
commit 096c69dc4f

View File

@ -1,7 +1,6 @@
""" """
Freqtrade is the main module of this bot. It contains the class Freqtrade() Freqtrade is the main module of this bot. It contains the class Freqtrade()
""" """
import copy import copy
import logging import logging
import traceback import traceback
@ -135,12 +134,11 @@ class FreqtradeBot:
self.strategy.informative_pairs()) self.strategy.informative_pairs())
# First process current opened trades # First process current opened trades
for trade in trades: self.process_maybe_execute_sells(trades)
self.process_maybe_execute_sell(trade)
# Then looking for buy opportunities # Then looking for buy opportunities
if len(trades) < self.config['max_open_trades']: if len(trades) < self.config['max_open_trades']:
self.process_maybe_execute_buy() self.process_maybe_execute_buys()
if 'unfilledtimeout' in self.config: if 'unfilledtimeout' in self.config:
# Check and handle any timed out open orders # Check and handle any timed out open orders
@ -262,11 +260,10 @@ class FreqtradeBot:
Checks pairs as long as the open trade count is below `max_open_trades`. Checks pairs as long as the open trade count is below `max_open_trades`.
:return: True if at least one trade has been created. :return: True if at least one trade has been created.
""" """
interval = self.strategy.ticker_interval
whitelist = copy.deepcopy(self.active_pair_whitelist) whitelist = copy.deepcopy(self.active_pair_whitelist)
if not whitelist: if not whitelist:
logger.warning("Whitelist is empty.") logger.info("Active pair whitelist is empty.")
return False return False
# Remove currently opened and latest pairs from whitelist # Remove currently opened and latest pairs from whitelist
@ -276,7 +273,8 @@ class FreqtradeBot:
logger.debug('Ignoring %s in pair whitelist', trade.pair) logger.debug('Ignoring %s in pair whitelist', trade.pair)
if not whitelist: if not whitelist:
logger.info("No currency pair in whitelist, but checking to sell open trades.") logger.info("No currency pair in active pair whitelist, "
"but checking to sell open trades.")
return False return False
buycount = 0 buycount = 0
@ -285,8 +283,10 @@ class FreqtradeBot:
if self.strategy.is_pair_locked(_pair): if self.strategy.is_pair_locked(_pair):
logger.info(f"Pair {_pair} is currently locked.") logger.info(f"Pair {_pair} is currently locked.")
continue continue
(buy, sell) = self.strategy.get_signal( (buy, sell) = self.strategy.get_signal(
_pair, interval, self.dataprovider.ohlcv(_pair, self.strategy.ticker_interval)) _pair, self.strategy.ticker_interval,
self.dataprovider.ohlcv(_pair, self.strategy.ticker_interval))
if buy and not sell and len(Trade.get_open_trades()) < self.config['max_open_trades']: if buy and not sell and len(Trade.get_open_trades()) < self.config['max_open_trades']:
stake_amount = self._get_trade_stake_amount(_pair) stake_amount = self._get_trade_stake_amount(_pair)
@ -431,10 +431,9 @@ class FreqtradeBot:
return True return True
def process_maybe_execute_buy(self) -> None: def process_maybe_execute_buys(self) -> None:
""" """
Tries to execute a buy trade in a safe way Tries to execute buy trades in a safe way
:return: True if executed
""" """
try: try:
# Create entity and execute trade # Create entity and execute trade
@ -443,21 +442,19 @@ class FreqtradeBot:
except DependencyException as exception: except DependencyException as exception:
logger.warning('Unable to create trade: %s', exception) logger.warning('Unable to create trade: %s', exception)
def process_maybe_execute_sell(self, trade: Trade) -> bool: def process_maybe_execute_sells(self, trades: List[Any]) -> None:
""" """
Tries to execute a sell trade Tries to execute sell trades in a safe way
:return: True if executed
""" """
for trade in trades:
try: try:
self.update_trade_state(trade) self.update_trade_state(trade)
if self.strategy.order_types.get('stoploss_on_exchange') and trade.is_open: if trade.is_open:
result = False
if self.strategy.order_types.get('stoploss_on_exchange'):
result = self.handle_stoploss_on_exchange(trade) result = self.handle_stoploss_on_exchange(trade)
if result: elif trade.open_order_id is None:
self.wallets.update()
return result
if trade.is_open and trade.open_order_id is None:
# Check if we can sell our current pair # Check if we can sell our current pair
result = self.handle_trade(trade) result = self.handle_trade(trade)
@ -465,11 +462,8 @@ class FreqtradeBot:
if result: if result:
self.wallets.update() self.wallets.update()
return result
except DependencyException as exception: except DependencyException as exception:
logger.warning('Unable to sell trade: %s', exception) logger.warning('Unable to sell trade: %s', exception)
return False
def get_real_amount(self, trade: Trade, order: Dict) -> float: def get_real_amount(self, trade: Trade, order: Dict) -> float:
""" """