stable/freqtrade/tests/test_persistence.py

429 lines
14 KiB
Python
Raw Normal View History

2018-01-28 07:38:41 +00:00
# pragma pylint: disable=missing-docstring, C0103
2018-06-07 03:27:55 +00:00
from copy import deepcopy
2018-03-17 21:44:47 +00:00
2017-11-09 19:02:41 +00:00
import pytest
2018-01-26 05:05:10 +00:00
from sqlalchemy import create_engine
2018-03-17 21:44:47 +00:00
2018-05-12 08:30:30 +00:00
from freqtrade.persistence import Trade, init, clean_dry_run_db
2017-12-27 10:41:11 +00:00
@pytest.fixture(scope='function')
def init_persistence(default_conf):
init(default_conf)
2017-12-27 10:41:11 +00:00
def test_init_create_session(default_conf, mocker):
mocker.patch.dict('freqtrade.persistence._CONF', default_conf)
# Check if init create a session
init(default_conf)
assert hasattr(Trade, 'session')
2018-01-28 07:38:41 +00:00
assert 'Session' in type(Trade.session).__name__
2017-12-27 10:41:11 +00:00
2018-06-07 03:27:55 +00:00
def test_init_custom_db_url(default_conf, mocker):
conf = deepcopy(default_conf)
2017-12-27 10:41:11 +00:00
2018-06-07 03:27:55 +00:00
# Update path to a value other than default, but still in-memory
conf.update({'db_url': 'sqlite:///'})
mocker.patch.dict('freqtrade.persistence._CONF', conf)
2017-12-27 10:41:11 +00:00
# Check if the new tradesv3.dry_run.sqlite was created
2018-06-07 03:27:55 +00:00
init(conf)
2017-12-27 10:41:11 +00:00
def test_init_prod_db(default_conf, mocker):
default_conf.update({'dry_run': False})
mocker.patch.dict('freqtrade.persistence._CONF', default_conf)
init(default_conf)
2017-11-09 19:02:41 +00:00
@pytest.mark.usefixtures("init_persistence")
def test_update_with_bittrex(limit_buy_order, limit_sell_order, fee):
2017-12-17 21:07:56 +00:00
"""
On this test we will buy and sell a crypto currency.
Buy
- Buy: 90.99181073 Crypto at 0.00001099 BTC
(90.99181073*0.00001099 = 0.0009999 BTC)
2017-12-17 21:07:56 +00:00
- Buying fee: 0.25%
- Total cost of buy trade: 0.001002500 BTC
((90.99181073*0.00001099) + ((90.99181073*0.00001099)*0.0025))
2017-12-17 21:07:56 +00:00
Sell
- Sell: 90.99181073 Crypto at 0.00001173 BTC
(90.99181073*0.00001173 = 0,00106733394 BTC)
2017-12-17 21:07:56 +00:00
- Selling fee: 0.25%
- Total cost of sell trade: 0.001064666 BTC
((90.99181073*0.00001173) - ((90.99181073*0.00001173)*0.0025))
2017-12-17 21:07:56 +00:00
Profit/Loss: +0.000062166 BTC
(Sell:0.001064666 - Buy:0.001002500)
Profit/Loss percentage: 0.0620
((0.001064666/0.001002500)-1 = 6.20%)
2017-12-17 21:07:56 +00:00
:param limit_buy_order:
:param limit_sell_order:
:return:
"""
2017-11-09 19:02:41 +00:00
trade = Trade(
pair='ETH/BTC',
2017-12-17 21:07:56 +00:00
stake_amount=0.001,
fee_open=fee.return_value,
fee_close=fee.return_value,
2018-03-25 10:57:59 +00:00
exchange='bittrex',
2017-11-09 19:02:41 +00:00
)
assert trade.open_order_id is None
assert trade.open_rate is None
assert trade.close_profit is None
assert trade.close_date is None
trade.open_order_id = 'something'
trade.update(limit_buy_order)
assert trade.open_order_id is None
2017-12-17 21:07:56 +00:00
assert trade.open_rate == 0.00001099
2017-11-09 19:02:41 +00:00
assert trade.close_profit is None
assert trade.close_date is None
trade.open_order_id = 'something'
trade.update(limit_sell_order)
assert trade.open_order_id is None
2017-12-17 21:07:56 +00:00
assert trade.close_rate == 0.00001173
assert trade.close_profit == 0.06201057
2017-11-09 19:02:41 +00:00
assert trade.close_date is not None
@pytest.mark.usefixtures("init_persistence")
def test_calc_open_close_trade_price(limit_buy_order, limit_sell_order, fee):
2017-12-17 21:07:56 +00:00
trade = Trade(
2018-03-24 19:02:13 +00:00
pair='ETH/BTC',
2017-12-17 21:07:56 +00:00
stake_amount=0.001,
fee_open=fee.return_value,
fee_close=fee.return_value,
2018-03-25 10:57:59 +00:00
exchange='bittrex',
2017-12-17 21:07:56 +00:00
)
trade.open_order_id = 'something'
trade.update(limit_buy_order)
assert trade.calc_open_trade_price() == 0.001002500
trade.update(limit_sell_order)
assert trade.calc_close_trade_price() == 0.0010646656
# Profit in BTC
assert trade.calc_profit() == 0.00006217
# Profit in percent
assert trade.calc_profit_percent() == 0.06201057
@pytest.mark.usefixtures("init_persistence")
def test_calc_close_trade_price_exception(limit_buy_order, fee):
2017-12-27 10:41:11 +00:00
trade = Trade(
2018-03-24 19:02:13 +00:00
pair='ETH/BTC',
2017-12-27 10:41:11 +00:00
stake_amount=0.001,
fee_open=fee.return_value,
fee_close=fee.return_value,
2018-03-25 10:57:59 +00:00
exchange='bittrex',
2017-12-27 10:41:11 +00:00
)
trade.open_order_id = 'something'
trade.update(limit_buy_order)
assert trade.calc_close_trade_price() == 0.0
@pytest.mark.usefixtures("init_persistence")
2017-11-09 19:02:41 +00:00
def test_update_open_order(limit_buy_order):
trade = Trade(
2018-03-24 19:02:13 +00:00
pair='ETH/BTC',
2017-11-09 19:02:41 +00:00
stake_amount=1.00,
fee_open=0.1,
fee_close=0.1,
2018-03-25 10:57:59 +00:00
exchange='bittrex',
2017-11-09 19:02:41 +00:00
)
assert trade.open_order_id is None
assert trade.open_rate is None
assert trade.close_profit is None
assert trade.close_date is None
limit_buy_order['status'] = 'open'
2017-11-09 19:02:41 +00:00
trade.update(limit_buy_order)
assert trade.open_order_id is None
assert trade.open_rate is None
assert trade.close_profit is None
assert trade.close_date is None
@pytest.mark.usefixtures("init_persistence")
2017-11-09 19:02:41 +00:00
def test_update_invalid_order(limit_buy_order):
trade = Trade(
2018-03-24 19:02:13 +00:00
pair='ETH/BTC',
2017-11-09 19:02:41 +00:00
stake_amount=1.00,
fee_open=0.1,
fee_close=0.1,
2018-03-25 10:57:59 +00:00
exchange='bittrex',
2017-11-09 19:02:41 +00:00
)
limit_buy_order['type'] = 'invalid'
with pytest.raises(ValueError, match=r'Unknown order type'):
trade.update(limit_buy_order)
2017-12-17 21:07:56 +00:00
@pytest.mark.usefixtures("init_persistence")
def test_calc_open_trade_price(limit_buy_order, fee):
2017-12-17 21:07:56 +00:00
trade = Trade(
2018-03-24 19:02:13 +00:00
pair='ETH/BTC',
2017-12-17 21:07:56 +00:00
stake_amount=0.001,
fee_open=fee.return_value,
fee_close=fee.return_value,
2018-03-25 10:57:59 +00:00
exchange='bittrex',
2017-12-17 21:07:56 +00:00
)
trade.open_order_id = 'open_trade'
trade.update(limit_buy_order) # Buy @ 0.00001099
# Get the open rate price with the standard fee rate
assert trade.calc_open_trade_price() == 0.001002500
# Get the open rate price with a custom fee rate
assert trade.calc_open_trade_price(fee=0.003) == 0.001003000
@pytest.mark.usefixtures("init_persistence")
def test_calc_close_trade_price(limit_buy_order, limit_sell_order, fee):
2017-12-17 21:07:56 +00:00
trade = Trade(
2018-03-24 19:02:13 +00:00
pair='ETH/BTC',
2017-12-17 21:07:56 +00:00
stake_amount=0.001,
fee_open=fee.return_value,
fee_close=fee.return_value,
2018-03-25 10:57:59 +00:00
exchange='bittrex',
2017-12-17 21:07:56 +00:00
)
trade.open_order_id = 'close_trade'
trade.update(limit_buy_order) # Buy @ 0.00001099
# Get the close rate price with a custom close rate and a regular fee rate
assert trade.calc_close_trade_price(rate=0.00001234) == 0.0011200318
# Get the close rate price with a custom close rate and a custom fee rate
assert trade.calc_close_trade_price(rate=0.00001234, fee=0.003) == 0.0011194704
# Test when we apply a Sell order, and ask price with a custom fee rate
trade.update(limit_sell_order)
assert trade.calc_close_trade_price(fee=0.005) == 0.0010619972
@pytest.mark.usefixtures("init_persistence")
def test_calc_profit(limit_buy_order, limit_sell_order, fee):
2017-12-17 21:07:56 +00:00
trade = Trade(
2018-03-24 19:02:13 +00:00
pair='ETH/BTC',
2017-12-17 21:07:56 +00:00
stake_amount=0.001,
fee_open=fee.return_value,
fee_close=fee.return_value,
2018-03-25 10:57:59 +00:00
exchange='bittrex',
2017-12-17 21:07:56 +00:00
)
trade.open_order_id = 'profit_percent'
trade.update(limit_buy_order) # Buy @ 0.00001099
# Custom closing rate and regular fee rate
# Higher than open rate
assert trade.calc_profit(rate=0.00001234) == 0.00011753
# Lower than open rate
assert trade.calc_profit(rate=0.00000123) == -0.00089086
# Custom closing rate and custom fee rate
# Higher than open rate
assert trade.calc_profit(rate=0.00001234, fee=0.003) == 0.00011697
# Lower than open rate
assert trade.calc_profit(rate=0.00000123, fee=0.003) == -0.00089092
# Test when we apply a Sell order. Sell higher than open rate @ 0.00001173
trade.update(limit_sell_order)
assert trade.calc_profit() == 0.00006217
# Test with a custom fee rate on the close trade
assert trade.calc_profit(fee=0.003) == 0.00006163
@pytest.mark.usefixtures("init_persistence")
def test_calc_profit_percent(limit_buy_order, limit_sell_order, fee):
2017-12-17 21:07:56 +00:00
trade = Trade(
2018-03-24 19:02:13 +00:00
pair='ETH/BTC',
2017-12-17 21:07:56 +00:00
stake_amount=0.001,
fee_open=fee.return_value,
fee_close=fee.return_value,
2018-03-25 10:57:59 +00:00
exchange='bittrex',
2017-12-17 21:07:56 +00:00
)
trade.open_order_id = 'profit_percent'
trade.update(limit_buy_order) # Buy @ 0.00001099
# Get percent of profit with a custom rate (Higher than open rate)
assert trade.calc_profit_percent(rate=0.00001234) == 0.1172387
# Get percent of profit with a custom rate (Lower than open rate)
assert trade.calc_profit_percent(rate=0.00000123) == -0.88863827
# Test when we apply a Sell order. Sell higher than open rate @ 0.00001173
trade.update(limit_sell_order)
assert trade.calc_profit_percent() == 0.06201057
# Test with a custom fee rate on the close trade
assert trade.calc_profit_percent(fee=0.003) == 0.0614782
def test_clean_dry_run_db(default_conf, fee):
2018-06-07 03:27:55 +00:00
init(default_conf)
# Simulate dry_run entries
trade = Trade(
2018-03-24 19:02:13 +00:00
pair='ETH/BTC',
stake_amount=0.001,
amount=123.0,
fee_open=fee.return_value,
fee_close=fee.return_value,
open_rate=0.123,
2018-03-25 10:57:59 +00:00
exchange='bittrex',
open_order_id='dry_run_buy_12345'
)
Trade.session.add(trade)
trade = Trade(
2018-03-24 19:02:13 +00:00
pair='ETC/BTC',
stake_amount=0.001,
amount=123.0,
fee_open=fee.return_value,
fee_close=fee.return_value,
open_rate=0.123,
2018-03-25 10:57:59 +00:00
exchange='bittrex',
open_order_id='dry_run_sell_12345'
)
Trade.session.add(trade)
# Simulate prod entry
trade = Trade(
2018-03-24 19:02:13 +00:00
pair='ETC/BTC',
stake_amount=0.001,
amount=123.0,
fee_open=fee.return_value,
fee_close=fee.return_value,
open_rate=0.123,
2018-03-25 10:57:59 +00:00
exchange='bittrex',
open_order_id='prod_buy_12345'
)
Trade.session.add(trade)
# We have 3 entries: 2 dry_run, 1 prod
assert len(Trade.query.filter(Trade.open_order_id.isnot(None)).all()) == 3
clean_dry_run_db()
# We have now only the prod
assert len(Trade.query.filter(Trade.open_order_id.isnot(None)).all()) == 1
2018-05-12 08:29:26 +00:00
2018-06-07 03:27:55 +00:00
def test_migrate_old(mocker, default_conf, fee):
"""
Test Database migration(starting with old pairformat)
"""
2018-05-12 08:37:17 +00:00
amount = 103.223
2018-05-12 08:29:26 +00:00
create_table_old = """CREATE TABLE IF NOT EXISTS "trades" (
id INTEGER NOT NULL,
exchange VARCHAR NOT NULL,
pair VARCHAR NOT NULL,
is_open BOOLEAN NOT NULL,
fee FLOAT NOT NULL,
open_rate FLOAT,
close_rate FLOAT,
close_profit FLOAT,
stake_amount FLOAT NOT NULL,
amount FLOAT,
open_date DATETIME NOT NULL,
close_date DATETIME,
open_order_id VARCHAR,
PRIMARY KEY (id),
CHECK (is_open IN (0, 1))
);"""
insert_table_old = """INSERT INTO trades (exchange, pair, is_open, fee,
open_rate, stake_amount, amount, open_date)
2018-05-12 08:37:17 +00:00
VALUES ('BITTREX', 'BTC_ETC', 1, {fee},
0.00258580, {stake}, {amount},
'2017-11-28 12:44:24.000000')
""".format(fee=fee.return_value,
stake=default_conf.get("stake_amount"),
amount=amount
)
2018-05-12 08:29:26 +00:00
engine = create_engine('sqlite://')
2018-06-07 03:27:55 +00:00
mocker.patch('freqtrade.persistence.create_engine', lambda *args, **kwargs: engine)
2018-05-12 08:31:24 +00:00
# Create table using the old format
2018-05-12 08:29:26 +00:00
engine.execute(create_table_old)
engine.execute(insert_table_old)
2018-05-12 08:31:24 +00:00
# Run init to test migration
2018-06-07 03:27:55 +00:00
init(default_conf)
2018-05-12 08:31:24 +00:00
2018-05-12 08:29:26 +00:00
assert len(Trade.query.filter(Trade.id == 1).all()) == 1
trade = Trade.query.filter(Trade.id == 1).first()
assert trade.fee_open == fee.return_value
assert trade.fee_close == fee.return_value
assert trade.open_rate_requested is None
assert trade.close_rate_requested is None
2018-05-12 08:37:17 +00:00
assert trade.is_open == 1
assert trade.amount == amount
assert trade.stake_amount == default_conf.get("stake_amount")
assert trade.pair == "ETC/BTC"
assert trade.exchange == "bittrex"
2018-06-07 03:27:55 +00:00
def test_migrate_new(mocker, default_conf, fee):
"""
Test Database migration (starting with new pairformat)
"""
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 FLOAT NOT NULL,
open_rate FLOAT,
close_rate FLOAT,
close_profit FLOAT,
stake_amount FLOAT NOT NULL,
amount FLOAT,
open_date DATETIME NOT NULL,
close_date DATETIME,
open_order_id VARCHAR,
PRIMARY KEY (id),
CHECK (is_open IN (0, 1))
);"""
insert_table_old = """INSERT INTO trades (exchange, pair, is_open, fee,
open_rate, stake_amount, amount, open_date)
VALUES ('binance', 'ETC/BTC', 1, {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://')
2018-06-07 03:27:55 +00:00
mocker.patch('freqtrade.persistence.create_engine', lambda *args, **kwargs: engine)
# Create table using the old format
engine.execute(create_table_old)
engine.execute(insert_table_old)
# Run init to test migration
2018-06-07 03:27:55 +00:00
init(default_conf)
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"