2018-01-28 07:38:41 +00:00
|
|
|
# pragma pylint: disable=missing-docstring, C0103
|
2018-08-16 11:15:46 +00:00
|
|
|
import logging
|
2021-03-13 14:46:20 +00:00
|
|
|
from datetime import datetime, timedelta, timezone
|
2021-07-20 23:56:57 +00:00
|
|
|
from math import isclose
|
2021-03-22 18:39:06 +00:00
|
|
|
from pathlib import Path
|
2021-02-22 05:54:33 +00:00
|
|
|
from types import FunctionType
|
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
|
2021-04-06 12:53:08 +00:00
|
|
|
from sqlalchemy import create_engine, inspect, text
|
2018-03-17 21:44:47 +00:00
|
|
|
|
2019-12-30 14:02:17 +00:00
|
|
|
from freqtrade import constants
|
2021-07-20 23:56:57 +00:00
|
|
|
from freqtrade.enums import InterestMode
|
2020-09-06 12:33:45 +00:00
|
|
|
from freqtrade.exceptions import DependencyException, OperationalException
|
2021-02-20 19:07:00 +00:00
|
|
|
from freqtrade.persistence import LocalTrade, Order, Trade, clean_dry_run_db, init_db
|
2021-07-20 23:56:57 +00:00
|
|
|
from tests.conftest import create_mock_trades, create_mock_trades_with_leverage, log_has, log_has_re
|
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
|
2020-10-16 05:39:12 +00:00
|
|
|
init_db(default_conf['db_url'], default_conf['dry_run'])
|
2021-04-05 06:46:12 +00:00
|
|
|
assert hasattr(Trade, '_session')
|
|
|
|
assert 'scoped_session' in type(Trade._session).__name__
|
2017-12-27 10:41:11 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:39:06 +00:00
|
|
|
def test_init_custom_db_url(default_conf, tmpdir):
|
2018-06-07 03:27:55 +00:00
|
|
|
# Update path to a value other than default, but still in-memory
|
2021-03-22 18:39:06 +00:00
|
|
|
filename = f"{tmpdir}/freqtrade2_test.sqlite"
|
|
|
|
assert not Path(filename).is_file()
|
|
|
|
|
|
|
|
default_conf.update({'db_url': f'sqlite:///{filename}'})
|
2017-12-27 10:41:11 +00:00
|
|
|
|
2020-10-16 05:39:12 +00:00
|
|
|
init_db(default_conf['db_url'], default_conf['dry_run'])
|
2021-03-22 18:39:06 +00:00
|
|
|
assert Path(filename).is_file()
|
2018-06-07 19:35:57 +00:00
|
|
|
|
|
|
|
|
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*'):
|
2020-10-16 05:39:12 +00:00
|
|
|
init_db(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
|
|
|
|
2020-08-13 06:28:46 +00:00
|
|
|
create_engine_mock = mocker.patch('freqtrade.persistence.models.create_engine', MagicMock())
|
2018-06-07 18:41:52 +00:00
|
|
|
|
2020-10-16 05:39:12 +00:00
|
|
|
init_db(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'
|
|
|
|
|
|
|
|
|
2021-03-22 18:39:06 +00:00
|
|
|
def test_init_dryrun_db(default_conf, tmpdir):
|
|
|
|
filename = f"{tmpdir}/freqtrade2_prod.sqlite"
|
|
|
|
assert not Path(filename).is_file()
|
|
|
|
default_conf.update({
|
|
|
|
'dry_run': True,
|
|
|
|
'db_url': f'sqlite:///{filename}'
|
|
|
|
})
|
2018-06-07 18:41:52 +00:00
|
|
|
|
2020-10-16 05:39:12 +00:00
|
|
|
init_db(default_conf['db_url'], default_conf['dry_run'])
|
2021-03-22 18:39:06 +00:00
|
|
|
assert Path(filename).is_file()
|
2017-11-09 19:02:41 +00:00
|
|
|
|
|
|
|
|
2021-06-27 09:38:56 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_is_opening_closing_trade(fee):
|
|
|
|
trade = Trade(
|
|
|
|
id=2,
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.001,
|
|
|
|
open_rate=0.01,
|
|
|
|
amount=5,
|
|
|
|
is_open=True,
|
|
|
|
open_date=arrow.utcnow().datetime,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
exchange='binance',
|
|
|
|
is_short=False,
|
|
|
|
leverage=2.0
|
|
|
|
)
|
2021-07-08 03:14:08 +00:00
|
|
|
assert trade.is_opening_trade('buy') is True
|
|
|
|
assert trade.is_opening_trade('sell') is False
|
|
|
|
assert trade.is_closing_trade('buy') is False
|
|
|
|
assert trade.is_closing_trade('sell') is True
|
2021-07-17 07:57:57 +00:00
|
|
|
assert trade.enter_side == 'buy'
|
|
|
|
assert trade.exit_side == 'sell'
|
2021-06-27 09:38:56 +00:00
|
|
|
|
|
|
|
trade = Trade(
|
|
|
|
id=2,
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.001,
|
|
|
|
open_rate=0.01,
|
|
|
|
amount=5,
|
|
|
|
is_open=True,
|
|
|
|
open_date=arrow.utcnow().datetime,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
exchange='binance',
|
|
|
|
is_short=True,
|
|
|
|
leverage=2.0
|
|
|
|
)
|
|
|
|
|
2021-07-08 03:14:08 +00:00
|
|
|
assert trade.is_opening_trade('buy') is False
|
|
|
|
assert trade.is_opening_trade('sell') is True
|
|
|
|
assert trade.is_closing_trade('buy') is True
|
|
|
|
assert trade.is_closing_trade('sell') is False
|
2021-07-17 07:57:57 +00:00
|
|
|
assert trade.enter_side == 'sell'
|
|
|
|
assert trade.exit_side == 'buy'
|
2021-06-27 09:38:56 +00:00
|
|
|
|
|
|
|
|
2021-07-13 01:39:35 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2021-07-25 08:57:17 +00:00
|
|
|
def test_set_stop_loss_isolated_liq(fee):
|
2021-07-13 01:39:35 +00:00
|
|
|
trade = Trade(
|
|
|
|
id=2,
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.001,
|
|
|
|
open_rate=0.01,
|
|
|
|
amount=5,
|
|
|
|
is_open=True,
|
|
|
|
open_date=arrow.utcnow().datetime,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
exchange='binance',
|
|
|
|
is_short=False,
|
|
|
|
leverage=2.0
|
|
|
|
)
|
2021-07-25 08:57:17 +00:00
|
|
|
trade.set_isolated_liq(0.09)
|
|
|
|
assert trade.isolated_liq == 0.09
|
2021-07-13 01:39:35 +00:00
|
|
|
assert trade.stop_loss == 0.09
|
|
|
|
assert trade.initial_stop_loss == 0.09
|
|
|
|
|
|
|
|
trade.set_stop_loss(0.1)
|
2021-07-25 08:57:17 +00:00
|
|
|
assert trade.isolated_liq == 0.09
|
2021-07-13 01:39:35 +00:00
|
|
|
assert trade.stop_loss == 0.1
|
|
|
|
assert trade.initial_stop_loss == 0.09
|
|
|
|
|
2021-07-25 08:57:17 +00:00
|
|
|
trade.set_isolated_liq(0.08)
|
|
|
|
assert trade.isolated_liq == 0.08
|
2021-07-13 01:39:35 +00:00
|
|
|
assert trade.stop_loss == 0.1
|
|
|
|
assert trade.initial_stop_loss == 0.09
|
|
|
|
|
2021-07-25 08:57:17 +00:00
|
|
|
trade.set_isolated_liq(0.11)
|
|
|
|
assert trade.isolated_liq == 0.11
|
2021-07-13 01:39:35 +00:00
|
|
|
assert trade.stop_loss == 0.11
|
|
|
|
assert trade.initial_stop_loss == 0.09
|
|
|
|
|
|
|
|
trade.set_stop_loss(0.1)
|
2021-07-25 08:57:17 +00:00
|
|
|
assert trade.isolated_liq == 0.11
|
2021-07-13 01:39:35 +00:00
|
|
|
assert trade.stop_loss == 0.11
|
|
|
|
assert trade.initial_stop_loss == 0.09
|
|
|
|
|
|
|
|
trade.stop_loss = None
|
2021-07-25 08:57:17 +00:00
|
|
|
trade.isolated_liq = None
|
2021-07-13 01:39:35 +00:00
|
|
|
trade.initial_stop_loss = None
|
|
|
|
trade.set_stop_loss(0.07)
|
2021-07-25 08:57:17 +00:00
|
|
|
assert trade.isolated_liq is None
|
2021-07-13 01:39:35 +00:00
|
|
|
assert trade.stop_loss == 0.07
|
|
|
|
assert trade.initial_stop_loss == 0.07
|
|
|
|
|
2021-07-20 23:56:57 +00:00
|
|
|
trade.set_is_short(True)
|
2021-07-13 01:39:35 +00:00
|
|
|
trade.stop_loss = None
|
|
|
|
trade.initial_stop_loss = None
|
|
|
|
|
2021-07-25 08:57:17 +00:00
|
|
|
trade.set_isolated_liq(0.09)
|
|
|
|
assert trade.isolated_liq == 0.09
|
2021-07-13 01:39:35 +00:00
|
|
|
assert trade.stop_loss == 0.09
|
|
|
|
assert trade.initial_stop_loss == 0.09
|
|
|
|
|
|
|
|
trade.set_stop_loss(0.08)
|
2021-07-25 08:57:17 +00:00
|
|
|
assert trade.isolated_liq == 0.09
|
2021-07-13 01:39:35 +00:00
|
|
|
assert trade.stop_loss == 0.08
|
|
|
|
assert trade.initial_stop_loss == 0.09
|
|
|
|
|
2021-07-25 08:57:17 +00:00
|
|
|
trade.set_isolated_liq(0.1)
|
|
|
|
assert trade.isolated_liq == 0.1
|
2021-07-13 01:39:35 +00:00
|
|
|
assert trade.stop_loss == 0.08
|
|
|
|
assert trade.initial_stop_loss == 0.09
|
|
|
|
|
2021-07-25 08:57:17 +00:00
|
|
|
trade.set_isolated_liq(0.07)
|
|
|
|
assert trade.isolated_liq == 0.07
|
2021-07-13 01:39:35 +00:00
|
|
|
assert trade.stop_loss == 0.07
|
|
|
|
assert trade.initial_stop_loss == 0.09
|
|
|
|
|
|
|
|
trade.set_stop_loss(0.1)
|
2021-07-25 08:57:17 +00:00
|
|
|
assert trade.isolated_liq == 0.07
|
2021-07-13 01:39:35 +00:00
|
|
|
assert trade.stop_loss == 0.07
|
|
|
|
assert trade.initial_stop_loss == 0.09
|
|
|
|
|
|
|
|
|
2018-04-07 18:06:53 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2021-07-20 23:56:57 +00:00
|
|
|
def test_interest(market_buy_order_usdt, fee):
|
2017-12-17 21:07:56 +00:00
|
|
|
"""
|
2021-07-20 23:56:57 +00:00
|
|
|
10min, 5hr limit trade on Binance/Kraken at 3x,5x leverage
|
|
|
|
fee: 0.25 % quote
|
|
|
|
interest_rate: 0.05 % per 4 hrs
|
|
|
|
open_rate: 2.00 quote
|
|
|
|
close_rate: 2.20 quote
|
|
|
|
amount: = 30.0 crypto
|
|
|
|
stake_amount
|
|
|
|
3x, -3x: 20.0 quote
|
|
|
|
5x, -5x: 12.0 quote
|
|
|
|
borrowed
|
|
|
|
10min
|
|
|
|
3x: 40 quote
|
|
|
|
-3x: 30 crypto
|
|
|
|
5x: 48 quote
|
|
|
|
-5x: 30 crypto
|
|
|
|
1x: 0
|
|
|
|
-1x: 30 crypto
|
|
|
|
hours: 1/6 (10 minutes)
|
|
|
|
time-periods:
|
|
|
|
10min
|
|
|
|
kraken: (1 + 1) 4hr_periods = 2 4hr_periods
|
|
|
|
binance: 1/24 24hr_periods
|
|
|
|
4.95hr
|
|
|
|
kraken: ceil(1 + 4.95/4) 4hr_periods = 3 4hr_periods
|
|
|
|
binance: ceil(4.95)/24 24hr_periods = 5/24 24hr_periods
|
|
|
|
interest: borrowed * interest_rate * time-periods
|
|
|
|
10min
|
|
|
|
binance 3x: 40 * 0.0005 * 1/24 = 0.0008333333333333334 quote
|
|
|
|
kraken 3x: 40 * 0.0005 * 2 = 0.040 quote
|
|
|
|
binace -3x: 30 * 0.0005 * 1/24 = 0.000625 crypto
|
|
|
|
kraken -3x: 30 * 0.0005 * 2 = 0.030 crypto
|
|
|
|
5hr
|
|
|
|
binance 3x: 40 * 0.0005 * 5/24 = 0.004166666666666667 quote
|
|
|
|
kraken 3x: 40 * 0.0005 * 3 = 0.06 quote
|
|
|
|
binace -3x: 30 * 0.0005 * 5/24 = 0.0031249999999999997 crypto
|
|
|
|
kraken -3x: 30 * 0.0005 * 3 = 0.045 crypto
|
|
|
|
0.00025 interest
|
|
|
|
binance 3x: 40 * 0.00025 * 5/24 = 0.0020833333333333333 quote
|
|
|
|
kraken 3x: 40 * 0.00025 * 3 = 0.03 quote
|
|
|
|
binace -3x: 30 * 0.00025 * 5/24 = 0.0015624999999999999 crypto
|
|
|
|
kraken -3x: 30 * 0.00025 * 3 = 0.0225 crypto
|
|
|
|
5x leverage, 0.0005 interest, 5hr
|
|
|
|
binance 5x: 48 * 0.0005 * 5/24 = 0.005 quote
|
|
|
|
kraken 5x: 48 * 0.0005 * 3 = 0.07200000000000001 quote
|
|
|
|
binace -5x: 30 * 0.0005 * 5/24 = 0.0031249999999999997 crypto
|
|
|
|
kraken -5x: 30 * 0.0005 * 3 = 0.045 crypto
|
|
|
|
1x leverage, 0.0005 interest, 5hr
|
|
|
|
binance,kraken 1x: 0.0 quote
|
|
|
|
binace -1x: 30 * 0.0005 * 5/24 = 0.003125 crypto
|
|
|
|
kraken -1x: 30 * 0.0005 * 3 = 0.045 crypto
|
|
|
|
"""
|
|
|
|
|
|
|
|
trade = Trade(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=20.0,
|
|
|
|
amount=30.0,
|
|
|
|
open_rate=2.0,
|
|
|
|
open_date=datetime.utcnow() - timedelta(hours=0, minutes=10),
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
exchange='kraken',
|
|
|
|
leverage=3.0,
|
|
|
|
interest_rate=0.0005,
|
|
|
|
interest_mode=InterestMode.HOURSPERDAY
|
|
|
|
)
|
|
|
|
|
|
|
|
# 10min, 3x leverage
|
|
|
|
# binance
|
|
|
|
assert round(float(trade.calculate_interest()), 8) == round(0.0008333333333333334, 8)
|
|
|
|
# kraken
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4
|
|
|
|
assert float(trade.calculate_interest()) == 0.040
|
|
|
|
# Short
|
|
|
|
trade.set_is_short(True)
|
|
|
|
# binace
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY
|
|
|
|
assert float(trade.calculate_interest()) == 0.000625
|
|
|
|
# kraken
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4
|
|
|
|
assert isclose(float(trade.calculate_interest()), 0.030)
|
|
|
|
|
|
|
|
# 5hr, long
|
|
|
|
trade.open_date = datetime.utcnow() - timedelta(hours=4, minutes=55)
|
|
|
|
trade.set_is_short(False)
|
|
|
|
# binance
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY
|
|
|
|
assert round(float(trade.calculate_interest()), 8) == round(0.004166666666666667, 8)
|
|
|
|
# kraken
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4
|
|
|
|
assert float(trade.calculate_interest()) == 0.06
|
|
|
|
# short
|
|
|
|
trade.set_is_short(True)
|
|
|
|
# binace
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY
|
|
|
|
assert round(float(trade.calculate_interest()), 8) == round(0.0031249999999999997, 8)
|
|
|
|
# kraken
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4
|
|
|
|
assert float(trade.calculate_interest()) == 0.045
|
|
|
|
|
|
|
|
# 0.00025 interest, 5hr, long
|
|
|
|
trade.set_is_short(False)
|
|
|
|
# binance
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY
|
|
|
|
assert round(float(trade.calculate_interest(interest_rate=0.00025)),
|
|
|
|
8) == round(0.0020833333333333333, 8)
|
|
|
|
# kraken
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4
|
|
|
|
assert isclose(float(trade.calculate_interest(interest_rate=0.00025)), 0.03)
|
|
|
|
# short
|
|
|
|
trade.set_is_short(True)
|
|
|
|
# binace
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY
|
|
|
|
assert round(float(trade.calculate_interest(interest_rate=0.00025)),
|
|
|
|
8) == round(0.0015624999999999999, 8)
|
|
|
|
# kraken
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4
|
|
|
|
assert float(trade.calculate_interest(interest_rate=0.00025)) == 0.0225
|
|
|
|
|
|
|
|
# 5x leverage, 0.0005 interest, 5hr, long
|
|
|
|
trade.set_is_short(False)
|
|
|
|
trade.leverage = 5.0
|
|
|
|
# binance
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY
|
|
|
|
assert round(float(trade.calculate_interest()), 8) == 0.005
|
|
|
|
# kraken
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4
|
|
|
|
assert float(trade.calculate_interest()) == round(0.07200000000000001, 8)
|
|
|
|
# short
|
|
|
|
trade.set_is_short(True)
|
|
|
|
# binace
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY
|
|
|
|
assert round(float(trade.calculate_interest()), 8) == round(0.0031249999999999997, 8)
|
|
|
|
# kraken
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4
|
|
|
|
assert float(trade.calculate_interest()) == 0.045
|
|
|
|
|
|
|
|
# 1x leverage, 0.0005 interest, 5hr
|
|
|
|
trade.set_is_short(False)
|
|
|
|
trade.leverage = 1.0
|
|
|
|
# binance
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY
|
|
|
|
assert float(trade.calculate_interest()) == 0.0
|
|
|
|
# kraken
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4
|
|
|
|
assert float(trade.calculate_interest()) == 0.0
|
|
|
|
# short
|
|
|
|
trade.set_is_short(True)
|
|
|
|
# binace
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY
|
|
|
|
assert float(trade.calculate_interest()) == 0.003125
|
|
|
|
# kraken
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4
|
|
|
|
assert float(trade.calculate_interest()) == 0.045
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_borrowed(limit_buy_order_usdt, limit_sell_order_usdt, fee, caplog):
|
|
|
|
"""
|
|
|
|
10 minute limit trade on Binance/Kraken at 1x, 3x leverage
|
|
|
|
fee: 0.25% quote
|
|
|
|
interest_rate: 0.05% per 4 hrs
|
|
|
|
open_rate: 2.00 quote
|
|
|
|
close_rate: 2.20 quote
|
|
|
|
amount: = 30.0 crypto
|
|
|
|
stake_amount
|
|
|
|
1x,-1x: 60.0 quote
|
|
|
|
3x,-3x: 20.0 quote
|
|
|
|
borrowed
|
|
|
|
1x: 0 quote
|
|
|
|
3x: 40 quote
|
|
|
|
-1x: 30 crypto
|
|
|
|
-3x: 30 crypto
|
|
|
|
hours: 1/6 (10 minutes)
|
|
|
|
time-periods:
|
|
|
|
kraken: (1 + 1) 4hr_periods = 2 4hr_periods
|
|
|
|
binance: 1/24 24hr_periods
|
|
|
|
interest: borrowed * interest_rate * time-periods
|
|
|
|
1x : /
|
|
|
|
binance 3x: 40 * 0.0005 * 1/24 = 0.0008333333333333334 quote
|
|
|
|
kraken 3x: 40 * 0.0005 * 2 = 0.040 quote
|
|
|
|
binace -1x,-3x: 30 * 0.0005 * 1/24 = 0.000625 crypto
|
|
|
|
kraken -1x,-3x: 30 * 0.0005 * 2 = 0.030 crypto
|
|
|
|
open_value: (amount * open_rate) ± (amount * open_rate * fee)
|
|
|
|
1x, 3x: 30 * 2 + 30 * 2 * 0.0025 = 60.15 quote
|
|
|
|
-1x,-3x: 30 * 2 - 30 * 2 * 0.0025 = 59.850 quote
|
|
|
|
amount_closed:
|
|
|
|
1x, 3x : amount
|
|
|
|
-1x, -3x : amount + interest
|
|
|
|
binance -1x,-3x: 30 + 0.000625 = 30.000625 crypto
|
|
|
|
kraken -1x,-3x: 30 + 0.03 = 30.03 crypto
|
|
|
|
close_value:
|
|
|
|
1x, 3x: (amount_closed * close_rate) - (amount_closed * close_rate * fee) - interest
|
|
|
|
-1x,-3x: (amount_closed * close_rate) + (amount_closed * close_rate * fee)
|
|
|
|
binance,kraken 1x: (30.00 * 2.20) - (30.00 * 2.20 * 0.0025) = 65.835
|
|
|
|
binance 3x: (30.00 * 2.20) - (30.00 * 2.20 * 0.0025) - 0.00083333 = 65.83416667
|
|
|
|
kraken 3x: (30.00 * 2.20) - (30.00 * 2.20 * 0.0025) - 0.040 = 65.795
|
|
|
|
binance -1x,-3x: (30.000625 * 2.20) + (30.000625 * 2.20 * 0.0025) = 66.16637843750001
|
|
|
|
kraken -1x,-3x: (30.03 * 2.20) + (30.03 * 2.20 * 0.0025) = 66.231165
|
|
|
|
total_profit:
|
|
|
|
1x, 3x : close_value - open_value
|
|
|
|
-1x,-3x: open_value - close_value
|
|
|
|
binance,kraken 1x: 65.835 - 60.15 = 5.685
|
|
|
|
binance 3x: 65.83416667 - 60.15 = 5.684166670000003
|
|
|
|
kraken 3x: 65.795 - 60.15 = 5.645
|
|
|
|
binance -1x,-3x: 59.850 - 66.16637843750001 = -6.316378437500013
|
|
|
|
kraken -1x,-3x: 59.850 - 66.231165 = -6.381165
|
|
|
|
total_profit_ratio:
|
|
|
|
1x, 3x : ((close_value/open_value) - 1) * leverage
|
|
|
|
-1x,-3x: (1 - (close_value/open_value)) * leverage
|
|
|
|
binance 1x: ((65.835 / 60.15) - 1) * 1 = 0.0945137157107232
|
|
|
|
binance 3x: ((65.83416667 / 60.15) - 1) * 3 = 0.2834995845386534
|
|
|
|
kraken 1x: ((65.835 / 60.15) - 1) * 1 = 0.0945137157107232
|
|
|
|
kraken 3x: ((65.795 / 60.15) - 1) * 3 = 0.2815461346633419
|
|
|
|
binance -1x: (1-(66.1663784375 / 59.85)) * 1 = -0.1055368159983292
|
|
|
|
binance -3x: (1-(66.1663784375 / 59.85)) * 3 = -0.3166104479949876
|
|
|
|
kraken -1x: (1-(66.2311650 / 59.85)) * 1 = -0.106619298245614
|
|
|
|
kraken -3x: (1-(66.2311650 / 59.85)) * 3 = -0.319857894736842
|
2017-12-17 21:07:56 +00:00
|
|
|
"""
|
|
|
|
|
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',
|
2021-07-20 23:56:57 +00:00
|
|
|
stake_amount=60.0,
|
|
|
|
open_rate=2.0,
|
|
|
|
amount=30.0,
|
|
|
|
is_open=True,
|
|
|
|
open_date=arrow.utcnow().datetime,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
exchange='binance',
|
|
|
|
)
|
|
|
|
assert trade.borrowed == 0
|
|
|
|
trade.set_is_short(True)
|
|
|
|
assert trade.borrowed == 30.0
|
|
|
|
trade.leverage = 3.0
|
|
|
|
assert trade.borrowed == 30.0
|
|
|
|
trade.set_is_short(False)
|
|
|
|
assert trade.borrowed == 40.0
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_update_limit_order(limit_buy_order_usdt, limit_sell_order_usdt, fee, caplog):
|
|
|
|
"""
|
|
|
|
10 minute limit trade on Binance/Kraken at 1x, 3x leverage
|
|
|
|
fee: 0.25% quote
|
|
|
|
interest_rate: 0.05% per 4 hrs
|
|
|
|
open_rate: 2.00 quote
|
|
|
|
close_rate: 2.20 quote
|
|
|
|
amount: = 30.0 crypto
|
|
|
|
stake_amount
|
|
|
|
1x,-1x: 60.0 quote
|
|
|
|
3x,-3x: 20.0 quote
|
|
|
|
borrowed
|
|
|
|
1x: 0 quote
|
|
|
|
3x: 40 quote
|
|
|
|
-1x: 30 crypto
|
|
|
|
-3x: 30 crypto
|
|
|
|
hours: 1/6 (10 minutes)
|
|
|
|
time-periods:
|
|
|
|
kraken: (1 + 1) 4hr_periods = 2 4hr_periods
|
|
|
|
binance: 1/24 24hr_periods
|
|
|
|
interest: borrowed * interest_rate * time-periods
|
|
|
|
1x : /
|
|
|
|
binance 3x: 40 * 0.0005 * 1/24 = 0.0008333333333333334 quote
|
|
|
|
kraken 3x: 40 * 0.0005 * 2 = 0.040 quote
|
|
|
|
binace -1x,-3x: 30 * 0.0005 * 1/24 = 0.000625 crypto
|
|
|
|
kraken -1x,-3x: 30 * 0.0005 * 2 = 0.030 crypto
|
|
|
|
open_value: (amount * open_rate) ± (amount * open_rate * fee)
|
|
|
|
1x, 3x: 30 * 2 + 30 * 2 * 0.0025 = 60.15 quote
|
|
|
|
-1x,-3x: 30 * 2 - 30 * 2 * 0.0025 = 59.850 quote
|
|
|
|
amount_closed:
|
|
|
|
1x, 3x : amount
|
|
|
|
-1x, -3x : amount + interest
|
|
|
|
binance -1x,-3x: 30 + 0.000625 = 30.000625 crypto
|
|
|
|
kraken -1x,-3x: 30 + 0.03 = 30.03 crypto
|
|
|
|
close_value:
|
|
|
|
1x, 3x: (amount_closed * close_rate) - (amount_closed * close_rate * fee) - interest
|
|
|
|
-1x,-3x: (amount_closed * close_rate) + (amount_closed * close_rate * fee)
|
|
|
|
binance,kraken 1x: (30.00 * 2.20) - (30.00 * 2.20 * 0.0025) = 65.835
|
|
|
|
binance 3x: (30.00 * 2.20) - (30.00 * 2.20 * 0.0025) - 0.00083333 = 65.83416667
|
|
|
|
kraken 3x: (30.00 * 2.20) - (30.00 * 2.20 * 0.0025) - 0.040 = 65.795
|
|
|
|
binance -1x,-3x: (30.000625 * 2.20) + (30.000625 * 2.20 * 0.0025) = 66.16637843750001
|
|
|
|
kraken -1x,-3x: (30.03 * 2.20) + (30.03 * 2.20 * 0.0025) = 66.231165
|
|
|
|
total_profit:
|
|
|
|
1x, 3x : close_value - open_value
|
|
|
|
-1x,-3x: open_value - close_value
|
|
|
|
binance,kraken 1x: 65.835 - 60.15 = 5.685
|
|
|
|
binance 3x: 65.83416667 - 60.15 = 5.684166670000003
|
|
|
|
kraken 3x: 65.795 - 60.15 = 5.645
|
|
|
|
binance -1x,-3x: 59.850 - 66.16637843750001 = -6.316378437500013
|
|
|
|
kraken -1x,-3x: 59.850 - 66.231165 = -6.381165
|
|
|
|
total_profit_ratio:
|
|
|
|
1x, 3x : ((close_value/open_value) - 1) * leverage
|
|
|
|
-1x,-3x: (1 - (close_value/open_value)) * leverage
|
|
|
|
binance 1x: ((65.835 / 60.15) - 1) * 1 = 0.0945137157107232
|
|
|
|
binance 3x: ((65.83416667 / 60.15) - 1) * 3 = 0.2834995845386534
|
|
|
|
kraken 1x: ((65.835 / 60.15) - 1) * 1 = 0.0945137157107232
|
|
|
|
kraken 3x: ((65.795 / 60.15) - 1) * 3 = 0.2815461346633419
|
|
|
|
binance -1x: (1-(66.1663784375 / 59.85)) * 1 = -0.1055368159983292
|
|
|
|
binance -3x: (1-(66.1663784375 / 59.85)) * 3 = -0.3166104479949876
|
|
|
|
kraken -1x: (1-(66.2311650 / 59.85)) * 1 = -0.106619298245614
|
|
|
|
kraken -3x: (1-(66.2311650 / 59.85)) * 3 = -0.319857894736842
|
|
|
|
"""
|
|
|
|
|
|
|
|
trade = Trade(
|
|
|
|
id=2,
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=60.0,
|
|
|
|
open_rate=2.0,
|
|
|
|
amount=30.0,
|
2020-09-19 06:33:12 +00:00
|
|
|
is_open=True,
|
|
|
|
open_date=arrow.utcnow().datetime,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
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'
|
2021-07-20 23:56:57 +00:00
|
|
|
trade.update(limit_buy_order_usdt)
|
2017-11-09 19:02:41 +00:00
|
|
|
assert trade.open_order_id is None
|
2021-07-20 23:56:57 +00:00
|
|
|
assert trade.open_rate == 2.00
|
2017-11-09 19:02:41 +00:00
|
|
|
assert trade.close_profit is None
|
|
|
|
assert trade.close_date is None
|
2020-09-19 06:33:12 +00:00
|
|
|
assert log_has_re(r"LIMIT_BUY has been fulfilled for Trade\(id=2, "
|
2021-07-20 23:56:57 +00:00
|
|
|
r"pair=ETH/BTC, amount=30.00000000, open_rate=2.00000000, open_since=.*\).",
|
2020-09-19 06:33:12 +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'
|
2021-07-20 23:56:57 +00:00
|
|
|
trade.update(limit_sell_order_usdt)
|
2017-11-09 19:02:41 +00:00
|
|
|
assert trade.open_order_id is None
|
2021-07-20 23:56:57 +00:00
|
|
|
assert trade.close_rate == 2.20
|
|
|
|
assert trade.close_profit == round(0.0945137157107232, 8)
|
2017-11-09 19:02:41 +00:00
|
|
|
assert trade.close_date is not None
|
2020-09-19 06:33:12 +00:00
|
|
|
assert log_has_re(r"LIMIT_SELL has been fulfilled for Trade\(id=2, "
|
2021-07-20 23:56:57 +00:00
|
|
|
r"pair=ETH/BTC, amount=30.00000000, open_rate=2.00000000, open_since=.*\).",
|
2020-09-19 06:33:12 +00:00
|
|
|
caplog)
|
2017-11-09 19:02:41 +00:00
|
|
|
|
|
|
|
|
2018-12-27 08:31:21 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2021-07-20 23:56:57 +00:00
|
|
|
def test_update_market_order(market_buy_order_usdt, market_sell_order_usdt, 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',
|
2021-07-20 23:56:57 +00:00
|
|
|
stake_amount=60.0,
|
|
|
|
open_rate=2.0,
|
|
|
|
amount=30.0,
|
2020-09-19 06:33:12 +00:00
|
|
|
is_open=True,
|
2018-12-27 08:31:21 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2020-09-19 06:33:12 +00:00
|
|
|
open_date=arrow.utcnow().datetime,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2018-12-27 08:31:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
trade.open_order_id = 'something'
|
2021-07-20 23:56:57 +00:00
|
|
|
trade.update(market_buy_order_usdt)
|
2018-12-27 08:31:21 +00:00
|
|
|
assert trade.open_order_id is None
|
2021-07-20 23:56:57 +00:00
|
|
|
assert trade.open_rate == 2.0
|
2018-12-27 08:31:21 +00:00
|
|
|
assert trade.close_profit is None
|
|
|
|
assert trade.close_date is None
|
2020-09-19 06:33:12 +00:00
|
|
|
assert log_has_re(r"MARKET_BUY has been fulfilled for Trade\(id=1, "
|
2021-07-20 23:56:57 +00:00
|
|
|
r"pair=ETH/BTC, amount=30.00000000, open_rate=2.00000000, open_since=.*\).",
|
2020-09-19 06:33:12 +00:00
|
|
|
caplog)
|
2018-12-27 08:31:21 +00:00
|
|
|
|
2018-12-27 10:19:26 +00:00
|
|
|
caplog.clear()
|
2020-09-19 06:33:12 +00:00
|
|
|
trade.is_open = True
|
2018-12-27 08:31:21 +00:00
|
|
|
trade.open_order_id = 'something'
|
2021-07-20 23:56:57 +00:00
|
|
|
trade.update(market_sell_order_usdt)
|
2018-12-27 08:31:21 +00:00
|
|
|
assert trade.open_order_id is None
|
2021-07-20 23:56:57 +00:00
|
|
|
assert trade.close_rate == 2.2
|
|
|
|
assert trade.close_profit == round(0.0945137157107232, 8)
|
2018-12-27 08:31:21 +00:00
|
|
|
assert trade.close_date is not None
|
2020-09-19 06:33:12 +00:00
|
|
|
assert log_has_re(r"MARKET_SELL has been fulfilled for Trade\(id=1, "
|
2021-07-20 23:56:57 +00:00
|
|
|
r"pair=ETH/BTC, amount=30.00000000, open_rate=2.00000000, open_since=.*\).",
|
2020-09-19 06:33:12 +00:00
|
|
|
caplog)
|
2018-12-27 08:31:21 +00:00
|
|
|
|
|
|
|
|
2018-04-07 18:06:53 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2021-07-20 23:56:57 +00:00
|
|
|
def test_calc_open_close_trade_price(limit_buy_order_usdt, limit_sell_order_usdt, fee):
|
2017-12-17 21:07:56 +00:00
|
|
|
trade = Trade(
|
2018-03-24 19:02:13 +00:00
|
|
|
pair='ETH/BTC',
|
2021-07-20 23:56:57 +00:00
|
|
|
stake_amount=60.0,
|
|
|
|
open_rate=2.0,
|
|
|
|
amount=30.0,
|
|
|
|
open_date=datetime.now(tz=timezone.utc) - timedelta(minutes=10),
|
|
|
|
interest_rate=0.0005,
|
|
|
|
interest_mode=InterestMode.HOURSPERDAY,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2017-12-17 21:07:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
trade.open_order_id = 'something'
|
2021-07-20 23:56:57 +00:00
|
|
|
trade.update(limit_buy_order_usdt)
|
|
|
|
trade.update(limit_sell_order_usdt)
|
|
|
|
# 1x leverage, binance
|
|
|
|
assert trade._calc_open_trade_value() == 60.15
|
|
|
|
assert isclose(trade.calc_close_trade_value(), 65.835)
|
|
|
|
assert trade.calc_profit() == 5.685
|
|
|
|
assert trade.calc_profit_ratio() == round(0.0945137157107232, 8)
|
|
|
|
# 3x leverage, binance
|
|
|
|
trade.leverage = 3
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY
|
|
|
|
assert trade._calc_open_trade_value() == 60.15
|
|
|
|
assert round(trade.calc_close_trade_value(), 8) == 65.83416667
|
|
|
|
assert trade.calc_profit() == round(5.684166670000003, 8)
|
|
|
|
assert trade.calc_profit_ratio() == round(0.2834995845386534, 8)
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4
|
|
|
|
# 3x leverage, kraken
|
|
|
|
assert trade._calc_open_trade_value() == 60.15
|
|
|
|
assert trade.calc_close_trade_value() == 65.795
|
|
|
|
assert trade.calc_profit() == 5.645
|
|
|
|
assert trade.calc_profit_ratio() == round(0.2815461346633419, 8)
|
|
|
|
trade.set_is_short(True)
|
|
|
|
# 3x leverage, short, kraken
|
|
|
|
assert trade._calc_open_trade_value() == 59.850
|
|
|
|
assert trade.calc_close_trade_value() == 66.231165
|
|
|
|
assert trade.calc_profit() == round(-6.381165000000003, 8)
|
|
|
|
assert trade.calc_profit_ratio() == round(-0.319857894736842, 8)
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY
|
|
|
|
# 3x leverage, short, binance
|
|
|
|
assert trade._calc_open_trade_value() == 59.85
|
|
|
|
assert trade.calc_close_trade_value() == 66.1663784375
|
|
|
|
assert trade.calc_profit() == round(-6.316378437500013, 8)
|
|
|
|
assert trade.calc_profit_ratio() == round(-0.3166104479949876, 8)
|
|
|
|
# 1x leverage, short, binance
|
|
|
|
trade.leverage = 1.0
|
|
|
|
assert trade._calc_open_trade_value() == 59.850
|
|
|
|
assert trade.calc_close_trade_value() == 66.1663784375
|
|
|
|
assert trade.calc_profit() == round(-6.316378437500013, 8)
|
|
|
|
assert trade.calc_profit_ratio() == round(-0.1055368159983292, 8)
|
|
|
|
# 1x leverage, short, kraken
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4
|
|
|
|
assert trade._calc_open_trade_value() == 59.850
|
|
|
|
assert trade.calc_close_trade_value() == 66.231165
|
|
|
|
assert trade.calc_profit() == -6.381165
|
|
|
|
assert trade.calc_profit_ratio() == round(-0.106619298245614, 8)
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
|
2020-09-19 06:42:15 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2021-07-20 23:56:57 +00:00
|
|
|
def test_trade_close(limit_buy_order_usdt, limit_sell_order_usdt, fee):
|
2020-09-19 06:42:15 +00:00
|
|
|
trade = Trade(
|
|
|
|
pair='ETH/BTC',
|
2021-07-20 23:56:57 +00:00
|
|
|
stake_amount=60.0,
|
|
|
|
open_rate=2.0,
|
|
|
|
amount=30.0,
|
2020-09-19 06:42:15 +00:00
|
|
|
is_open=True,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2021-07-20 23:56:57 +00:00
|
|
|
open_date=datetime.now(tz=timezone.utc) - timedelta(minutes=10),
|
|
|
|
interest_rate=0.0005,
|
|
|
|
interest_mode=InterestMode.HOURSPERDAY,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2020-09-19 06:42:15 +00:00
|
|
|
)
|
|
|
|
assert trade.close_profit is None
|
|
|
|
assert trade.close_date is None
|
|
|
|
assert trade.is_open is True
|
2021-07-20 23:56:57 +00:00
|
|
|
trade.close(2.2)
|
2020-09-19 06:42:15 +00:00
|
|
|
assert trade.is_open is False
|
2021-07-20 23:56:57 +00:00
|
|
|
assert trade.close_profit == round(0.0945137157107232, 8)
|
2020-09-19 06:42:15 +00:00
|
|
|
assert trade.close_date is not None
|
|
|
|
|
|
|
|
new_date = arrow.Arrow(2020, 2, 2, 15, 6, 1).datetime,
|
|
|
|
assert trade.close_date != new_date
|
|
|
|
# Close should NOT update close_date if the trade has been closed already
|
|
|
|
assert trade.is_open is False
|
|
|
|
trade.close_date = new_date
|
|
|
|
trade.close(0.02)
|
|
|
|
assert trade.close_date == new_date
|
|
|
|
|
|
|
|
|
2018-04-07 18:06:53 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2021-07-20 23:56:57 +00:00
|
|
|
def test_calc_close_trade_price_exception(limit_buy_order_usdt, fee):
|
2017-12-27 10:41:11 +00:00
|
|
|
trade = Trade(
|
2018-03-24 19:02:13 +00:00
|
|
|
pair='ETH/BTC',
|
2021-07-20 23:56:57 +00:00
|
|
|
stake_amount=60.0,
|
|
|
|
open_rate=2.0,
|
|
|
|
amount=30.0,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2017-12-27 10:41:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
trade.open_order_id = 'something'
|
2021-07-20 23:56:57 +00:00
|
|
|
trade.update(limit_buy_order_usdt)
|
2020-12-10 18:21:20 +00:00
|
|
|
assert trade.calc_close_trade_value() == 0.0
|
2017-12-27 10:41:11 +00:00
|
|
|
|
|
|
|
|
2018-04-07 18:06:53 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2021-07-20 23:56:57 +00:00
|
|
|
def test_update_open_order(limit_buy_order_usdt):
|
2017-11-09 19:02:41 +00:00
|
|
|
trade = Trade(
|
2018-03-24 19:02:13 +00:00
|
|
|
pair='ETH/BTC',
|
2021-07-20 23:56:57 +00:00
|
|
|
stake_amount=60.0,
|
|
|
|
open_rate=2.0,
|
|
|
|
amount=30.0,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=0.1,
|
|
|
|
fee_close=0.1,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
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
|
|
|
|
|
2021-07-20 23:56:57 +00:00
|
|
|
limit_buy_order_usdt['status'] = 'open'
|
|
|
|
trade.update(limit_buy_order_usdt)
|
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-04-07 18:06:53 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2021-07-20 23:56:57 +00:00
|
|
|
def test_update_invalid_order(limit_buy_order_usdt):
|
2017-11-09 19:02:41 +00:00
|
|
|
trade = Trade(
|
2018-03-24 19:02:13 +00:00
|
|
|
pair='ETH/BTC',
|
2021-07-20 23:56:57 +00:00
|
|
|
stake_amount=60.0,
|
|
|
|
amount=30.0,
|
|
|
|
open_rate=2.0,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=0.1,
|
|
|
|
fee_close=0.1,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2017-11-09 19:02:41 +00:00
|
|
|
)
|
2021-07-20 23:56:57 +00:00
|
|
|
limit_buy_order_usdt['type'] = 'invalid'
|
2017-11-09 19:02:41 +00:00
|
|
|
with pytest.raises(ValueError, match=r'Unknown order type'):
|
2021-07-20 23:56:57 +00:00
|
|
|
trade.update(limit_buy_order_usdt)
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
|
2018-04-07 18:06:53 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2021-07-20 23:56:57 +00:00
|
|
|
def test_calc_open_trade_value(limit_buy_order_usdt, fee):
|
|
|
|
# 10 minute limit trade on Binance/Kraken at 1x, 3x leverage
|
|
|
|
# fee: 0.25 %, 0.3% quote
|
|
|
|
# open_rate: 2.00 quote
|
|
|
|
# amount: = 30.0 crypto
|
|
|
|
# stake_amount
|
|
|
|
# 1x, -1x: 60.0 quote
|
|
|
|
# 3x, -3x: 20.0 quote
|
|
|
|
# open_value: (amount * open_rate) ± (amount * open_rate * fee)
|
|
|
|
# 0.25% fee
|
|
|
|
# 1x, 3x: 30 * 2 + 30 * 2 * 0.0025 = 60.15 quote
|
|
|
|
# -1x,-3x: 30 * 2 - 30 * 2 * 0.0025 = 59.85 quote
|
|
|
|
# 0.3% fee
|
|
|
|
# 1x, 3x: 30 * 2 + 30 * 2 * 0.003 = 60.18 quote
|
|
|
|
# -1x,-3x: 30 * 2 - 30 * 2 * 0.003 = 59.82 quote
|
2017-12-17 21:07:56 +00:00
|
|
|
trade = Trade(
|
2018-03-24 19:02:13 +00:00
|
|
|
pair='ETH/BTC',
|
2021-07-20 23:56:57 +00:00
|
|
|
stake_amount=60.0,
|
|
|
|
amount=30.0,
|
|
|
|
open_rate=2.0,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2017-12-17 21:07:56 +00:00
|
|
|
)
|
|
|
|
trade.open_order_id = 'open_trade'
|
2021-07-20 23:56:57 +00:00
|
|
|
trade.update(limit_buy_order_usdt)
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
# Get the open rate price with the standard fee rate
|
2021-07-20 23:56:57 +00:00
|
|
|
assert trade._calc_open_trade_value() == 60.15
|
|
|
|
trade.set_is_short(True)
|
|
|
|
assert trade._calc_open_trade_value() == 59.85
|
|
|
|
trade.leverage = 3
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY
|
|
|
|
assert trade._calc_open_trade_value() == 59.85
|
|
|
|
trade.set_is_short(False)
|
|
|
|
assert trade._calc_open_trade_value() == 60.15
|
|
|
|
|
2017-12-17 21:07:56 +00:00
|
|
|
# Get the open rate price with a custom fee rate
|
2021-07-20 23:56:57 +00:00
|
|
|
trade.fee_open = 0.003
|
|
|
|
|
|
|
|
assert trade._calc_open_trade_value() == 60.18
|
|
|
|
trade.set_is_short(True)
|
|
|
|
assert trade._calc_open_trade_value() == 59.82
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
|
2018-04-07 18:06:53 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2021-07-20 23:56:57 +00:00
|
|
|
def test_calc_close_trade_price(limit_buy_order_usdt, limit_sell_order_usdt, fee):
|
2017-12-17 21:07:56 +00:00
|
|
|
trade = Trade(
|
2018-03-24 19:02:13 +00:00
|
|
|
pair='ETH/BTC',
|
2021-07-20 23:56:57 +00:00
|
|
|
stake_amount=60.0,
|
|
|
|
amount=30.0,
|
|
|
|
open_rate=2.0,
|
|
|
|
open_date=datetime.now(tz=timezone.utc) - timedelta(minutes=10),
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2021-07-20 23:56:57 +00:00
|
|
|
interest_rate=0.0005,
|
|
|
|
interest_mode=InterestMode.HOURSPERDAY
|
2017-12-17 21:07:56 +00:00
|
|
|
)
|
|
|
|
trade.open_order_id = 'close_trade'
|
2021-07-20 23:56:57 +00:00
|
|
|
trade.update(limit_buy_order_usdt)
|
|
|
|
|
|
|
|
# 1x leverage binance
|
|
|
|
assert trade.calc_close_trade_value(rate=2.5) == 74.8125
|
|
|
|
assert trade.calc_close_trade_value(rate=2.5, fee=0.003) == 74.775
|
|
|
|
trade.update(limit_sell_order_usdt)
|
|
|
|
assert trade.calc_close_trade_value(fee=0.005) == 65.67
|
2017-12-17 21:07:56 +00:00
|
|
|
|
2021-07-20 23:56:57 +00:00
|
|
|
# 3x leverage binance
|
|
|
|
trade.leverage = 3.0
|
|
|
|
assert round(trade.calc_close_trade_value(rate=2.5), 8) == 74.81166667
|
|
|
|
assert round(trade.calc_close_trade_value(rate=2.5, fee=0.003), 8) == 74.77416667
|
2017-12-17 21:07:56 +00:00
|
|
|
|
2021-07-20 23:56:57 +00:00
|
|
|
# 3x leverage kraken
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4
|
|
|
|
assert trade.calc_close_trade_value(rate=2.5) == 74.7725
|
|
|
|
assert trade.calc_close_trade_value(rate=2.5, fee=0.003) == 74.735
|
2017-12-17 21:07:56 +00:00
|
|
|
|
2021-07-20 23:56:57 +00:00
|
|
|
# 3x leverage kraken, short
|
|
|
|
trade.set_is_short(True)
|
|
|
|
assert round(trade.calc_close_trade_value(rate=2.5), 8) == 75.2626875
|
|
|
|
assert trade.calc_close_trade_value(rate=2.5, fee=0.003) == 75.300225
|
|
|
|
|
|
|
|
# 3x leverage binance, short
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY
|
|
|
|
assert round(trade.calc_close_trade_value(rate=2.5), 8) == 75.18906641
|
|
|
|
assert round(trade.calc_close_trade_value(rate=2.5, fee=0.003), 8) == 75.22656719
|
|
|
|
|
|
|
|
trade.leverage = 1.0
|
|
|
|
# 1x leverage binance, short
|
|
|
|
assert round(trade.calc_close_trade_value(rate=2.5), 8) == 75.18906641
|
|
|
|
assert round(trade.calc_close_trade_value(rate=2.5, fee=0.003), 8) == 75.22656719
|
|
|
|
|
|
|
|
# 1x leverage kraken, short
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4
|
|
|
|
assert round(trade.calc_close_trade_value(rate=2.5), 8) == 75.2626875
|
|
|
|
assert trade.calc_close_trade_value(rate=2.5, fee=0.003) == 75.300225
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
|
2018-04-07 18:06:53 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2021-07-20 23:56:57 +00:00
|
|
|
def test_calc_profit(limit_buy_order_usdt, limit_sell_order_usdt, fee):
|
|
|
|
"""
|
|
|
|
10 minute limit trade on Binance/Kraken at 1x, 3x leverage
|
|
|
|
arguments:
|
|
|
|
fee:
|
|
|
|
0.25% quote
|
|
|
|
0.30% quote
|
|
|
|
interest_rate: 0.05% per 4 hrs
|
|
|
|
open_rate: 2.0 quote
|
|
|
|
close_rate:
|
|
|
|
1.9 quote
|
|
|
|
2.1 quote
|
|
|
|
2.2 quote
|
|
|
|
amount: = 30.0 crypto
|
|
|
|
stake_amount
|
|
|
|
1x,-1x: 60.0 quote
|
|
|
|
3x,-3x: 20.0 quote
|
|
|
|
hours: 1/6 (10 minutes)
|
|
|
|
borrowed
|
|
|
|
1x: 0 quote
|
|
|
|
3x: 40 quote
|
|
|
|
-1x: 30 crypto
|
|
|
|
-3x: 30 crypto
|
|
|
|
time-periods:
|
|
|
|
kraken: (1 + 1) 4hr_periods = 2 4hr_periods
|
|
|
|
binance: 1/24 24hr_periods
|
|
|
|
interest: borrowed * interest_rate * time-periods
|
|
|
|
1x : /
|
|
|
|
binance 3x: 40 * 0.0005 * 1/24 = 0.0008333333333333334 quote
|
|
|
|
kraken 3x: 40 * 0.0005 * 2 = 0.040 quote
|
|
|
|
binace -1x,-3x: 30 * 0.0005 * 1/24 = 0.000625 crypto
|
|
|
|
kraken -1x,-3x: 30 * 0.0005 * 2 = 0.030 crypto
|
|
|
|
open_value: (amount * open_rate) ± (amount * open_rate * fee)
|
|
|
|
0.0025 fee
|
|
|
|
1x, 3x: 30 * 2 + 30 * 2 * 0.0025 = 60.15 quote
|
|
|
|
-1x,-3x: 30 * 2 - 30 * 2 * 0.0025 = 59.85 quote
|
|
|
|
0.003 fee: Is only applied to close rate in this test
|
|
|
|
amount_closed:
|
|
|
|
1x, 3x = amount
|
|
|
|
-1x, -3x = amount + interest
|
|
|
|
binance -1x,-3x: 30 + 0.000625 = 30.000625 crypto
|
|
|
|
kraken -1x,-3x: 30 + 0.03 = 30.03 crypto
|
|
|
|
close_value:
|
|
|
|
equations:
|
|
|
|
1x, 3x: (amount_closed * close_rate) - (amount_closed * close_rate * fee) - interest
|
|
|
|
-1x,-3x: (amount_closed * close_rate) + (amount_closed * close_rate * fee)
|
|
|
|
2.1 quote
|
|
|
|
bin,krak 1x: (30.00 * 2.1) - (30.00 * 2.1 * 0.0025) = 62.8425
|
|
|
|
bin 3x: (30.00 * 2.1) - (30.00 * 2.1 * 0.0025) - 0.0008333333 = 62.8416666667
|
|
|
|
krak 3x: (30.00 * 2.1) - (30.00 * 2.1 * 0.0025) - 0.040 = 62.8025
|
|
|
|
bin -1x,-3x: (30.000625 * 2.1) + (30.000625 * 2.1 * 0.0025) = 63.15881578125
|
|
|
|
krak -1x,-3x: (30.03 * 2.1) + (30.03 * 2.1 * 0.0025) = 63.2206575
|
|
|
|
1.9 quote
|
|
|
|
bin,krak 1x: (30.00 * 1.9) - (30.00 * 1.9 * 0.0025) = 56.8575
|
|
|
|
bin 3x: (30.00 * 1.9) - (30.00 * 1.9 * 0.0025) - 0.0008333333 = 56.85666667
|
|
|
|
krak 3x: (30.00 * 1.9) - (30.00 * 1.9 * 0.0025) - 0.040 = 56.8175
|
|
|
|
bin -1x,-3x: (30.000625 * 1.9) + (30.000625 * 1.9 * 0.0025) = 57.14369046875
|
|
|
|
krak -1x,-3x: (30.03 * 1.9) + (30.03 * 1.9 * 0.0025) = 57.1996425
|
|
|
|
2.2 quote
|
|
|
|
bin,krak 1x: (30.00 * 2.20) - (30.00 * 2.20 * 0.0025) = 65.835
|
|
|
|
bin 3x: (30.00 * 2.20) - (30.00 * 2.20 * 0.0025) - 0.00083333 = 65.83416667
|
|
|
|
krak 3x: (30.00 * 2.20) - (30.00 * 2.20 * 0.0025) - 0.040 = 65.795
|
|
|
|
bin -1x,-3x: (30.000625 * 2.20) + (30.000625 * 2.20 * 0.0025) = 66.1663784375
|
|
|
|
krak -1x,-3x: (30.03 * 2.20) + (30.03 * 2.20 * 0.0025) = 66.231165
|
|
|
|
total_profit:
|
|
|
|
equations:
|
|
|
|
1x, 3x : close_value - open_value
|
|
|
|
-1x,-3x: open_value - close_value
|
|
|
|
2.1 quote
|
|
|
|
binance,kraken 1x: 62.8425 - 60.15 = 2.6925
|
|
|
|
binance 3x: 62.84166667 - 60.15 = 2.69166667
|
|
|
|
kraken 3x: 62.8025 - 60.15 = 2.6525
|
|
|
|
binance -1x,-3x: 59.850 - 63.15881578125 = -3.308815781249997
|
|
|
|
kraken -1x,-3x: 59.850 - 63.2206575 = -3.3706575
|
|
|
|
1.9 quote
|
|
|
|
binance,kraken 1x: 56.8575 - 60.15 = -3.2925
|
|
|
|
binance 3x: 56.85666667 - 60.15 = -3.29333333
|
|
|
|
kraken 3x: 56.8175 - 60.15 = -3.3325
|
|
|
|
binance -1x,-3x: 59.850 - 57.14369046875 = 2.7063095312499996
|
|
|
|
kraken -1x,-3x: 59.850 - 57.1996425 = 2.6503575
|
|
|
|
2.2 quote
|
|
|
|
binance,kraken 1x: 65.835 - 60.15 = 5.685
|
|
|
|
binance 3x: 65.83416667 - 60.15 = 5.68416667
|
|
|
|
kraken 3x: 65.795 - 60.15 = 5.645
|
|
|
|
binance -1x,-3x: 59.850 - 66.1663784375 = -6.316378437499999
|
|
|
|
kraken -1x,-3x: 59.850 - 66.231165 = -6.381165
|
|
|
|
total_profit_ratio:
|
|
|
|
equations:
|
|
|
|
1x, 3x : ((close_value/open_value) - 1) * leverage
|
|
|
|
-1x,-3x: (1 - (close_value/open_value)) * leverage
|
|
|
|
2.1 quote
|
|
|
|
binance,kraken 1x: (62.8425 / 60.15) - 1 = 0.04476309226932673
|
|
|
|
binance 3x: ((62.84166667 / 60.15) - 1)*3 = 0.13424771421446402
|
|
|
|
kraken 3x: ((62.8025 / 60.15) - 1)*3 = 0.13229426433915248
|
|
|
|
binance -1x: 1 - (63.15881578125 / 59.850) = -0.05528514254385963
|
|
|
|
binance -3x: (1 - (63.15881578125 / 59.850))*3 = -0.1658554276315789
|
|
|
|
kraken -1x: 1 - (63.2206575 / 59.850) = -0.05631842105263152
|
|
|
|
kraken -3x: (1 - (63.2206575 / 59.850))*3 = -0.16895526315789455
|
|
|
|
1.9 quote
|
|
|
|
binance,kraken 1x: (56.8575 / 60.15) - 1 = -0.05473815461346632
|
|
|
|
binance 3x: ((56.85666667 / 60.15) - 1)*3 = -0.16425602643391513
|
|
|
|
kraken 3x: ((56.8175 / 60.15) - 1)*3 = -0.16620947630922667
|
|
|
|
binance -1x: 1 - (57.14369046875 / 59.850) = 0.045218204365079395
|
|
|
|
binance -3x: (1 - (57.14369046875 / 59.850))*3 = 0.13565461309523819
|
|
|
|
kraken -1x: 1 - (57.1996425 / 59.850) = 0.04428333333333334
|
|
|
|
kraken -3x: (1 - (57.1996425 / 59.850))*3 = 0.13285000000000002
|
|
|
|
2.2 quote
|
|
|
|
binance,kraken 1x: (65.835 / 60.15) - 1 = 0.0945137157107232
|
|
|
|
binance 3x: ((65.83416667 / 60.15) - 1)*3 = 0.2834995845386534
|
|
|
|
kraken 3x: ((65.795 / 60.15) - 1)*3 = 0.2815461346633419
|
|
|
|
binance -1x: 1 - (66.1663784375 / 59.850) = -0.1055368159983292
|
|
|
|
binance -3x: (1 - (66.1663784375 / 59.850))*3 = -0.3166104479949876
|
|
|
|
kraken -1x: 1 - (66.231165 / 59.850) = -0.106619298245614
|
|
|
|
kraken -3x: (1 - (66.231165 / 59.850))*3 = -0.319857894736842
|
|
|
|
fee: 0.003, 1x
|
|
|
|
close_value:
|
|
|
|
2.1 quote: (30.00 * 2.1) - (30.00 * 2.1 * 0.003) = 62.811
|
|
|
|
1.9 quote: (30.00 * 1.9) - (30.00 * 1.9 * 0.003) = 56.829
|
|
|
|
2.2 quote: (30.00 * 2.2) - (30.00 * 2.2 * 0.003) = 65.802
|
|
|
|
total_profit
|
|
|
|
fee: 0.003, 1x
|
|
|
|
2.1 quote: 62.811 - 60.15 = 2.6610000000000014
|
|
|
|
1.9 quote: 56.829 - 60.15 = -3.320999999999998
|
|
|
|
2.2 quote: 65.802 - 60.15 = 5.652000000000008
|
|
|
|
total_profit_ratio
|
|
|
|
fee: 0.003, 1x
|
|
|
|
2.1 quote: (62.811 / 60.15) - 1 = 0.04423940149625927
|
|
|
|
1.9 quote: (56.829 / 60.15) - 1 = -0.05521197007481293
|
|
|
|
2.2 quote: (65.802 / 60.15) - 1 = 0.09396508728179565
|
|
|
|
"""
|
2017-12-17 21:07:56 +00:00
|
|
|
trade = Trade(
|
2018-03-24 19:02:13 +00:00
|
|
|
pair='ETH/BTC',
|
2021-07-20 23:56:57 +00:00
|
|
|
stake_amount=60.0,
|
|
|
|
amount=30.0,
|
|
|
|
open_rate=2.0,
|
|
|
|
open_date=datetime.now(tz=timezone.utc) - timedelta(minutes=10),
|
|
|
|
interest_rate=0.0005,
|
|
|
|
interest_mode=InterestMode.HOURSPERDAY,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2021-07-20 23:56:57 +00:00
|
|
|
exchange='binance'
|
2017-12-17 21:07:56 +00:00
|
|
|
)
|
2020-06-07 13:02:56 +00:00
|
|
|
trade.open_order_id = 'something'
|
2021-07-20 23:56:57 +00:00
|
|
|
trade.update(limit_buy_order_usdt) # Buy @ 2.0
|
2017-12-17 21:07:56 +00:00
|
|
|
|
2021-07-20 23:56:57 +00:00
|
|
|
# 1x Leverage, long
|
2017-12-17 21:07:56 +00:00
|
|
|
# Custom closing rate and regular fee rate
|
2021-07-20 23:56:57 +00:00
|
|
|
# Higher than open rate - 2.1 quote
|
|
|
|
assert trade.calc_profit(rate=2.1) == 2.6925
|
|
|
|
# Lower than open rate - 1.9 quote
|
|
|
|
assert trade.calc_profit(rate=1.9) == round(-3.292499999999997, 8)
|
2017-12-17 21:07:56 +00:00
|
|
|
|
2021-07-20 23:56:57 +00:00
|
|
|
# fee 0.003
|
|
|
|
# Higher than open rate - 2.1 quote
|
|
|
|
assert trade.calc_profit(rate=2.1, fee=0.003) == 2.661
|
|
|
|
# Lower than open rate - 1.9 quote
|
|
|
|
assert trade.calc_profit(rate=1.9, fee=0.003) == round(-3.320999999999998, 8)
|
2017-12-17 21:07:56 +00:00
|
|
|
|
2021-07-20 23:56:57 +00:00
|
|
|
# Test when we apply a Sell order. Sell higher than open rate @ 2.2
|
|
|
|
trade.update(limit_sell_order_usdt)
|
|
|
|
assert trade.calc_profit() == round(5.684999999999995, 8)
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
# Test with a custom fee rate on the close trade
|
2021-07-20 23:56:57 +00:00
|
|
|
assert trade.calc_profit(fee=0.003) == round(5.652000000000008, 8)
|
|
|
|
|
|
|
|
trade.open_trade_value = 0.0
|
|
|
|
trade.open_trade_value = trade._calc_open_trade_value()
|
|
|
|
|
|
|
|
# 3x leverage, long ###################################################
|
|
|
|
trade.leverage = 3.0
|
|
|
|
# Higher than open rate - 2.1 quote
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY # binance
|
|
|
|
assert trade.calc_profit(rate=2.1, fee=0.0025) == 2.69166667
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4 # kraken
|
|
|
|
assert trade.calc_profit(rate=2.1, fee=0.0025) == 2.6525
|
|
|
|
|
|
|
|
# 1.9 quote
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY # binance
|
|
|
|
assert trade.calc_profit(rate=1.9, fee=0.0025) == -3.29333333
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4 # kraken
|
|
|
|
assert trade.calc_profit(rate=1.9, fee=0.0025) == -3.3325
|
|
|
|
|
|
|
|
# 2.2 quote
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY # binance
|
|
|
|
assert trade.calc_profit(fee=0.0025) == 5.68416667
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4 # kraken
|
|
|
|
assert trade.calc_profit(fee=0.0025) == 5.645
|
|
|
|
|
|
|
|
# 3x leverage, short ###################################################
|
|
|
|
trade.set_is_short(True)
|
|
|
|
# 2.1 quote - Higher than open rate
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY # binance
|
|
|
|
assert trade.calc_profit(rate=2.1, fee=0.0025) == round(-3.308815781249997, 8)
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4 # kraken
|
|
|
|
assert trade.calc_profit(rate=2.1, fee=0.0025) == -3.3706575
|
|
|
|
|
|
|
|
# 1.9 quote - Lower than open rate
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY # binance
|
|
|
|
assert trade.calc_profit(rate=1.9, fee=0.0025) == round(2.7063095312499996, 8)
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4 # kraken
|
|
|
|
assert trade.calc_profit(rate=1.9, fee=0.0025) == 2.6503575
|
|
|
|
|
|
|
|
# Test when we apply a Sell order. Uses sell order used above
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY # binance
|
|
|
|
assert trade.calc_profit(fee=0.0025) == round(-6.316378437499999, 8)
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4 # kraken
|
|
|
|
assert trade.calc_profit(fee=0.0025) == -6.381165
|
|
|
|
|
|
|
|
# 1x leverage, short ###################################################
|
|
|
|
trade.leverage = 1.0
|
|
|
|
# 2.1 quote - Higher than open rate
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY # binance
|
|
|
|
assert trade.calc_profit(rate=2.1, fee=0.0025) == round(-3.308815781249997, 8)
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4 # kraken
|
|
|
|
assert trade.calc_profit(rate=2.1, fee=0.0025) == -3.3706575
|
|
|
|
|
|
|
|
# 1.9 quote - Lower than open rate
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY # binance
|
|
|
|
assert trade.calc_profit(rate=1.9, fee=0.0025) == round(2.7063095312499996, 8)
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4 # kraken
|
|
|
|
assert trade.calc_profit(rate=1.9, fee=0.0025) == 2.6503575
|
|
|
|
|
|
|
|
# Test when we apply a Sell order. Uses sell order used above
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY # binance
|
|
|
|
assert trade.calc_profit(fee=0.0025) == round(-6.316378437499999, 8)
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4 # kraken
|
|
|
|
assert trade.calc_profit(fee=0.0025) == -6.381165
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
|
2018-04-07 18:06:53 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2021-07-20 23:56:57 +00:00
|
|
|
def test_calc_profit_ratio(limit_buy_order_usdt, limit_sell_order_usdt, fee):
|
2017-12-17 21:07:56 +00:00
|
|
|
trade = Trade(
|
2018-03-24 19:02:13 +00:00
|
|
|
pair='ETH/BTC',
|
2021-07-20 23:56:57 +00:00
|
|
|
stake_amount=60.0,
|
|
|
|
amount=30.0,
|
|
|
|
open_rate=2.0,
|
|
|
|
open_date=datetime.now(tz=timezone.utc) - timedelta(minutes=10),
|
|
|
|
interest_rate=0.0005,
|
|
|
|
interest_mode=InterestMode.HOURSPERDAY,
|
2018-04-21 17:55:48 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2021-07-20 23:56:57 +00:00
|
|
|
exchange='binance'
|
2017-12-17 21:07:56 +00:00
|
|
|
)
|
2020-06-07 13:02:56 +00:00
|
|
|
trade.open_order_id = 'something'
|
2021-07-20 23:56:57 +00:00
|
|
|
trade.update(limit_buy_order_usdt) # Buy @ 2.0
|
2017-12-17 21:07:56 +00:00
|
|
|
|
2021-07-20 23:56:57 +00:00
|
|
|
# 1x Leverage, long
|
|
|
|
# Custom closing rate and regular fee rate
|
|
|
|
# Higher than open rate - 2.1 quote
|
|
|
|
assert trade.calc_profit_ratio(rate=2.1) == round(0.04476309226932673, 8)
|
|
|
|
# Lower than open rate - 1.9 quote
|
|
|
|
assert trade.calc_profit_ratio(rate=1.9) == round(-0.05473815461346632, 8)
|
2017-12-17 21:07:56 +00:00
|
|
|
|
2021-07-20 23:56:57 +00:00
|
|
|
# fee 0.003
|
|
|
|
# Higher than open rate - 2.1 quote
|
|
|
|
assert trade.calc_profit_ratio(rate=2.1, fee=0.003) == round(0.04423940149625927, 8)
|
|
|
|
# Lower than open rate - 1.9 quote
|
|
|
|
assert trade.calc_profit_ratio(rate=1.9, fee=0.003) == round(-0.05521197007481293, 8)
|
2017-12-17 21:07:56 +00:00
|
|
|
|
2021-07-20 23:56:57 +00:00
|
|
|
# Test when we apply a Sell order. Sell higher than open rate @ 2.2
|
|
|
|
trade.update(limit_sell_order_usdt)
|
|
|
|
assert trade.calc_profit_ratio() == round(0.0945137157107232, 8)
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
# Test with a custom fee rate on the close trade
|
2021-07-20 23:56:57 +00:00
|
|
|
assert trade.calc_profit_ratio(fee=0.003) == round(0.09396508728179565, 8)
|
2018-01-23 07:23:29 +00:00
|
|
|
|
2021-04-20 19:41:18 +00:00
|
|
|
trade.open_trade_value = 0.0
|
|
|
|
assert trade.calc_profit_ratio(fee=0.003) == 0.0
|
2021-07-20 23:56:57 +00:00
|
|
|
trade.open_trade_value = trade._calc_open_trade_value()
|
|
|
|
|
|
|
|
# 3x leverage, long ###################################################
|
|
|
|
trade.leverage = 3.0
|
|
|
|
# 2.1 quote - Higher than open rate
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY # binance
|
|
|
|
assert trade.calc_profit_ratio(rate=2.1) == round(0.13424771421446402, 8)
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4 # kraken
|
|
|
|
assert trade.calc_profit_ratio(rate=2.1) == round(0.13229426433915248, 8)
|
|
|
|
|
|
|
|
# 1.9 quote - Lower than open rate
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY # binance
|
|
|
|
assert trade.calc_profit_ratio(rate=1.9) == round(-0.16425602643391513, 8)
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4 # kraken
|
|
|
|
assert trade.calc_profit_ratio(rate=1.9) == round(-0.16620947630922667, 8)
|
|
|
|
|
|
|
|
# Test when we apply a Sell order. Uses sell order used above
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY # binance
|
|
|
|
assert trade.calc_profit_ratio() == round(0.2834995845386534, 8)
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4 # kraken
|
|
|
|
assert trade.calc_profit_ratio() == round(0.2815461346633419, 8)
|
|
|
|
|
|
|
|
# 3x leverage, short ###################################################
|
|
|
|
trade.set_is_short(True)
|
|
|
|
# 2.1 quote - Higher than open rate
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY # binance
|
|
|
|
assert trade.calc_profit_ratio(rate=2.1) == round(-0.1658554276315789, 8)
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4 # kraken
|
|
|
|
assert trade.calc_profit_ratio(rate=2.1) == round(-0.16895526315789455, 8)
|
|
|
|
|
|
|
|
# 1.9 quote - Lower than open rate
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY # binance
|
|
|
|
assert trade.calc_profit_ratio(rate=1.9) == round(0.13565461309523819, 8)
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4 # kraken
|
|
|
|
assert trade.calc_profit_ratio(rate=1.9) == round(0.13285000000000002, 8)
|
|
|
|
|
|
|
|
# Test when we apply a Sell order. Uses sell order used above
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY # binance
|
|
|
|
assert trade.calc_profit_ratio() == round(-0.3166104479949876, 8)
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4 # kraken
|
|
|
|
assert trade.calc_profit_ratio() == round(-0.319857894736842, 8)
|
|
|
|
|
|
|
|
# 1x leverage, short ###################################################
|
|
|
|
trade.leverage = 1.0
|
|
|
|
# 2.1 quote - Higher than open rate
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY # binance
|
|
|
|
assert trade.calc_profit_ratio(rate=2.1) == round(-0.05528514254385963, 8)
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4 # kraken
|
|
|
|
assert trade.calc_profit_ratio(rate=2.1) == round(-0.05631842105263152, 8)
|
|
|
|
|
|
|
|
# 1.9 quote - Lower than open rate
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY # binance
|
|
|
|
assert trade.calc_profit_ratio(rate=1.9) == round(0.045218204365079395, 8)
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4 # kraken
|
|
|
|
assert trade.calc_profit_ratio(rate=1.9) == round(0.04428333333333334, 8)
|
|
|
|
|
|
|
|
# Test when we apply a Sell order. Uses sell order used above
|
|
|
|
trade.interest_mode = InterestMode.HOURSPERDAY # binance
|
|
|
|
assert trade.calc_profit_ratio() == round(-0.1055368159983292, 8)
|
|
|
|
trade.interest_mode = InterestMode.HOURSPER4 # kraken
|
|
|
|
assert trade.calc_profit_ratio() == round(-0.106619298245614, 8)
|
2021-04-20 19:41:18 +00:00
|
|
|
|
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,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2018-01-23 07:23:29 +00:00
|
|
|
open_order_id='dry_run_buy_12345'
|
|
|
|
)
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(trade)
|
2018-01-23 07:23:29 +00:00
|
|
|
|
|
|
|
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,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2018-01-23 07:23:29 +00:00
|
|
|
open_order_id='dry_run_sell_12345'
|
|
|
|
)
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(trade)
|
2018-01-23 07:23:29 +00:00
|
|
|
|
|
|
|
# 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,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2018-01-23 07:23:29 +00:00
|
|
|
open_order_id='prod_buy_12345'
|
|
|
|
)
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(trade)
|
2018-01-23 07:23:29 +00:00
|
|
|
|
|
|
|
# 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))
|
|
|
|
);"""
|
2020-08-13 12:50:57 +00:00
|
|
|
insert_table_old = """INSERT INTO trades (exchange, pair, is_open, open_order_id, fee,
|
2018-05-12 08:29:26 +00:00
|
|
|
open_rate, stake_amount, amount, open_date)
|
2021-04-20 10:54:22 +00:00
|
|
|
VALUES ('binance', 'BTC_ETC', 1, '123123', {fee},
|
2018-05-12 08:37:17 +00:00
|
|
|
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)
|
2021-04-20 10:54:22 +00:00
|
|
|
VALUES ('binance', 'BTC_ETC', 0, {fee},
|
2020-03-22 10:16:09 +00:00
|
|
|
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://')
|
2020-08-13 06:28:46 +00:00
|
|
|
mocker.patch('freqtrade.persistence.models.create_engine', lambda *args, **kwargs: engine)
|
2018-06-07 03:27:55 +00:00
|
|
|
|
2018-05-12 08:31:24 +00:00
|
|
|
# Create table using the old format
|
2021-04-06 12:53:08 +00:00
|
|
|
with engine.begin() as connection:
|
|
|
|
connection.execute(text(create_table_old))
|
|
|
|
connection.execute(text(insert_table_old))
|
|
|
|
connection.execute(text(insert_table_old2))
|
2018-05-12 08:31:24 +00:00
|
|
|
# Run init to test migration
|
2020-10-16 05:39:12 +00:00
|
|
|
init_db(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
|
2020-07-15 18:15:29 +00:00
|
|
|
assert trade.amount_requested == amount
|
2018-05-12 08:37:17 +00:00
|
|
|
assert trade.stake_amount == default_conf.get("stake_amount")
|
2018-05-12 11:39:29 +00:00
|
|
|
assert trade.pair == "ETC/BTC"
|
2021-04-20 10:54:22 +00:00
|
|
|
assert trade.exchange == "binance"
|
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
|
2020-12-10 18:36:52 +00:00
|
|
|
assert trade.open_trade_value == trade._calc_open_trade_value()
|
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
|
|
|
|
2020-08-13 12:50:57 +00:00
|
|
|
# Should've created one order
|
|
|
|
assert len(Order.query.all()) == 1
|
|
|
|
order = Order.query.first()
|
|
|
|
assert order.order_id == '123123'
|
|
|
|
assert order.ft_order_side == 'buy'
|
|
|
|
|
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,
|
2020-08-13 12:50:57 +00:00
|
|
|
stoploss_order_id VARCHAR,
|
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-08-13 12:50:57 +00:00
|
|
|
stop_loss, initial_stop_loss, max_rate, ticker_interval,
|
|
|
|
open_order_id, stoploss_order_id)
|
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-08-13 12:50:57 +00:00
|
|
|
0.0, 0.0, 0.0, '5m',
|
|
|
|
'buy_order', 'stop_order_id222')
|
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://')
|
2020-08-13 06:28:46 +00:00
|
|
|
mocker.patch('freqtrade.persistence.models.create_engine', lambda *args, **kwargs: engine)
|
2018-06-07 03:27:55 +00:00
|
|
|
|
2018-05-12 11:39:29 +00:00
|
|
|
# Create table using the old format
|
2021-04-06 12:53:08 +00:00
|
|
|
with engine.begin() as connection:
|
|
|
|
connection.execute(text(create_table_old))
|
|
|
|
connection.execute(text("create index ix_trades_is_open on trades(is_open)"))
|
|
|
|
connection.execute(text("create index ix_trades_pair on trades(pair)"))
|
|
|
|
connection.execute(text(insert_table_old))
|
2018-06-27 18:15:25 +00:00
|
|
|
|
2021-04-06 12:53:08 +00:00
|
|
|
# fake previous backup
|
|
|
|
connection.execute(text("create table trades_bak as select * from trades"))
|
2018-07-01 17:55:51 +00:00
|
|
|
|
2021-04-06 12:53:08 +00:00
|
|
|
connection.execute(text("create table trades_bak1 as select * from trades"))
|
2018-05-12 11:39:29 +00:00
|
|
|
# Run init to test migration
|
2020-10-16 05:39:12 +00:00
|
|
|
init_db(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
|
2020-07-15 18:15:29 +00:00
|
|
|
assert trade.amount_requested == amount
|
2018-05-12 11:39:29 +00:00
|
|
|
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'
|
2020-08-13 12:50:57 +00:00
|
|
|
assert trade.stoploss_order_id == 'stop_order_id222'
|
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)
|
2020-08-13 12:50:57 +00:00
|
|
|
assert log_has("Running database migration for trades - backup: trades_bak2", caplog)
|
2020-12-10 18:36:52 +00:00
|
|
|
assert trade.open_trade_value == trade._calc_open_trade_value()
|
2020-03-22 10:16:09 +00:00
|
|
|
assert trade.close_profit_abs is None
|
2018-06-27 04:23:49 +00:00
|
|
|
|
2020-08-13 12:50:57 +00:00
|
|
|
assert log_has("Moving open orders to Orders table.", caplog)
|
|
|
|
orders = Order.query.all()
|
|
|
|
assert len(orders) == 2
|
|
|
|
assert orders[0].order_id == 'buy_order'
|
|
|
|
assert orders[0].ft_order_side == 'buy'
|
2021-05-21 18:35:39 +00:00
|
|
|
|
|
|
|
assert orders[1].order_id == 'stop_order_id222'
|
|
|
|
assert orders[1].ft_order_side == 'stoploss'
|
|
|
|
|
|
|
|
caplog.clear()
|
|
|
|
# Drop latest column
|
2021-05-23 06:34:58 +00:00
|
|
|
with engine.begin() as connection:
|
|
|
|
connection.execute(text("alter table orders rename to orders_bak"))
|
2021-05-21 18:53:38 +00:00
|
|
|
inspector = inspect(engine)
|
|
|
|
|
2021-05-23 06:34:58 +00:00
|
|
|
with engine.begin() as connection:
|
|
|
|
for index in inspector.get_indexes('orders_bak'):
|
|
|
|
connection.execute(text(f"drop index {index['name']}"))
|
|
|
|
# Recreate table
|
|
|
|
connection.execute(text("""
|
|
|
|
CREATE TABLE orders (
|
|
|
|
id INTEGER NOT NULL,
|
|
|
|
ft_trade_id INTEGER,
|
|
|
|
ft_order_side VARCHAR NOT NULL,
|
|
|
|
ft_pair VARCHAR NOT NULL,
|
|
|
|
ft_is_open BOOLEAN NOT NULL,
|
|
|
|
order_id VARCHAR NOT NULL,
|
|
|
|
status VARCHAR,
|
|
|
|
symbol VARCHAR,
|
|
|
|
order_type VARCHAR,
|
|
|
|
side VARCHAR,
|
|
|
|
price FLOAT,
|
|
|
|
amount FLOAT,
|
|
|
|
filled FLOAT,
|
|
|
|
remaining FLOAT,
|
|
|
|
cost FLOAT,
|
|
|
|
order_date DATETIME,
|
|
|
|
order_filled_date DATETIME,
|
|
|
|
order_update_date DATETIME,
|
|
|
|
PRIMARY KEY (id),
|
|
|
|
CONSTRAINT _order_pair_order_id UNIQUE (ft_pair, order_id),
|
|
|
|
FOREIGN KEY(ft_trade_id) REFERENCES trades (id)
|
|
|
|
)
|
|
|
|
"""))
|
2021-07-04 06:11:59 +00:00
|
|
|
|
2021-05-23 06:34:58 +00:00
|
|
|
connection.execute(text("""
|
|
|
|
insert into orders ( id, ft_trade_id, ft_order_side, ft_pair, ft_is_open, order_id, status,
|
|
|
|
symbol, order_type, side, price, amount, filled, remaining, cost, order_date,
|
|
|
|
order_filled_date, order_update_date)
|
|
|
|
select id, ft_trade_id, ft_order_side, ft_pair, ft_is_open, order_id, status,
|
|
|
|
symbol, order_type, side, price, amount, filled, remaining, cost, order_date,
|
|
|
|
order_filled_date, order_update_date
|
|
|
|
from orders_bak
|
|
|
|
"""))
|
2021-05-21 18:53:38 +00:00
|
|
|
|
2021-05-21 18:35:39 +00:00
|
|
|
# Run init to test migration
|
|
|
|
init_db(default_conf['db_url'], default_conf['dry_run'])
|
|
|
|
|
2021-05-21 18:53:38 +00:00
|
|
|
assert log_has("trying orders_bak1", caplog)
|
2021-05-21 18:35:39 +00:00
|
|
|
|
|
|
|
orders = Order.query.all()
|
|
|
|
assert len(orders) == 2
|
|
|
|
assert orders[0].order_id == 'buy_order'
|
|
|
|
assert orders[0].ft_order_side == 'buy'
|
2020-08-13 12:50:57 +00:00
|
|
|
|
|
|
|
assert orders[1].order_id == 'stop_order_id222'
|
|
|
|
assert orders[1].ft_order_side == 'stoploss'
|
|
|
|
|
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://')
|
2020-08-13 06:28:46 +00:00
|
|
|
mocker.patch('freqtrade.persistence.models.create_engine', lambda *args, **kwargs: engine)
|
2018-07-23 08:09:56 +00:00
|
|
|
|
|
|
|
# Create table using the old format
|
2021-04-06 12:53:08 +00:00
|
|
|
with engine.begin() as connection:
|
|
|
|
connection.execute(text(create_table_old))
|
|
|
|
connection.execute(text(insert_table_old))
|
2018-07-23 08:09:56 +00:00
|
|
|
|
|
|
|
# Run init to test migration
|
2020-10-16 05:39:12 +00:00
|
|
|
init_db(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
|
2020-12-10 18:36:52 +00:00
|
|
|
assert trade.open_trade_value == trade._calc_open_trade_value()
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has("trying trades_bak0", caplog)
|
2020-08-13 12:50:57 +00:00
|
|
|
assert log_has("Running database migration for trades - backup: 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,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2018-06-27 04:23:49 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2021-07-20 23:56:57 +00:00
|
|
|
def test_adjust_stop_loss_short(fee):
|
|
|
|
trade = Trade(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.001,
|
|
|
|
amount=5,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
exchange='binance',
|
|
|
|
open_rate=1,
|
|
|
|
max_rate=1,
|
|
|
|
is_short=True,
|
|
|
|
interest_mode=InterestMode.HOURSPERDAY
|
|
|
|
)
|
|
|
|
trade.adjust_stop_loss(trade.open_rate, 0.05, True)
|
|
|
|
assert trade.stop_loss == 1.05
|
|
|
|
assert trade.stop_loss_pct == 0.05
|
|
|
|
assert trade.initial_stop_loss == 1.05
|
|
|
|
assert trade.initial_stop_loss_pct == 0.05
|
|
|
|
# Get percent of profit with a lower rate
|
|
|
|
trade.adjust_stop_loss(1.04, 0.05)
|
|
|
|
assert trade.stop_loss == 1.05
|
|
|
|
assert trade.stop_loss_pct == 0.05
|
|
|
|
assert trade.initial_stop_loss == 1.05
|
|
|
|
assert trade.initial_stop_loss_pct == 0.05
|
|
|
|
# Get percent of profit with a custom rate (Higher than open rate)
|
|
|
|
trade.adjust_stop_loss(0.7, 0.1)
|
|
|
|
# If the price goes down to 0.7, with a trailing stop of 0.1,
|
|
|
|
# the new stoploss at 0.1 above 0.7 would be 0.7*0.1 higher
|
|
|
|
assert round(trade.stop_loss, 8) == 0.77
|
|
|
|
assert trade.stop_loss_pct == 0.1
|
|
|
|
assert trade.initial_stop_loss == 1.05
|
|
|
|
assert trade.initial_stop_loss_pct == 0.05
|
|
|
|
# current rate lower again ... should not change
|
|
|
|
trade.adjust_stop_loss(0.8, -0.1)
|
|
|
|
assert round(trade.stop_loss, 8) == 0.77
|
|
|
|
assert trade.initial_stop_loss == 1.05
|
|
|
|
assert trade.initial_stop_loss_pct == 0.05
|
|
|
|
# current rate higher... should raise stoploss
|
|
|
|
trade.adjust_stop_loss(0.6, -0.1)
|
|
|
|
assert round(trade.stop_loss, 8) == 0.66
|
|
|
|
assert trade.initial_stop_loss == 1.05
|
|
|
|
assert trade.initial_stop_loss_pct == 0.05
|
|
|
|
# Initial is true but stop_loss set - so doesn't do anything
|
|
|
|
trade.adjust_stop_loss(0.3, -0.1, True)
|
|
|
|
assert round(trade.stop_loss, 8) == 0.66 # TODO-mg: What is this test?
|
|
|
|
assert trade.initial_stop_loss == 1.05
|
|
|
|
assert trade.initial_stop_loss_pct == 0.05
|
|
|
|
assert trade.stop_loss_pct == 0.1
|
2021-07-25 08:57:17 +00:00
|
|
|
trade.set_isolated_liq(0.63)
|
2021-07-20 23:56:57 +00:00
|
|
|
trade.adjust_stop_loss(0.59, -0.1)
|
|
|
|
assert trade.stop_loss == 0.63
|
2021-07-25 08:57:17 +00:00
|
|
|
assert trade.isolated_liq == 0.63
|
2021-07-20 23:56:57 +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,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2019-03-16 19:04:55 +00:00
|
|
|
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")
|
2021-04-27 18:25:36 +00:00
|
|
|
@pytest.mark.parametrize('use_db', [True, False])
|
|
|
|
def test_get_open(fee, use_db):
|
|
|
|
Trade.use_db = use_db
|
|
|
|
Trade.reset_trades()
|
2019-02-25 19:00:17 +00:00
|
|
|
|
2021-04-27 18:25:36 +00:00
|
|
|
create_mock_trades(fee, use_db)
|
2020-09-10 05:40:19 +00:00
|
|
|
assert len(Trade.get_open_trades()) == 4
|
2019-05-06 04:56:22 +00:00
|
|
|
|
2021-04-27 18:25:36 +00:00
|
|
|
Trade.use_db = True
|
|
|
|
|
2019-05-06 04:56:22 +00:00
|
|
|
|
2021-07-20 23:56:57 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
@pytest.mark.parametrize('use_db', [True, False])
|
|
|
|
def test_get_open_lev(fee, use_db):
|
|
|
|
Trade.use_db = use_db
|
|
|
|
Trade.reset_trades()
|
|
|
|
|
|
|
|
create_mock_trades_with_leverage(fee, use_db)
|
|
|
|
assert len(Trade.get_open_trades()) == 5
|
|
|
|
|
|
|
|
Trade.use_db = True
|
|
|
|
|
|
|
|
|
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,
|
2020-07-15 18:15:29 +00:00
|
|
|
amount_requested=123.0,
|
2019-05-06 04:56:22 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
open_date=arrow.utcnow().shift(hours=-2).datetime,
|
|
|
|
open_rate=0.123,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2019-05-06 04:56:22 +00:00
|
|
|
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': 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': 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,
|
2020-12-10 18:21:20 +00:00
|
|
|
'open_trade_value': 15.1668225,
|
2020-04-06 13:49:24 +00:00
|
|
|
'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,
|
2020-07-15 18:15:29 +00:00
|
|
|
'amount_requested': 123.0,
|
2019-05-06 04:56:22 +00:00
|
|
|
'stake_amount': 0.001,
|
2021-01-23 11:43:27 +00:00
|
|
|
'trade_duration': None,
|
2021-01-24 19:09:18 +00:00
|
|
|
'trade_duration_s': None,
|
2020-04-06 13:49:24 +00:00
|
|
|
'close_profit': None,
|
2020-11-03 06:34:21 +00:00
|
|
|
'close_profit_pct': None,
|
2020-05-30 09:34:39 +00:00
|
|
|
'close_profit_abs': None,
|
2020-11-03 06:34:21 +00:00
|
|
|
'profit_ratio': None,
|
|
|
|
'profit_pct': None,
|
|
|
|
'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,
|
2020-06-01 09:05:37 +00:00
|
|
|
'stop_loss_abs': None,
|
2020-06-01 08:56:05 +00:00
|
|
|
'stop_loss_ratio': None,
|
2019-05-06 04:56:22 +00:00
|
|
|
'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,
|
2020-06-01 09:05:37 +00:00
|
|
|
'initial_stop_loss_abs': None,
|
2020-04-06 13:49:24 +00:00
|
|
|
'initial_stop_loss_pct': None,
|
2020-06-01 08:56:05 +00:00
|
|
|
'initial_stop_loss_ratio': None,
|
2020-04-06 13:49:24 +00:00
|
|
|
'min_rate': None,
|
|
|
|
'max_rate': None,
|
|
|
|
'strategy': None,
|
2020-06-01 18:47:27 +00:00
|
|
|
'timeframe': None,
|
2021-04-20 10:54:22 +00:00
|
|
|
'exchange': 'binance',
|
2021-06-27 02:36:19 +00:00
|
|
|
'leverage': None,
|
|
|
|
'interest_rate': None,
|
2021-07-25 08:57:17 +00:00
|
|
|
'isolated_liq': None,
|
2021-06-27 02:36:19 +00:00
|
|
|
'is_short': None,
|
2020-05-30 09:34:39 +00:00
|
|
|
}
|
2019-05-06 04:56:22 +00:00
|
|
|
|
|
|
|
# Simulate dry_run entries
|
|
|
|
trade = Trade(
|
|
|
|
pair='XRP/BTC',
|
|
|
|
stake_amount=0.001,
|
|
|
|
amount=100.0,
|
2020-07-15 18:15:29 +00:00
|
|
|
amount_requested=101.0,
|
2019-05-06 04:56:22 +00:00
|
|
|
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,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2019-05-06 04:56:22 +00:00
|
|
|
)
|
|
|
|
result = trade.to_json()
|
|
|
|
assert isinstance(result, dict)
|
|
|
|
|
|
|
|
assert result == {'trade_id': None,
|
|
|
|
'pair': 'XRP/BTC',
|
|
|
|
'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': 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,
|
2020-07-15 18:15:29 +00:00
|
|
|
'amount_requested': 101.0,
|
2019-05-06 04:56:22 +00:00
|
|
|
'stake_amount': 0.001,
|
2021-01-23 11:43:27 +00:00
|
|
|
'trade_duration': 60,
|
2021-01-24 19:09:18 +00:00
|
|
|
'trade_duration_s': 3600,
|
2020-06-01 09:05:37 +00:00
|
|
|
'stop_loss_abs': None,
|
2019-05-06 04:56:22 +00:00
|
|
|
'stop_loss_pct': None,
|
2020-06-01 08:56:05 +00:00
|
|
|
'stop_loss_ratio': None,
|
2020-05-30 09:34:39 +00:00
|
|
|
'stoploss_order_id': None,
|
|
|
|
'stoploss_last_update': None,
|
|
|
|
'stoploss_last_update_timestamp': None,
|
2020-06-01 09:05:37 +00:00
|
|
|
'initial_stop_loss_abs': None,
|
2020-04-06 13:49:24 +00:00
|
|
|
'initial_stop_loss_pct': None,
|
2020-06-01 08:56:05 +00:00
|
|
|
'initial_stop_loss_ratio': None,
|
2020-04-06 13:49:24 +00:00
|
|
|
'close_profit': None,
|
2020-11-03 06:34:21 +00:00
|
|
|
'close_profit_pct': None,
|
2020-05-30 09:34:39 +00:00
|
|
|
'close_profit_abs': None,
|
2020-11-03 06:34:21 +00:00
|
|
|
'profit_ratio': None,
|
|
|
|
'profit_pct': None,
|
|
|
|
'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,
|
2020-12-10 18:21:20 +00:00
|
|
|
'open_trade_value': 12.33075,
|
2020-04-06 13:49:24 +00:00
|
|
|
'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-06-01 18:47:27 +00:00
|
|
|
'timeframe': None,
|
2021-04-20 10:54:22 +00:00
|
|
|
'exchange': 'binance',
|
2021-06-27 02:36:19 +00:00
|
|
|
'leverage': None,
|
|
|
|
'interest_rate': None,
|
2021-07-25 08:57:17 +00:00
|
|
|
'isolated_liq': None,
|
2021-06-27 02:36:19 +00:00
|
|
|
'is_short': None,
|
2020-05-30 09:34:39 +00:00
|
|
|
}
|
2019-05-20 05:12:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_stoploss_reinitialization(default_conf, fee):
|
2020-10-16 05:39:12 +00:00
|
|
|
init_db(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,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2019-05-20 05:12:54 +00:00
|
|
|
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
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(trade)
|
2019-05-20 05:12:54 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
2021-07-20 23:56:57 +00:00
|
|
|
def test_stoploss_reinitialization_short(default_conf, fee):
|
|
|
|
init_db(default_conf['db_url'])
|
|
|
|
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='binance',
|
|
|
|
open_rate=1,
|
|
|
|
max_rate=1,
|
|
|
|
is_short=True,
|
|
|
|
leverage=3.0,
|
|
|
|
interest_mode=InterestMode.HOURSPERDAY
|
|
|
|
)
|
|
|
|
trade.adjust_stop_loss(trade.open_rate, -0.05, True)
|
|
|
|
assert trade.stop_loss == 1.05
|
|
|
|
assert trade.stop_loss_pct == 0.05
|
|
|
|
assert trade.initial_stop_loss == 1.05
|
|
|
|
assert trade.initial_stop_loss_pct == 0.05
|
|
|
|
Trade.query.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 == 1.06
|
|
|
|
assert trade_adj.stop_loss_pct == 0.06
|
|
|
|
assert trade_adj.initial_stop_loss == 1.06
|
|
|
|
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 == 1.04
|
|
|
|
assert trade_adj.stop_loss_pct == 0.04
|
|
|
|
assert trade_adj.initial_stop_loss == 1.04
|
|
|
|
assert trade_adj.initial_stop_loss_pct == 0.04
|
|
|
|
# Trailing stoploss
|
|
|
|
trade.adjust_stop_loss(0.98, -0.04)
|
|
|
|
assert trade_adj.stop_loss == 1.0192
|
|
|
|
assert trade_adj.initial_stop_loss == 1.04
|
|
|
|
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 == 1.0192
|
|
|
|
assert trade_adj.stop_loss_pct == 0.04
|
|
|
|
assert trade_adj.initial_stop_loss == 1.04
|
|
|
|
assert trade_adj.initial_stop_loss_pct == 0.04
|
|
|
|
# Stoploss can't go above liquidation price
|
2021-07-25 08:57:17 +00:00
|
|
|
trade_adj.set_isolated_liq(1.0)
|
2021-07-20 23:56:57 +00:00
|
|
|
trade.adjust_stop_loss(0.97, -0.04)
|
|
|
|
assert trade_adj.stop_loss == 1.0
|
|
|
|
assert trade_adj.stop_loss == 1.0
|
|
|
|
|
|
|
|
|
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,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2020-05-01 14:00:42 +00:00
|
|
|
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,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2020-05-01 18:02:38 +00:00
|
|
|
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")
|
2021-02-10 18:33:39 +00:00
|
|
|
@pytest.mark.parametrize('use_db', [True, False])
|
|
|
|
def test_total_open_trades_stakes(fee, use_db):
|
2019-10-29 10:09:41 +00:00
|
|
|
|
2021-02-10 18:33:39 +00:00
|
|
|
Trade.use_db = use_db
|
2021-03-13 14:46:20 +00:00
|
|
|
Trade.reset_trades()
|
2019-10-29 10:09:41 +00:00
|
|
|
res = Trade.total_open_trades_stakes()
|
|
|
|
assert res == 0
|
2021-02-10 18:33:39 +00:00
|
|
|
create_mock_trades(fee, use_db)
|
2019-10-29 10:09:41 +00:00
|
|
|
res = Trade.total_open_trades_stakes()
|
2020-09-10 05:40:19 +00:00
|
|
|
assert res == 0.004
|
2019-10-29 10:09:41 +00:00
|
|
|
|
2021-02-10 18:33:39 +00:00
|
|
|
Trade.use_db = True
|
|
|
|
|
2019-10-29 10:09:41 +00:00
|
|
|
|
2021-07-10 10:18:55 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
@pytest.mark.parametrize('use_db', [True, False])
|
|
|
|
def test_get_total_closed_profit(fee, use_db):
|
|
|
|
|
|
|
|
Trade.use_db = use_db
|
|
|
|
Trade.reset_trades()
|
|
|
|
res = Trade.get_total_closed_profit()
|
|
|
|
assert res == 0
|
|
|
|
create_mock_trades(fee, use_db)
|
|
|
|
res = Trade.get_total_closed_profit()
|
|
|
|
assert res == 0.000739127
|
|
|
|
|
|
|
|
Trade.use_db = True
|
|
|
|
|
|
|
|
|
2021-03-13 14:46:20 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
@pytest.mark.parametrize('use_db', [True, False])
|
|
|
|
def test_get_trades_proxy(fee, use_db):
|
|
|
|
Trade.use_db = use_db
|
|
|
|
Trade.reset_trades()
|
|
|
|
create_mock_trades(fee, use_db)
|
|
|
|
trades = Trade.get_trades_proxy()
|
|
|
|
assert len(trades) == 6
|
|
|
|
|
|
|
|
assert isinstance(trades[0], Trade)
|
|
|
|
|
2021-03-16 18:24:03 +00:00
|
|
|
trades = Trade.get_trades_proxy(is_open=True)
|
|
|
|
assert len(trades) == 4
|
|
|
|
assert trades[0].is_open
|
|
|
|
trades = Trade.get_trades_proxy(is_open=False)
|
|
|
|
|
|
|
|
assert len(trades) == 2
|
|
|
|
assert not trades[0].is_open
|
|
|
|
|
2021-03-13 14:46:20 +00:00
|
|
|
opendate = datetime.now(tz=timezone.utc) - timedelta(minutes=15)
|
|
|
|
|
|
|
|
assert len(Trade.get_trades_proxy(open_date=opendate)) == 3
|
|
|
|
|
|
|
|
Trade.use_db = True
|
|
|
|
|
|
|
|
|
2021-04-27 18:25:36 +00:00
|
|
|
def test_get_trades_backtest():
|
|
|
|
Trade.use_db = False
|
|
|
|
with pytest.raises(NotImplementedError, match=r"`Trade.get_trades\(\)` not .*"):
|
|
|
|
Trade.get_trades([])
|
|
|
|
Trade.use_db = True
|
|
|
|
|
|
|
|
|
2019-10-29 10:09:41 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_get_overall_performance(fee):
|
|
|
|
|
|
|
|
create_mock_trades(fee)
|
|
|
|
res = Trade.get_overall_performance()
|
|
|
|
|
2020-07-23 05:49:44 +00:00
|
|
|
assert len(res) == 2
|
2019-10-29 10:09:41 +00:00
|
|
|
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
|
2020-07-23 05:49:44 +00:00
|
|
|
assert res[0] == 'XRP/BTC'
|
|
|
|
assert res[1] == 0.01
|
2020-09-06 12:17:45 +00:00
|
|
|
|
|
|
|
|
2021-07-20 23:56:57 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_get_best_pair_lev(fee):
|
|
|
|
|
|
|
|
res = Trade.get_best_pair()
|
|
|
|
assert res is None
|
|
|
|
|
|
|
|
create_mock_trades_with_leverage(fee)
|
|
|
|
res = Trade.get_best_pair()
|
|
|
|
assert len(res) == 2
|
|
|
|
assert res[0] == 'DOGE/BTC'
|
|
|
|
assert res[1] == 0.1713156134055116
|
|
|
|
|
|
|
|
|
2020-09-06 12:17:45 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2021-02-08 18:35:22 +00:00
|
|
|
def test_update_order_from_ccxt(caplog):
|
2020-09-06 12:17:45 +00:00
|
|
|
# Most basic order return (only has orderid)
|
|
|
|
o = Order.parse_from_ccxt_object({'id': '1234'}, 'ETH/BTC', 'buy')
|
|
|
|
assert isinstance(o, Order)
|
|
|
|
assert o.ft_pair == 'ETH/BTC'
|
|
|
|
assert o.ft_order_side == 'buy'
|
|
|
|
assert o.order_id == '1234'
|
|
|
|
assert o.ft_is_open
|
|
|
|
ccxt_order = {
|
|
|
|
'id': '1234',
|
|
|
|
'side': 'buy',
|
|
|
|
'symbol': 'ETH/BTC',
|
|
|
|
'type': 'limit',
|
|
|
|
'price': 1234.5,
|
|
|
|
'amount': 20.0,
|
|
|
|
'filled': 9,
|
|
|
|
'remaining': 11,
|
|
|
|
'status': 'open',
|
|
|
|
'timestamp': 1599394315123
|
|
|
|
}
|
|
|
|
o = Order.parse_from_ccxt_object(ccxt_order, 'ETH/BTC', 'buy')
|
|
|
|
assert isinstance(o, Order)
|
|
|
|
assert o.ft_pair == 'ETH/BTC'
|
|
|
|
assert o.ft_order_side == 'buy'
|
|
|
|
assert o.order_id == '1234'
|
|
|
|
assert o.order_type == 'limit'
|
|
|
|
assert o.price == 1234.5
|
|
|
|
assert o.filled == 9
|
|
|
|
assert o.remaining == 11
|
|
|
|
assert o.order_date is not None
|
|
|
|
assert o.ft_is_open
|
|
|
|
assert o.order_filled_date is None
|
|
|
|
|
|
|
|
# Order has been closed
|
|
|
|
ccxt_order.update({'filled': 20.0, 'remaining': 0.0, 'status': 'closed'})
|
|
|
|
o.update_from_ccxt_object(ccxt_order)
|
|
|
|
|
|
|
|
assert o.filled == 20.0
|
|
|
|
assert o.remaining == 0.0
|
|
|
|
assert not o.ft_is_open
|
|
|
|
assert o.order_filled_date is not None
|
2020-09-06 12:33:45 +00:00
|
|
|
|
|
|
|
ccxt_order.update({'id': 'somethingelse'})
|
|
|
|
with pytest.raises(DependencyException, match=r"Order-id's don't match"):
|
|
|
|
o.update_from_ccxt_object(ccxt_order)
|
2020-09-11 05:12:10 +00:00
|
|
|
|
2021-02-08 18:35:22 +00:00
|
|
|
message = "aaaa is not a valid response object."
|
|
|
|
assert not log_has(message, caplog)
|
|
|
|
Order.update_orders([o], 'aaaa')
|
|
|
|
assert log_has(message, caplog)
|
|
|
|
|
|
|
|
# Call regular update - shouldn't fail.
|
|
|
|
Order.update_orders([o], {'id': '1234'})
|
|
|
|
|
2020-09-11 05:12:10 +00:00
|
|
|
|
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_select_order(fee):
|
|
|
|
create_mock_trades(fee)
|
|
|
|
|
|
|
|
trades = Trade.get_trades().all()
|
|
|
|
|
|
|
|
# Open buy order, no sell order
|
|
|
|
order = trades[0].select_order('buy', True)
|
|
|
|
assert order is None
|
|
|
|
order = trades[0].select_order('buy', False)
|
|
|
|
assert order is not None
|
|
|
|
order = trades[0].select_order('sell', None)
|
|
|
|
assert order is None
|
|
|
|
|
|
|
|
# closed buy order, and open sell order
|
|
|
|
order = trades[1].select_order('buy', True)
|
|
|
|
assert order is None
|
|
|
|
order = trades[1].select_order('buy', False)
|
|
|
|
assert order is not None
|
|
|
|
order = trades[1].select_order('buy', None)
|
|
|
|
assert order is not None
|
|
|
|
order = trades[1].select_order('sell', True)
|
|
|
|
assert order is None
|
|
|
|
order = trades[1].select_order('sell', False)
|
|
|
|
assert order is not None
|
|
|
|
|
|
|
|
# Has open buy order
|
|
|
|
order = trades[3].select_order('buy', True)
|
|
|
|
assert order is not None
|
|
|
|
order = trades[3].select_order('buy', False)
|
|
|
|
assert order is None
|
|
|
|
|
|
|
|
# Open sell order
|
|
|
|
order = trades[4].select_order('buy', True)
|
|
|
|
assert order is None
|
|
|
|
order = trades[4].select_order('buy', False)
|
|
|
|
assert order is not None
|
|
|
|
|
|
|
|
order = trades[4].select_order('sell', True)
|
|
|
|
assert order is not None
|
|
|
|
assert order.ft_order_side == 'stoploss'
|
|
|
|
order = trades[4].select_order('sell', False)
|
|
|
|
assert order is None
|
2021-02-20 19:07:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_Trade_object_idem():
|
|
|
|
|
|
|
|
assert issubclass(Trade, LocalTrade)
|
|
|
|
|
|
|
|
trade = vars(Trade)
|
|
|
|
localtrade = vars(LocalTrade)
|
|
|
|
|
2021-04-27 18:25:36 +00:00
|
|
|
excludes = (
|
|
|
|
'delete',
|
|
|
|
'session',
|
2021-04-15 05:57:52 +00:00
|
|
|
'commit',
|
2021-04-27 18:25:36 +00:00
|
|
|
'query',
|
|
|
|
'open_date',
|
|
|
|
'get_best_pair',
|
|
|
|
'get_overall_performance',
|
2021-07-10 10:18:55 +00:00
|
|
|
'get_total_closed_profit',
|
2021-04-27 18:25:36 +00:00
|
|
|
'total_open_trades_stakes',
|
2021-06-27 02:36:19 +00:00
|
|
|
'get_closed_trades_without_assigned_fees',
|
2021-04-27 18:25:36 +00:00
|
|
|
'get_open_trades_without_assigned_fees',
|
|
|
|
'get_open_order_trades',
|
|
|
|
'get_trades',
|
2021-06-22 03:26:31 +00:00
|
|
|
)
|
2021-04-27 18:25:36 +00:00
|
|
|
|
2021-02-20 19:07:00 +00:00
|
|
|
# Parent (LocalTrade) should have the same attributes
|
|
|
|
for item in trade:
|
|
|
|
# Exclude private attributes and open_date (as it's not assigned a default)
|
2021-04-27 18:25:36 +00:00
|
|
|
if (not item.startswith('_') and item not in excludes):
|
2021-02-20 19:07:00 +00:00
|
|
|
assert item in localtrade
|
|
|
|
|
|
|
|
# Fails if only a column is added without corresponding parent field
|
|
|
|
for item in localtrade:
|
|
|
|
if (not item.startswith('__')
|
2021-03-13 09:16:32 +00:00
|
|
|
and item not in ('trades', 'trades_open', 'total_profit')
|
2021-02-20 19:07:00 +00:00
|
|
|
and type(getattr(LocalTrade, item)) not in (property, FunctionType)):
|
|
|
|
assert item in trade
|