Merge pull request #50 from gcarq/feature/fix-whitelist-vanishing

fix whitelist vanishing
This commit is contained in:
Janne Sinivirta 2017-09-30 20:00:38 +03:00 committed by GitHub
commit d73c656514
2 changed files with 29 additions and 24 deletions

View File

@ -6,6 +6,7 @@ import traceback
from datetime import datetime
from typing import Dict, Optional
import copy
from jsonschema import validate
from freqtrade import exchange, persistence, __version__
@ -88,11 +89,7 @@ def execute_sell(trade: Trade, current_rate: float) -> None:
# Get available balance
currency = trade.pair.split('_')[1]
balance = exchange.get_balance(currency)
whitelist = _CONF[trade.exchange.name.lower()]['pair_whitelist']
profit = trade.exec_sell_order(current_rate, balance)
if trade.pair not in whitelist:
whitelist.append(trade.pair)
message = '*{}:* Selling [{}]({}) at rate `{:f} (profit: {}%)`'.format(
trade.exchange.name,
trade.pair.replace('_', '/'),
@ -161,7 +158,7 @@ def create_trade(stake_amount: float, _exchange: exchange.Exchange) -> Optional[
:param _exchange: exchange to use
"""
logger.info('Creating new trade with stake_amount: %f ...', stake_amount)
whitelist = _CONF[_exchange.name.lower()]['pair_whitelist']
whitelist = copy.deepcopy(_CONF[_exchange.name.lower()]['pair_whitelist'])
# Check if stake_amount is fulfilled
if exchange.get_balance(_CONF['stake_currency']) < stake_amount:
raise ValueError(
@ -169,11 +166,7 @@ def create_trade(stake_amount: float, _exchange: exchange.Exchange) -> Optional[
)
# Remove currently opened and latest pairs from whitelist
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
latest_trade = Trade.query.filter(Trade.is_open.is_(False)).order_by(Trade.id.desc()).first()
if latest_trade:
trades.append(latest_trade)
for trade in trades:
for trade in Trade.query.filter(Trade.is_open.is_(True)).all():
if trade.pair in whitelist:
whitelist.remove(trade.pair)
logger.debug('Ignoring %s in pair whitelist', trade.pair)

View File

@ -1,6 +1,7 @@
import unittest
from unittest.mock import patch, MagicMock
from unittest.mock import patch, MagicMock, call
import copy
from jsonschema import validate
from freqtrade import exchange
@ -29,7 +30,10 @@ class TestMain(unittest.TestCase):
"key": "key",
"secret": "secret",
"pair_whitelist": [
"BTC_ETH"
"BTC_ETH",
"BTC_TKN",
"BTC_TRST",
"BTC_SWT",
]
},
"telegram": {
@ -50,19 +54,27 @@ class TestMain(unittest.TestCase):
'last': 0.07256061
}),
buy=MagicMock(return_value='mocked_order_id')):
# Save state of current whitelist
whitelist = copy.deepcopy(self.conf['bittrex']['pair_whitelist'])
init(self.conf, 'sqlite://')
for pair in ['BTC_ETH', 'BTC_TKN', 'BTC_TRST', 'BTC_SWT']:
trade = create_trade(15.0, exchange.Exchange.BITTREX)
Trade.session.add(trade)
Trade.session.flush()
self.assertIsNotNone(trade)
self.assertEqual(trade.open_rate, 0.072661)
self.assertEqual(trade.pair, 'BTC_ETH')
self.assertEqual(trade.pair, pair)
self.assertEqual(trade.exchange, exchange.Exchange.BITTREX)
self.assertEqual(trade.amount, 206.43811673387373)
self.assertEqual(trade.stake_amount, 15.0)
self.assertEqual(trade.is_open, True)
self.assertIsNotNone(trade.open_date)
buy_signal.assert_called_once_with('BTC_ETH')
self.assertEqual(whitelist, self.conf['bittrex']['pair_whitelist'])
buy_signal.assert_has_calls(
[call('BTC_ETH'), call('BTC_TKN'), call('BTC_TRST'), call('BTC_SWT')]
)
def test_2_handle_trade(self):
with patch.dict('freqtrade.main._CONF', self.conf):