From 4f5d74d74ec4b3af1a96b559b2a6c9275e732cc0 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Wed, 25 Aug 2021 14:27:55 -0600 Subject: [PATCH] separated test_leverage into test_interest and test_liquidation_price, and paramaterized tests --- tests/leverage/test_interest.py | 37 +++++++ tests/leverage/test_leverage.py | 123 ----------------------- tests/leverage/test_liquidation_price.py | 112 +++++++++++++++++++++ 3 files changed, 149 insertions(+), 123 deletions(-) create mode 100644 tests/leverage/test_interest.py delete mode 100644 tests/leverage/test_leverage.py create mode 100644 tests/leverage/test_liquidation_price.py diff --git a/tests/leverage/test_interest.py b/tests/leverage/test_interest.py new file mode 100644 index 000000000..7b7ca0f9b --- /dev/null +++ b/tests/leverage/test_interest.py @@ -0,0 +1,37 @@ +from decimal import Decimal +from math import isclose + +import pytest + +from freqtrade.leverage import interest + + +ten_mins = Decimal(1/6) +five_hours = Decimal(5.0) +twentyfive_hours = Decimal(25.0) + + +@pytest.mark.parametrize('exchange,interest_rate,hours,expected', [ + ('binance', 0.0005, ten_mins, 0.00125), + ('binance', 0.00025, ten_mins, 0.000625), + ('binance', 0.00025, five_hours, 0.003125), + ('binance', 0.00025, twentyfive_hours, 0.015625), + # Kraken + ('kraken', 0.0005, ten_mins, 0.06), + ('kraken', 0.00025, ten_mins, 0.03), + ('kraken', 0.00025, five_hours, 0.045), + ('kraken', 0.00025, twentyfive_hours, 0.12), + # FTX + # TODO-lev: - implement FTX tests + # ('ftx', Decimal(0.0005), ten_mins, 0.06), + # ('ftx', Decimal(0.0005), five_hours, 0.045), +]) +def test_interest(exchange, interest_rate, hours, expected): + borrowed = Decimal(60.0) + + assert isclose(interest( + exchange_name=exchange, + borrowed=borrowed, + rate=Decimal(interest_rate), + hours=hours + ), expected) diff --git a/tests/leverage/test_leverage.py b/tests/leverage/test_leverage.py deleted file mode 100644 index 6d8d1f825..000000000 --- a/tests/leverage/test_leverage.py +++ /dev/null @@ -1,123 +0,0 @@ -from decimal import Decimal -from math import isclose - -import pytest - -from freqtrade.enums import Collateral, TradingMode -from freqtrade.leverage import interest, liquidation_price - - -# from freqtrade.exceptions import OperationalException -binance = "binance" -kraken = "kraken" -ftx = "ftx" -other = "bittrex" - - -def test_liquidation_price(): - - spot = TradingMode.SPOT - margin = TradingMode.MARGIN - futures = TradingMode.FUTURES - - cross = Collateral.CROSS - isolated = Collateral.ISOLATED - - # NONE - assert liquidation_price(exchange_name=other, trading_mode=spot) is None - assert liquidation_price(exchange_name=other, trading_mode=margin, - collateral=cross) is None - assert liquidation_price(exchange_name=other, trading_mode=margin, - collateral=isolated) is None - assert liquidation_price( - exchange_name=other, trading_mode=futures, collateral=cross) is None - assert liquidation_price(exchange_name=other, trading_mode=futures, - collateral=isolated) is None - - # Binance - assert liquidation_price(exchange_name=binance, trading_mode=spot) is None - assert liquidation_price(exchange_name=binance, trading_mode=spot, - collateral=cross) is None - assert liquidation_price(exchange_name=binance, trading_mode=spot, - collateral=isolated) is None - # TODO-lev: Uncomment these assertions and make them real calculation tests - # TODO-lev: Replace 1.0 with real value - # assert liquidation_price( - # exchange_name=binance, - # trading_mode=margin, - # collateral=cross - # ) == 1.0 - # assert liquidation_price( - # exchange_name=binance, - # trading_mode=margin, - # collateral=isolated - # ) == 1.0 - # assert liquidation_price( - # exchange_name=binance, - # trading_mode=futures, - # collateral=cross - # ) == 1.0 - - # Binance supports isolated margin, but freqtrade likely won't for a while on Binance - # liquidation_price(exchange_name=binance, trading_mode=margin, collateral=isolated) - # assert exception thrown #TODO-lev: Check that exception is thrown - - # Kraken - assert liquidation_price(exchange_name=kraken, trading_mode=spot) is None - assert liquidation_price(exchange_name=kraken, trading_mode=spot, collateral=cross) is None - assert liquidation_price(exchange_name=kraken, trading_mode=spot, - collateral=isolated) is None - # TODO-lev: Uncomment these assertions and make them real calculation tests - # assert liquidation_price(kraken, trading_mode=margin, collateral=cross) == 1.0 - # assert liquidation_price(kraken, trading_mode=margin, collateral=isolated) == 1.0 - - # liquidation_price(kraken, trading_mode=futures, collateral=cross) - # assert exception thrown #TODO-lev: Check that exception is thrown - - # liquidation_price(kraken, trading_mode=futures, collateral=isolated) - # assert exception thrown #TODO-lev: Check that exception is thrown - - # FTX - assert liquidation_price(ftx, trading_mode=spot) is None - assert liquidation_price(ftx, trading_mode=spot, collateral=cross) is None - assert liquidation_price(ftx, trading_mode=spot, collateral=isolated) is None - # TODO-lev: Uncomment these assertions and make them real calculation tests - # assert liquidation_price(ftx, trading_mode=margin, collateral=cross) == 1.0 - # assert liquidation_price(ftx, trading_mode=margin, collateral=isolated) == 1.0 - - # liquidation_price(ftx, trading_mode=futures, collateral=cross) - # assert exception thrown #TODO-lev: Check that exception is thrown - - # liquidation_price(ftx, trading_mode=futures, collateral=isolated) - # assert exception thrown #TODO-lev: Check that exception is thrown - - -ten_mins = Decimal(1/6) -five_hours = Decimal(5.0) -twentyfive_hours = Decimal(25.0) - - -@pytest.mark.parametrize('exchange,interest_rate,hours,expected', [ - ('binance', 0.0005, ten_mins, 0.00125), - ('binance', 0.00025, ten_mins, 0.000625), - ('binance', 0.00025, five_hours, 0.003125), - ('binance', 0.00025, twentyfive_hours, 0.015625), - # Kraken - ('kraken', 0.0005, ten_mins, 0.06), - ('kraken', 0.00025, ten_mins, 0.03), - ('kraken', 0.00025, five_hours, 0.045), - ('kraken', 0.00025, twentyfive_hours, 0.12), - # FTX - # TODO-lev: - implement FTX tests - # ('ftx', Decimal(0.0005), ten_mins, 0.06), - # ('ftx', Decimal(0.0005), five_hours, 0.045), -]) -def test_interest(exchange, interest_rate, hours, expected): - borrowed = Decimal(60.0) - - assert isclose(interest( - exchange_name=exchange, - borrowed=borrowed, - rate=Decimal(interest_rate), - hours=hours - ), expected) diff --git a/tests/leverage/test_liquidation_price.py b/tests/leverage/test_liquidation_price.py new file mode 100644 index 000000000..e4c8caaf4 --- /dev/null +++ b/tests/leverage/test_liquidation_price.py @@ -0,0 +1,112 @@ +import pytest + +from freqtrade.enums import Collateral, TradingMode +from freqtrade.leverage import liquidation_price + + +# from freqtrade.exceptions import OperationalException + +spot = TradingMode.SPOT +margin = TradingMode.MARGIN +futures = TradingMode.FUTURES + +cross = Collateral.CROSS +isolated = Collateral.ISOLATED + + +@pytest.mark.parametrize('exchange_name,open_rate,is_short,leverage,trading_mode,collateral', [ + # Bittrex + ('bittrex', "2.0", False, "3.0", spot, None), + ('bittrex', "2.0", False, "1.0", spot, cross), + ('bittrex', "2.0", True, "3.0", spot, isolated), + ('bittrex', "2.0", False, "3.0", margin, cross), + ('bittrex', "2.0", False, "3.0", margin, isolated), + ('bittrex', "2.0", False, "3.0", futures, cross), + ('bittrex', "2.0", False, "3.0", futures, isolated), + # Binance + ('binance', "2.0", False, "3.0", spot, None), + ('binance', "2.0", False, "1.0", spot, cross), + ('binance', "2.0", True, "3.0", spot, isolated), + # Kraken + ('kraken', "2.0", False, "3.0", spot, None), + ('kraken', "2.0", True, "3.0", spot, cross), + ('kraken', "2.0", False, "1.0", spot, isolated), + # FTX + ('ftx', "2.0", True, "3.0", spot, None), + ('ftx', "2.0", False, "3.0", spot, cross), + ('ftx', "2.0", False, "3.0", spot, isolated), +]) +def test_liquidation_price_is_none( + exchange_name, + open_rate, + is_short, + leverage, + trading_mode, + collateral +): + assert liquidation_price( + exchange_name, + open_rate, + is_short, + leverage, + trading_mode, + collateral + ) is None + + +@pytest.mark.parametrize('exchange_name,open_rate,is_short,leverage,trading_mode,collateral', [ + # Binance + # Binance supports isolated margin, but freqtrade likely won't for a while on Binance + ('binance', "2.0", True, "3.0", margin, isolated), + # Kraken + ('kraken', "2.0", False, "1.0", margin, isolated), + ('kraken', "2.0", False, "1.0", futures, isolated), + # FTX + ('ftx', "2.0", False, "3.0", margin, isolated), + ('ftx', "2.0", False, "3.0", futures, isolated), +]) +def test_liquidation_price_exception_thrown( + exchange_name, + open_rate, + is_short, + leverage, + trading_mode, + collateral, + result +): + # TODO-lev assert exception is thrown + return # Here to avoid indent error, remove when implemented + + +@pytest.mark.parametrize( + 'exchange_name,open_rate,is_short,leverage,trading_mode,collateral,result', [ + # Binance + ('binance', "2.0", False, "1.0", margin, cross, 1.0), + ('binance', "2.0", False, "1.0", futures, cross, 1.0), + ('binance', "2.0", False, "1.0", futures, isolated, 1.0), + # Kraken + ('kraken', "2.0", True, "3.0", margin, cross, 1.0), + ('kraken', "2.0", True, "3.0", futures, cross, 1.0), + # FTX + ('ftx', "2.0", False, "3.0", margin, cross, 1.0), + ('ftx', "2.0", False, "3.0", futures, cross, 1.0), + ] +) +def test_liquidation_price( + exchange_name, + open_rate, + is_short, + leverage, + trading_mode, + collateral, + result +): + # assert liquidation_price( + # exchange_name, + # open_rate, + # is_short, + # leverage, + # trading_mode, + # collateral + # ) == result + return # Here to avoid indent error