2018-01-28 07:38:41 +00:00
|
|
|
# pragma pylint: disable=missing-docstring, C0103
|
2018-08-16 11:15:46 +00:00
|
|
|
import logging
|
2019-05-06 04:56:22 +00:00
|
|
|
from unittest.mock import MagicMock
|
2018-03-17 21:44:47 +00:00
|
|
|
|
2019-05-06 04:56:22 +00:00
|
|
|
import arrow
|
2017-11-09 19:02:41 +00:00
|
|
|
import pytest
|
2018-01-26 05:05:10 +00:00
|
|
|
from sqlalchemy import create_engine
|
2018-03-17 21:44:47 +00:00
|
|
|
|
2019-12-30 14:02:17 +00:00
|
|
|
from freqtrade import constants
|
|
|
|
from freqtrade.exceptions import OperationalException
|
2018-07-04 07:31:35 +00:00
|
|
|
from freqtrade.persistence import Trade, clean_dry_run_db, init
|
2020-04-07 17:42:16 +00:00
|
|
|
from tests.conftest import log_has, create_mock_trades
|
2019-06-16 08:57:21 +00:00
|
|
|
|
|
|
|
|
2018-06-23 11:50:22 +00:00
|
|
|
def test_init_create_session(default_conf):
|
2017-12-27 10:41:11 +00:00
|
|
|
# Check if init create a session
|
2019-05-29 17:54:47 +00:00
|
|
|
init(default_conf['db_url'], default_conf['dry_run'])
|
2017-12-27 10:41:11 +00:00
|
|
|
assert hasattr(Trade, 'session')
|
2019-10-29 13:26:03 +00:00
|
|
|
assert 'scoped_session' in type(Trade.session).__name__
|
2017-12-27 10:41:11 +00:00
|
|
|
|
|
|
|
|
2018-06-07 03:27:55 +00:00
|
|
|
def test_init_custom_db_url(default_conf, mocker):
|
|
|
|
# Update path to a value other than default, but still in-memory
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf.update({'db_url': 'sqlite:///tmp/freqtrade2_test.sqlite'})
|
2018-06-07 18:41:52 +00:00
|
|
|
create_engine_mock = mocker.patch('freqtrade.persistence.create_engine', MagicMock())
|
2017-12-27 10:41:11 +00:00
|
|
|
|
2019-05-29 17:54:47 +00:00
|
|
|
init(default_conf['db_url'], default_conf['dry_run'])
|
2018-06-07 18:41:52 +00:00
|
|
|
assert create_engine_mock.call_count == 1
|
2018-06-07 19:35:57 +00:00
|
|
|
assert create_engine_mock.mock_calls[0][1][0] == 'sqlite:///tmp/freqtrade2_test.sqlite'
|
|
|
|
|
|
|
|
|
2018-06-23 11:50:22 +00:00
|
|
|
def test_init_invalid_db_url(default_conf):
|
2018-06-07 19:35:57 +00:00
|
|
|
# Update path to a value other than default, but still in-memory
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf.update({'db_url': 'unknown:///some.url'})
|
2018-06-07 19:35:57 +00:00
|
|
|
with pytest.raises(OperationalException, match=r'.*no valid database URL*'):
|
2019-05-29 17:54:47 +00:00
|
|
|
init(default_conf['db_url'], default_conf['dry_run'])
|
2017-12-27 10:41:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_init_prod_db(default_conf, mocker):
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf.update({'dry_run': False})
|
|
|
|
default_conf.update({'db_url': constants.DEFAULT_DB_PROD_URL})
|
2017-12-27 10:41:11 +00:00
|
|
|
|
2018-06-07 18:41:52 +00:00
|
|
|
create_engine_mock = mocker.patch('freqtrade.persistence.create_engine', MagicMock())
|
|
|
|
|
2019-05-29 17:54:47 +00:00
|
|
|
init(default_conf['db_url'], default_conf['dry_run'])
|
2018-06-07 18:41:52 +00:00
|
|
|
assert create_engine_mock.call_count == 1
|
|
|
|
assert create_engine_mock.mock_calls[0][1][0] == 'sqlite:///tradesv3.sqlite'
|
|
|
|
|
|
|
|
|
|
|
|
def test_init_dryrun_db(default_conf, mocker):
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf.update({'dry_run': True})
|
|
|
|
default_conf.update({'db_url': constants.DEFAULT_DB_DRYRUN_URL})
|
2018-06-07 18:41:52 +00:00
|
|
|
|
|
|
|
create_engine_mock = mocker.patch('freqtrade.persistence.create_engine', MagicMock())
|
|
|
|
|
2019-05-29 17:54:47 +00:00
|
|
|
init(default_conf['db_url'], default_conf['dry_run'])
|
2018-06-07 18:41:52 +00:00
|
|
|
assert create_engine_mock.call_count == 1
|
2019-12-22 09:16:16 +00:00
|
|
|
assert create_engine_mock.mock_calls[0][1][0] == 'sqlite:///tradesv3.dryrun.sqlite'
|
2017-11-09 19:02:41 +00:00
|
|
|
|
|
|
|
|
2018-04-07 18:06:53 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2018-12-27 10:19:26 +00:00
|
|
|
def test_update_with_bittrex(limit_buy_order, limit_sell_order, fee, caplog):
|
2017-12-17 21:07:56 +00:00
|
|
|
"""
|
|
|
|
On this test we will buy and sell a crypto currency.
|
|
|
|
|
|
|
|
Buy
|
2017-12-19 05:58:02 +00:00
|
|
|
- Buy: 90.99181073 Crypto at 0.00001099 BTC
|
|
|
|
(90.99181073*0.00001099 = 0.0009999 BTC)
|
2017-12-17 21:07:56 +00:00
|
|
|
- Buying fee: 0.25%
|
2017-12-19 05:58:02 +00:00
|
|
|
- Total cost of buy trade: 0.001002500 BTC
|
|
|
|
((90.99181073*0.00001099) + ((90.99181073*0.00001099)*0.0025))
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
Sell
|
2017-12-19 05:58:02 +00:00
|
|
|
- Sell: 90.99181073 Crypto at 0.00001173 BTC
|
|
|
|
(90.99181073*0.00001173 = 0,00106733394 BTC)
|
2017-12-17 21:07:56 +00:00
|
|
|
- Selling fee: 0.25%
|
2017-12-19 05:58:02 +00:00
|
|
|
- Total cost of sell trade: 0.001064666 BTC
|
|
|
|
((90.99181073*0.00001173) - ((90.99181073*0.00001173)*0.0025))
|
2017-12-17 21:07:56 +00:00
|
|
|
|
2017-12-19 05:58:02 +00:00
|
|
|
Profit/Loss: +0.000062166 BTC
|
|
|
|
(Sell:0.001064666 - Buy:0.001002500)
|
|
|
|
Profit/Loss percentage: 0.0620
|
|
|
|
((0.001064666/0.001002500)-1 = 6.20%)
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
:param limit_buy_order:
|
|
|
|
:param limit_sell_order:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
2017-11-09 19:02:41 +00:00
|
|
|
trade = Trade(
|
2018-12-27 10:19:26 +00:00
|
|
|
id=2,
|
2018-02-03 16:15:40 +00:00
|
|
|
pair='ETH/BTC',
|
2017-12-17 21:07:56 +00:00
|
|
|
stake_amount=0.001,
|
2019-12-17 05:58:10 +00:00
|
|
|
open_rate=0.01,
|
|
|
|
amount=5,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2018-03-25 10:57:59 +00:00
|
|
|
exchange='bittrex',
|
2017-11-09 19:02:41 +00:00
|
|
|
)
|
|
|
|
assert trade.open_order_id is None
|
|
|
|
assert trade.close_profit is None
|
|
|
|
assert trade.close_date is None
|
|
|
|
|
|
|
|
trade.open_order_id = 'something'
|
|
|
|
trade.update(limit_buy_order)
|
|
|
|
assert trade.open_order_id is None
|
2017-12-17 21:07:56 +00:00
|
|
|
assert trade.open_rate == 0.00001099
|
2017-11-09 19:02:41 +00:00
|
|
|
assert trade.close_profit is None
|
|
|
|
assert trade.close_date is None
|
2018-12-27 10:19:26 +00:00
|
|
|
assert log_has("LIMIT_BUY has been fulfilled for Trade(id=2, "
|
|
|
|
"pair=ETH/BTC, amount=90.99181073, open_rate=0.00001099, open_since=closed).",
|
2019-08-11 18:17:39 +00:00
|
|
|
caplog)
|
2017-11-09 19:02:41 +00:00
|
|
|
|
2018-12-27 10:19:26 +00:00
|
|
|
caplog.clear()
|
2017-11-09 19:02:41 +00:00
|
|
|
trade.open_order_id = 'something'
|
|
|
|
trade.update(limit_sell_order)
|
|
|
|
assert trade.open_order_id is None
|
2017-12-17 21:07:56 +00:00
|
|
|
assert trade.close_rate == 0.00001173
|
2018-10-21 07:21:32 +00:00
|
|
|
assert trade.close_profit == 0.06201058
|
2017-11-09 19:02:41 +00:00
|
|
|
assert trade.close_date is not None
|
2018-12-27 10:19:26 +00:00
|
|
|
assert log_has("LIMIT_SELL has been fulfilled for Trade(id=2, "
|
|
|
|
"pair=ETH/BTC, amount=90.99181073, open_rate=0.00001099, open_since=closed).",
|
2019-08-11 18:17:39 +00:00
|
|
|
caplog)
|
2017-11-09 19:02:41 +00:00
|
|
|
|
|
|
|
|
2018-12-27 08:31:21 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2018-12-27 10:19:26 +00:00
|
|
|
def test_update_market_order(market_buy_order, market_sell_order, fee, caplog):
|
2018-12-27 08:31:21 +00:00
|
|
|
trade = Trade(
|
2018-12-27 10:19:26 +00:00
|
|
|
id=1,
|
2018-12-27 08:31:21 +00:00
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.001,
|
2019-12-17 05:58:10 +00:00
|
|
|
amount=5,
|
|
|
|
open_rate=0.01,
|
2018-12-27 08:31:21 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
exchange='bittrex',
|
|
|
|
)
|
|
|
|
|
|
|
|
trade.open_order_id = 'something'
|
|
|
|
trade.update(market_buy_order)
|
|
|
|
assert trade.open_order_id is None
|
|
|
|
assert trade.open_rate == 0.00004099
|
|
|
|
assert trade.close_profit is None
|
|
|
|
assert trade.close_date is None
|
2018-12-27 10:19:26 +00:00
|
|
|
assert log_has("MARKET_BUY has been fulfilled for Trade(id=1, "
|
|
|
|
"pair=ETH/BTC, amount=91.99181073, open_rate=0.00004099, open_since=closed).",
|
2019-08-11 18:17:39 +00:00
|
|
|
caplog)
|
2018-12-27 08:31:21 +00:00
|
|
|
|
2018-12-27 10:19:26 +00:00
|
|
|
caplog.clear()
|
2018-12-27 08:31:21 +00:00
|
|
|
trade.open_order_id = 'something'
|
|
|
|
trade.update(market_sell_order)
|
|
|
|
assert trade.open_order_id is None
|
|
|
|
assert trade.close_rate == 0.00004173
|
|
|
|
assert trade.close_profit == 0.01297561
|
|
|
|
assert trade.close_date is not None
|
2018-12-27 10:19:26 +00:00
|
|
|
assert log_has("MARKET_SELL has been fulfilled for Trade(id=1, "
|
|
|
|
"pair=ETH/BTC, amount=91.99181073, open_rate=0.00004099, open_since=closed).",
|
2019-08-11 18:17:39 +00:00
|
|
|
caplog)
|
2018-12-27 08:31:21 +00:00
|
|
|
|
|
|
|
|
2018-04-07 18:06:53 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2018-04-15 10:09:12 +00:00
|
|
|
def test_calc_open_close_trade_price(limit_buy_order, limit_sell_order, fee):
|
2017-12-17 21:07:56 +00:00
|
|
|
trade = Trade(
|
2018-03-24 19:02:13 +00:00
|
|
|
pair='ETH/BTC',
|
2017-12-17 21:07:56 +00:00
|
|
|
stake_amount=0.001,
|
2019-12-17 05:58:10 +00:00
|
|
|
open_rate=0.01,
|
|
|
|
amount=5,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2018-03-25 10:57:59 +00:00
|
|
|
exchange='bittrex',
|
2017-12-17 21:07:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
trade.open_order_id = 'something'
|
|
|
|
trade.update(limit_buy_order)
|
2019-12-17 05:58:10 +00:00
|
|
|
assert trade._calc_open_trade_price() == 0.0010024999999225068
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
trade.update(limit_sell_order)
|
2018-10-21 07:21:32 +00:00
|
|
|
assert trade.calc_close_trade_price() == 0.0010646656050132426
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
# Profit in BTC
|
|
|
|
assert trade.calc_profit() == 0.00006217
|
|
|
|
|
|
|
|
# Profit in percent
|
2019-12-17 07:53:30 +00:00
|
|
|
assert trade.calc_profit_ratio() == 0.06201058
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
|
2018-04-07 18:06:53 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2018-04-15 10:09:12 +00:00
|
|
|
def test_calc_close_trade_price_exception(limit_buy_order, fee):
|
2017-12-27 10:41:11 +00:00
|
|
|
trade = Trade(
|
2018-03-24 19:02:13 +00:00
|
|
|
pair='ETH/BTC',
|
2017-12-27 10:41:11 +00:00
|
|
|
stake_amount=0.001,
|
2019-12-17 05:58:10 +00:00
|
|
|
open_rate=0.1,
|
|
|
|
amount=5,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2018-03-25 10:57:59 +00:00
|
|
|
exchange='bittrex',
|
2017-12-27 10:41:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
trade.open_order_id = 'something'
|
|
|
|
trade.update(limit_buy_order)
|
|
|
|
assert trade.calc_close_trade_price() == 0.0
|
|
|
|
|
|
|
|
|
2018-04-07 18:06:53 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2017-11-09 19:02:41 +00:00
|
|
|
def test_update_open_order(limit_buy_order):
|
|
|
|
trade = Trade(
|
2018-03-24 19:02:13 +00:00
|
|
|
pair='ETH/BTC',
|
2017-11-09 19:02:41 +00:00
|
|
|
stake_amount=1.00,
|
2019-12-17 05:58:10 +00:00
|
|
|
open_rate=0.01,
|
|
|
|
amount=5,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=0.1,
|
|
|
|
fee_close=0.1,
|
2018-03-25 10:57:59 +00:00
|
|
|
exchange='bittrex',
|
2017-11-09 19:02:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert trade.open_order_id is None
|
|
|
|
assert trade.close_profit is None
|
|
|
|
assert trade.close_date is None
|
|
|
|
|
2018-03-25 20:25:26 +00:00
|
|
|
limit_buy_order['status'] = 'open'
|
2017-11-09 19:02:41 +00:00
|
|
|
trade.update(limit_buy_order)
|
|
|
|
|
|
|
|
assert trade.open_order_id is None
|
|
|
|
assert trade.close_profit is None
|
|
|
|
assert trade.close_date is None
|
|
|
|
|
|
|
|
|
2018-04-07 18:06:53 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2017-11-09 19:02:41 +00:00
|
|
|
def test_update_invalid_order(limit_buy_order):
|
|
|
|
trade = Trade(
|
2018-03-24 19:02:13 +00:00
|
|
|
pair='ETH/BTC',
|
2017-11-09 19:02:41 +00:00
|
|
|
stake_amount=1.00,
|
2019-12-17 05:58:10 +00:00
|
|
|
amount=5,
|
|
|
|
open_rate=0.001,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=0.1,
|
|
|
|
fee_close=0.1,
|
2018-03-25 10:57:59 +00:00
|
|
|
exchange='bittrex',
|
2017-11-09 19:02:41 +00:00
|
|
|
)
|
|
|
|
limit_buy_order['type'] = 'invalid'
|
|
|
|
with pytest.raises(ValueError, match=r'Unknown order type'):
|
|
|
|
trade.update(limit_buy_order)
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
|
2018-04-07 18:06:53 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2018-04-15 10:09:12 +00:00
|
|
|
def test_calc_open_trade_price(limit_buy_order, fee):
|
2017-12-17 21:07:56 +00:00
|
|
|
trade = Trade(
|
2018-03-24 19:02:13 +00:00
|
|
|
pair='ETH/BTC',
|
2017-12-17 21:07:56 +00:00
|
|
|
stake_amount=0.001,
|
2019-12-17 05:58:10 +00:00
|
|
|
amount=5,
|
|
|
|
open_rate=0.00001099,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2018-03-25 10:57:59 +00:00
|
|
|
exchange='bittrex',
|
2017-12-17 21:07:56 +00:00
|
|
|
)
|
|
|
|
trade.open_order_id = 'open_trade'
|
|
|
|
trade.update(limit_buy_order) # Buy @ 0.00001099
|
|
|
|
|
|
|
|
# Get the open rate price with the standard fee rate
|
2019-12-17 05:58:10 +00:00
|
|
|
assert trade._calc_open_trade_price() == 0.0010024999999225068
|
|
|
|
trade.fee_open = 0.003
|
2017-12-17 21:07:56 +00:00
|
|
|
# Get the open rate price with a custom fee rate
|
2019-12-17 05:58:10 +00:00
|
|
|
assert trade._calc_open_trade_price() == 0.001002999999922468
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
|
2018-04-07 18:06:53 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2018-04-15 10:09:12 +00:00
|
|
|
def test_calc_close_trade_price(limit_buy_order, limit_sell_order, fee):
|
2017-12-17 21:07:56 +00:00
|
|
|
trade = Trade(
|
2018-03-24 19:02:13 +00:00
|
|
|
pair='ETH/BTC',
|
2017-12-17 21:07:56 +00:00
|
|
|
stake_amount=0.001,
|
2019-12-17 05:58:10 +00:00
|
|
|
amount=5,
|
|
|
|
open_rate=0.00001099,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2018-03-25 10:57:59 +00:00
|
|
|
exchange='bittrex',
|
2017-12-17 21:07:56 +00:00
|
|
|
)
|
|
|
|
trade.open_order_id = 'close_trade'
|
|
|
|
trade.update(limit_buy_order) # Buy @ 0.00001099
|
|
|
|
|
|
|
|
# Get the close rate price with a custom close rate and a regular fee rate
|
2018-10-21 07:21:32 +00:00
|
|
|
assert trade.calc_close_trade_price(rate=0.00001234) == 0.0011200318470471794
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
# Get the close rate price with a custom close rate and a custom fee rate
|
2018-10-21 07:21:32 +00:00
|
|
|
assert trade.calc_close_trade_price(rate=0.00001234, fee=0.003) == 0.0011194704275749754
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
# Test when we apply a Sell order, and ask price with a custom fee rate
|
|
|
|
trade.update(limit_sell_order)
|
2018-10-21 07:21:32 +00:00
|
|
|
assert trade.calc_close_trade_price(fee=0.005) == 0.0010619972701635854
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
|
2018-04-07 18:06:53 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2018-04-15 10:09:12 +00:00
|
|
|
def test_calc_profit(limit_buy_order, limit_sell_order, fee):
|
2017-12-17 21:07:56 +00:00
|
|
|
trade = Trade(
|
2018-03-24 19:02:13 +00:00
|
|
|
pair='ETH/BTC',
|
2017-12-17 21:07:56 +00:00
|
|
|
stake_amount=0.001,
|
2019-12-17 05:58:10 +00:00
|
|
|
amount=5,
|
|
|
|
open_rate=0.00001099,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2018-03-25 10:57:59 +00:00
|
|
|
exchange='bittrex',
|
2017-12-17 21:07:56 +00:00
|
|
|
)
|
|
|
|
trade.open_order_id = 'profit_percent'
|
|
|
|
trade.update(limit_buy_order) # Buy @ 0.00001099
|
|
|
|
|
|
|
|
# Custom closing rate and regular fee rate
|
|
|
|
# Higher than open rate
|
|
|
|
assert trade.calc_profit(rate=0.00001234) == 0.00011753
|
|
|
|
# Lower than open rate
|
|
|
|
assert trade.calc_profit(rate=0.00000123) == -0.00089086
|
|
|
|
|
|
|
|
# Custom closing rate and custom fee rate
|
|
|
|
# Higher than open rate
|
|
|
|
assert trade.calc_profit(rate=0.00001234, fee=0.003) == 0.00011697
|
|
|
|
# Lower than open rate
|
|
|
|
assert trade.calc_profit(rate=0.00000123, fee=0.003) == -0.00089092
|
|
|
|
|
|
|
|
# Test when we apply a Sell order. Sell higher than open rate @ 0.00001173
|
|
|
|
trade.update(limit_sell_order)
|
|
|
|
assert trade.calc_profit() == 0.00006217
|
|
|
|
|
|
|
|
# Test with a custom fee rate on the close trade
|
|
|
|
assert trade.calc_profit(fee=0.003) == 0.00006163
|
|
|
|
|
|
|
|
|
2018-04-07 18:06:53 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2019-12-17 07:53:30 +00:00
|
|
|
def test_calc_profit_ratio(limit_buy_order, limit_sell_order, fee):
|
2017-12-17 21:07:56 +00:00
|
|
|
trade = Trade(
|
2018-03-24 19:02:13 +00:00
|
|
|
pair='ETH/BTC',
|
2017-12-17 21:07:56 +00:00
|
|
|
stake_amount=0.001,
|
2019-12-17 05:58:10 +00:00
|
|
|
amount=5,
|
|
|
|
open_rate=0.00001099,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2018-03-25 10:57:59 +00:00
|
|
|
exchange='bittrex',
|
2017-12-17 21:07:56 +00:00
|
|
|
)
|
|
|
|
trade.open_order_id = 'profit_percent'
|
|
|
|
trade.update(limit_buy_order) # Buy @ 0.00001099
|
|
|
|
|
|
|
|
# Get percent of profit with a custom rate (Higher than open rate)
|
2019-12-17 07:53:30 +00:00
|
|
|
assert trade.calc_profit_ratio(rate=0.00001234) == 0.11723875
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
# Get percent of profit with a custom rate (Lower than open rate)
|
2019-12-17 07:53:30 +00:00
|
|
|
assert trade.calc_profit_ratio(rate=0.00000123) == -0.88863828
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
# Test when we apply a Sell order. Sell higher than open rate @ 0.00001173
|
|
|
|
trade.update(limit_sell_order)
|
2019-12-17 07:53:30 +00:00
|
|
|
assert trade.calc_profit_ratio() == 0.06201058
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
# Test with a custom fee rate on the close trade
|
2019-12-17 07:53:30 +00:00
|
|
|
assert trade.calc_profit_ratio(fee=0.003) == 0.06147824
|
2018-01-23 07:23:29 +00:00
|
|
|
|
|
|
|
|
2019-05-29 17:54:47 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2018-04-15 10:09:12 +00:00
|
|
|
def test_clean_dry_run_db(default_conf, fee):
|
2018-01-23 07:23:29 +00:00
|
|
|
|
|
|
|
# Simulate dry_run entries
|
|
|
|
trade = Trade(
|
2018-03-24 19:02:13 +00:00
|
|
|
pair='ETH/BTC',
|
2018-01-23 07:23:29 +00:00
|
|
|
stake_amount=0.001,
|
|
|
|
amount=123.0,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2018-01-23 07:23:29 +00:00
|
|
|
open_rate=0.123,
|
2018-03-25 10:57:59 +00:00
|
|
|
exchange='bittrex',
|
2018-01-23 07:23:29 +00:00
|
|
|
open_order_id='dry_run_buy_12345'
|
|
|
|
)
|
|
|
|
Trade.session.add(trade)
|
|
|
|
|
|
|
|
trade = Trade(
|
2018-03-24 19:02:13 +00:00
|
|
|
pair='ETC/BTC',
|
2018-01-23 07:23:29 +00:00
|
|
|
stake_amount=0.001,
|
|
|
|
amount=123.0,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2018-01-23 07:23:29 +00:00
|
|
|
open_rate=0.123,
|
2018-03-25 10:57:59 +00:00
|
|
|
exchange='bittrex',
|
2018-01-23 07:23:29 +00:00
|
|
|
open_order_id='dry_run_sell_12345'
|
|
|
|
)
|
|
|
|
Trade.session.add(trade)
|
|
|
|
|
|
|
|
# Simulate prod entry
|
|
|
|
trade = Trade(
|
2018-03-24 19:02:13 +00:00
|
|
|
pair='ETC/BTC',
|
2018-01-23 07:23:29 +00:00
|
|
|
stake_amount=0.001,
|
|
|
|
amount=123.0,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2018-01-23 07:23:29 +00:00
|
|
|
open_rate=0.123,
|
2018-03-25 10:57:59 +00:00
|
|
|
exchange='bittrex',
|
2018-01-23 07:23:29 +00:00
|
|
|
open_order_id='prod_buy_12345'
|
|
|
|
)
|
|
|
|
Trade.session.add(trade)
|
|
|
|
|
|
|
|
# We have 3 entries: 2 dry_run, 1 prod
|
|
|
|
assert len(Trade.query.filter(Trade.open_order_id.isnot(None)).all()) == 3
|
|
|
|
|
|
|
|
clean_dry_run_db()
|
|
|
|
|
|
|
|
# We have now only the prod
|
|
|
|
assert len(Trade.query.filter(Trade.open_order_id.isnot(None)).all()) == 1
|
2018-05-12 08:29:26 +00:00
|
|
|
|
|
|
|
|
2018-06-07 03:27:55 +00:00
|
|
|
def test_migrate_old(mocker, default_conf, fee):
|
2018-05-12 11:39:29 +00:00
|
|
|
"""
|
|
|
|
Test Database migration(starting with old pairformat)
|
|
|
|
"""
|
2018-05-12 08:37:17 +00:00
|
|
|
amount = 103.223
|
2018-05-12 08:29:26 +00:00
|
|
|
create_table_old = """CREATE TABLE IF NOT EXISTS "trades" (
|
|
|
|
id INTEGER NOT NULL,
|
|
|
|
exchange VARCHAR NOT NULL,
|
|
|
|
pair VARCHAR NOT NULL,
|
|
|
|
is_open BOOLEAN NOT NULL,
|
|
|
|
fee FLOAT NOT NULL,
|
|
|
|
open_rate FLOAT,
|
|
|
|
close_rate FLOAT,
|
|
|
|
close_profit FLOAT,
|
|
|
|
stake_amount FLOAT NOT NULL,
|
|
|
|
amount FLOAT,
|
|
|
|
open_date DATETIME NOT NULL,
|
|
|
|
close_date DATETIME,
|
|
|
|
open_order_id VARCHAR,
|
|
|
|
PRIMARY KEY (id),
|
|
|
|
CHECK (is_open IN (0, 1))
|
|
|
|
);"""
|
|
|
|
insert_table_old = """INSERT INTO trades (exchange, pair, is_open, fee,
|
|
|
|
open_rate, stake_amount, amount, open_date)
|
2018-05-12 08:37:17 +00:00
|
|
|
VALUES ('BITTREX', 'BTC_ETC', 1, {fee},
|
|
|
|
0.00258580, {stake}, {amount},
|
|
|
|
'2017-11-28 12:44:24.000000')
|
|
|
|
""".format(fee=fee.return_value,
|
|
|
|
stake=default_conf.get("stake_amount"),
|
|
|
|
amount=amount
|
|
|
|
)
|
2020-03-22 10:16:09 +00:00
|
|
|
insert_table_old2 = """INSERT INTO trades (exchange, pair, is_open, fee,
|
|
|
|
open_rate, close_rate, stake_amount, amount, open_date)
|
|
|
|
VALUES ('BITTREX', 'BTC_ETC', 0, {fee},
|
|
|
|
0.00258580, 0.00268580, {stake}, {amount},
|
|
|
|
'2017-11-28 12:44:24.000000')
|
|
|
|
""".format(fee=fee.return_value,
|
|
|
|
stake=default_conf.get("stake_amount"),
|
|
|
|
amount=amount
|
|
|
|
)
|
2018-05-12 08:29:26 +00:00
|
|
|
engine = create_engine('sqlite://')
|
2018-06-07 03:27:55 +00:00
|
|
|
mocker.patch('freqtrade.persistence.create_engine', lambda *args, **kwargs: engine)
|
|
|
|
|
2018-05-12 08:31:24 +00:00
|
|
|
# Create table using the old format
|
2018-05-12 08:29:26 +00:00
|
|
|
engine.execute(create_table_old)
|
|
|
|
engine.execute(insert_table_old)
|
2020-03-22 10:16:09 +00:00
|
|
|
engine.execute(insert_table_old2)
|
2018-05-12 08:31:24 +00:00
|
|
|
# Run init to test migration
|
2019-05-29 17:54:47 +00:00
|
|
|
init(default_conf['db_url'], default_conf['dry_run'])
|
2018-05-12 08:31:24 +00:00
|
|
|
|
2018-05-12 08:29:26 +00:00
|
|
|
assert len(Trade.query.filter(Trade.id == 1).all()) == 1
|
|
|
|
trade = Trade.query.filter(Trade.id == 1).first()
|
|
|
|
assert trade.fee_open == fee.return_value
|
|
|
|
assert trade.fee_close == fee.return_value
|
|
|
|
assert trade.open_rate_requested is None
|
|
|
|
assert trade.close_rate_requested is None
|
2018-05-12 08:37:17 +00:00
|
|
|
assert trade.is_open == 1
|
|
|
|
assert trade.amount == amount
|
|
|
|
assert trade.stake_amount == default_conf.get("stake_amount")
|
2018-05-12 11:39:29 +00:00
|
|
|
assert trade.pair == "ETC/BTC"
|
|
|
|
assert trade.exchange == "bittrex"
|
2018-06-27 17:49:08 +00:00
|
|
|
assert trade.max_rate == 0.0
|
|
|
|
assert trade.stop_loss == 0.0
|
|
|
|
assert trade.initial_stop_loss == 0.0
|
2019-12-17 06:08:36 +00:00
|
|
|
assert trade.open_trade_price == trade._calc_open_trade_price()
|
2020-03-22 10:16:09 +00:00
|
|
|
assert trade.close_profit_abs is None
|
2020-04-30 04:51:42 +00:00
|
|
|
assert trade.fee_open_cost is None
|
|
|
|
assert trade.fee_open_currency is None
|
|
|
|
assert trade.fee_close_cost is None
|
|
|
|
assert trade.fee_close_currency is None
|
2020-06-02 08:02:24 +00:00
|
|
|
assert trade.timeframe is None
|
2020-03-22 10:16:09 +00:00
|
|
|
|
|
|
|
trade = Trade.query.filter(Trade.id == 2).first()
|
|
|
|
assert trade.close_rate is not None
|
|
|
|
assert trade.is_open == 0
|
|
|
|
assert trade.open_rate_requested is None
|
|
|
|
assert trade.close_rate_requested is None
|
|
|
|
assert trade.close_rate is not None
|
|
|
|
assert pytest.approx(trade.close_profit_abs) == trade.calc_profit()
|
2020-05-17 08:52:20 +00:00
|
|
|
assert trade.sell_order_status is None
|
2018-05-12 11:39:29 +00:00
|
|
|
|
|
|
|
|
2018-06-27 18:15:25 +00:00
|
|
|
def test_migrate_new(mocker, default_conf, fee, caplog):
|
2018-05-12 11:39:29 +00:00
|
|
|
"""
|
|
|
|
Test Database migration (starting with new pairformat)
|
|
|
|
"""
|
2018-08-16 11:15:46 +00:00
|
|
|
caplog.set_level(logging.DEBUG)
|
2018-05-12 11:39:29 +00:00
|
|
|
amount = 103.223
|
2018-08-04 18:22:16 +00:00
|
|
|
# Always create all columns apart from the last!
|
2018-05-12 11:39:29 +00:00
|
|
|
create_table_old = """CREATE TABLE IF NOT EXISTS "trades" (
|
|
|
|
id INTEGER NOT NULL,
|
|
|
|
exchange VARCHAR NOT NULL,
|
|
|
|
pair VARCHAR NOT NULL,
|
|
|
|
is_open BOOLEAN NOT NULL,
|
|
|
|
fee FLOAT NOT NULL,
|
|
|
|
open_rate FLOAT,
|
|
|
|
close_rate FLOAT,
|
|
|
|
close_profit FLOAT,
|
|
|
|
stake_amount FLOAT NOT NULL,
|
|
|
|
amount FLOAT,
|
|
|
|
open_date DATETIME NOT NULL,
|
|
|
|
close_date DATETIME,
|
|
|
|
open_order_id VARCHAR,
|
2018-08-04 18:22:16 +00:00
|
|
|
stop_loss FLOAT,
|
|
|
|
initial_stop_loss FLOAT,
|
|
|
|
max_rate FLOAT,
|
|
|
|
sell_reason VARCHAR,
|
|
|
|
strategy VARCHAR,
|
2018-11-26 17:46:59 +00:00
|
|
|
ticker_interval INTEGER,
|
2018-05-12 11:39:29 +00:00
|
|
|
PRIMARY KEY (id),
|
|
|
|
CHECK (is_open IN (0, 1))
|
|
|
|
);"""
|
|
|
|
insert_table_old = """INSERT INTO trades (exchange, pair, is_open, fee,
|
2018-08-04 18:22:16 +00:00
|
|
|
open_rate, stake_amount, amount, open_date,
|
2020-06-02 08:02:24 +00:00
|
|
|
stop_loss, initial_stop_loss, max_rate, ticker_interval)
|
2018-05-12 11:39:29 +00:00
|
|
|
VALUES ('binance', 'ETC/BTC', 1, {fee},
|
|
|
|
0.00258580, {stake}, {amount},
|
2018-08-04 18:22:16 +00:00
|
|
|
'2019-11-28 12:44:24.000000',
|
2020-06-02 08:02:24 +00:00
|
|
|
0.0, 0.0, 0.0, '5m')
|
2018-05-12 11:39:29 +00:00
|
|
|
""".format(fee=fee.return_value,
|
|
|
|
stake=default_conf.get("stake_amount"),
|
|
|
|
amount=amount
|
|
|
|
)
|
2018-06-07 04:06:21 +00:00
|
|
|
engine = create_engine('sqlite://')
|
2018-06-07 03:27:55 +00:00
|
|
|
mocker.patch('freqtrade.persistence.create_engine', lambda *args, **kwargs: engine)
|
|
|
|
|
2018-05-12 11:39:29 +00:00
|
|
|
# Create table using the old format
|
|
|
|
engine.execute(create_table_old)
|
2018-12-09 08:03:17 +00:00
|
|
|
engine.execute("create index ix_trades_is_open on trades(is_open)")
|
|
|
|
engine.execute("create index ix_trades_pair on trades(pair)")
|
2018-05-12 11:39:29 +00:00
|
|
|
engine.execute(insert_table_old)
|
2018-06-27 18:15:25 +00:00
|
|
|
|
|
|
|
# fake previous backup
|
|
|
|
engine.execute("create table trades_bak as select * from trades")
|
2018-07-01 17:55:51 +00:00
|
|
|
|
|
|
|
engine.execute("create table trades_bak1 as select * from trades")
|
2018-05-12 11:39:29 +00:00
|
|
|
# Run init to test migration
|
2019-05-29 17:54:47 +00:00
|
|
|
init(default_conf['db_url'], default_conf['dry_run'])
|
2018-05-12 11:39:29 +00:00
|
|
|
|
|
|
|
assert len(Trade.query.filter(Trade.id == 1).all()) == 1
|
|
|
|
trade = Trade.query.filter(Trade.id == 1).first()
|
|
|
|
assert trade.fee_open == fee.return_value
|
|
|
|
assert trade.fee_close == fee.return_value
|
|
|
|
assert trade.open_rate_requested is None
|
|
|
|
assert trade.close_rate_requested is None
|
|
|
|
assert trade.is_open == 1
|
|
|
|
assert trade.amount == amount
|
|
|
|
assert trade.stake_amount == default_conf.get("stake_amount")
|
|
|
|
assert trade.pair == "ETC/BTC"
|
|
|
|
assert trade.exchange == "binance"
|
2018-06-27 18:15:25 +00:00
|
|
|
assert trade.max_rate == 0.0
|
2019-03-16 19:04:55 +00:00
|
|
|
assert trade.min_rate is None
|
2018-06-27 18:15:25 +00:00
|
|
|
assert trade.stop_loss == 0.0
|
|
|
|
assert trade.initial_stop_loss == 0.0
|
2018-07-11 17:57:20 +00:00
|
|
|
assert trade.sell_reason is None
|
2018-07-12 18:38:57 +00:00
|
|
|
assert trade.strategy is None
|
2020-06-02 08:02:24 +00:00
|
|
|
assert trade.timeframe == '5m'
|
2018-11-26 19:24:13 +00:00
|
|
|
assert trade.stoploss_order_id is None
|
2019-01-18 11:14:00 +00:00
|
|
|
assert trade.stoploss_last_update is None
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has("trying trades_bak1", caplog)
|
|
|
|
assert log_has("trying trades_bak2", caplog)
|
|
|
|
assert log_has("Running database migration - backup available as trades_bak2", caplog)
|
2019-12-17 06:08:36 +00:00
|
|
|
assert trade.open_trade_price == trade._calc_open_trade_price()
|
2020-03-22 10:16:09 +00:00
|
|
|
assert trade.close_profit_abs is None
|
2018-06-27 04:23:49 +00:00
|
|
|
|
|
|
|
|
2018-07-23 08:09:56 +00:00
|
|
|
def test_migrate_mid_state(mocker, default_conf, fee, caplog):
|
|
|
|
"""
|
|
|
|
Test Database migration (starting with new pairformat)
|
|
|
|
"""
|
2018-08-16 11:15:46 +00:00
|
|
|
caplog.set_level(logging.DEBUG)
|
2018-07-23 08:09:56 +00:00
|
|
|
amount = 103.223
|
|
|
|
create_table_old = """CREATE TABLE IF NOT EXISTS "trades" (
|
|
|
|
id INTEGER NOT NULL,
|
|
|
|
exchange VARCHAR NOT NULL,
|
|
|
|
pair VARCHAR NOT NULL,
|
|
|
|
is_open BOOLEAN NOT NULL,
|
|
|
|
fee_open FLOAT NOT NULL,
|
|
|
|
fee_close FLOAT NOT NULL,
|
|
|
|
open_rate FLOAT,
|
|
|
|
close_rate FLOAT,
|
|
|
|
close_profit FLOAT,
|
|
|
|
stake_amount FLOAT NOT NULL,
|
|
|
|
amount FLOAT,
|
|
|
|
open_date DATETIME NOT NULL,
|
|
|
|
close_date DATETIME,
|
|
|
|
open_order_id VARCHAR,
|
|
|
|
PRIMARY KEY (id),
|
|
|
|
CHECK (is_open IN (0, 1))
|
|
|
|
);"""
|
|
|
|
insert_table_old = """INSERT INTO trades (exchange, pair, is_open, fee_open, fee_close,
|
|
|
|
open_rate, stake_amount, amount, open_date)
|
|
|
|
VALUES ('binance', 'ETC/BTC', 1, {fee}, {fee},
|
|
|
|
0.00258580, {stake}, {amount},
|
|
|
|
'2019-11-28 12:44:24.000000')
|
|
|
|
""".format(fee=fee.return_value,
|
|
|
|
stake=default_conf.get("stake_amount"),
|
|
|
|
amount=amount
|
|
|
|
)
|
|
|
|
engine = create_engine('sqlite://')
|
|
|
|
mocker.patch('freqtrade.persistence.create_engine', lambda *args, **kwargs: engine)
|
|
|
|
|
|
|
|
# Create table using the old format
|
|
|
|
engine.execute(create_table_old)
|
|
|
|
engine.execute(insert_table_old)
|
|
|
|
|
|
|
|
# Run init to test migration
|
2019-05-29 17:54:47 +00:00
|
|
|
init(default_conf['db_url'], default_conf['dry_run'])
|
2018-07-23 08:09:56 +00:00
|
|
|
|
|
|
|
assert len(Trade.query.filter(Trade.id == 1).all()) == 1
|
|
|
|
trade = Trade.query.filter(Trade.id == 1).first()
|
|
|
|
assert trade.fee_open == fee.return_value
|
|
|
|
assert trade.fee_close == fee.return_value
|
|
|
|
assert trade.open_rate_requested is None
|
|
|
|
assert trade.close_rate_requested is None
|
|
|
|
assert trade.is_open == 1
|
|
|
|
assert trade.amount == amount
|
|
|
|
assert trade.stake_amount == default_conf.get("stake_amount")
|
|
|
|
assert trade.pair == "ETC/BTC"
|
|
|
|
assert trade.exchange == "binance"
|
|
|
|
assert trade.max_rate == 0.0
|
|
|
|
assert trade.stop_loss == 0.0
|
|
|
|
assert trade.initial_stop_loss == 0.0
|
2019-12-17 06:13:08 +00:00
|
|
|
assert trade.open_trade_price == trade._calc_open_trade_price()
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has("trying trades_bak0", caplog)
|
|
|
|
assert log_has("Running database migration - backup available as trades_bak0", caplog)
|
2018-07-23 08:09:56 +00:00
|
|
|
|
|
|
|
|
2019-03-16 19:04:55 +00:00
|
|
|
def test_adjust_stop_loss(fee):
|
2018-06-27 04:23:49 +00:00
|
|
|
trade = Trade(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.001,
|
2019-12-17 05:58:10 +00:00
|
|
|
amount=5,
|
2018-06-27 04:23:49 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
exchange='bittrex',
|
|
|
|
open_rate=1,
|
2019-03-16 19:04:55 +00:00
|
|
|
max_rate=1,
|
2018-06-27 04:23:49 +00:00
|
|
|
)
|
|
|
|
|
2018-06-27 04:38:49 +00:00
|
|
|
trade.adjust_stop_loss(trade.open_rate, 0.05, True)
|
|
|
|
assert trade.stop_loss == 0.95
|
2019-03-31 11:15:35 +00:00
|
|
|
assert trade.stop_loss_pct == -0.05
|
2018-06-27 04:38:49 +00:00
|
|
|
assert trade.initial_stop_loss == 0.95
|
2019-03-31 11:15:35 +00:00
|
|
|
assert trade.initial_stop_loss_pct == -0.05
|
2018-06-27 04:38:49 +00:00
|
|
|
|
2019-03-16 19:04:55 +00:00
|
|
|
# Get percent of profit with a lower rate
|
2018-06-27 04:38:49 +00:00
|
|
|
trade.adjust_stop_loss(0.96, 0.05)
|
2018-06-27 04:23:49 +00:00
|
|
|
assert trade.stop_loss == 0.95
|
2019-03-31 11:15:35 +00:00
|
|
|
assert trade.stop_loss_pct == -0.05
|
2018-06-27 04:23:49 +00:00
|
|
|
assert trade.initial_stop_loss == 0.95
|
2019-03-31 11:15:35 +00:00
|
|
|
assert trade.initial_stop_loss_pct == -0.05
|
2018-06-27 04:23:49 +00:00
|
|
|
|
2018-06-27 04:38:49 +00:00
|
|
|
# Get percent of profit with a custom rate (Higher than open rate)
|
2018-06-27 04:23:49 +00:00
|
|
|
trade.adjust_stop_loss(1.3, -0.1)
|
|
|
|
assert round(trade.stop_loss, 8) == 1.17
|
2019-03-31 11:15:35 +00:00
|
|
|
assert trade.stop_loss_pct == -0.1
|
2018-06-27 04:23:49 +00:00
|
|
|
assert trade.initial_stop_loss == 0.95
|
2019-03-31 11:15:35 +00:00
|
|
|
assert trade.initial_stop_loss_pct == -0.05
|
2018-06-27 04:23:49 +00:00
|
|
|
|
2018-06-27 04:38:49 +00:00
|
|
|
# current rate lower again ... should not change
|
2018-06-27 04:23:49 +00:00
|
|
|
trade.adjust_stop_loss(1.2, 0.1)
|
|
|
|
assert round(trade.stop_loss, 8) == 1.17
|
|
|
|
assert trade.initial_stop_loss == 0.95
|
2019-03-31 11:15:35 +00:00
|
|
|
assert trade.initial_stop_loss_pct == -0.05
|
2018-06-27 04:23:49 +00:00
|
|
|
|
|
|
|
# current rate higher... should raise stoploss
|
|
|
|
trade.adjust_stop_loss(1.4, 0.1)
|
|
|
|
assert round(trade.stop_loss, 8) == 1.26
|
|
|
|
assert trade.initial_stop_loss == 0.95
|
2019-03-31 11:15:35 +00:00
|
|
|
assert trade.initial_stop_loss_pct == -0.05
|
2018-06-27 04:38:49 +00:00
|
|
|
|
|
|
|
# Initial is true but stop_loss set - so doesn't do anything
|
|
|
|
trade.adjust_stop_loss(1.7, 0.1, True)
|
|
|
|
assert round(trade.stop_loss, 8) == 1.26
|
|
|
|
assert trade.initial_stop_loss == 0.95
|
2019-03-31 11:15:35 +00:00
|
|
|
assert trade.initial_stop_loss_pct == -0.05
|
|
|
|
assert trade.stop_loss_pct == -0.1
|
2019-02-25 19:00:17 +00:00
|
|
|
|
|
|
|
|
2019-03-16 19:06:15 +00:00
|
|
|
def test_adjust_min_max_rates(fee):
|
2019-03-16 19:04:55 +00:00
|
|
|
trade = Trade(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.001,
|
2019-12-17 05:58:10 +00:00
|
|
|
amount=5,
|
2019-03-16 19:04:55 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
exchange='bittrex',
|
|
|
|
open_rate=1,
|
|
|
|
)
|
|
|
|
|
2019-03-16 19:06:15 +00:00
|
|
|
trade.adjust_min_max_rates(trade.open_rate)
|
2019-03-16 19:04:55 +00:00
|
|
|
assert trade.max_rate == 1
|
|
|
|
assert trade.min_rate == 1
|
|
|
|
|
|
|
|
# check min adjusted, max remained
|
2019-03-16 19:06:15 +00:00
|
|
|
trade.adjust_min_max_rates(0.96)
|
2019-03-16 19:04:55 +00:00
|
|
|
assert trade.max_rate == 1
|
|
|
|
assert trade.min_rate == 0.96
|
|
|
|
|
|
|
|
# check max adjusted, min remains
|
2019-03-16 19:06:15 +00:00
|
|
|
trade.adjust_min_max_rates(1.05)
|
2019-03-16 19:04:55 +00:00
|
|
|
assert trade.max_rate == 1.05
|
|
|
|
assert trade.min_rate == 0.96
|
|
|
|
|
|
|
|
# current rate "in the middle" - no adjustment
|
2019-03-16 19:06:15 +00:00
|
|
|
trade.adjust_min_max_rates(1.03)
|
2019-03-16 19:04:55 +00:00
|
|
|
assert trade.max_rate == 1.05
|
|
|
|
assert trade.min_rate == 0.96
|
|
|
|
|
|
|
|
|
2019-05-29 17:54:47 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2019-02-25 19:00:17 +00:00
|
|
|
def test_get_open(default_conf, fee):
|
|
|
|
|
2019-06-16 08:57:21 +00:00
|
|
|
create_mock_trades(fee)
|
2019-02-25 19:00:17 +00:00
|
|
|
assert len(Trade.get_open_trades()) == 2
|
2019-05-06 04:56:22 +00:00
|
|
|
|
|
|
|
|
2019-05-29 17:54:47 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2019-05-06 04:56:22 +00:00
|
|
|
def test_to_json(default_conf, fee):
|
|
|
|
|
|
|
|
# Simulate dry_run entries
|
|
|
|
trade = Trade(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.001,
|
|
|
|
amount=123.0,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
open_date=arrow.utcnow().shift(hours=-2).datetime,
|
|
|
|
open_rate=0.123,
|
|
|
|
exchange='bittrex',
|
|
|
|
open_order_id='dry_run_buy_12345'
|
|
|
|
)
|
|
|
|
result = trade.to_json()
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
|
|
|
|
assert result == {'trade_id': None,
|
|
|
|
'pair': 'ETH/BTC',
|
2020-04-06 13:49:24 +00:00
|
|
|
'is_open': None,
|
2019-05-06 04:56:22 +00:00
|
|
|
'open_date_hum': '2 hours ago',
|
|
|
|
'open_date': trade.open_date.strftime("%Y-%m-%d %H:%M:%S"),
|
2020-05-24 07:07:24 +00:00
|
|
|
'open_timestamp': int(trade.open_date.timestamp() * 1000),
|
2020-04-06 13:49:24 +00:00
|
|
|
'open_order_id': 'dry_run_buy_12345',
|
2019-05-06 04:56:22 +00:00
|
|
|
'close_date_hum': None,
|
|
|
|
'close_date': None,
|
2020-05-24 07:07:24 +00:00
|
|
|
'close_timestamp': None,
|
2019-05-06 04:56:22 +00:00
|
|
|
'open_rate': 0.123,
|
2020-04-06 13:49:24 +00:00
|
|
|
'open_rate_requested': None,
|
|
|
|
'open_trade_price': 15.1668225,
|
|
|
|
'fee_close': 0.0025,
|
2020-04-30 04:58:43 +00:00
|
|
|
'fee_close_cost': None,
|
|
|
|
'fee_close_currency': None,
|
2020-04-06 13:49:24 +00:00
|
|
|
'fee_open': 0.0025,
|
2020-04-30 04:58:43 +00:00
|
|
|
'fee_open_cost': None,
|
|
|
|
'fee_open_currency': None,
|
2019-05-06 04:56:22 +00:00
|
|
|
'close_rate': None,
|
2020-04-06 13:49:24 +00:00
|
|
|
'close_rate_requested': None,
|
2019-05-06 04:56:22 +00:00
|
|
|
'amount': 123.0,
|
|
|
|
'stake_amount': 0.001,
|
2020-04-06 13:49:24 +00:00
|
|
|
'close_profit': None,
|
2020-05-30 09:34:39 +00:00
|
|
|
'close_profit_abs': None,
|
2020-04-06 13:49:24 +00:00
|
|
|
'sell_reason': None,
|
2020-05-17 08:52:20 +00:00
|
|
|
'sell_order_status': None,
|
2019-05-06 04:56:22 +00:00
|
|
|
'stop_loss': None,
|
|
|
|
'stop_loss_pct': None,
|
2020-05-30 09:34:39 +00:00
|
|
|
'stoploss_order_id': None,
|
|
|
|
'stoploss_last_update': None,
|
|
|
|
'stoploss_last_update_timestamp': None,
|
2019-05-06 04:56:22 +00:00
|
|
|
'initial_stop_loss': None,
|
2020-04-06 13:49:24 +00:00
|
|
|
'initial_stop_loss_pct': None,
|
|
|
|
'min_rate': None,
|
|
|
|
'max_rate': None,
|
|
|
|
'strategy': None,
|
2020-05-30 09:34:39 +00:00
|
|
|
'ticker_interval': None,
|
2020-06-01 18:47:27 +00:00
|
|
|
'timeframe': None,
|
2020-05-30 09:34:39 +00:00
|
|
|
'exchange': 'bittrex',
|
|
|
|
}
|
2019-05-06 04:56:22 +00:00
|
|
|
|
|
|
|
# Simulate dry_run entries
|
|
|
|
trade = Trade(
|
|
|
|
pair='XRP/BTC',
|
|
|
|
stake_amount=0.001,
|
|
|
|
amount=100.0,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
open_date=arrow.utcnow().shift(hours=-2).datetime,
|
|
|
|
close_date=arrow.utcnow().shift(hours=-1).datetime,
|
|
|
|
open_rate=0.123,
|
|
|
|
close_rate=0.125,
|
|
|
|
exchange='bittrex',
|
|
|
|
)
|
|
|
|
result = trade.to_json()
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
|
|
|
|
assert result == {'trade_id': None,
|
|
|
|
'pair': 'XRP/BTC',
|
|
|
|
'open_date_hum': '2 hours ago',
|
|
|
|
'open_date': trade.open_date.strftime("%Y-%m-%d %H:%M:%S"),
|
2020-05-24 07:07:24 +00:00
|
|
|
'open_timestamp': int(trade.open_date.timestamp() * 1000),
|
2019-05-06 04:56:22 +00:00
|
|
|
'close_date_hum': 'an hour ago',
|
|
|
|
'close_date': trade.close_date.strftime("%Y-%m-%d %H:%M:%S"),
|
2020-05-24 07:07:24 +00:00
|
|
|
'close_timestamp': int(trade.close_date.timestamp() * 1000),
|
2019-05-06 04:56:22 +00:00
|
|
|
'open_rate': 0.123,
|
|
|
|
'close_rate': 0.125,
|
|
|
|
'amount': 100.0,
|
|
|
|
'stake_amount': 0.001,
|
|
|
|
'stop_loss': None,
|
|
|
|
'stop_loss_pct': None,
|
2020-05-30 09:34:39 +00:00
|
|
|
'stoploss_order_id': None,
|
|
|
|
'stoploss_last_update': None,
|
|
|
|
'stoploss_last_update_timestamp': None,
|
2019-05-06 04:56:22 +00:00
|
|
|
'initial_stop_loss': None,
|
2020-04-06 13:49:24 +00:00
|
|
|
'initial_stop_loss_pct': None,
|
|
|
|
'close_profit': None,
|
2020-05-30 09:34:39 +00:00
|
|
|
'close_profit_abs': None,
|
2020-04-06 13:49:24 +00:00
|
|
|
'close_rate_requested': None,
|
|
|
|
'fee_close': 0.0025,
|
2020-04-30 04:58:43 +00:00
|
|
|
'fee_close_cost': None,
|
|
|
|
'fee_close_currency': None,
|
2020-04-06 13:49:24 +00:00
|
|
|
'fee_open': 0.0025,
|
2020-04-30 04:58:43 +00:00
|
|
|
'fee_open_cost': None,
|
|
|
|
'fee_open_currency': None,
|
2020-04-06 13:49:24 +00:00
|
|
|
'is_open': None,
|
|
|
|
'max_rate': None,
|
|
|
|
'min_rate': None,
|
|
|
|
'open_order_id': None,
|
|
|
|
'open_rate_requested': None,
|
|
|
|
'open_trade_price': 12.33075,
|
|
|
|
'sell_reason': None,
|
2020-05-17 08:52:20 +00:00
|
|
|
'sell_order_status': None,
|
2020-04-06 13:49:24 +00:00
|
|
|
'strategy': None,
|
2020-05-30 09:34:39 +00:00
|
|
|
'ticker_interval': None,
|
2020-06-01 18:47:27 +00:00
|
|
|
'timeframe': None,
|
2020-05-30 09:34:39 +00:00
|
|
|
'exchange': 'bittrex',
|
|
|
|
}
|
2019-05-20 05:12:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_stoploss_reinitialization(default_conf, fee):
|
2019-06-08 18:26:25 +00:00
|
|
|
init(default_conf['db_url'])
|
2019-05-20 05:12:54 +00:00
|
|
|
trade = Trade(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.001,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
open_date=arrow.utcnow().shift(hours=-2).datetime,
|
|
|
|
amount=10,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
exchange='bittrex',
|
|
|
|
open_rate=1,
|
|
|
|
max_rate=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
trade.adjust_stop_loss(trade.open_rate, 0.05, True)
|
|
|
|
assert trade.stop_loss == 0.95
|
|
|
|
assert trade.stop_loss_pct == -0.05
|
|
|
|
assert trade.initial_stop_loss == 0.95
|
|
|
|
assert trade.initial_stop_loss_pct == -0.05
|
|
|
|
Trade.session.add(trade)
|
|
|
|
|
|
|
|
# Lower stoploss
|
|
|
|
Trade.stoploss_reinitialization(0.06)
|
|
|
|
|
|
|
|
trades = Trade.get_open_trades()
|
|
|
|
assert len(trades) == 1
|
|
|
|
trade_adj = trades[0]
|
|
|
|
assert trade_adj.stop_loss == 0.94
|
|
|
|
assert trade_adj.stop_loss_pct == -0.06
|
|
|
|
assert trade_adj.initial_stop_loss == 0.94
|
|
|
|
assert trade_adj.initial_stop_loss_pct == -0.06
|
|
|
|
|
|
|
|
# Raise stoploss
|
|
|
|
Trade.stoploss_reinitialization(0.04)
|
|
|
|
|
|
|
|
trades = Trade.get_open_trades()
|
|
|
|
assert len(trades) == 1
|
|
|
|
trade_adj = trades[0]
|
|
|
|
assert trade_adj.stop_loss == 0.96
|
|
|
|
assert trade_adj.stop_loss_pct == -0.04
|
|
|
|
assert trade_adj.initial_stop_loss == 0.96
|
|
|
|
assert trade_adj.initial_stop_loss_pct == -0.04
|
|
|
|
|
|
|
|
# Trailing stoploss (move stoplos up a bit)
|
|
|
|
trade.adjust_stop_loss(1.02, 0.04)
|
|
|
|
assert trade_adj.stop_loss == 0.9792
|
|
|
|
assert trade_adj.initial_stop_loss == 0.96
|
|
|
|
|
|
|
|
Trade.stoploss_reinitialization(0.04)
|
|
|
|
|
|
|
|
trades = Trade.get_open_trades()
|
|
|
|
assert len(trades) == 1
|
|
|
|
trade_adj = trades[0]
|
|
|
|
# Stoploss should not change in this case.
|
|
|
|
assert trade_adj.stop_loss == 0.9792
|
|
|
|
assert trade_adj.stop_loss_pct == -0.04
|
|
|
|
assert trade_adj.initial_stop_loss == 0.96
|
|
|
|
assert trade_adj.initial_stop_loss_pct == -0.04
|
2019-10-29 10:09:41 +00:00
|
|
|
|
|
|
|
|
2020-05-01 14:00:42 +00:00
|
|
|
def test_update_fee(fee):
|
|
|
|
trade = Trade(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.001,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
open_date=arrow.utcnow().shift(hours=-2).datetime,
|
|
|
|
amount=10,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
exchange='bittrex',
|
|
|
|
open_rate=1,
|
|
|
|
max_rate=1,
|
|
|
|
)
|
|
|
|
fee_cost = 0.15
|
|
|
|
fee_currency = 'BTC'
|
|
|
|
fee_rate = 0.0075
|
|
|
|
assert trade.fee_open_currency is None
|
2020-05-01 17:54:16 +00:00
|
|
|
assert not trade.fee_updated('buy')
|
|
|
|
assert not trade.fee_updated('sell')
|
2020-05-01 14:00:42 +00:00
|
|
|
|
|
|
|
trade.update_fee(fee_cost, fee_currency, fee_rate, 'buy')
|
2020-05-01 17:54:16 +00:00
|
|
|
assert trade.fee_updated('buy')
|
|
|
|
assert not trade.fee_updated('sell')
|
2020-05-01 14:00:42 +00:00
|
|
|
assert trade.fee_open_currency == fee_currency
|
|
|
|
assert trade.fee_open_cost == fee_cost
|
|
|
|
assert trade.fee_open == fee_rate
|
|
|
|
# Setting buy rate should "guess" close rate
|
|
|
|
assert trade.fee_close == fee_rate
|
|
|
|
assert trade.fee_close_currency is None
|
|
|
|
assert trade.fee_close_cost is None
|
|
|
|
|
|
|
|
fee_rate = 0.0076
|
|
|
|
trade.update_fee(fee_cost, fee_currency, fee_rate, 'sell')
|
2020-05-01 17:54:16 +00:00
|
|
|
assert trade.fee_updated('buy')
|
|
|
|
assert trade.fee_updated('sell')
|
2020-05-01 14:00:42 +00:00
|
|
|
assert trade.fee_close == 0.0076
|
|
|
|
assert trade.fee_close_cost == fee_cost
|
|
|
|
assert trade.fee_close == fee_rate
|
|
|
|
|
|
|
|
|
2020-05-01 18:02:38 +00:00
|
|
|
def test_fee_updated(fee):
|
|
|
|
trade = Trade(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.001,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
open_date=arrow.utcnow().shift(hours=-2).datetime,
|
|
|
|
amount=10,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
exchange='bittrex',
|
|
|
|
open_rate=1,
|
|
|
|
max_rate=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert trade.fee_open_currency is None
|
|
|
|
assert not trade.fee_updated('buy')
|
|
|
|
assert not trade.fee_updated('sell')
|
|
|
|
assert not trade.fee_updated('asdf')
|
|
|
|
|
|
|
|
trade.update_fee(0.15, 'BTC', 0.0075, 'buy')
|
|
|
|
assert trade.fee_updated('buy')
|
|
|
|
assert not trade.fee_updated('sell')
|
|
|
|
assert trade.fee_open_currency is not None
|
|
|
|
assert trade.fee_close_currency is None
|
|
|
|
|
|
|
|
trade.update_fee(0.15, 'ABC', 0.0075, 'sell')
|
|
|
|
assert trade.fee_updated('buy')
|
|
|
|
assert trade.fee_updated('sell')
|
|
|
|
assert not trade.fee_updated('asfd')
|
|
|
|
|
|
|
|
|
2019-10-29 10:09:41 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_total_open_trades_stakes(fee):
|
|
|
|
|
|
|
|
res = Trade.total_open_trades_stakes()
|
|
|
|
assert res == 0
|
|
|
|
create_mock_trades(fee)
|
|
|
|
res = Trade.total_open_trades_stakes()
|
|
|
|
assert res == 0.002
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_get_overall_performance(fee):
|
|
|
|
|
|
|
|
create_mock_trades(fee)
|
|
|
|
res = Trade.get_overall_performance()
|
|
|
|
|
|
|
|
assert len(res) == 1
|
|
|
|
assert 'pair' in res[0]
|
|
|
|
assert 'profit' in res[0]
|
|
|
|
assert 'count' in res[0]
|
2019-10-29 10:15:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_get_best_pair(fee):
|
|
|
|
|
|
|
|
res = Trade.get_best_pair()
|
|
|
|
assert res is None
|
|
|
|
|
|
|
|
create_mock_trades(fee)
|
|
|
|
res = Trade.get_best_pair()
|
|
|
|
assert len(res) == 2
|
|
|
|
assert res[0] == 'ETC/BTC'
|
|
|
|
assert res[1] == 0.005
|