refactor _process to update trade state

This commit is contained in:
gcarq 2017-11-01 00:25:12 +01:00
parent 0e96197a94
commit 9ff4a7b205

View File

@ -8,6 +8,7 @@ from datetime import datetime
from typing import Dict, Optional from typing import Dict, Optional
from signal import signal, SIGINT, SIGABRT, SIGTERM from signal import signal, SIGINT, SIGABRT, SIGTERM
import requests
from jsonschema import validate from jsonschema import validate
from freqtrade import __version__, exchange, persistence from freqtrade import __version__, exchange, persistence
@ -44,22 +45,21 @@ def _process() -> None:
logger.exception('Unable to create trade') logger.exception('Unable to create trade')
for trade in trades: for trade in trades:
# Check if there is already an open order for this trade # Get order details for actual price per unit
orders = exchange.get_open_orders(trade.pair) if trade.open_order_id:
orders = [o for o in orders if o['id'] == trade.open_order_id] # Update trade with order values
if orders: logger.info('Got open order for %s', trade)
logger.info('There is an open order for: %s', orders[0]) trade.update(exchange.get_order(trade.open_order_id))
else:
# Update state if not close_trade_if_fulfilled(trade) and trade.open_rate:
trade.open_order_id = None
# Check if this trade can be closed
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) handle_trade(trade)
Trade.session.flush() Trade.session.flush()
except (ConnectionError, json.JSONDecodeError) as error: except (requests.exceptions.ConnectionError, json.JSONDecodeError) as error:
msg = 'Got {} in _process()'.format(error.__class__.__name__) msg = 'Got {} in _process(), retrying in 30 seconds...'.format(error.__class__.__name__)
logger.exception(msg) logger.exception(msg)
time.sleep(30)
def close_trade_if_fulfilled(trade: Trade) -> bool: def close_trade_if_fulfilled(trade: Trade) -> bool:
@ -133,7 +133,7 @@ def handle_trade(trade: Trade) -> None:
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 open trade %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()):