return state changes

This commit is contained in:
gcarq 2017-11-07 22:26:08 +01:00
parent f8084b117e
commit ba485fe2b2
2 changed files with 16 additions and 19 deletions

View File

@ -17,7 +17,6 @@ class Exchange(ABC):
Fee for placing an order Fee for placing an order
:return: percentage in float :return: percentage in float
""" """
return 0.0
@property @property
@abstractmethod @abstractmethod

View File

@ -24,12 +24,13 @@ logger = logging.getLogger(__name__)
_CONF = {} _CONF = {}
def _process() -> None: def _process() -> bool:
""" """
Queries the persistence layer for open trades and handles them, Queries the persistence layer for open trades and handles them,
otherwise a new trade is created. otherwise a new trade is created.
:return: None :return: True if a trade has been created or closed, False otherwise
""" """
state_changed = False
try: try:
# Query trades from persistence layer # Query trades from persistence layer
trades = Trade.query.filter(Trade.is_open.is_(True)).all() trades = Trade.query.filter(Trade.is_open.is_(True)).all()
@ -39,6 +40,7 @@ def _process() -> None:
trade = create_trade(float(_CONF['stake_amount'])) trade = create_trade(float(_CONF['stake_amount']))
if trade: if trade:
Trade.session.add(trade) Trade.session.add(trade)
state_changed = True
else: else:
logging.info('Got no buy signal...') logging.info('Got no buy signal...')
except ValueError: except ValueError:
@ -53,7 +55,7 @@ def _process() -> None:
if not close_trade_if_fulfilled(trade): if not close_trade_if_fulfilled(trade):
# Check if we can sell our current pair # Check if we can sell our current pair
handle_trade(trade) state_changed = handle_trade(trade) or state_changed
Trade.session.flush() Trade.session.flush()
except (requests.exceptions.RequestException, json.JSONDecodeError) as error: except (requests.exceptions.RequestException, json.JSONDecodeError) as error:
@ -67,6 +69,7 @@ def _process() -> None:
)) ))
logger.exception('Got RuntimeError. Stopping trader ...') logger.exception('Got RuntimeError. Stopping trader ...')
update_state(State.STOPPED) update_state(State.STOPPED)
return state_changed
def close_trade_if_fulfilled(trade: Trade) -> bool: def close_trade_if_fulfilled(trade: Trade) -> bool:
@ -97,7 +100,6 @@ def execute_sell(trade: Trade, limit: float) -> None:
# Execute sell and update trade record # Execute sell and update trade record
order_id = exchange.sell(str(trade.pair), limit, trade.amount) order_id = exchange.sell(str(trade.pair), limit, trade.amount)
trade.open_order_id = order_id trade.open_order_id = order_id
trade.close_date = datetime.utcnow()
fmt_exp_profit = round(trade.calc_profit(limit) * 100, 2) fmt_exp_profit = round(trade.calc_profit(limit) * 100, 2)
message = '*{}:* Selling [{}]({}) with limit `{:.8f} (profit: ~{:.2f}%)`'.format( message = '*{}:* Selling [{}]({}) with limit `{:.8f} (profit: ~{:.2f}%)`'.format(
@ -131,24 +133,20 @@ def should_sell(trade: Trade, current_rate: float, current_time: datetime) -> bo
return False return False
def handle_trade(trade: Trade) -> None: def handle_trade(trade: Trade) -> bool:
""" """
Sells the current pair if the threshold is reached and updates the trade record. Sells the current pair if the threshold is reached and updates the trade record.
:return: None :return: True if trade has been sold, False otherwise
""" """
try: if not trade.is_open:
if not trade.is_open: raise ValueError('attempt to handle closed trade: {}'.format(trade))
raise ValueError('attempt to handle closed trade: {}'.format(trade))
logger.debug('Handling %s ...', trade) logger.debug('Handling %s ...', trade)
current_rate = exchange.get_ticker(trade.pair)['bid']
current_rate = exchange.get_ticker(trade.pair)['bid'] if should_sell(trade, current_rate, datetime.utcnow()):
if should_sell(trade, current_rate, datetime.utcnow()): execute_sell(trade, current_rate)
execute_sell(trade, current_rate) return True
return return False
except ValueError:
logger.exception('Unable to handle open order')
def get_target_bid(ticker: Dict[str, float]) -> float: def get_target_bid(ticker: Dict[str, float]) -> float: