Refactor create_trades()

This commit is contained in:
hroff-1902 2019-12-29 04:17:49 +03:00
parent 433fd2a7c3
commit 762604300f

View File

@ -294,65 +294,48 @@ class FreqtradeBot:
# See also #2575 at github. # See also #2575 at github.
return max(min_stake_amounts) / amount_reserve_percent return max(min_stake_amounts) / amount_reserve_percent
def create_trades(self) -> bool: def create_trade(self, pair: str) -> bool:
""" """
Checks the implemented trading strategy for buy-signals, using the active pair whitelist. Check the implemented trading strategy for buy-signals.
If a pair triggers the buy_signal a new trade record gets created. If the pair triggers the buy_signal a new trade record gets created.
Checks pairs as long as the open trade count is below `max_open_trades`. :return: True if a trade has been created.
:return: True if at least one trade has been created.
""" """
whitelist = copy.deepcopy(self.active_pair_whitelist) logger.debug(f"create_trade for pair {pair}")
if not whitelist: if self.strategy.is_pair_locked(pair):
logger.info("Active pair whitelist is empty.") logger.info(f"Pair {pair} is currently locked.")
return False return False
# Remove currently opened and latest pairs from whitelist
for trade in Trade.get_open_trades():
if trade.pair in whitelist:
whitelist.remove(trade.pair)
logger.debug('Ignoring %s in pair whitelist', trade.pair)
if not whitelist:
logger.info("No currency pair in active pair whitelist, "
"but checking to sell open trades.")
return False
buycount = 0
# running get_signal on historical data fetched # running get_signal on historical data fetched
for pair in whitelist: (buy, sell) = self.strategy.get_signal(
if self.strategy.is_pair_locked(pair): pair, self.strategy.ticker_interval,
logger.info(f"Pair {pair} is currently locked.") self.dataprovider.ohlcv(pair, self.strategy.ticker_interval))
continue
(buy, sell) = self.strategy.get_signal( if buy and not sell:
pair, self.strategy.ticker_interval, if not self.get_free_open_trades():
self.dataprovider.ohlcv(pair, self.strategy.ticker_interval)) logger.debug("Can't open a new trade: max number of trades is reached")
return False
if buy and not sell: stake_amount = self.get_trade_stake_amount(pair)
if not self.get_free_open_trades(): if not stake_amount:
logger.debug("Can't open a new trade: max number of trades is reached") logger.debug("Stake amount is 0, ignoring possible trade for {pair}.")
continue return False
stake_amount = self.get_trade_stake_amount(pair) logger.info(f"Buy signal found: about create a new trade with stake_amount: "
if not stake_amount: f"{stake_amount} ...")
logger.debug("Stake amount is 0, ignoring possible trade for {pair}.")
continue
logger.info(f"Buy signal found: about create a new trade with stake_amount: " bidstrat_check_depth_of_market = self.config.get('bid_strategy', {}).\
f"{stake_amount} ...") get('check_depth_of_market', {})
if (bidstrat_check_depth_of_market.get('enabled', False)) and\
(bidstrat_check_depth_of_market.get('bids_to_ask_delta', 0) > 0):
if self._check_depth_of_market_buy(pair, bidstrat_check_depth_of_market):
return self.execute_buy(pair, stake_amount)
else:
return False
bidstrat_check_depth_of_market = self.config.get('bid_strategy', {}).\ return self.execute_buy(pair, stake_amount)
get('check_depth_of_market', {}) else:
if (bidstrat_check_depth_of_market.get('enabled', False)) and\ return False
(bidstrat_check_depth_of_market.get('bids_to_ask_delta', 0) > 0):
if self._check_depth_of_market_buy(pair, bidstrat_check_depth_of_market):
buycount += self.execute_buy(pair, stake_amount)
continue
buycount += self.execute_buy(pair, stake_amount)
return buycount > 0
def _check_depth_of_market_buy(self, pair: str, conf: Dict) -> bool: def _check_depth_of_market_buy(self, pair: str, conf: Dict) -> bool:
""" """
@ -479,10 +462,31 @@ class FreqtradeBot:
""" """
Tries to execute buy orders for trades in a safe way Tries to execute buy orders for trades in a safe way
""" """
result = False
try: try:
# Create entity and execute trade whitelist = copy.deepcopy(self.active_pair_whitelist)
if not self.create_trades():
if not whitelist:
logger.info("Active pair whitelist is empty.")
else:
# Remove currently opened and latest pairs from whitelist
for trade in Trade.get_open_trades():
if trade.pair in whitelist:
whitelist.remove(trade.pair)
logger.debug('Ignoring %s in pair whitelist', trade.pair)
if not whitelist:
logger.info("No currency pair in active pair whitelist, "
"but checking to sell open trades.")
else:
# Create entity and execute trade for each pair from whitelist
for pair in whitelist:
if self.create_trade(pair):
result = True
if not result:
logger.debug('Found no buy signals for whitelisted currencies. Trying again...') logger.debug('Found no buy signals for whitelisted currencies. Trying again...')
except DependencyException as exception: except DependencyException as exception:
logger.warning('Unable to create trade: %s', exception) logger.warning('Unable to create trade: %s', exception)