Skip to content

Commit

Permalink
Refactor Freqtradebot
Browse files Browse the repository at this point in the history
  • Loading branch information
hroff-1902 committed Oct 2, 2019
1 parent 8c5b299 commit 096c69d
Showing 1 changed file with 30 additions and 36 deletions.
66 changes: 30 additions & 36 deletions freqtrade/freqtradebot.py
@@ -1,7 +1,6 @@
"""
Freqtrade is the main module of this bot. It contains the class Freqtrade()
"""

import copy
import logging
import traceback
Expand Down Expand Up @@ -135,12 +134,11 @@ def process(self) -> None:
self.strategy.informative_pairs())

# First process current opened trades
for trade in trades:
self.process_maybe_execute_sell(trade)
self.process_maybe_execute_sells(trades)

# Then looking for buy opportunities
if len(trades) < self.config['max_open_trades']:
self.process_maybe_execute_buy()
self.process_maybe_execute_buys()

if 'unfilledtimeout' in self.config:
# Check and handle any timed out open orders
Expand Down Expand Up @@ -262,11 +260,10 @@ def create_trades(self) -> bool:
Checks pairs as long as the open trade count is below `max_open_trades`.
:return: True if at least one trade has been created.
"""
interval = self.strategy.ticker_interval
whitelist = copy.deepcopy(self.active_pair_whitelist)

if not whitelist:
logger.warning("Whitelist is empty.")
logger.info("Active pair whitelist is empty.")
return False

# Remove currently opened and latest pairs from whitelist
Expand All @@ -276,7 +273,8 @@ def create_trades(self) -> bool:
logger.debug('Ignoring %s in pair whitelist', trade.pair)

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

buycount = 0
Expand All @@ -285,8 +283,10 @@ def create_trades(self) -> bool:
if self.strategy.is_pair_locked(_pair):
logger.info(f"Pair {_pair} is currently locked.")
continue

(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']:
stake_amount = self._get_trade_stake_amount(_pair)
Expand Down Expand Up @@ -431,10 +431,9 @@ def execute_buy(self, pair: str, stake_amount: float, price: Optional[float] = N

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
:return: True if executed
Tries to execute buy trades in a safe way
"""
try:
# Create entity and execute trade
Expand All @@ -443,33 +442,28 @@ def process_maybe_execute_buy(self) -> None:
except DependencyException as 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
:return: True if executed
Tries to execute sell trades in a safe way
"""
try:
self.update_trade_state(trade)

if self.strategy.order_types.get('stoploss_on_exchange') and trade.is_open:
result = self.handle_stoploss_on_exchange(trade)
if result:
self.wallets.update()
return result

if trade.is_open and trade.open_order_id is None:
# Check if we can sell our current pair
result = self.handle_trade(trade)

# Updating wallets if any trade occured
if result:
self.wallets.update()

return result

except DependencyException as exception:
logger.warning('Unable to sell trade: %s', exception)
return False
for trade in trades:
try:
self.update_trade_state(trade)

if trade.is_open:
result = False
if self.strategy.order_types.get('stoploss_on_exchange'):
result = self.handle_stoploss_on_exchange(trade)
elif trade.open_order_id is None:
# Check if we can sell our current pair
result = self.handle_trade(trade)

# Updating wallets if any trade occured
if result:
self.wallets.update()

except DependencyException as exception:
logger.warning('Unable to sell trade: %s', exception)

def get_real_amount(self, trade: Trade, order: Dict) -> float:
"""
Expand Down

0 comments on commit 096c69d

Please sign in to comment.