adapt tests

This commit is contained in:
gcarq 2017-11-01 00:26:32 +01:00
parent 0d0d822904
commit 1e5b0e8726
5 changed files with 127 additions and 42 deletions

View File

@ -11,7 +11,7 @@ from freqtrade.analyze import analyze_ticker
from freqtrade.main import should_sell from freqtrade.main import should_sell
from freqtrade.persistence import Trade from freqtrade.persistence import Trade
logging.disable(logging.DEBUG) # disable debug logs that slow backtesting a lot logging.disable(logging.DEBUG) # disable debug logs that slow backtesting a lot
def format_results(results): def format_results(results):
@ -63,7 +63,7 @@ def backtest(conf, pairs, mocker):
# calculate win/lose forwards from buy point # calculate win/lose forwards from buy point
for row2 in ticker[row.Index:].itertuples(index=True): for row2 in ticker[row.Index:].itertuples(index=True):
if should_sell(trade, row2.close, row2.date): if should_sell(trade, row2.close, row2.date):
current_profit = (row2.close - trade.open_rate) / trade.open_rate current_profit = trade.calc_profit(row2.close)
trades.append((pair, current_profit, row2.Index - row.Index)) trades.append((pair, current_profit, row2.Index - row.Index))
break break

View File

@ -12,7 +12,7 @@ from pandas import DataFrame
from freqtrade.tests.test_backtesting import backtest, format_results from freqtrade.tests.test_backtesting import backtest, format_results
from freqtrade.vendor.qtpylib.indicators import crossed_above from freqtrade.vendor.qtpylib.indicators import crossed_above
logging.disable(logging.DEBUG) # disable debug logs that slow backtesting a lot logging.disable(logging.DEBUG) # disable debug logs that slow backtesting a lot
# set TARGET_TRADES to suit your number concurrent trades so its realistic to 20days of data # set TARGET_TRADES to suit your number concurrent trades so its realistic to 20days of data
TARGET_TRADES = 1200 TARGET_TRADES = 1200

View File

@ -1,5 +1,6 @@
# pragma pylint: disable=missing-docstring # pragma pylint: disable=missing-docstring
import copy import copy
from datetime import datetime
from unittest.mock import MagicMock, call from unittest.mock import MagicMock, call
import pytest import pytest
@ -60,23 +61,36 @@ def test_create_trade(conf, mocker):
'ask': 0.072661, 'ask': 0.072661,
'last': 0.07256061 'last': 0.07256061
}), }),
buy=MagicMock(return_value='mocked_order_id')) buy=MagicMock(return_value='mocked_limit_buy'))
# Save state of current whitelist # Save state of current whitelist
whitelist = copy.deepcopy(conf['exchange']['pair_whitelist']) whitelist = copy.deepcopy(conf['exchange']['pair_whitelist'])
init(conf, 'sqlite://') init(conf, 'sqlite://')
for pair in ['BTC_ETH', 'BTC_TKN', 'BTC_TRST', 'BTC_SWT']: for _ in ['BTC_ETH', 'BTC_TKN', 'BTC_TRST', 'BTC_SWT']:
trade = create_trade(15.0) trade = create_trade(15.0)
Trade.session.add(trade) Trade.session.add(trade)
Trade.session.flush() Trade.session.flush()
assert trade is not None assert trade is not None
assert trade.open_rate == 0.072661
assert trade.pair == pair
assert trade.exchange == Exchanges.BITTREX.name
assert trade.amount == 206.43811673387373
assert trade.stake_amount == 15.0 assert trade.stake_amount == 15.0
assert trade.is_open assert trade.is_open
assert trade.open_date is not None assert trade.open_date is not None
assert trade.exchange == Exchanges.BITTREX.name
# Simulate fulfilled LIMIT_BUY order for trade
trade.update({
'id': 'mocked_limit_buy',
'type': 'LIMIT_BUY',
'pair': 'mocked',
'opened': datetime.utcnow(),
'rate': 0.072661,
'amount': 206.43811673387373,
'remaining': 0.0,
'closed': datetime.utcnow(),
})
assert trade.open_rate == 0.072661
assert trade.amount == 206.43811673387373
assert whitelist == conf['exchange']['pair_whitelist'] assert whitelist == conf['exchange']['pair_whitelist']
buy_signal.assert_has_calls( buy_signal.assert_has_calls(
@ -94,14 +108,28 @@ def test_handle_trade(conf, mocker):
'ask': 0.172661, 'ask': 0.172661,
'last': 0.17256061 'last': 0.17256061
}), }),
buy=MagicMock(return_value='mocked_order_id')) sell=MagicMock(return_value='mocked_limit_sell'))
trade = Trade.query.filter(Trade.is_open.is_(True)).first() trade = Trade.query.filter(Trade.is_open.is_(True)).first()
assert trade assert trade
handle_trade(trade) handle_trade(trade)
assert trade.open_order_id == 'mocked_limit_sell'
# Simulate fulfilled LIMIT_SELL order for trade
trade.update({
'id': 'mocked_sell_limit',
'type': 'LIMIT_SELL',
'pair': 'mocked',
'opened': datetime.utcnow(),
'rate': 0.17256061,
'amount': 206.43811673387373,
'remaining': 0.0,
'closed': datetime.utcnow(),
})
assert trade.close_rate == 0.17256061 assert trade.close_rate == 0.17256061
assert trade.close_profit == 137.4872490056564 assert trade.close_profit == 1.3748724900565639
assert trade.close_date is not None assert trade.close_date is not None
assert trade.open_order_id == 'dry_run'
def test_close_trade(conf, mocker): def test_close_trade(conf, mocker):

