ruff format: More updates to tests

This commit is contained in:
Matthias 2024-05-12 15:38:09 +02:00
parent 23427bec08
commit 099b1fc8c4
5 changed files with 852 additions and 795 deletions

View File

@ -13,17 +13,22 @@ from tests.conftest import EXMS, log_has, log_has_re, patch_exchange
def test_import_kraken_trades_from_csv(testdatadir, tmp_path, caplog, default_conf_usdt, mocker):
with pytest.raises(OperationalException, match="This function is only for the kraken exchange"):
import_kraken_trades_from_csv(default_conf_usdt, 'feather')
import_kraken_trades_from_csv(default_conf_usdt, "feather")
default_conf_usdt['exchange']['name'] = 'kraken'
default_conf_usdt["exchange"]["name"] = "kraken"
patch_exchange(mocker, id='kraken')
mocker.patch(f'{EXMS}.markets', PropertyMock(return_value={
'BCH/EUR': {'symbol': 'BCH/EUR', 'id': 'BCHEUR', 'altname': 'BCHEUR'},
}))
dstfile = tmp_path / 'BCH_EUR-trades.feather'
patch_exchange(mocker, id="kraken")
mocker.patch(
f"{EXMS}.markets",
PropertyMock(
return_value={
"BCH/EUR": {"symbol": "BCH/EUR", "id": "BCHEUR", "altname": "BCHEUR"},
}
),
)
dstfile = tmp_path / "BCH_EUR-trades.feather"
assert not dstfile.is_file()
default_conf_usdt['datadir'] = tmp_path
default_conf_usdt["datadir"] = tmp_path
# There's 2 files in this tree, containing a total of 2 days.
# tests/testdata/kraken/
# └── trades_csv
@ -31,29 +36,31 @@ def test_import_kraken_trades_from_csv(testdatadir, tmp_path, caplog, default_co
# └── incremental_q2
# └── BCHEUR.csv <-- 2023-01-02
copytree(testdatadir / 'kraken/trades_csv', tmp_path / 'trades_csv')
copytree(testdatadir / "kraken/trades_csv", tmp_path / "trades_csv")
import_kraken_trades_from_csv(default_conf_usdt, 'feather')
import_kraken_trades_from_csv(default_conf_usdt, "feather")
assert log_has("Found csv files for BCHEUR.", caplog)
assert log_has("Converting pairs: BCH/EUR.", caplog)
assert log_has_re(r"BCH/EUR: 340 trades.* 2023-01-01.* 2023-01-02.*", caplog)
assert dstfile.is_file()
dh = get_datahandler(tmp_path, 'feather')
trades = dh.trades_load('BCH_EUR', TradingMode.SPOT)
dh = get_datahandler(tmp_path, "feather")
trades = dh.trades_load("BCH_EUR", TradingMode.SPOT)
assert len(trades) == 340
assert trades['date'].min().to_pydatetime() == datetime(2023, 1, 1, 0, 3, 56,
tzinfo=timezone.utc)
assert trades['date'].max().to_pydatetime() == datetime(2023, 1, 2, 23, 17, 3,
tzinfo=timezone.utc)
assert trades["date"].min().to_pydatetime() == datetime(
2023, 1, 1, 0, 3, 56, tzinfo=timezone.utc
)
assert trades["date"].max().to_pydatetime() == datetime(
2023, 1, 2, 23, 17, 3, tzinfo=timezone.utc
)
# ID is not filled
assert len(trades.loc[trades['id'] != '']) == 0
assert len(trades.loc[trades["id"] != ""]) == 0
caplog.clear()
default_conf_usdt['pairs'] = ['XRP/EUR']
default_conf_usdt["pairs"] = ["XRP/EUR"]
# Filtered to non-existing pair
import_kraken_trades_from_csv(default_conf_usdt, 'feather')
import_kraken_trades_from_csv(default_conf_usdt, "feather")
assert log_has("Found csv files for BCHEUR.", caplog)
assert log_has("No data found for pairs XRP/EUR.", caplog)

View File

@ -37,70 +37,82 @@ timeframe_in_minute = 60
# End helper functions
# Open trade should be removed from the end
tc0 = BTContainer(data=[
# D O H L C V B S
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 1]], # enter trade (signal on last candle)
stop_loss=-0.99, roi={"0": float('inf')}, profit_perc=0.00,
trades=[]
tc0 = BTContainer(
data=[
# D O H L C V B S
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 1],
], # enter trade (signal on last candle)
stop_loss=-0.99,
roi={"0": float("inf")},
profit_perc=0.00,
trades=[],
)
# Two complete trades within dataframe(with sell hit for all)
tc1 = BTContainer(data=[
# D O H L C V B S
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 1], # enter trade (signal on last candle)
[2, 5000, 5025, 4975, 4987, 6172, 0, 0], # exit at open
[3, 5000, 5025, 4975, 4987, 6172, 1, 0], # no action
[4, 5000, 5025, 4975, 4987, 6172, 0, 0], # should enter the trade
[5, 5000, 5025, 4975, 4987, 6172, 0, 1], # no action
[6, 5000, 5025, 4975, 4987, 6172, 0, 0], # should sell
],
stop_loss=-0.99, roi={"0": float('inf')}, profit_perc=0.00,
trades=[BTrade(exit_reason=ExitType.EXIT_SIGNAL, open_tick=1, close_tick=2),
BTrade(exit_reason=ExitType.EXIT_SIGNAL, open_tick=4, close_tick=6)]
tc1 = BTContainer(
data=[
# D O H L C V B S
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 1], # enter trade (signal on last candle)
[2, 5000, 5025, 4975, 4987, 6172, 0, 0], # exit at open
[3, 5000, 5025, 4975, 4987, 6172, 1, 0], # no action
[4, 5000, 5025, 4975, 4987, 6172, 0, 0], # should enter the trade
[5, 5000, 5025, 4975, 4987, 6172, 0, 1], # no action
[6, 5000, 5025, 4975, 4987, 6172, 0, 0], # should sell
],
stop_loss=-0.99,
roi={"0": float("inf")},
profit_perc=0.00,
trades=[
BTrade(exit_reason=ExitType.EXIT_SIGNAL, open_tick=1, close_tick=2),
BTrade(exit_reason=ExitType.EXIT_SIGNAL, open_tick=4, close_tick=6),
],
)
# 3) Entered, sl 1%, candle drops 8% => Trade closed, 1% loss
tc2 = BTContainer(data=[
# D O H L C V B S
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4600, 4987, 6172, 0, 0], # enter trade, stoploss hit
[2, 5000, 5025, 4975, 4987, 6172, 0, 0],
],
stop_loss=-0.01, roi={"0": float('inf')}, profit_perc=-0.01,
trades=[BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=1, close_tick=1)]
tc2 = BTContainer(
data=[
# D O H L C V B S
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4600, 4987, 6172, 0, 0], # enter trade, stoploss hit
[2, 5000, 5025, 4975, 4987, 6172, 0, 0],
],
stop_loss=-0.01,
roi={"0": float("inf")},
profit_perc=-0.01,
trades=[BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=1, close_tick=1)],
)
# 4) Entered, sl 3 %, candle drops 4%, recovers to 1 % = > Trade closed, 3 % loss
tc3 = BTContainer(data=[
# D O H L C V B S
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4800, 4987, 6172, 0, 0], # enter trade, stoploss hit
[2, 5000, 5025, 4975, 4987, 6172, 0, 0],
],
stop_loss=-0.03, roi={"0": float('inf')}, profit_perc=-0.03,
trades=[BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=1, close_tick=1)]
tc3 = BTContainer(
data=[
# D O H L C V B S
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4800, 4987, 6172, 0, 0], # enter trade, stoploss hit
[2, 5000, 5025, 4975, 4987, 6172, 0, 0],
],
stop_loss=-0.03,
roi={"0": float("inf")},
profit_perc=-0.03,
trades=[BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=1, close_tick=1)],
)
# 5) Stoploss and sell are hit. should sell on stoploss
tc4 = BTContainer(data=[
# D O H L C V B S
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4800, 4987, 6172, 0, 1], # enter trade, stoploss hit, sell signal
[2, 5000, 5025, 4975, 4987, 6172, 0, 0],
],
stop_loss=-0.03, roi={"0": float('inf')}, profit_perc=-0.03,
trades=[BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=1, close_tick=1)]
tc4 = BTContainer(
data=[
# D O H L C V B S
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4800, 4987, 6172, 0, 1], # enter trade, stoploss hit, sell signal
[2, 5000, 5025, 4975, 4987, 6172, 0, 0],
],
stop_loss=-0.03,
roi={"0": float("inf")},
profit_perc=-0.03,
trades=[BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=1, close_tick=1)],
)
TESTS = [
tc0,
tc1,
tc2,
tc3,
tc4
]
TESTS = [tc0, tc1, tc2, tc3, tc4]
@pytest.mark.parametrize("data", TESTS)
@ -114,7 +126,7 @@ def test_edge_results(edge_conf, mocker, caplog, data) -> None:
caplog.set_level(logging.DEBUG)
edge.fee = 0
trades = edge._find_trades_for_stoploss_range(frame, 'TEST/BTC', [data.stop_loss])
trades = edge._find_trades_for_stoploss_range(frame, "TEST/BTC", [data.stop_loss])
results = edge._fill_calculable_fields(DataFrame(trades)) if trades else DataFrame()
assert len(trades) == len(data.trades)
@ -132,106 +144,117 @@ def test_edge_results(edge_conf, mocker, caplog, data) -> None:
def test_adjust(mocker, edge_conf):
freqtrade = get_patched_freqtradebot(mocker, edge_conf)
edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy)
mocker.patch('freqtrade.edge.Edge._cached_pairs', mocker.PropertyMock(
return_value={
'E/F': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60),
'C/D': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60),
'N/O': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60)
}
))
mocker.patch(
"freqtrade.edge.Edge._cached_pairs",
mocker.PropertyMock(
return_value={
"E/F": PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60),
"C/D": PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60),
"N/O": PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60),
}
),
)
pairs = ['A/B', 'C/D', 'E/F', 'G/H']
assert (edge.adjust(pairs) == ['E/F', 'C/D'])
pairs = ["A/B", "C/D", "E/F", "G/H"]
assert edge.adjust(pairs) == ["E/F", "C/D"]
def test_edge_get_stoploss(mocker, edge_conf):
freqtrade = get_patched_freqtradebot(mocker, edge_conf)
edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy)
mocker.patch('freqtrade.edge.Edge._cached_pairs', mocker.PropertyMock(
return_value={
'E/F': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60),
'C/D': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60),
'N/O': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60)
}
))
mocker.patch(
"freqtrade.edge.Edge._cached_pairs",
mocker.PropertyMock(
return_value={
"E/F": PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60),
"C/D": PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60),
"N/O": PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60),
}
),
)
assert edge.get_stoploss('E/F') == -0.01
assert edge.get_stoploss("E/F") == -0.01
def test_nonexisting_get_stoploss(mocker, edge_conf):
freqtrade = get_patched_freqtradebot(mocker, edge_conf)
edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy)
mocker.patch('freqtrade.edge.Edge._cached_pairs', mocker.PropertyMock(
return_value={
'E/F': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60),
}
))
mocker.patch(
"freqtrade.edge.Edge._cached_pairs",
mocker.PropertyMock(
return_value={
"E/F": PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60),
}
),
)
assert edge.get_stoploss('N/O') == -0.1
assert edge.get_stoploss("N/O") == -0.1
def test_edge_stake_amount(mocker, edge_conf):
freqtrade = get_patched_freqtradebot(mocker, edge_conf)
edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy)
mocker.patch('freqtrade.edge.Edge._cached_pairs', mocker.PropertyMock(
return_value={
'E/F': PairInfo(-0.02, 0.66, 3.71, 0.50, 1.71, 10, 60),
}
))
mocker.patch(
"freqtrade.edge.Edge._cached_pairs",
mocker.PropertyMock(
return_value={
"E/F": PairInfo(-0.02, 0.66, 3.71, 0.50, 1.71, 10, 60),
}
),
)
assert edge._capital_ratio == 0.5
assert edge.stake_amount('E/F', free_capital=100, total_capital=100,
capital_in_trade=25) == 31.25
assert (
edge.stake_amount("E/F", free_capital=100, total_capital=100, capital_in_trade=25) == 31.25
)
assert edge.stake_amount('E/F', free_capital=20, total_capital=100,
capital_in_trade=25) == 20
assert edge.stake_amount("E/F", free_capital=20, total_capital=100, capital_in_trade=25) == 20
assert edge.stake_amount('E/F', free_capital=0, total_capital=100,
capital_in_trade=25) == 0
assert edge.stake_amount("E/F", free_capital=0, total_capital=100, capital_in_trade=25) == 0
# Test with increased allowed_risk
# Result should be no more than allowed capital
edge._allowed_risk = 0.4
edge._capital_ratio = 0.5
assert edge.stake_amount('E/F', free_capital=100, total_capital=100,
capital_in_trade=25) == 62.5
assert (
edge.stake_amount("E/F", free_capital=100, total_capital=100, capital_in_trade=25) == 62.5
)
assert edge.stake_amount('E/F', free_capital=100, total_capital=100,
capital_in_trade=0) == 50
assert edge.stake_amount("E/F", free_capital=100, total_capital=100, capital_in_trade=0) == 50
edge._capital_ratio = 1
# Full capital is available
assert edge.stake_amount('E/F', free_capital=100, total_capital=100,
capital_in_trade=0) == 100
assert edge.stake_amount("E/F", free_capital=100, total_capital=100, capital_in_trade=0) == 100
# Full capital is available
assert edge.stake_amount('E/F', free_capital=0, total_capital=100,
capital_in_trade=0) == 0
assert edge.stake_amount("E/F", free_capital=0, total_capital=100, capital_in_trade=0) == 0
def test_nonexisting_stake_amount(mocker, edge_conf):
freqtrade = get_patched_freqtradebot(mocker, edge_conf)
edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy)
mocker.patch('freqtrade.edge.Edge._cached_pairs', mocker.PropertyMock(
return_value={
'E/F': PairInfo(-0.11, 0.66, 3.71, 0.50, 1.71, 10, 60),
}
))
mocker.patch(
"freqtrade.edge.Edge._cached_pairs",
mocker.PropertyMock(
return_value={
"E/F": PairInfo(-0.11, 0.66, 3.71, 0.50, 1.71, 10, 60),
}
),
)
# should use strategy stoploss
assert edge.stake_amount('N/O', 1, 2, 1) == 0.15
assert edge.stake_amount("N/O", 1, 2, 1) == 0.15
def test_edge_heartbeat_calculate(mocker, edge_conf):
freqtrade = get_patched_freqtradebot(mocker, edge_conf)
edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy)
heartbeat = edge_conf['edge']['process_throttle_secs']
heartbeat = edge_conf["edge"]["process_throttle_secs"]
# should not recalculate if heartbeat not reached
edge._last_updated = dt_ts() - heartbeat + 1
assert edge.calculate(edge_conf['exchange']['pair_whitelist']) is False
assert edge.calculate(edge_conf["exchange"]["pair_whitelist"]) is False
def mocked_load_data(datadir, pairs=None, timeframe='0m',
timerange=None, *args, **kwargs):
def mocked_load_data(datadir, pairs=None, timeframe="0m", timerange=None, *args, **kwargs):
if pairs is None:
pairs = []
hz = 0.1
@ -244,8 +267,10 @@ def mocked_load_data(datadir, pairs=None, timeframe='0m',
math.sin(x * hz) / 1000 + base + 0.0001,
math.sin(x * hz) / 1000 + base - 0.0001,
math.sin(x * hz) / 1000 + base,
123.45
] for x in range(0, 500)]
123.45,
]
for x in range(0, 500)
]
hz = 0.2
base = 0.002
@ -256,36 +281,38 @@ def mocked_load_data(datadir, pairs=None, timeframe='0m',
math.sin(x * hz) / 1000 + base + 0.0001,
math.sin(x * hz) / 1000 + base - 0.0001,
math.sin(x * hz) / 1000 + base,
123.45
] for x in range(0, 500)]
123.45,
]
for x in range(0, 500)
]
pairdata = {'NEO/BTC': ohlcv_to_dataframe(NEOBTC, '1h', pair="NEO/BTC",
fill_missing=True),
'LTC/BTC': ohlcv_to_dataframe(LTCBTC, '1h', pair="LTC/BTC",
fill_missing=True)}
pairdata = {
"NEO/BTC": ohlcv_to_dataframe(NEOBTC, "1h", pair="NEO/BTC", fill_missing=True),
"LTC/BTC": ohlcv_to_dataframe(LTCBTC, "1h", pair="LTC/BTC", fill_missing=True),
}
return pairdata
def test_edge_process_downloaded_data(mocker, edge_conf):
freqtrade = get_patched_freqtradebot(mocker, edge_conf)
mocker.patch(f'{EXMS}.get_fee', MagicMock(return_value=0.001))
mocker.patch('freqtrade.edge.edge_positioning.refresh_data', MagicMock())
mocker.patch('freqtrade.edge.edge_positioning.load_data', mocked_load_data)
mocker.patch(f"{EXMS}.get_fee", MagicMock(return_value=0.001))
mocker.patch("freqtrade.edge.edge_positioning.refresh_data", MagicMock())
mocker.patch("freqtrade.edge.edge_positioning.load_data", mocked_load_data)
edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy)
assert edge.calculate(edge_conf['exchange']['pair_whitelist'])
assert edge.calculate(edge_conf["exchange"]["pair_whitelist"])
assert len(edge._cached_pairs) == 2
assert edge._last_updated <= dt_ts() + 2
def test_edge_process_no_data(mocker, edge_conf, caplog):
freqtrade = get_patched_freqtradebot(mocker, edge_conf)
mocker.patch(f'{EXMS}.get_fee', MagicMock(return_value=0.001))
mocker.patch('freqtrade.edge.edge_positioning.refresh_data', MagicMock())
mocker.patch('freqtrade.edge.edge_positioning.load_data', MagicMock(return_value={}))
mocker.patch(f"{EXMS}.get_fee", MagicMock(return_value=0.001))
mocker.patch("freqtrade.edge.edge_positioning.refresh_data", MagicMock())
mocker.patch("freqtrade.edge.edge_positioning.load_data", MagicMock(return_value={}))
edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy)
assert not edge.calculate(edge_conf['exchange']['pair_whitelist'])
assert not edge.calculate(edge_conf["exchange"]["pair_whitelist"])
assert len(edge._cached_pairs) == 0
assert log_has("No data found. Edge is stopped ...", caplog)
assert edge._last_updated == 0
@ -293,50 +320,55 @@ def test_edge_process_no_data(mocker, edge_conf, caplog):
def test_edge_process_no_trades(mocker, edge_conf, caplog):
freqtrade = get_patched_freqtradebot(mocker, edge_conf)
mocker.patch(f'{EXMS}.get_fee', return_value=0.001)
mocker.patch('freqtrade.edge.edge_positioning.refresh_data', )
mocker.patch('freqtrade.edge.edge_positioning.load_data', mocked_load_data)
mocker.patch(f"{EXMS}.get_fee", return_value=0.001)
mocker.patch(
"freqtrade.edge.edge_positioning.refresh_data",
)
mocker.patch("freqtrade.edge.edge_positioning.load_data", mocked_load_data)
# Return empty
mocker.patch('freqtrade.edge.Edge._find_trades_for_stoploss_range', return_value=[])
mocker.patch("freqtrade.edge.Edge._find_trades_for_stoploss_range", return_value=[])
edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy)
assert not edge.calculate(edge_conf['exchange']['pair_whitelist'])
assert not edge.calculate(edge_conf["exchange"]["pair_whitelist"])
assert len(edge._cached_pairs) == 0
assert log_has("No trades found.", caplog)
def test_edge_process_no_pairs(mocker, edge_conf, caplog):
edge_conf['exchange']['pair_whitelist'] = []
mocker.patch('freqtrade.freqtradebot.validate_config_consistency')
edge_conf["exchange"]["pair_whitelist"] = []
mocker.patch("freqtrade.freqtradebot.validate_config_consistency")
freqtrade = get_patched_freqtradebot(mocker, edge_conf)
fee_mock = mocker.patch(f'{EXMS}.get_fee', return_value=0.001)
mocker.patch('freqtrade.edge.edge_positioning.refresh_data')
mocker.patch('freqtrade.edge.edge_positioning.load_data', mocked_load_data)
fee_mock = mocker.patch(f"{EXMS}.get_fee", return_value=0.001)
mocker.patch("freqtrade.edge.edge_positioning.refresh_data")
mocker.patch("freqtrade.edge.edge_positioning.load_data", mocked_load_data)
# Return empty
mocker.patch('freqtrade.edge.Edge._find_trades_for_stoploss_range', return_value=[])
mocker.patch("freqtrade.edge.Edge._find_trades_for_stoploss_range", return_value=[])
edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy)
assert fee_mock.call_count == 0
assert edge.fee is None
assert not edge.calculate(['XRP/USDT'])
assert not edge.calculate(["XRP/USDT"])
assert fee_mock.call_count == 1
assert edge.fee == 0.001
def test_edge_init_error(mocker, edge_conf,):
edge_conf['stake_amount'] = 0.5
mocker.patch(f'{EXMS}.get_fee', MagicMock(return_value=0.001))
with pytest.raises(OperationalException, match='Edge works only with unlimited stake amount'):
def test_edge_init_error(mocker, edge_conf):
edge_conf["stake_amount"] = 0.5
mocker.patch(f"{EXMS}.get_fee", MagicMock(return_value=0.001))
with pytest.raises(OperationalException, match="Edge works only with unlimited stake amount"):
get_patched_freqtradebot(mocker, edge_conf)
@pytest.mark.parametrize("fee,risk_reward_ratio,expectancy", [
(0.0005, 306.5384615384, 101.5128205128),
(0.001, 152.6923076923, 50.2307692308),
])
@pytest.mark.parametrize(
"fee,risk_reward_ratio,expectancy",
[
(0.0005, 306.5384615384, 101.5128205128),
(0.001, 152.6923076923, 50.2307692308),
],
)
def test_process_expectancy(mocker, edge_conf, fee, risk_reward_ratio, expectancy):
edge_conf['edge']['min_trade_number'] = 2
edge_conf["edge"]["min_trade_number"] = 2
freqtrade = get_patched_freqtradebot(mocker, edge_conf)
def get_fee(*args, **kwargs):
@ -346,38 +378,42 @@ def test_process_expectancy(mocker, edge_conf, fee, risk_reward_ratio, expectanc
edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy)
trades = [
{'pair': 'TEST/BTC',
'stoploss': -0.9,
'profit_percent': '',
'profit_abs': '',
'open_date': np.datetime64('2018-10-03T00:05:00.000000000'),
'close_date': np.datetime64('2018-10-03T00:10:00.000000000'),
'trade_duration': '',
'open_rate': 17,
'close_rate': 17,
'exit_type': 'exit_signal'},
{'pair': 'TEST/BTC',
'stoploss': -0.9,
'profit_percent': '',
'profit_abs': '',
'open_date': np.datetime64('2018-10-03T00:20:00.000000000'),
'close_date': np.datetime64('2018-10-03T00:25:00.000000000'),
'trade_duration': '',
'open_rate': 20,
'close_rate': 20,
'exit_type': 'exit_signal'},
{'pair': 'TEST/BTC',
'stoploss': -0.9,
'profit_percent': '',
'profit_abs': '',
'open_date': np.datetime64('2018-10-03T00:30:00.000000000'),
'close_date': np.datetime64('2018-10-03T00:40:00.000000000'),
'trade_duration': '',
'open_rate': 26,
'close_rate': 34,
'exit_type': 'exit_signal'}
{
"pair": "TEST/BTC",
"stoploss": -0.9,
"profit_percent": "",
"profit_abs": "",
"open_date": np.datetime64("2018-10-03T00:05:00.000000000"),
"close_date": np.datetime64("2018-10-03T00:10:00.000000000"),
"trade_duration": "",
"open_rate": 17,
"close_rate": 17,
"exit_type": "exit_signal",
},
{
"pair": "TEST/BTC",
"stoploss": -0.9,
"profit_percent": "",
"profit_abs": "",
"open_date": np.datetime64("2018-10-03T00:20:00.000000000"),
"close_date": np.datetime64("2018-10-03T00:25:00.000000000"),
"trade_duration": "",
"open_rate": 20,
"close_rate": 20,
"exit_type": "exit_signal",
},
{
"pair": "TEST/BTC",
"stoploss": -0.9,
"profit_percent": "",
"profit_abs": "",
"open_date": np.datetime64("2018-10-03T00:30:00.000000000"),
"close_date": np.datetime64("2018-10-03T00:40:00.000000000"),
"trade_duration": "",
"open_rate": 26,
"close_rate": 34,
"exit_type": "exit_signal",
},
]
trades_df = DataFrame(trades)
@ -385,12 +421,12 @@ def test_process_expectancy(mocker, edge_conf, fee, risk_reward_ratio, expectanc
final = edge._process_expectancy(trades_df)
assert len(final) == 1
assert 'TEST/BTC' in final
assert final['TEST/BTC'].stoploss == -0.9
assert round(final['TEST/BTC'].winrate, 10) == 0.3333333333
assert round(final['TEST/BTC'].risk_reward_ratio, 10) == risk_reward_ratio
assert round(final['TEST/BTC'].required_risk_reward, 10) == 2.0
assert round(final['TEST/BTC'].expectancy, 10) == expectancy
assert "TEST/BTC" in final
assert final["TEST/BTC"].stoploss == -0.9
assert round(final["TEST/BTC"].winrate, 10) == 0.3333333333
assert round(final["TEST/BTC"].risk_reward_ratio, 10) == risk_reward_ratio
assert round(final["TEST/BTC"].required_risk_reward, 10) == 2.0
assert round(final["TEST/BTC"].expectancy, 10) == expectancy
# Pop last item so no trade is profitable
trades.pop()
@ -401,154 +437,170 @@ def test_process_expectancy(mocker, edge_conf, fee, risk_reward_ratio, expectanc
assert isinstance(final, dict)
def test_process_expectancy_remove_pumps(mocker, edge_conf, fee,):
edge_conf['edge']['min_trade_number'] = 2
edge_conf['edge']['remove_pumps'] = True
def test_process_expectancy_remove_pumps(mocker, edge_conf, fee):
edge_conf["edge"]["min_trade_number"] = 2
edge_conf["edge"]["remove_pumps"] = True
freqtrade = get_patched_freqtradebot(mocker, edge_conf)
freqtrade.exchange.get_fee = fee
edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy)
trades = [
{'pair': 'TEST/BTC',
'stoploss': -0.9,
'profit_percent': '',
'profit_abs': '',
'open_date': np.datetime64('2018-10-03T00:05:00.000000000'),
'close_date': np.datetime64('2018-10-03T00:10:00.000000000'),
'open_index': 1,
'close_index': 1,
'trade_duration': '',
'open_rate': 17,
'close_rate': 15,
'exit_type': 'sell_signal'},
{'pair': 'TEST/BTC',
'stoploss': -0.9,
'profit_percent': '',
'profit_abs': '',
'open_date': np.datetime64('2018-10-03T00:20:00.000000000'),
'close_date': np.datetime64('2018-10-03T00:25:00.000000000'),
'open_index': 4,
'close_index': 4,
'trade_duration': '',
'open_rate': 20,
'close_rate': 10,
'exit_type': 'sell_signal'},
{'pair': 'TEST/BTC',
'stoploss': -0.9,
'profit_percent': '',
'profit_abs': '',
'open_date': np.datetime64('2018-10-03T00:20:00.000000000'),
'close_date': np.datetime64('2018-10-03T00:25:00.000000000'),
'open_index': 4,
'close_index': 4,
'trade_duration': '',
'open_rate': 20,
'close_rate': 10,
'exit_type': 'sell_signal'},
{'pair': 'TEST/BTC',
'stoploss': -0.9,
'profit_percent': '',
'profit_abs': '',
'open_date': np.datetime64('2018-10-03T00:20:00.000000000'),
'close_date': np.datetime64('2018-10-03T00:25:00.000000000'),
'open_index': 4,
'close_index': 4,
'trade_duration': '',
'open_rate': 20,
'close_rate': 10,
'exit_type': 'sell_signal'},
{'pair': 'TEST/BTC',
'stoploss': -0.9,
'profit_percent': '',
'profit_abs': '',
'open_date': np.datetime64('2018-10-03T00:20:00.000000000'),
'close_date': np.datetime64('2018-10-03T00:25:00.000000000'),
'open_index': 4,
'close_index': 4,
'trade_duration': '',
'open_rate': 20,
'close_rate': 10,
'exit_type': 'sell_signal'},
{'pair': 'TEST/BTC',
'stoploss': -0.9,
'profit_percent': '',
'profit_abs': '',
'open_date': np.datetime64('2018-10-03T00:30:00.000000000'),
'close_date': np.datetime64('2018-10-03T00:40:00.000000000'),
'open_index': 6,
'close_index': 7,
'trade_duration': '',
'open_rate': 26,
'close_rate': 134,
'exit_type': 'sell_signal'}
{
"pair": "TEST/BTC",
"stoploss": -0.9,
"profit_percent": "",
"profit_abs": "",
"open_date": np.datetime64("2018-10-03T00:05:00.000000000"),
"close_date": np.datetime64("2018-10-03T00:10:00.000000000"),
"open_index": 1,
"close_index": 1,
"trade_duration": "",
"open_rate": 17,
"close_rate": 15,
"exit_type": "sell_signal",
},
{
"pair": "TEST/BTC",
"stoploss": -0.9,
"profit_percent": "",
"profit_abs": "",
"open_date": np.datetime64("2018-10-03T00:20:00.000000000"),
"close_date": np.datetime64("2018-10-03T00:25:00.000000000"),
"open_index": 4,
"close_index": 4,
"trade_duration": "",
"open_rate": 20,
"close_rate": 10,
"exit_type": "sell_signal",
},
{
"pair": "TEST/BTC",
"stoploss": -0.9,
"profit_percent": "",
"profit_abs": "",
"open_date": np.datetime64("2018-10-03T00:20:00.000000000"),
"close_date": np.datetime64("2018-10-03T00:25:00.000000000"),
"open_index": 4,
"close_index": 4,
"trade_duration": "",
"open_rate": 20,
"close_rate": 10,
"exit_type": "sell_signal",
},
{
"pair": "TEST/BTC",
"stoploss": -0.9,
"profit_percent": "",
"profit_abs": "",
"open_date": np.datetime64("2018-10-03T00:20:00.000000000"),
"close_date": np.datetime64("2018-10-03T00:25:00.000000000"),
"open_index": 4,
"close_index": 4,
"trade_duration": "",
"open_rate": 20,
"close_rate": 10,
"exit_type": "sell_signal",
},
{
"pair": "TEST/BTC",
"stoploss": -0.9,
"profit_percent": "",
"profit_abs": "",
"open_date": np.datetime64("2018-10-03T00:20:00.000000000"),
"close_date": np.datetime64("2018-10-03T00:25:00.000000000"),
"open_index": 4,
"close_index": 4,
"trade_duration": "",
"open_rate": 20,
"close_rate": 10,
"exit_type": "sell_signal",
},
{
"pair": "TEST/BTC",
"stoploss": -0.9,
"profit_percent": "",
"profit_abs": "",
"open_date": np.datetime64("2018-10-03T00:30:00.000000000"),
"close_date": np.datetime64("2018-10-03T00:40:00.000000000"),
"open_index": 6,
"close_index": 7,
"trade_duration": "",
"open_rate": 26,
"close_rate": 134,
"exit_type": "sell_signal",
},
]
trades_df = DataFrame(trades)
trades_df = edge._fill_calculable_fields(trades_df)
final = edge._process_expectancy(trades_df)
assert 'TEST/BTC' in final
assert final['TEST/BTC'].stoploss == -0.9
assert final['TEST/BTC'].nb_trades == len(trades_df) - 1
assert round(final['TEST/BTC'].winrate, 10) == 0.0
assert "TEST/BTC" in final
assert final["TEST/BTC"].stoploss == -0.9
assert final["TEST/BTC"].nb_trades == len(trades_df) - 1
assert round(final["TEST/BTC"].winrate, 10) == 0.0
def test_process_expectancy_only_wins(mocker, edge_conf, fee,):
edge_conf['edge']['min_trade_number'] = 2
def test_process_expectancy_only_wins(mocker, edge_conf, fee):
edge_conf["edge"]["min_trade_number"] = 2
freqtrade = get_patched_freqtradebot(mocker, edge_conf)
freqtrade.exchange.get_fee = fee
edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy)
trades = [
{'pair': 'TEST/BTC',
'stoploss': -0.9,
'profit_percent': '',
'profit_abs': '',
'open_date': np.datetime64('2018-10-03T00:05:00.000000000'),
'close_date': np.datetime64('2018-10-03T00:10:00.000000000'),
'open_index': 1,
'close_index': 1,
'trade_duration': '',
'open_rate': 15,
'close_rate': 17,
'exit_type': 'sell_signal'},
{'pair': 'TEST/BTC',
'stoploss': -0.9,
'profit_percent': '',
'profit_abs': '',
'open_date': np.datetime64('2018-10-03T00:20:00.000000000'),
'close_date': np.datetime64('2018-10-03T00:25:00.000000000'),
'open_index': 4,
'close_index': 4,
'trade_duration': '',
'open_rate': 10,
'close_rate': 20,
'exit_type': 'sell_signal'},
{'pair': 'TEST/BTC',
'stoploss': -0.9,
'profit_percent': '',
'profit_abs': '',
'open_date': np.datetime64('2018-10-03T00:30:00.000000000'),
'close_date': np.datetime64('2018-10-03T00:40:00.000000000'),
'open_index': 6,
'close_index': 7,
'trade_duration': '',
'open_rate': 26,
'close_rate': 134,
'exit_type': 'sell_signal'}
{
"pair": "TEST/BTC",
"stoploss": -0.9,
"profit_percent": "",
"profit_abs": "",
"open_date": np.datetime64("2018-10-03T00:05:00.000000000"),
"close_date": np.datetime64("2018-10-03T00:10:00.000000000"),
"open_index": 1,
"close_index": 1,
"trade_duration": "",
"open_rate": 15,
"close_rate": 17,
"exit_type": "sell_signal",
},
{
"pair": "TEST/BTC",
"stoploss": -0.9,
"profit_percent": "",
"profit_abs": "",
"open_date": np.datetime64("2018-10-03T00:20:00.000000000"),
"close_date": np.datetime64("2018-10-03T00:25:00.000000000"),
"open_index": 4,
"close_index": 4,
"trade_duration": "",
"open_rate": 10,
"close_rate": 20,
"exit_type": "sell_signal",
},
{
"pair": "TEST/BTC",
"stoploss": -0.9,
"profit_percent": "",
"profit_abs": "",
"open_date": np.datetime64("2018-10-03T00:30:00.000000000"),
"close_date": np.datetime64("2018-10-03T00:40:00.000000000"),
"open_index": 6,
"close_index": 7,
"trade_duration": "",
"open_rate": 26,
"close_rate": 134,
"exit_type": "sell_signal",
},
]
trades_df = DataFrame(trades)
trades_df = edge._fill_calculable_fields(trades_df)
final = edge._process_expectancy(trades_df)
assert 'TEST/BTC' in final
assert final['TEST/BTC'].stoploss == -0.9
assert final['TEST/BTC'].nb_trades == len(trades_df)
assert round(final['TEST/BTC'].winrate, 10) == 1.0
assert round(final['TEST/BTC'].risk_reward_ratio, 10) == float('inf')
assert round(final['TEST/BTC'].expectancy, 10) == float('inf')
assert "TEST/BTC" in final
assert final["TEST/BTC"].stoploss == -0.9
assert final["TEST/BTC"].nb_trades == len(trades_df)
assert round(final["TEST/BTC"].winrate, 10) == 1.0
assert round(final["TEST/BTC"].risk_reward_ratio, 10) == float("inf")
assert round(final["TEST/BTC"].expectancy, 10) == float("inf")

