implement support for concurrent open trades
This commit is contained in:
parent
3fa8aae66d
commit
d572b7bdbe
24
main.py
24
main.py
@ -24,7 +24,6 @@ __license__ = "custom"
|
|||||||
__version__ = "0.5.1"
|
__version__ = "0.5.1"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
conf = get_conf()
|
conf = get_conf()
|
||||||
api_wrapper = get_exchange_api(conf)
|
api_wrapper = get_exchange_api(conf)
|
||||||
|
|
||||||
@ -79,12 +78,14 @@ class TradeThread(threading.Thread):
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
# Query trades from persistence layer
|
# Query trades from persistence layer
|
||||||
trade = Trade.query.filter(Trade.is_open.is_(True)).first()
|
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
|
||||||
if not trade:
|
if len(trades) < conf['max_open_trades']:
|
||||||
# Create entity and execute trade
|
# Create entity and execute trade
|
||||||
|
try:
|
||||||
Session.add(create_trade(float(conf['stake_amount']), api_wrapper.exchange))
|
Session.add(create_trade(float(conf['stake_amount']), api_wrapper.exchange))
|
||||||
return
|
except ValueError:
|
||||||
|
logger.exception('ValueError during trade creation')
|
||||||
|
for trade in trades:
|
||||||
# Check if there is already an open order for this trade
|
# Check if there is already an open order for this trade
|
||||||
orders = api_wrapper.get_open_orders(trade.pair)
|
orders = api_wrapper.get_open_orders(trade.pair)
|
||||||
orders = [o for o in orders if o['id'] == trade.open_order_id]
|
orders = [o for o in orders if o['id'] == trade.open_order_id]
|
||||||
@ -95,14 +96,14 @@ class TradeThread(threading.Thread):
|
|||||||
orders[0]['type'],
|
orders[0]['type'],
|
||||||
orders[0]['id'])
|
orders[0]['id'])
|
||||||
logger.info(msg)
|
logger.info(msg)
|
||||||
return
|
continue
|
||||||
|
|
||||||
# Update state
|
# Update state
|
||||||
trade.open_order_id = None
|
trade.open_order_id = None
|
||||||
# Check if this trade can be marked as closed
|
# Check if this trade can be marked as closed
|
||||||
if close_trade_if_fulfilled(trade):
|
if close_trade_if_fulfilled(trade):
|
||||||
logger.info('No open orders found and trade is fulfilled. Marking as closed ...')
|
logger.info('No open orders found and trade is fulfilled. Marking as closed ...')
|
||||||
return
|
continue
|
||||||
|
|
||||||
# Check if we can sell our current pair
|
# Check if we can sell our current pair
|
||||||
handle_trade(trade)
|
handle_trade(trade)
|
||||||
@ -178,16 +179,21 @@ def create_trade(stake_amount: float, exchange):
|
|||||||
:param stake_amount: amount of btc to spend
|
:param stake_amount: amount of btc to spend
|
||||||
:param exchange: exchange to use
|
:param exchange: exchange to use
|
||||||
"""
|
"""
|
||||||
|
logger.info('Creating new trade with stake_amount: {} ...'.format(stake_amount))
|
||||||
whitelist = conf[exchange.name.lower()]['pair_whitelist']
|
whitelist = conf[exchange.name.lower()]['pair_whitelist']
|
||||||
# Check if btc_amount is fulfilled
|
# Check if btc_amount is fulfilled
|
||||||
if api_wrapper.get_balance('BTC') < stake_amount:
|
if api_wrapper.get_balance('BTC') < stake_amount:
|
||||||
raise ValueError('BTC amount is not fulfilled')
|
raise ValueError('BTC amount is not fulfilled')
|
||||||
|
|
||||||
# Remove latest trade pair from whitelist
|
# Remove currently opened and latest pairs from whitelist
|
||||||
latest_trade = Trade.query.order_by(Trade.id.desc()).first()
|
latest_trade = Trade.query.filter(Trade.is_open.is_(False)).order_by(Trade.id.desc()).first()
|
||||||
|
open_trades = Trade.query.filter(Trade.is_open.is_(True)).all()
|
||||||
if latest_trade and latest_trade.pair in whitelist:
|
if latest_trade and latest_trade.pair in whitelist:
|
||||||
whitelist.remove(latest_trade.pair)
|
whitelist.remove(latest_trade.pair)
|
||||||
logger.debug('Ignoring {} in pair whitelist'.format(latest_trade.pair))
|
logger.debug('Ignoring {} in pair whitelist'.format(latest_trade.pair))
|
||||||
|
for trade in open_trades:
|
||||||
|
whitelist.remove(trade.pair)
|
||||||
|
logger.debug('Ignoring {} in pair whitelist'.format(trade.pair))
|
||||||
if not whitelist:
|
if not whitelist:
|
||||||
raise ValueError('No pair in whitelist')
|
raise ValueError('No pair in whitelist')
|
||||||
|
|
||||||
|
@ -54,18 +54,20 @@ class TelegramHandler(object):
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
# Fetch open trade
|
# Fetch open trade
|
||||||
trade = Trade.query.filter(Trade.is_open.is_(True)).first()
|
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
|
||||||
from main import get_instance
|
from main import get_instance
|
||||||
if not get_instance().is_alive():
|
if not get_instance().is_alive():
|
||||||
message = '*Status:* `trader stopped`'
|
TelegramHandler.send_msg('*Status:* `trader stopped`', bot=bot)
|
||||||
elif not trade:
|
elif not trades:
|
||||||
message = '*Status:* `no active order`'
|
TelegramHandler.send_msg('*Status:* `no active order`', bot=bot)
|
||||||
else:
|
else:
|
||||||
|
for trade in trades:
|
||||||
# calculate profit and send message to user
|
# calculate profit and send message to user
|
||||||
current_rate = api_wrapper.get_ticker(trade.pair)['bid']
|
current_rate = api_wrapper.get_ticker(trade.pair)['bid']
|
||||||
current_profit = 100 * ((current_rate - trade.open_rate) / trade.open_rate)
|
current_profit = 100 * ((current_rate - trade.open_rate) / trade.open_rate)
|
||||||
open_orders = api_wrapper.get_open_orders(trade.pair)
|
orders = api_wrapper.get_open_orders(trade.pair)
|
||||||
order = open_orders[0] if open_orders else None
|
orders = [o for o in orders if o['id'] == trade.open_order_id]
|
||||||
|
order = orders[0] if orders else None
|
||||||
message = """
|
message = """
|
||||||
*Current Pair:* [{pair}](https://bittrex.com/Market/Index?MarketName={pair})
|
*Current Pair:* [{pair}](https://bittrex.com/Market/Index?MarketName={pair})
|
||||||
*Open Since:* `{date}`
|
*Open Since:* `{date}`
|
||||||
@ -167,6 +169,10 @@ class TelegramHandler(object):
|
|||||||
:param update: message update
|
:param update: message update
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
|
# TODO: make compatible with max_open_orders
|
||||||
|
TelegramHandler.send_msg('`Currently not implemented`')
|
||||||
|
return
|
||||||
|
|
||||||
trade = Trade.query.filter(Trade.is_open.is_(True)).first()
|
trade = Trade.query.filter(Trade.is_open.is_(True)).first()
|
||||||
if not trade:
|
if not trade:
|
||||||
TelegramHandler.send_msg('`There is no open trade`')
|
TelegramHandler.send_msg('`There is no open trade`')
|
||||||
|
2
utils.py
2
utils.py
@ -28,6 +28,8 @@ def validate_conf(conf):
|
|||||||
:param conf: config as dict
|
:param conf: config as dict
|
||||||
:return: None, raises ValueError if something is wrong
|
:return: None, raises ValueError if something is wrong
|
||||||
"""
|
"""
|
||||||
|
if not isinstance(conf.get('max_open_trades'), int):
|
||||||
|
raise ValueError('max_open_trades must be a int')
|
||||||
if not isinstance(conf.get('stake_amount'), float):
|
if not isinstance(conf.get('stake_amount'), float):
|
||||||
raise ValueError('stake_amount must be a float')
|
raise ValueError('stake_amount must be a float')
|
||||||
if not isinstance(conf.get('dry_run'), bool):
|
if not isinstance(conf.get('dry_run'), bool):
|
||||||
|
Loading…
Reference in New Issue
Block a user