View File

@ -1,21 +0,0 @@
# pragma pylint: disable=missing-docstring
from freqtrade.exchange import Exchanges
from freqtrade.persistence import Trade
def test_exec_sell_order(mocker):
api_mock = mocker.patch('freqtrade.main.exchange.sell', side_effect='mocked_order_id')
trade = Trade(
pair='BTC_ETH',
stake_amount=1.00,
open_rate=0.50,
amount=10.00,
exchange=Exchanges.BITTREX,
open_order_id='mocked'
)
profit = trade.exec_sell_order(1.00, 10.00)
api_mock.assert_called_once_with('BTC_ETH', 1.0, 10.0)
assert profit == 100.0
assert trade.close_rate == 1.0
assert trade.close_profit == profit
assert trade.close_date is not None

View File

@ -46,6 +46,7 @@ def conf():
validate(configuration, CONF_SCHEMA) validate(configuration, CONF_SCHEMA)
return configuration return configuration
@pytest.fixture @pytest.fixture
def update(): def update():
_update = Update(0) _update = Update(0)
@ -78,8 +79,27 @@ def test_status_handle(conf, update, mocker):
Trade.session.add(trade) Trade.session.add(trade)
Trade.session.flush() Trade.session.flush()
# Trigger status while we don't know the open_rate yet
_status(bot=MagicBot(), update=update) _status(bot=MagicBot(), update=update)
assert msg_mock.call_count == 2
# Simulate fulfilled LIMIT_BUY order for trade
trade.update({
'id': 'mocked_limit_buy',
'type': 'LIMIT_BUY',
'pair': 'mocked',
'opened': datetime.utcnow(),
'rate': 0.07256060,
'amount': 206.43811673387373,
'remaining': 0.0,
'closed': datetime.utcnow(),
})
Trade.session.flush()
# Trigger status while we have a fulfilled order for the open trade
_status(bot=MagicBot(), update=update)
assert msg_mock.call_count == 3
assert 'Waiting until order is fulfilled' in msg_mock.call_args_list[-2][0][0]
assert '[BTC_ETH]' in msg_mock.call_args_list[-1][0][0] assert '[BTC_ETH]' in msg_mock.call_args_list[-1][0][0]
@ -95,14 +115,36 @@ def test_profit_handle(conf, update, mocker):
'ask': 0.072661, 'ask': 0.072661,
'last': 0.07256061 'last': 0.07256061
}), }),
buy=MagicMock(return_value='mocked_order_id')) buy=MagicMock(return_value='mocked_limit_buy'))
init(conf, 'sqlite://') init(conf, 'sqlite://')
# Create some test data # Create some test data
trade = create_trade(15.0) trade = create_trade(15.0)
assert trade assert trade
trade.close_rate = 0.07256061
trade.close_profit = 100.00 # Simulate fulfilled LIMIT_BUY order for trade
trade.update({
'id': 'mocked_limit_buy',
'type': 'LIMIT_BUY',
'pair': 'mocked',
'opened': datetime.utcnow(),
'rate': 0.07256061,
'amount': 206.43811673387373,
'remaining': 0.0,
'closed': datetime.utcnow(),
})
# Simulate fulfilled LIMIT_SELL order for trade
trade.update({
'id': 'mocked_limit_sell',
'type': 'LIMIT_SELL',
'pair': 'mocked',
'opened': datetime.utcnow(),
'rate': 0.0802134,
'amount': 206.43811673387373,
'remaining': 0.0,
'closed': datetime.utcnow(),
})
trade.close_date = datetime.utcnow() trade.close_date = datetime.utcnow()
trade.open_order_id = None trade.open_order_id = None
trade.is_open = False trade.is_open = False
@ -111,7 +153,8 @@ def test_profit_handle(conf, update, mocker):
_profit(bot=MagicBot(), update=update) _profit(bot=MagicBot(), update=update)
assert msg_mock.call_count == 2 assert msg_mock.call_count == 2
assert '(100.00%)' in msg_mock.call_args_list[-1][0][0] assert '*ROI:* `1.582013 (10.55%)`' in msg_mock.call_args_list[-1][0][0]
assert 'Best Performing:* `BTC_ETH: 10.55%`' in msg_mock.call_args_list[-1][0][0]
def test_forcesell_handle(conf, update, mocker): def test_forcesell_handle(conf, update, mocker):
@ -132,6 +175,19 @@ def test_forcesell_handle(conf, update, mocker):
# Create some test data # Create some test data
trade = create_trade(15.0) trade = create_trade(15.0)
assert trade assert trade
# Simulate fulfilled LIMIT_BUY order for trade
trade.update({
'id': 'mocked_limit_buy',
'type': 'LIMIT_BUY',
'pair': 'mocked',
'opened': datetime.utcnow(),
'rate': 0.07256060,
'amount': 206.43811673387373,
'remaining': 0.0,
'closed': datetime.utcnow(),
})
Trade.session.add(trade) Trade.session.add(trade)
Trade.session.flush() Trade.session.flush()
@ -161,10 +217,32 @@ def test_performance_handle(conf, update, mocker):
# Create some test data # Create some test data
trade = create_trade(15.0) trade = create_trade(15.0)
assert trade assert trade
trade.close_rate = 0.07256061
trade.close_profit = 100.00 # Simulate fulfilled LIMIT_BUY order for trade
trade.update({
'id': 'mocked_limit_buy',
'type': 'LIMIT_BUY',
'pair': 'mocked',
'opened': datetime.utcnow(),
'rate': 0.07256061,
'amount': 206.43811673387373,
'remaining': 0.0,
'closed': datetime.utcnow(),
})
# Simulate fulfilled LIMIT_SELL order for trade
trade.update({
'id': 'mocked_limit_sell',
'type': 'LIMIT_SELL',
'pair': 'mocked',
'opened': datetime.utcnow(),
'rate': 0.0802134,
'amount': 206.43811673387373,
'remaining': 0.0,
'closed': datetime.utcnow(),
})
trade.close_date = datetime.utcnow() trade.close_date = datetime.utcnow()
trade.open_order_id = None
trade.is_open = False trade.is_open = False
Trade.session.add(trade) Trade.session.add(trade)
Trade.session.flush() Trade.session.flush()
@ -172,7 +250,7 @@ def test_performance_handle(conf, update, mocker):
_performance(bot=MagicBot(), update=update) _performance(bot=MagicBot(), update=update)
assert msg_mock.call_count == 2 assert msg_mock.call_count == 2
assert 'Performance' in msg_mock.call_args_list[-1][0][0] assert 'Performance' in msg_mock.call_args_list[-1][0][0]
assert 'BTC_ETH 100.00%' in msg_mock.call_args_list[-1][0][0] assert '<code>BTC_ETH\t10.55%</code>' in msg_mock.call_args_list[-1][0][0]
def test_start_handle(conf, update, mocker): def test_start_handle(conf, update, mocker):