Refactor create_trades()
This commit is contained in:
parent
433fd2a7c3
commit
762604300f
@ -294,37 +294,19 @@ 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:
|
|
||||||
logger.info("Active pair whitelist is empty.")
|
|
||||||
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
|
|
||||||
for pair in whitelist:
|
|
||||||
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
|
return False
|
||||||
|
|
||||||
|
# running get_signal on historical data fetched
|
||||||
(buy, sell) = self.strategy.get_signal(
|
(buy, sell) = self.strategy.get_signal(
|
||||||
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))
|
||||||
@ -332,12 +314,12 @@ class FreqtradeBot:
|
|||||||
if buy and not sell:
|
if buy and not sell:
|
||||||
if not self.get_free_open_trades():
|
if not self.get_free_open_trades():
|
||||||
logger.debug("Can't open a new trade: max number of trades is reached")
|
logger.debug("Can't open a new trade: max number of trades is reached")
|
||||||
continue
|
return False
|
||||||
|
|
||||||
stake_amount = self.get_trade_stake_amount(pair)
|
stake_amount = self.get_trade_stake_amount(pair)
|
||||||
if not stake_amount:
|
if not stake_amount:
|
||||||
logger.debug("Stake amount is 0, ignoring possible trade for {pair}.")
|
logger.debug("Stake amount is 0, ignoring possible trade for {pair}.")
|
||||||
continue
|
return False
|
||||||
|
|
||||||
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} ...")
|
||||||
@ -347,12 +329,13 @@ class FreqtradeBot:
|
|||||||
if (bidstrat_check_depth_of_market.get('enabled', False)) and\
|
if (bidstrat_check_depth_of_market.get('enabled', False)) and\
|
||||||
(bidstrat_check_depth_of_market.get('bids_to_ask_delta', 0) > 0):
|
(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):
|
if self._check_depth_of_market_buy(pair, bidstrat_check_depth_of_market):
|
||||||
buycount += self.execute_buy(pair, stake_amount)
|
return self.execute_buy(pair, stake_amount)
|
||||||
continue
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
buycount += self.execute_buy(pair, stake_amount)
|
return self.execute_buy(pair, stake_amount)
|
||||||
|
else:
|
||||||
return buycount > 0
|
return False
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user