View File

@ -14,128 +14,130 @@ EXCHANGE_FIXTURE_TYPE = Tuple[Exchange, str]
# Exchanges that should be tested online
EXCHANGES = {
'binance': {
'pair': 'BTC/USDT',
'stake_currency': 'USDT',
'use_ci_proxy': True,
'hasQuoteVolume': True,
'timeframe': '1h',
'futures': True,
'futures_pair': 'BTC/USDT:USDT',
'hasQuoteVolumeFutures': True,
'leverage_tiers_public': False,
'leverage_in_spot_market': False,
'trades_lookback_hours': 4,
'private_methods': [
'fapiPrivateGetPositionSideDual',
'fapiPrivateGetMultiAssetsMargin'
],
'sample_order': [{
"symbol": "SOLUSDT",
"orderId": 3551312894,
"orderListId": -1,
"clientOrderId": "x-R4DD3S8297c73a11ccb9dc8f2811ba",
"transactTime": 1674493798550,
"price": "15.50000000",
"origQty": "1.10000000",
"executedQty": "0.00000000",
"cummulativeQuoteQty": "0.00000000",
"status": "NEW",
"timeInForce": "GTC",
"type": "LIMIT",
"side": "BUY",
"workingTime": 1674493798550,
"fills": [],
"selfTradePreventionMode": "NONE",
}]
},
'binanceus': {
'pair': 'BTC/USDT',
'stake_currency': 'USDT',
'hasQuoteVolume': True,
'timeframe': '1h',
'futures': False,
'sample_order': [{
"symbol": "SOLUSDT",
"orderId": 3551312894,
"orderListId": -1,
"clientOrderId": "x-R4DD3S8297c73a11ccb9dc8f2811ba",
"transactTime": 1674493798550,
"price": "15.50000000",
"origQty": "1.10000000",
"executedQty": "0.00000000",
"cummulativeQuoteQty": "0.00000000",
"status": "NEW",
"timeInForce": "GTC",
"type": "LIMIT",
"side": "BUY",
"workingTime": 1674493798550,
"fills": [],
"selfTradePreventionMode": "NONE",
}]
},
'kraken': {
'pair': 'BTC/USD',
'stake_currency': 'USD',
'hasQuoteVolume': True,
'timeframe': '1h',
'leverage_tiers_public': False,
'leverage_in_spot_market': True,
'trades_lookback_hours': 12,
},
'kucoin': {
'pair': 'XRP/USDT',
'stake_currency': 'USDT',
'hasQuoteVolume': True,
'timeframe': '1h',
'leverage_tiers_public': False,
'leverage_in_spot_market': True,
'sample_order': [
{'id': '63d6742d0adc5570001d2bbf7'}, # create order
"binance": {
"pair": "BTC/USDT",
"stake_currency": "USDT",
"use_ci_proxy": True,
"hasQuoteVolume": True,
"timeframe": "1h",
"futures": True,
"futures_pair": "BTC/USDT:USDT",
"hasQuoteVolumeFutures": True,
"leverage_tiers_public": False,
"leverage_in_spot_market": False,
"trades_lookback_hours": 4,
"private_methods": ["fapiPrivateGetPositionSideDual", "fapiPrivateGetMultiAssetsMargin"],
"sample_order": [
{
'id': '63d6742d0adc5570001d2bbf7',
'symbol': 'SOL-USDT',
'opType': 'DEAL',
'type': 'limit',
'side': 'buy',
'price': '15.5',
'size': '1.1',
'funds': '0',
'dealFunds': '17.05',
'dealSize': '1.1',
'fee': '0.000065252',
'feeCurrency': 'USDT',
'stp': '',
'stop': '',
'stopTriggered': False,
'stopPrice': '0',
'timeInForce': 'GTC',
'postOnly': False,
'hidden': False,
'iceberg': False,
'visibleSize': '0',
'cancelAfter': 0,
'channel': 'API',
'clientOid': '0a053870-11bf-41e5-be61-b272a4cb62e1',
'remark': None,
'tags': 'partner:ccxt',
'isActive': False,
'cancelExist': False,
'createdAt': 1674493798550,
'tradeType': 'TRADE'
}],
"symbol": "SOLUSDT",
"orderId": 3551312894,
"orderListId": -1,
"clientOrderId": "x-R4DD3S8297c73a11ccb9dc8f2811ba",
"transactTime": 1674493798550,
"price": "15.50000000",
"origQty": "1.10000000",
"executedQty": "0.00000000",
"cummulativeQuoteQty": "0.00000000",
"status": "NEW",
"timeInForce": "GTC",
"type": "LIMIT",
"side": "BUY",
"workingTime": 1674493798550,
"fills": [],
"selfTradePreventionMode": "NONE",
}
],
},
'gate': {
'pair': 'BTC/USDT',
'stake_currency': 'USDT',
'hasQuoteVolume': True,
'timeframe': '1h',
'futures': True,
'futures_pair': 'BTC/USDT:USDT',
'hasQuoteVolumeFutures': True,
'leverage_tiers_public': True,
'leverage_in_spot_market': True,
'sample_order': [
"binanceus": {
"pair": "BTC/USDT",
"stake_currency": "USDT",
"hasQuoteVolume": True,
"timeframe": "1h",
"futures": False,
"sample_order": [
{
"symbol": "SOLUSDT",
"orderId": 3551312894,
"orderListId": -1,
"clientOrderId": "x-R4DD3S8297c73a11ccb9dc8f2811ba",
"transactTime": 1674493798550,
"price": "15.50000000",
"origQty": "1.10000000",
"executedQty": "0.00000000",
"cummulativeQuoteQty": "0.00000000",
"status": "NEW",
"timeInForce": "GTC",
"type": "LIMIT",
"side": "BUY",
"workingTime": 1674493798550,
"fills": [],
"selfTradePreventionMode": "NONE",
}
],
},
"kraken": {
"pair": "BTC/USD",
"stake_currency": "USD",
"hasQuoteVolume": True,
"timeframe": "1h",
"leverage_tiers_public": False,
"leverage_in_spot_market": True,
"trades_lookback_hours": 12,
},
"kucoin": {
"pair": "XRP/USDT",
"stake_currency": "USDT",
"hasQuoteVolume": True,
"timeframe": "1h",
"leverage_tiers_public": False,
"leverage_in_spot_market": True,
"sample_order": [
{"id": "63d6742d0adc5570001d2bbf7"}, # create order
{
"id": "63d6742d0adc5570001d2bbf7",
"symbol": "SOL-USDT",
"opType": "DEAL",
"type": "limit",
"side": "buy",
"price": "15.5",
"size": "1.1",
"funds": "0",
"dealFunds": "17.05",
"dealSize": "1.1",
"fee": "0.000065252",
"feeCurrency": "USDT",
"stp": "",
"stop": "",
"stopTriggered": False,
"stopPrice": "0",
"timeInForce": "GTC",
"postOnly": False,
"hidden": False,
"iceberg": False,
"visibleSize": "0",
"cancelAfter": 0,
"channel": "API",
"clientOid": "0a053870-11bf-41e5-be61-b272a4cb62e1",
"remark": None,
"tags": "partner:ccxt",
"isActive": False,
"cancelExist": False,
"createdAt": 1674493798550,
"tradeType": "TRADE",
},
],
},
"gate": {
"pair": "BTC/USDT",
"stake_currency": "USDT",
"hasQuoteVolume": True,
"timeframe": "1h",
"futures": True,
"futures_pair": "BTC/USDT:USDT",
"hasQuoteVolumeFutures": True,
"leverage_tiers_public": True,
"leverage_in_spot_market": True,
"sample_order": [
{
"id": "276266139423",
"text": "apiv4",
@ -164,65 +166,65 @@ EXCHANGES = {
"gt_taker_fee": "0.0015",
"gt_discount": True,
"rebated_fee": "0",
"rebated_fee_currency": "USDT"
"rebated_fee_currency": "USDT",
},
{
# market order
'id': '276401180529',
'text': 'apiv4',
'create_time': '1674493798',
'update_time': '1674493798',
'create_time_ms': '1674493798550',
'update_time_ms': '1674493798550',
'status': 'cancelled',
'currency_pair': 'SOL_USDT',
'type': 'market',
'account': 'spot',
'side': 'buy',
'amount': '17.05',
'price': '0',
'time_in_force': 'ioc',
'iceberg': '0',
'left': '0.0000000016228',
'fill_price': '17.05',
'filled_total': '17.05',
'avg_deal_price': '15.5',
'fee': '0',
'fee_currency': 'SOL',
'point_fee': '0.0199999999967544',
'gt_fee': '0',
'gt_maker_fee': '0',
'gt_taker_fee': '0',
'gt_discount': False,
'rebated_fee': '0',
'rebated_fee_currency': 'USDT'
}
"id": "276401180529",
"text": "apiv4",
"create_time": "1674493798",
"update_time": "1674493798",
"create_time_ms": "1674493798550",
"update_time_ms": "1674493798550",
"status": "cancelled",
"currency_pair": "SOL_USDT",
"type": "market",
"account": "spot",
"side": "buy",
"amount": "17.05",
"price": "0",
"time_in_force": "ioc",
"iceberg": "0",
"left": "0.0000000016228",
"fill_price": "17.05",
"filled_total": "17.05",
"avg_deal_price": "15.5",
"fee": "0",
"fee_currency": "SOL",
"point_fee": "0.0199999999967544",
"gt_fee": "0",
"gt_maker_fee": "0",
"gt_taker_fee": "0",
"gt_discount": False,
"rebated_fee": "0",
"rebated_fee_currency": "USDT",
},
],
},
'okx': {
'pair': 'BTC/USDT',
'stake_currency': 'USDT',
'hasQuoteVolume': True,
'timeframe': '1h',
'futures': True,
'futures_pair': 'BTC/USDT:USDT',
'hasQuoteVolumeFutures': False,
'leverage_tiers_public': True,
'leverage_in_spot_market': True,
'private_methods': ['fetch_accounts'],
"okx": {
"pair": "BTC/USDT",
"stake_currency": "USDT",
"hasQuoteVolume": True,
"timeframe": "1h",
"futures": True,
"futures_pair": "BTC/USDT:USDT",
"hasQuoteVolumeFutures": False,
"leverage_tiers_public": True,
"leverage_in_spot_market": True,
"private_methods": ["fetch_accounts"],
},
'bybit': {
'pair': 'BTC/USDT',
'stake_currency': 'USDT',
'hasQuoteVolume': True,
'use_ci_proxy': True,
'timeframe': '1h',
'futures_pair': 'BTC/USDT:USDT',
'futures': True,
'orderbook_max_entries': 50,
'leverage_tiers_public': True,
'leverage_in_spot_market': True,
'sample_order': [
"bybit": {
"pair": "BTC/USDT",
"stake_currency": "USDT",
"hasQuoteVolume": True,
"use_ci_proxy": True,
"timeframe": "1h",
"futures_pair": "BTC/USDT:USDT",
"futures": True,
"orderbook_max_entries": 50,
"leverage_tiers_public": True,
"leverage_in_spot_market": True,
"sample_order": [
{
"orderId": "1274754916287346280",
"orderLinkId": "1666798627015730",
@ -236,38 +238,38 @@ EXCHANGES = {
"timeInForce": "GTC",
"accountId": "5555555",
"execQty": "0",
"orderCategory": "0"
"orderCategory": "0",
}
]
],
},
'bitmart': {
'pair': 'BTC/USDT',
'stake_currency': 'USDT',
'hasQuoteVolume': True,
'timeframe': '1h',
'orderbook_max_entries': 50,
"bitmart": {
"pair": "BTC/USDT",
"stake_currency": "USDT",
"hasQuoteVolume": True,
"timeframe": "1h",
"orderbook_max_entries": 50,
},
'htx': {
'pair': 'ETH/BTC',
'stake_currency': 'BTC',
'hasQuoteVolume': True,
'timeframe': '1h',
'futures': False,
"htx": {
"pair": "ETH/BTC",
"stake_currency": "BTC",
"hasQuoteVolume": True,
"timeframe": "1h",
"futures": False,
},
'bitvavo': {
'pair': 'BTC/EUR',
'stake_currency': 'EUR',
'hasQuoteVolume': True,
'timeframe': '1h',
'leverage_tiers_public': False,
'leverage_in_spot_market': False,
"bitvavo": {
"pair": "BTC/EUR",
"stake_currency": "EUR",
"hasQuoteVolume": True,
"timeframe": "1h",
"leverage_tiers_public": False,
"leverage_in_spot_market": False,
},
'bingx': {
'pair': 'BTC/USDT',
'stake_currency': 'USDT',
'hasQuoteVolume': True,
'timeframe': '1h',
'futures': False,
"bingx": {
"pair": "BTC/USDT",
"stake_currency": "USDT",
"hasQuoteVolume": True,
"timeframe": "1h",
"futures": False,
},
}
@ -275,21 +277,22 @@ EXCHANGES = {
@pytest.fixture(scope="class")
def exchange_conf():
config = get_default_conf_usdt((Path(__file__).parent / "testdata").resolve())
config['exchange']['pair_whitelist'] = []
config['exchange']['key'] = ''
config['exchange']['secret'] = ''
config['dry_run'] = False
config['entry_pricing']['use_order_book'] = True
config['exit_pricing']['use_order_book'] = True
config["exchange"]["pair_whitelist"] = []
config["exchange"]["key"] = ""
config["exchange"]["secret"] = ""
config["dry_run"] = False
config["entry_pricing"]["use_order_book"] = True
config["exit_pricing"]["use_order_book"] = True
return config
def set_test_proxy(config: Config, use_proxy: bool) -> Config:
# Set proxy to test in CI.
import os
if use_proxy and (proxy := os.environ.get('CI_WEB_PROXY')):
if use_proxy and (proxy := os.environ.get("CI_WEB_PROXY")):
config1 = deepcopy(config)
config1['exchange']['ccxt_config'] = {
config1["exchange"]["ccxt_config"] = {
"httpsProxy": proxy,
}
return config1
@ -299,44 +302,45 @@ def set_test_proxy(config: Config, use_proxy: bool) -> Config:
def get_exchange(exchange_name, exchange_conf):
exchange_conf = set_test_proxy(
exchange_conf, EXCHANGES[exchange_name].get('use_ci_proxy', False))
exchange_conf['exchange']['name'] = exchange_name
exchange_conf['stake_currency'] = EXCHANGES[exchange_name]['stake_currency']
exchange = ExchangeResolver.load_exchange(exchange_conf, validate=True,
load_leverage_tiers=True)
exchange_conf, EXCHANGES[exchange_name].get("use_ci_proxy", False)
)
exchange_conf["exchange"]["name"] = exchange_name
exchange_conf["stake_currency"] = EXCHANGES[exchange_name]["stake_currency"]
exchange = ExchangeResolver.load_exchange(
exchange_conf, validate=True, load_leverage_tiers=True
)
yield exchange, exchange_name
def get_futures_exchange(exchange_name, exchange_conf, class_mocker):
if EXCHANGES[exchange_name].get('futures') is not True:
if EXCHANGES[exchange_name].get("futures") is not True:
pytest.skip(f"Exchange {exchange_name} does not support futures.")
else:
exchange_conf = deepcopy(exchange_conf)
exchange_conf = set_test_proxy(
exchange_conf, EXCHANGES[exchange_name].get('use_ci_proxy', False))
exchange_conf['trading_mode'] = 'futures'
exchange_conf['margin_mode'] = 'isolated'
exchange_conf, EXCHANGES[exchange_name].get("use_ci_proxy", False)
)
exchange_conf["trading_mode"] = "futures"
exchange_conf["margin_mode"] = "isolated"
class_mocker.patch(
'freqtrade.exchange.binance.Binance.fill_leverage_tiers')
class_mocker.patch(f'{EXMS}.fetch_trading_fees')
class_mocker.patch('freqtrade.exchange.okx.Okx.additional_exchange_init')
class_mocker.patch('freqtrade.exchange.binance.Binance.additional_exchange_init')
class_mocker.patch('freqtrade.exchange.bybit.Bybit.additional_exchange_init')
class_mocker.patch(f'{EXMS}.load_cached_leverage_tiers', return_value=None)
class_mocker.patch(f'{EXMS}.cache_leverage_tiers')
class_mocker.patch("freqtrade.exchange.binance.Binance.fill_leverage_tiers")
class_mocker.patch(f"{EXMS}.fetch_trading_fees")
class_mocker.patch("freqtrade.exchange.okx.Okx.additional_exchange_init")
class_mocker.patch("freqtrade.exchange.binance.Binance.additional_exchange_init")
class_mocker.patch("freqtrade.exchange.bybit.Bybit.additional_exchange_init")
class_mocker.patch(f"{EXMS}.load_cached_leverage_tiers", return_value=None)
class_mocker.patch(f"{EXMS}.cache_leverage_tiers")
yield from get_exchange(exchange_name, exchange_conf)
@pytest.fixture(params=EXCHANGES, scope="class")
def exchange(request, exchange_conf, class_mocker):
class_mocker.patch('freqtrade.exchange.bybit.Bybit.additional_exchange_init')
class_mocker.patch("freqtrade.exchange.bybit.Bybit.additional_exchange_init")
yield from get_exchange(request.param, exchange_conf)
@pytest.fixture(params=EXCHANGES, scope="class")
def exchange_futures(request, exchange_conf, class_mocker):
yield from get_futures_exchange(request.param, exchange_conf, class_mocker)

View File

@ -18,38 +18,40 @@ from tests.exchange_online.conftest import EXCHANGE_FIXTURE_TYPE, EXCHANGES
@pytest.mark.longrun
class TestCCXTExchange:
def test_load_markets(self, exchange: EXCHANGE_FIXTURE_TYPE):
exch, exchangename = exchange
pair = EXCHANGES[exchangename]['pair']
pair = EXCHANGES[exchangename]["pair"]
markets = exch.markets
assert pair in markets
assert isinstance(markets[pair], dict)
assert exch.market_is_spot(markets[pair])
def test_has_validations(self, exchange: EXCHANGE_FIXTURE_TYPE):
exch, exchangename = exchange
exch.validate_ordertypes({
'entry': 'limit',
'exit': 'limit',
'stoploss': 'limit',
})
exch.validate_ordertypes(
{
"entry": "limit",
"exit": "limit",
"stoploss": "limit",
}
)
if exchangename == 'gate':
if exchangename == "gate":
# gate doesn't have market orders on spot
return
exch.validate_ordertypes({
'entry': 'market',
'exit': 'market',
'stoploss': 'market',
})
exch.validate_ordertypes(
{
"entry": "market",
"exit": "market",
"stoploss": "market",
}
)
def test_load_markets_futures(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
exchange, exchangename = exchange_futures
pair = EXCHANGES[exchangename]['pair']
pair = EXCHANGES[exchangename].get('futures_pair', pair)
pair = EXCHANGES[exchangename]["pair"]
pair = EXCHANGES[exchangename].get("futures_pair", pair)
markets = exchange.markets
assert pair in markets
assert isinstance(markets[pair], dict)
@ -58,90 +60,90 @@ class TestCCXTExchange:
def test_ccxt_order_parse(self, exchange: EXCHANGE_FIXTURE_TYPE):
exch, exchange_name = exchange
if orders := EXCHANGES[exchange_name].get('sample_order'):
pair = 'SOL/USDT'
if orders := EXCHANGES[exchange_name].get("sample_order"):
pair = "SOL/USDT"
for order in orders:
market = exch._api.markets[pair]
po = exch._api.parse_order(order, market)
assert isinstance(po['id'], str)
assert po['id'] is not None
assert isinstance(po["id"], str)
assert po["id"] is not None
if len(order.keys()) < 5:
# Kucoin case
assert po['status'] is None
assert po["status"] is None
continue
assert po['timestamp'] == 1674493798550
assert isinstance(po['datetime'], str)
assert isinstance(po['timestamp'], int)
assert isinstance(po['price'], float)
assert po['price'] == 15.5
if po['average'] is not None:
assert isinstance(po['average'], float)
assert po['average'] == 15.5
assert po['symbol'] == pair
assert isinstance(po['amount'], float)
assert po['amount'] == 1.1
assert isinstance(po['status'], str)
assert po["timestamp"] == 1674493798550
assert isinstance(po["datetime"], str)
assert isinstance(po["timestamp"], int)
assert isinstance(po["price"], float)
assert po["price"] == 15.5
if po["average"] is not None:
assert isinstance(po["average"], float)
assert po["average"] == 15.5
assert po["symbol"] == pair
assert isinstance(po["amount"], float)
assert po["amount"] == 1.1
assert isinstance(po["status"], str)
else:
pytest.skip(f"No sample order available for exchange {exchange_name}")
def test_ccxt_fetch_tickers(self, exchange: EXCHANGE_FIXTURE_TYPE):
exch, exchangename = exchange
pair = EXCHANGES[exchangename]['pair']
pair = EXCHANGES[exchangename]["pair"]
tickers = exch.get_tickers()
assert pair in tickers
assert 'ask' in tickers[pair]
assert tickers[pair]['ask'] is not None
assert 'bid' in tickers[pair]
assert tickers[pair]['bid'] is not None
assert 'quoteVolume' in tickers[pair]
if EXCHANGES[exchangename].get('hasQuoteVolume'):
assert tickers[pair]['quoteVolume'] is not None
assert "ask" in tickers[pair]
assert tickers[pair]["ask"] is not None
assert "bid" in tickers[pair]
assert tickers[pair]["bid"] is not None
assert "quoteVolume" in tickers[pair]
if EXCHANGES[exchangename].get("hasQuoteVolume"):
assert tickers[pair]["quoteVolume"] is not None
def test_ccxt_fetch_tickers_futures(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
exch, exchangename = exchange_futures
if not exch or exchangename in ('gate'):
if not exch or exchangename in ("gate"):
# exchange_futures only returns values for supported exchanges
return
pair = EXCHANGES[exchangename]['pair']
pair = EXCHANGES[exchangename].get('futures_pair', pair)
pair = EXCHANGES[exchangename]["pair"]
pair = EXCHANGES[exchangename].get("futures_pair", pair)
tickers = exch.get_tickers()
assert pair in tickers
assert 'ask' in tickers[pair]
assert tickers[pair]['ask'] is not None
assert 'bid' in tickers[pair]
assert tickers[pair]['bid'] is not None
assert 'quoteVolume' in tickers[pair]
if EXCHANGES[exchangename].get('hasQuoteVolumeFutures'):
assert tickers[pair]['quoteVolume'] is not None
assert "ask" in tickers[pair]
assert tickers[pair]["ask"] is not None
assert "bid" in tickers[pair]
assert tickers[pair]["bid"] is not None
assert "quoteVolume" in tickers[pair]
if EXCHANGES[exchangename].get("hasQuoteVolumeFutures"):
assert tickers[pair]["quoteVolume"] is not None
def test_ccxt_fetch_ticker(self, exchange: EXCHANGE_FIXTURE_TYPE):
exch, exchangename = exchange
pair = EXCHANGES[exchangename]['pair']
pair = EXCHANGES[exchangename]["pair"]
ticker = exch.fetch_ticker(pair)
assert 'ask' in ticker
assert ticker['ask'] is not None
assert 'bid' in ticker
assert ticker['bid'] is not None
assert 'quoteVolume' in ticker
if EXCHANGES[exchangename].get('hasQuoteVolume'):
assert ticker['quoteVolume'] is not None
assert "ask" in ticker
assert ticker["ask"] is not None
assert "bid" in ticker
assert ticker["bid"] is not None
assert "quoteVolume" in ticker
if EXCHANGES[exchangename].get("hasQuoteVolume"):
assert ticker["quoteVolume"] is not None
def test_ccxt_fetch_l2_orderbook(self, exchange: EXCHANGE_FIXTURE_TYPE):
exch, exchangename = exchange
pair = EXCHANGES[exchangename]['pair']
pair = EXCHANGES[exchangename]["pair"]
l2 = exch.fetch_l2_order_book(pair)
orderbook_max_entries = EXCHANGES[exchangename].get('orderbook_max_entries')
assert 'asks' in l2
assert 'bids' in l2
assert len(l2['asks']) >= 1
assert len(l2['bids']) >= 1
l2_limit_range = exch._ft_has['l2_limit_range']
l2_limit_range_required = exch._ft_has['l2_limit_range_required']
if exchangename == 'gate':
orderbook_max_entries = EXCHANGES[exchangename].get("orderbook_max_entries")
assert "asks" in l2
assert "bids" in l2
assert len(l2["asks"]) >= 1
assert len(l2["bids"]) >= 1
l2_limit_range = exch._ft_has["l2_limit_range"]
l2_limit_range_required = exch._ft_has["l2_limit_range_required"]
if exchangename == "gate":
# TODO: Gate is unstable here at the moment, ignoring the limit partially.
return
for val in [1, 2, 5, 25, 50, 100]:
@ -151,29 +153,30 @@ class TestCCXTExchange:
if not l2_limit_range or val in l2_limit_range:
if val > 50:
# Orderbooks are not always this deep.
assert val - 5 < len(l2['asks']) <= val
assert val - 5 < len(l2['bids']) <= val
assert val - 5 < len(l2["asks"]) <= val
assert val - 5 < len(l2["bids"]) <= val
else:
assert len(l2['asks']) == val
assert len(l2['bids']) == val
assert len(l2["asks"]) == val
assert len(l2["bids"]) == val
else:
next_limit = exch.get_next_limit_in_list(
val, l2_limit_range, l2_limit_range_required)
val, l2_limit_range, l2_limit_range_required
)
if next_limit is None:
assert len(l2['asks']) > 100
assert len(l2['asks']) > 100
assert len(l2["asks"]) > 100
assert len(l2["asks"]) > 100
elif next_limit > 200:
# Large orderbook sizes can be a problem for some exchanges (bitrex ...)
assert len(l2['asks']) > 200
assert len(l2['asks']) > 200
assert len(l2["asks"]) > 200
assert len(l2["asks"]) > 200
else:
assert len(l2['asks']) == next_limit
assert len(l2['asks']) == next_limit
assert len(l2["asks"]) == next_limit
assert len(l2["asks"]) == next_limit
def test_ccxt_fetch_ohlcv(self, exchange: EXCHANGE_FIXTURE_TYPE):
exch, exchangename = exchange
pair = EXCHANGES[exchangename]['pair']
timeframe = EXCHANGES[exchangename]['timeframe']
pair = EXCHANGES[exchangename]["pair"]
timeframe = EXCHANGES[exchangename]["timeframe"]
pair_tf = (pair, timeframe, CandleType.SPOT)
@ -182,19 +185,20 @@ class TestCCXTExchange:
assert len(ohlcv[pair_tf]) == len(exch.klines(pair_tf))
# assert len(exch.klines(pair_tf)) > 200
# Assume 90% uptime ...
assert len(exch.klines(pair_tf)) > exch.ohlcv_candle_limit(
timeframe, CandleType.SPOT) * 0.90
assert (
len(exch.klines(pair_tf)) > exch.ohlcv_candle_limit(timeframe, CandleType.SPOT) * 0.90
)
# Check if last-timeframe is within the last 2 intervals
now = datetime.now(timezone.utc) - timedelta(minutes=(timeframe_to_minutes(timeframe) * 2))
assert exch.klines(pair_tf).iloc[-1]['date'] >= timeframe_to_prev_date(timeframe, now)
assert exch.klines(pair_tf).iloc[-1]["date"] >= timeframe_to_prev_date(timeframe, now)
def test_ccxt_fetch_ohlcv_startdate(self, exchange: EXCHANGE_FIXTURE_TYPE):
"""
Test that pair data starts at the provided startdate
"""
exch, exchangename = exchange
pair = EXCHANGES[exchangename]['pair']
timeframe = '1d'
pair = EXCHANGES[exchangename]["pair"]
timeframe = "1d"
pair_tf = (pair, timeframe, CandleType.SPOT)
# last 5 days ...
@ -204,25 +208,22 @@ class TestCCXTExchange:
assert len(ohlcv[pair_tf]) == len(exch.klines(pair_tf))
# Check if last-timeframe is within the last 2 intervals
now = datetime.now(timezone.utc) - timedelta(minutes=(timeframe_to_minutes(timeframe) * 2))
assert exch.klines(pair_tf).iloc[-1]['date'] >= timeframe_to_prev_date(timeframe, now)
assert exch.klines(pair_tf)['date'].astype(int).iloc[0] // 1e6 == since_ms
assert exch.klines(pair_tf).iloc[-1]["date"] >= timeframe_to_prev_date(timeframe, now)
assert exch.klines(pair_tf)["date"].astype(int).iloc[0] // 1e6 == since_ms
def ccxt__async_get_candle_history(
self, exchange, exchangename, pair, timeframe, candle_type, factor=0.9):
self, exchange, exchangename, pair, timeframe, candle_type, factor=0.9
):
timeframe_ms = timeframe_to_msecs(timeframe)
now = timeframe_to_prev_date(
timeframe, datetime.now(timezone.utc))
now = timeframe_to_prev_date(timeframe, datetime.now(timezone.utc))
for offset in (360, 120, 30, 10, 5, 2):
since = now - timedelta(days=offset)
since_ms = int(since.timestamp() * 1000)
res = exchange.loop.run_until_complete(exchange._async_get_candle_history(
pair=pair,
timeframe=timeframe,
since_ms=since_ms,
candle_type=candle_type
)
res = exchange.loop.run_until_complete(
exchange._async_get_candle_history(
pair=pair, timeframe=timeframe, since_ms=since_ms, candle_type=candle_type
)
)
assert res
assert res[0] == pair
@ -231,34 +232,39 @@ class TestCCXTExchange:
candles = res[3]
candle_count = exchange.ohlcv_candle_limit(timeframe, candle_type, since_ms) * factor
candle_count1 = (now.timestamp() * 1000 - since_ms) // timeframe_ms * factor
assert len(candles) >= min(candle_count, candle_count1), \
f"{len(candles)} < {candle_count} in {timeframe}, Offset: {offset} {factor}"
assert len(candles) >= min(
candle_count, candle_count1
), f"{len(candles)} < {candle_count} in {timeframe}, Offset: {offset} {factor}"
# Check if first-timeframe is either the start, or start + 1
assert candles[0][0] == since_ms or (since_ms + timeframe_ms)
def test_ccxt__async_get_candle_history(self, exchange: EXCHANGE_FIXTURE_TYPE):
exc, exchangename = exchange
if not exc._ft_has['ohlcv_has_history']:
if not exc._ft_has["ohlcv_has_history"]:
pytest.skip("Exchange does not support candle history")
pair = EXCHANGES[exchangename]['pair']
timeframe = EXCHANGES[exchangename]['timeframe']
self.ccxt__async_get_candle_history(
exc, exchangename, pair, timeframe, CandleType.SPOT)
pair = EXCHANGES[exchangename]["pair"]
timeframe = EXCHANGES[exchangename]["timeframe"]
self.ccxt__async_get_candle_history(exc, exchangename, pair, timeframe, CandleType.SPOT)
@pytest.mark.parametrize('candle_type', [
CandleType.FUTURES,
CandleType.FUNDING_RATE,
CandleType.MARK,
])
@pytest.mark.parametrize(
"candle_type",
[
CandleType.FUTURES,
CandleType.FUNDING_RATE,
CandleType.MARK,
],
)
def test_ccxt__async_get_candle_history_futures(
self, exchange_futures: EXCHANGE_FIXTURE_TYPE, candle_type):
self, exchange_futures: EXCHANGE_FIXTURE_TYPE, candle_type
):
exchange, exchangename = exchange_futures
pair = EXCHANGES[exchangename].get('futures_pair', EXCHANGES[exchangename]['pair'])
timeframe = EXCHANGES[exchangename]['timeframe']
pair = EXCHANGES[exchangename].get("futures_pair", EXCHANGES[exchangename]["pair"])
timeframe = EXCHANGES[exchangename]["timeframe"]
if candle_type == CandleType.FUNDING_RATE:
timeframe = exchange._ft_has.get('funding_fee_timeframe',
exchange._ft_has['mark_ohlcv_timeframe'])
timeframe = exchange._ft_has.get(
"funding_fee_timeframe", exchange._ft_has["mark_ohlcv_timeframe"]
)
self.ccxt__async_get_candle_history(
exchange,
exchangename,
@ -270,16 +276,16 @@ class TestCCXTExchange:
def test_ccxt_fetch_funding_rate_history(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
exchange, exchangename = exchange_futures
pair = EXCHANGES[exchangename].get('futures_pair', EXCHANGES[exchangename]['pair'])
pair = EXCHANGES[exchangename].get("futures_pair", EXCHANGES[exchangename]["pair"])
since = int((datetime.now(timezone.utc) - timedelta(days=5)).timestamp() * 1000)
timeframe_ff = exchange._ft_has.get('funding_fee_timeframe',
exchange._ft_has['mark_ohlcv_timeframe'])
timeframe_ff = exchange._ft_has.get(
"funding_fee_timeframe", exchange._ft_has["mark_ohlcv_timeframe"]
)
pair_tf = (pair, timeframe_ff, CandleType.FUNDING_RATE)
funding_ohlcv = exchange.refresh_latest_ohlcv(
[pair_tf],
since_ms=since,
drop_incomplete=False)
[pair_tf], since_ms=since, drop_incomplete=False
)
assert isinstance(funding_ohlcv, dict)
rate = funding_ohlcv[pair_tf]
@ -288,61 +294,58 @@ class TestCCXTExchange:
hour1 = timeframe_to_prev_date(timeframe_ff, this_hour - timedelta(minutes=1))
hour2 = timeframe_to_prev_date(timeframe_ff, hour1 - timedelta(minutes=1))
hour3 = timeframe_to_prev_date(timeframe_ff, hour2 - timedelta(minutes=1))
val0 = rate[rate['date'] == this_hour].iloc[0]['open']
val1 = rate[rate['date'] == hour1].iloc[0]['open']
val2 = rate[rate['date'] == hour2].iloc[0]['open']
val3 = rate[rate['date'] == hour3].iloc[0]['open']
val0 = rate[rate["date"] == this_hour].iloc[0]["open"]
val1 = rate[rate["date"] == hour1].iloc[0]["open"]
val2 = rate[rate["date"] == hour2].iloc[0]["open"]
val3 = rate[rate["date"] == hour3].iloc[0]["open"]
# Test For last 4 hours
# Avoids random test-failure when funding-fees are 0 for a few hours.
assert val0 != 0.0 or val1 != 0.0 or val2 != 0.0 or val3 != 0.0
# We expect funding rates to be different from 0.0 - or moving around.
assert (
rate['open'].max() != 0.0 or rate['open'].min() != 0.0 or
(rate['open'].min() != rate['open'].max())
rate["open"].max() != 0.0
or rate["open"].min() != 0.0
or (rate["open"].min() != rate["open"].max())
)
def test_ccxt_fetch_mark_price_history(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
exchange, exchangename = exchange_futures
pair = EXCHANGES[exchangename].get('futures_pair', EXCHANGES[exchangename]['pair'])
pair = EXCHANGES[exchangename].get("futures_pair", EXCHANGES[exchangename]["pair"])
since = int((datetime.now(timezone.utc) - timedelta(days=5)).timestamp() * 1000)
pair_tf = (pair, '1h', CandleType.MARK)
pair_tf = (pair, "1h", CandleType.MARK)
mark_ohlcv = exchange.refresh_latest_ohlcv(
[pair_tf],
since_ms=since,
drop_incomplete=False)
mark_ohlcv = exchange.refresh_latest_ohlcv([pair_tf], since_ms=since, drop_incomplete=False)
assert isinstance(mark_ohlcv, dict)
expected_tf = '1h'
expected_tf = "1h"
mark_candles = mark_ohlcv[pair_tf]
this_hour = timeframe_to_prev_date(expected_tf)
prev_hour = timeframe_to_prev_date(expected_tf, this_hour - timedelta(minutes=1))
assert mark_candles[mark_candles['date'] == prev_hour].iloc[0]['open'] != 0.0
assert mark_candles[mark_candles['date'] == this_hour].iloc[0]['open'] != 0.0
assert mark_candles[mark_candles["date"] == prev_hour].iloc[0]["open"] != 0.0
assert mark_candles[mark_candles["date"] == this_hour].iloc[0]["open"] != 0.0
def test_ccxt__calculate_funding_fees(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
exchange, exchangename = exchange_futures
pair = EXCHANGES[exchangename].get('futures_pair', EXCHANGES[exchangename]['pair'])
pair = EXCHANGES[exchangename].get("futures_pair", EXCHANGES[exchangename]["pair"])
since = datetime.now(timezone.utc) - timedelta(days=5)
funding_fee = exchange._fetch_and_calculate_funding_fees(
pair, 20, is_short=False, open_date=since)
pair, 20, is_short=False, open_date=since
)
assert isinstance(funding_fee, float)
# assert funding_fee > 0
def test_ccxt__async_get_trade_history(self, exchange: EXCHANGE_FIXTURE_TYPE):
exch, exchangename = exchange
if not (lookback := EXCHANGES[exchangename].get('trades_lookback_hours')):
pytest.skip('test_fetch_trades not enabled for this exchange')
pair = EXCHANGES[exchangename]['pair']
if not (lookback := EXCHANGES[exchangename].get("trades_lookback_hours")):
pytest.skip("test_fetch_trades not enabled for this exchange")
pair = EXCHANGES[exchangename]["pair"]
since = int((datetime.now(timezone.utc) - timedelta(hours=lookback)).timestamp() * 1000)
res = exch.loop.run_until_complete(
exch._async_get_trade_history(pair, since, None, None)
)
res = exch.loop.run_until_complete(exch._async_get_trade_history(pair, since, None, None))
assert len(res) == 2
res_pair, res_trades = res
assert res_pair == pair
@ -352,85 +355,73 @@ class TestCCXTExchange:
def test_ccxt_get_fee(self, exchange: EXCHANGE_FIXTURE_TYPE):
exch, exchangename = exchange
pair = EXCHANGES[exchangename]['pair']
pair = EXCHANGES[exchangename]["pair"]
threshold = 0.01
assert 0 < exch.get_fee(pair, 'limit', 'buy') < threshold
assert 0 < exch.get_fee(pair, 'limit', 'sell') < threshold
assert 0 < exch.get_fee(pair, 'market', 'buy') < threshold
assert 0 < exch.get_fee(pair, 'market', 'sell') < threshold
assert 0 < exch.get_fee(pair, "limit", "buy") < threshold
assert 0 < exch.get_fee(pair, "limit", "sell") < threshold
assert 0 < exch.get_fee(pair, "market", "buy") < threshold
assert 0 < exch.get_fee(pair, "market", "sell") < threshold
def test_ccxt_get_max_leverage_spot(self, exchange: EXCHANGE_FIXTURE_TYPE):
spot, spot_name = exchange
if spot:
leverage_in_market_spot = EXCHANGES[spot_name].get('leverage_in_spot_market')
leverage_in_market_spot = EXCHANGES[spot_name].get("leverage_in_spot_market")
if leverage_in_market_spot:
spot_pair = EXCHANGES[spot_name].get('pair', EXCHANGES[spot_name]['pair'])
spot_pair = EXCHANGES[spot_name].get("pair", EXCHANGES[spot_name]["pair"])
spot_leverage = spot.get_max_leverage(spot_pair, 20)
assert (isinstance(spot_leverage, float) or isinstance(spot_leverage, int))
assert isinstance(spot_leverage, float) or isinstance(spot_leverage, int)
assert spot_leverage >= 1.0
def test_ccxt_get_max_leverage_futures(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
futures, futures_name = exchange_futures
leverage_tiers_public = EXCHANGES[futures_name].get('leverage_tiers_public')
leverage_tiers_public = EXCHANGES[futures_name].get("leverage_tiers_public")
if leverage_tiers_public:
futures_pair = EXCHANGES[futures_name].get(
'futures_pair',
EXCHANGES[futures_name]['pair']
"futures_pair", EXCHANGES[futures_name]["pair"]
)
futures_leverage = futures.get_max_leverage(futures_pair, 20)
assert (isinstance(futures_leverage, float) or isinstance(futures_leverage, int))
assert isinstance(futures_leverage, float) or isinstance(futures_leverage, int)
assert futures_leverage >= 1.0
def test_ccxt_get_contract_size(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
futures, futures_name = exchange_futures
futures_pair = EXCHANGES[futures_name].get(
'futures_pair',
EXCHANGES[futures_name]['pair']
)
futures_pair = EXCHANGES[futures_name].get("futures_pair", EXCHANGES[futures_name]["pair"])
contract_size = futures.get_contract_size(futures_pair)
assert (isinstance(contract_size, float) or isinstance(contract_size, int))
assert isinstance(contract_size, float) or isinstance(contract_size, int)
assert contract_size >= 0.0
def test_ccxt_load_leverage_tiers(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
futures, futures_name = exchange_futures
if EXCHANGES[futures_name].get('leverage_tiers_public'):
if EXCHANGES[futures_name].get("leverage_tiers_public"):
leverage_tiers = futures.load_leverage_tiers()
futures_pair = EXCHANGES[futures_name].get(
'futures_pair',
EXCHANGES[futures_name]['pair']
"futures_pair", EXCHANGES[futures_name]["pair"]
)
assert (isinstance(leverage_tiers, dict))
assert isinstance(leverage_tiers, dict)
assert futures_pair in leverage_tiers
pair_tiers = leverage_tiers[futures_pair]
assert len(pair_tiers) > 0
oldLeverage = float('inf')
oldLeverage = float("inf")
oldMaintenanceMarginRate = oldminNotional = oldmaxNotional = -1
for tier in pair_tiers:
for key in [
'maintenanceMarginRate',
'minNotional',
'maxNotional',
'maxLeverage'
]:
for key in ["maintenanceMarginRate", "minNotional", "maxNotional", "maxLeverage"]:
assert key in tier
assert tier[key] >= 0.0
assert tier['maxNotional'] > tier['minNotional']
assert tier['maxLeverage'] <= oldLeverage
assert tier['maintenanceMarginRate'] >= oldMaintenanceMarginRate
assert tier['minNotional'] > oldminNotional
assert tier['maxNotional'] > oldmaxNotional
oldLeverage = tier['maxLeverage']
oldMaintenanceMarginRate = tier['maintenanceMarginRate']
oldminNotional = tier['minNotional']
oldmaxNotional = tier['maxNotional']
assert tier["maxNotional"] > tier["minNotional"]
assert tier["maxLeverage"] <= oldLeverage
assert tier["maintenanceMarginRate"] >= oldMaintenanceMarginRate
assert tier["minNotional"] > oldminNotional
assert tier["maxNotional"] > oldmaxNotional
oldLeverage = tier["maxLeverage"]
oldMaintenanceMarginRate = tier["maintenanceMarginRate"]
oldminNotional = tier["minNotional"]
oldmaxNotional = tier["maxNotional"]
def test_ccxt_dry_run_liquidation_price(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
futures, futures_name = exchange_futures
if EXCHANGES[futures_name].get('leverage_tiers_public'):
if EXCHANGES[futures_name].get("leverage_tiers_public"):
futures_pair = EXCHANGES[futures_name].get(
'futures_pair',
EXCHANGES[futures_name]['pair']
"futures_pair", EXCHANGES[futures_name]["pair"]
)
liquidation_price = futures.dry_run_liquidation_price(
@ -442,7 +433,7 @@ class TestCCXTExchange:
leverage=5,
wallet_balance=100,
)
assert (isinstance(liquidation_price, float))
assert isinstance(liquidation_price, float)
assert liquidation_price >= 0.0
liquidation_price = futures.dry_run_liquidation_price(
@ -454,20 +445,17 @@ class TestCCXTExchange:
leverage=5,
wallet_balance=100,
)
assert (isinstance(liquidation_price, float))
assert isinstance(liquidation_price, float)
assert liquidation_price >= 0.0
def test_ccxt_get_max_pair_stake_amount(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
futures, futures_name = exchange_futures
futures_pair = EXCHANGES[futures_name].get(
'futures_pair',
EXCHANGES[futures_name]['pair']
)
futures_pair = EXCHANGES[futures_name].get("futures_pair", EXCHANGES[futures_name]["pair"])
max_stake_amount = futures.get_max_pair_stake_amount(futures_pair, 40000)
assert (isinstance(max_stake_amount, float))
assert isinstance(max_stake_amount, float)
assert max_stake_amount >= 0.0
def test_private_method_presence(self, exchange: EXCHANGE_FIXTURE_TYPE):
exch, exchangename = exchange
for method in EXCHANGES[exchangename].get('private_methods', []):
for method in EXCHANGES[exchangename].get("private_methods", []):
assert hasattr(exch._api, method)

View File

@ -20,106 +20,112 @@ def test_setup_optimize_configuration_without_arguments(mocker, default_conf, ca
patched_configuration_load_config_file(mocker, default_conf)
args = [
'edge',
'--config', 'config.json',
'--strategy', CURRENT_TEST_STRATEGY,
"edge",
"--config",
"config.json",
"--strategy",
CURRENT_TEST_STRATEGY,
]
config = setup_optimize_configuration(get_args(args), RunMode.EDGE)
assert config['runmode'] == RunMode.EDGE
assert config["runmode"] == RunMode.EDGE
assert 'max_open_trades' in config
assert 'stake_currency' in config
assert 'stake_amount' in config
assert 'exchange' in config
assert 'pair_whitelist' in config['exchange']
assert 'datadir' in config
assert log_has('Using data directory: {} ...'.format(config['datadir']), caplog)
assert 'timeframe' in config
assert "max_open_trades" in config
assert "stake_currency" in config
assert "stake_amount" in config
assert "exchange" in config
assert "pair_whitelist" in config["exchange"]
assert "datadir" in config
assert log_has("Using data directory: {} ...".format(config["datadir"]), caplog)
assert "timeframe" in config
assert 'timerange' not in config
assert 'stoploss_range' not in config
assert "timerange" not in config
assert "stoploss_range" not in config
def test_setup_edge_configuration_with_arguments(mocker, edge_conf, caplog) -> None:
patched_configuration_load_config_file(mocker, edge_conf)
mocker.patch(
'freqtrade.configuration.configuration.create_datadir',
lambda c, x: x
)
mocker.patch("freqtrade.configuration.configuration.create_datadir", lambda c, x: x)
args = [
'edge',
'--config', 'config.json',
'--strategy', CURRENT_TEST_STRATEGY,
'--datadir', '/foo/bar',
'--timeframe', '1m',
'--timerange', ':100',
'--stoplosses=-0.01,-0.10,-0.001'
"edge",
"--config",
"config.json",
"--strategy",
CURRENT_TEST_STRATEGY,
"--datadir",
"/foo/bar",
"--timeframe",
"1m",
"--timerange",
":100",
"--stoplosses=-0.01,-0.10,-0.001",
]
config = setup_optimize_configuration(get_args(args), RunMode.EDGE)
assert 'max_open_trades' in config
assert 'stake_currency' in config
assert 'stake_amount' in config
assert 'exchange' in config
assert 'pair_whitelist' in config['exchange']
assert 'datadir' in config
assert config['runmode'] == RunMode.EDGE
assert log_has('Using data directory: {} ...'.format(config['datadir']), caplog)
assert 'timeframe' in config
assert log_has('Parameter -i/--timeframe detected ... Using timeframe: 1m ...',
caplog)
assert "max_open_trades" in config
assert "stake_currency" in config
assert "stake_amount" in config
assert "exchange" in config
assert "pair_whitelist" in config["exchange"]
assert "datadir" in config
assert config["runmode"] == RunMode.EDGE
assert log_has("Using data directory: {} ...".format(config["datadir"]), caplog)
assert "timeframe" in config
assert log_has("Parameter -i/--timeframe detected ... Using timeframe: 1m ...", caplog)
assert 'timerange' in config
assert log_has('Parameter --timerange detected: {} ...'.format(config['timerange']), caplog)
assert "timerange" in config
assert log_has("Parameter --timerange detected: {} ...".format(config["timerange"]), caplog)
def test_start(mocker, fee, edge_conf, caplog) -> None:
start_mock = MagicMock()
mocker.patch(f'{EXMS}.get_fee', fee)
mocker.patch(f"{EXMS}.get_fee", fee)
patch_exchange(mocker)
mocker.patch('freqtrade.optimize.edge_cli.EdgeCli.start', start_mock)
mocker.patch("freqtrade.optimize.edge_cli.EdgeCli.start", start_mock)
patched_configuration_load_config_file(mocker, edge_conf)
args = [
'edge',
'--config', 'config.json',
'--strategy', CURRENT_TEST_STRATEGY,
"edge",
"--config",
"config.json",
"--strategy",
CURRENT_TEST_STRATEGY,
]
pargs = get_args(args)
start_edge(pargs)
assert log_has('Starting freqtrade in Edge mode', caplog)
assert log_has("Starting freqtrade in Edge mode", caplog)
assert start_mock.call_count == 1
def test_edge_init(mocker, edge_conf) -> None:
patch_exchange(mocker)
edge_conf['stake_amount'] = 20
edge_conf["stake_amount"] = 20
edge_cli = EdgeCli(edge_conf)
assert edge_cli.config == edge_conf
assert edge_cli.config['stake_amount'] == 'unlimited'
assert edge_cli.config["stake_amount"] == "unlimited"
assert callable(edge_cli.edge.calculate)
assert edge_cli.strategy.bot_started is True
def test_edge_init_fee(mocker, edge_conf) -> None:
patch_exchange(mocker)
edge_conf['fee'] = 0.01234
edge_conf['stake_amount'] = 20
fee_mock = mocker.patch(f'{EXMS}.get_fee', return_value=0.5)
edge_conf["fee"] = 0.01234
edge_conf["stake_amount"] = 20
fee_mock = mocker.patch(f"{EXMS}.get_fee", return_value=0.5)
edge_cli = EdgeCli(edge_conf)
assert edge_cli.edge.fee == 0.01234
assert fee_mock.call_count == 0
def test_edge_start(mocker, edge_conf) -> None:
mock_calculate = mocker.patch('freqtrade.edge.edge_positioning.Edge.calculate',
return_value=True)
table_mock = mocker.patch('freqtrade.optimize.edge_cli.generate_edge_table')
mock_calculate = mocker.patch(
"freqtrade.edge.edge_positioning.Edge.calculate", return_value=True
)
table_mock = mocker.patch("freqtrade.optimize.edge_cli.generate_edge_table")
patch_exchange(mocker)
edge_conf['stake_amount'] = 20
edge_conf["stake_amount"] = 20
edge_cli = EdgeCli(edge_conf)
edge_cli.start()