Introduce get_free_open_trades() method

This commit is contained in:
hroff-1902 2019-12-28 03:46:42 +03:00
parent 8eeabd2372
commit 3dbd83e35a
1 changed files with 15 additions and 7 deletions

View File

@ -136,7 +136,7 @@ class FreqtradeBot:
self.process_maybe_execute_sells(trades) self.process_maybe_execute_sells(trades)
# Then looking for buy opportunities # Then looking for buy opportunities
if len(trades) < self.config['max_open_trades']: if self.get_free_open_trades():
self.process_maybe_execute_buys() self.process_maybe_execute_buys()
# Check and handle any timed out open orders # Check and handle any timed out open orders
@ -173,6 +173,14 @@ class FreqtradeBot:
""" """
return [(pair, self.config['ticker_interval']) for pair in pairs] return [(pair, self.config['ticker_interval']) for pair in pairs]
def get_free_open_trades(self):
"""
Return the number of free open trades slots or 0 if
max number of open trades reached
"""
open_trades = len(Trade.get_open_trades())
return max(0, self.config['max_open_trades'] - open_trades)
def get_target_bid(self, pair: str, tick: Dict = None) -> float: def get_target_bid(self, pair: str, tick: Dict = None) -> float:
""" """
Calculates bid target between current ask price and last price Calculates bid target between current ask price and last price
@ -229,11 +237,11 @@ class FreqtradeBot:
Calculate stake amount for "unlimited" stake amount Calculate stake amount for "unlimited" stake amount
:return: None if max number of trades reached :return: None if max number of trades reached
""" """
open_trades = len(Trade.get_open_trades()) free_open_trades = self.get_free_open_trades()
if open_trades >= self.config['max_open_trades']: if not free_open_trades:
return None return None
available_amount = self.wallets.get_free(self.config['stake_currency']) available_amount = self.wallets.get_free(self.config['stake_currency'])
return available_amount / (self.config['max_open_trades'] - open_trades) return available_amount / free_open_trades
def _check_available_stake_amount(self, stake_amount: Optional[float]) -> Optional[float]: def _check_available_stake_amount(self, stake_amount: Optional[float]) -> Optional[float]:
""" """
@ -321,12 +329,12 @@ class FreqtradeBot:
pair, self.strategy.ticker_interval, pair, self.strategy.ticker_interval,
self.dataprovider.ohlcv(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:
stake_amount = self.get_trade_stake_amount(pair) if not self.get_free_open_trades():
if stake_amount is None:
logger.warning("Can't open a new trade: max number of trades is reached") logger.warning("Can't open a new trade: max number of trades is reached")
continue continue
stake_amount = self.get_trade_stake_amount(pair)
logger.info(f"Buy signal found: about create a new trade with stake_amount: " logger.info(f"Buy signal found: about create a new trade with stake_amount: "
f"{stake_amount} ...") f"{stake_amount} ...")