Merge branch 'feat/short' into lev-freqtradebot

This commit is contained in:
Sam Germain 2021-10-03 02:26:41 -06:00
commit 56ff0a95a7
7 changed files with 76 additions and 121 deletions

View File

@ -109,6 +109,7 @@ All freqtrade arguments will be available by running `docker-compose run --rm fr
!!! Warning "`docker-compose` for trade commands"
Trade commands (`freqtrade trade <...>`) should not be ran via `docker-compose run` - but should use `docker-compose up -d` instead.
This makes sure that the container is properly started (including port forwardings) and will make sure that the container will restart after a system reboot.
If you intend to use freqUI, please also ensure to adjust the [configuration accordingly](rest-api.md#configuration-with-docker), otherwise the UI will not be available.
!!! Note "`docker-compose run --rm`"
Including `--rm` will remove the container after completion, and is highly recommended for all modes except trading mode (running with `freqtrade trade` command).

View File

@ -291,6 +291,7 @@ def create_mock_trades_with_leverage(fee, use_db: bool = True):
Trade.query.session.add(trade)
else:
LocalTrade.add_bt_trade(trade)
# Simulate dry_run entries
trade = mock_trade_1(fee, False)
add_trade(trade)
@ -329,6 +330,7 @@ def create_mock_trades_usdt(fee, use_db: bool = True):
Trade.query.session.add(trade)
else:
LocalTrade.add_bt_trade(trade)
# Simulate dry_run entries
trade = mock_trade_usdt_1(fee)
add_trade(trade)
@ -470,7 +472,7 @@ def get_default_conf(testdatadir):
def get_default_conf_usdt(testdatadir):
configuration = get_default_conf(testdatadir)
configuration.update({
"stake_amount": 10.0,
"stake_amount": 60.0,
"stake_currency": "USDT",
"exchange": {
"name": "binance",

View File

@ -329,5 +329,8 @@ def test_fill_leverage_brackets_kraken(default_conf, mocker):
'LTC/USDT': [1],
'LTC/USD': [1],
'XLTCUSDT': [1],
'LTC/ETH': [1]
'LTC/ETH': [1],
'NEO/USDT': [1],
'TKN/USDT': [1],
'XRP/USDT': [1]
}

View File

@ -39,16 +39,25 @@ def hyperopt(hyperopt_conf, mocker):
def hyperopt_results():
return pd.DataFrame(
{
'pair': ['ETH/BTC', 'ETH/BTC', 'ETH/BTC'],
'profit_ratio': [-0.1, 0.2, 0.3],
'profit_abs': [-0.2, 0.4, 0.6],
'trade_duration': [10, 30, 10],
'sell_reason': [SellType.STOP_LOSS, SellType.ROI, SellType.ROI],
'pair': ['ETH/USDT', 'ETH/USDT', 'ETH/USDT', 'ETH/USDT'],
'profit_ratio': [-0.1, 0.2, -0.1, 0.3],
'profit_abs': [-0.2, 0.4, -0.2, 0.6],
'trade_duration': [10, 30, 10, 10],
'amount': [0.1, 0.1, 0.1, 0.1],
'sell_reason': [SellType.STOP_LOSS, SellType.ROI, SellType.STOP_LOSS, SellType.ROI],
'open_date':
[
datetime(2019, 1, 1, 9, 15, 0),
datetime(2019, 1, 2, 8, 55, 0),
datetime(2019, 1, 3, 9, 15, 0),
datetime(2019, 1, 4, 9, 15, 0),
],
'close_date':
[
datetime(2019, 1, 1, 9, 26, 3, 478039),
datetime(2019, 2, 1, 9, 26, 3, 478039),
datetime(2019, 3, 1, 9, 26, 3, 478039)
]
datetime(2019, 1, 1, 9, 25, 0),
datetime(2019, 1, 2, 9, 25, 0),
datetime(2019, 1, 3, 9, 25, 0),
datetime(2019, 1, 4, 9, 25, 0),
],
}
)

View File

@ -35,6 +35,7 @@ def test_hyperoptlossresolver_wrongname(default_conf) -> None:
def test_loss_calculation_prefer_correct_trade_count(hyperopt_conf, hyperopt_results) -> None:
hyperopt_conf.update({'hyperopt_loss': "ShortTradeDurHyperOptLoss"})
hl = HyperOptLossResolver.load_hyperoptloss(hyperopt_conf)
correct = hl.hyperopt_loss_function(hyperopt_results, 600,
datetime(2019, 1, 1), datetime(2019, 5, 1))
@ -50,6 +51,7 @@ def test_loss_calculation_prefer_shorter_trades(hyperopt_conf, hyperopt_results)
resultsb = hyperopt_results.copy()
resultsb.loc[1, 'trade_duration'] = 20
hyperopt_conf.update({'hyperopt_loss': "ShortTradeDurHyperOptLoss"})
hl = HyperOptLossResolver.load_hyperoptloss(hyperopt_conf)
longer = hl.hyperopt_loss_function(hyperopt_results, 100,
datetime(2019, 1, 1), datetime(2019, 5, 1))
@ -64,6 +66,7 @@ def test_loss_calculation_has_limited_profit(hyperopt_conf, hyperopt_results) ->
results_under = hyperopt_results.copy()
results_under['profit_ratio'] = hyperopt_results['profit_ratio'] / 2
hyperopt_conf.update({'hyperopt_loss': "ShortTradeDurHyperOptLoss"})
hl = HyperOptLossResolver.load_hyperoptloss(hyperopt_conf)
correct = hl.hyperopt_loss_function(hyperopt_results, 600,
datetime(2019, 1, 1), datetime(2019, 5, 1))
@ -75,91 +78,28 @@ def test_loss_calculation_has_limited_profit(hyperopt_conf, hyperopt_results) ->
assert under > correct
def test_sharpe_loss_prefers_higher_profits(default_conf, hyperopt_results) -> None:
results_over = hyperopt_results.copy()
results_over['profit_ratio'] = hyperopt_results['profit_ratio'] * 2
results_under = hyperopt_results.copy()
results_under['profit_ratio'] = hyperopt_results['profit_ratio'] / 2
default_conf.update({'hyperopt_loss': 'SharpeHyperOptLoss'})
hl = HyperOptLossResolver.load_hyperoptloss(default_conf)
correct = hl.hyperopt_loss_function(hyperopt_results, len(hyperopt_results),
datetime(2019, 1, 1), datetime(2019, 5, 1))
over = hl.hyperopt_loss_function(results_over, len(hyperopt_results),
datetime(2019, 1, 1), datetime(2019, 5, 1))
under = hl.hyperopt_loss_function(results_under, len(hyperopt_results),
datetime(2019, 1, 1), datetime(2019, 5, 1))
assert over < correct
assert under > correct
def test_sharpe_loss_daily_prefers_higher_profits(default_conf, hyperopt_results) -> None:
results_over = hyperopt_results.copy()
results_over['profit_ratio'] = hyperopt_results['profit_ratio'] * 2
results_under = hyperopt_results.copy()
results_under['profit_ratio'] = hyperopt_results['profit_ratio'] / 2
default_conf.update({'hyperopt_loss': 'SharpeHyperOptLossDaily'})
hl = HyperOptLossResolver.load_hyperoptloss(default_conf)
correct = hl.hyperopt_loss_function(hyperopt_results, len(hyperopt_results),
datetime(2019, 1, 1), datetime(2019, 5, 1))
over = hl.hyperopt_loss_function(results_over, len(hyperopt_results),
datetime(2019, 1, 1), datetime(2019, 5, 1))
under = hl.hyperopt_loss_function(results_under, len(hyperopt_results),
datetime(2019, 1, 1), datetime(2019, 5, 1))
assert over < correct
assert under > correct
def test_sortino_loss_prefers_higher_profits(default_conf, hyperopt_results) -> None:
results_over = hyperopt_results.copy()
results_over['profit_ratio'] = hyperopt_results['profit_ratio'] * 2
results_under = hyperopt_results.copy()
results_under['profit_ratio'] = hyperopt_results['profit_ratio'] / 2
default_conf.update({'hyperopt_loss': 'SortinoHyperOptLoss'})
hl = HyperOptLossResolver.load_hyperoptloss(default_conf)
correct = hl.hyperopt_loss_function(hyperopt_results, len(hyperopt_results),
datetime(2019, 1, 1), datetime(2019, 5, 1))
over = hl.hyperopt_loss_function(results_over, len(hyperopt_results),
datetime(2019, 1, 1), datetime(2019, 5, 1))
under = hl.hyperopt_loss_function(results_under, len(hyperopt_results),
datetime(2019, 1, 1), datetime(2019, 5, 1))
assert over < correct
assert under > correct
def test_sortino_loss_daily_prefers_higher_profits(default_conf, hyperopt_results) -> None:
results_over = hyperopt_results.copy()
results_over['profit_ratio'] = hyperopt_results['profit_ratio'] * 2
results_under = hyperopt_results.copy()
results_under['profit_ratio'] = hyperopt_results['profit_ratio'] / 2
default_conf.update({'hyperopt_loss': 'SortinoHyperOptLossDaily'})
hl = HyperOptLossResolver.load_hyperoptloss(default_conf)
correct = hl.hyperopt_loss_function(hyperopt_results, len(hyperopt_results),
datetime(2019, 1, 1), datetime(2019, 5, 1))
over = hl.hyperopt_loss_function(results_over, len(hyperopt_results),
datetime(2019, 1, 1), datetime(2019, 5, 1))
under = hl.hyperopt_loss_function(results_under, len(hyperopt_results),
datetime(2019, 1, 1), datetime(2019, 5, 1))
assert over < correct
assert under > correct
def test_onlyprofit_loss_prefers_higher_profits(default_conf, hyperopt_results) -> None:
@pytest.mark.parametrize('lossfunction', [
"OnlyProfitHyperOptLoss",
"SortinoHyperOptLoss",
"SortinoHyperOptLossDaily",
"SharpeHyperOptLoss",
"SharpeHyperOptLossDaily",
])
def test_loss_functions_better_profits(default_conf, hyperopt_results, lossfunction) -> None:
results_over = hyperopt_results.copy()
results_over['profit_abs'] = hyperopt_results['profit_abs'] * 2
results_over['profit_ratio'] = hyperopt_results['profit_ratio'] * 2
results_under = hyperopt_results.copy()
results_under['profit_abs'] = hyperopt_results['profit_abs'] / 2
results_under['profit_ratio'] = hyperopt_results['profit_ratio'] / 2
default_conf.update({'hyperopt_loss': 'OnlyProfitHyperOptLoss'})
default_conf.update({'hyperopt_loss': lossfunction})
hl = HyperOptLossResolver.load_hyperoptloss(default_conf)
correct = hl.hyperopt_loss_function(hyperopt_results, len(hyperopt_results),
datetime(2019, 1, 1), datetime(2019, 5, 1))
over = hl.hyperopt_loss_function(results_over, len(hyperopt_results),
over = hl.hyperopt_loss_function(results_over, len(results_over),
datetime(2019, 1, 1), datetime(2019, 5, 1))
under = hl.hyperopt_loss_function(results_under, len(hyperopt_results),
under = hl.hyperopt_loss_function(results_under, len(results_under),
datetime(2019, 1, 1), datetime(2019, 5, 1))
assert over < correct
assert under > correct

View File

@ -9,13 +9,13 @@ from freqtrade.strategy import (merge_informative_pair, stoploss_from_absolute,
timeframe_to_minutes)
def generate_test_data(timeframe: str, size: int):
def generate_test_data(timeframe: str, size: int, start: str = '2020-07-05'):
np.random.seed(42)
tf_mins = timeframe_to_minutes(timeframe)
base = np.random.normal(20, 2, size=size)
date = pd.period_range('2020-07-05', periods=size, freq=f'{tf_mins}min').to_timestamp()
date = pd.date_range(start, periods=size, freq=f'{tf_mins}min', tz='UTC')
df = pd.DataFrame({
'date': date,
'open': base,

View File

@ -135,14 +135,14 @@ def test_get_trade_stake_amount(default_conf_usdt, mocker) -> None:
@pytest.mark.parametrize("amend_last,wallet,max_open,lsamr,expected", [
(False, 20, 2, 0.5, [10, None]),
(True, 20, 2, 0.5, [10, 9.8]),
(False, 30, 3, 0.5, [10, 10, None]),
(True, 30, 3, 0.5, [10, 10, 9.7]),
(False, 22, 3, 0.5, [10, 10, None]),
(True, 22, 3, 0.5, [10, 10, 0.0]),
(True, 27, 3, 0.5, [10, 10, 6.73]),
(True, 22, 3, 1, [10, 10, 0.0]),
(False, 120, 2, 0.5, [60, None]),
(True, 120, 2, 0.5, [60, 58.8]),
(False, 180, 3, 0.5, [60, 60, None]),
(True, 180, 3, 0.5, [60, 60, 58.2]),
(False, 122, 3, 0.5, [60, 60, None]),
(True, 122, 3, 0.5, [60, 60, 0.0]),
(True, 167, 3, 0.5, [60, 60, 45.33]),
(True, 122, 3, 1, [60, 60, 0.0]),
])
def test_check_available_stake_amount(
default_conf_usdt, ticker_usdt, mocker, fee, limit_buy_order_usdt_open,
@ -267,7 +267,7 @@ def test_total_open_trades_stakes(mocker, default_conf_usdt, ticker_usdt, fee) -
trade = Trade.query.first()
assert trade is not None
assert trade.stake_amount == 10.0
assert trade.stake_amount == 60.0
assert trade.is_open
assert trade.open_date is not None
@ -275,11 +275,11 @@ def test_total_open_trades_stakes(mocker, default_conf_usdt, ticker_usdt, fee) -
trade = Trade.query.order_by(Trade.id.desc()).first()
assert trade is not None
assert trade.stake_amount == 10.0
assert trade.stake_amount == 60.0
assert trade.is_open
assert trade.open_date is not None
assert Trade.total_open_trades_stakes() == 20.0
assert Trade.total_open_trades_stakes() == 120.0
@pytest.mark.parametrize("is_short,open_rate", [
@ -306,7 +306,7 @@ def test_create_trade(default_conf_usdt, ticker_usdt, limit_order,
trade = Trade.query.first()
trade.is_short = is_short
assert trade is not None
assert trade.stake_amount == 10.0
assert trade.stake_amount == 60.0
assert trade.is_open
assert trade.open_date is not None
assert trade.exchange == 'binance'
@ -487,7 +487,7 @@ def test_create_trades_multiple_trades(
patch_exchange(mocker)
default_conf_usdt['max_open_trades'] = max_open
default_conf_usdt['tradable_balance_ratio'] = tradable_balance_ratio
default_conf_usdt['dry_run_wallet'] = 10.0 * max_open
default_conf_usdt['dry_run_wallet'] = 60.0 * max_open
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
@ -566,10 +566,10 @@ def test_process_trade_creation(default_conf_usdt, ticker_usdt, limit_order, lim
assert trade.open_date is not None
assert trade.exchange == 'binance'
assert trade.open_rate == 2.0
assert trade.amount == 5.0
assert trade.amount == 30.0
assert log_has(
'Long signal found: about create a new trade for ETH/USDT with stake_amount: 10.0 ...',
'Long signal found: about create a new trade for ETH/USDT with stake_amount: 60.0 ...',
caplog
)
@ -1330,7 +1330,7 @@ def test_handle_stoploss_on_exchange_trailing(mocker, default_conf_usdt, fee, is
cancel_order_mock.assert_called_once_with(100, 'ETH/USDT')
stoploss_order_mock.assert_called_once_with(
amount=4.56621004,
amount=27.39726027,
pair='ETH/USDT',
order_types=freqtrade.strategy.order_types,
stop_price=4.4 * 0.95,
@ -1533,7 +1533,7 @@ def test_handle_stoploss_on_exchange_custom_stop(
cancel_order_mock.assert_called_once_with(100, 'ETH/USDT')
stoploss_order_mock.assert_called_once_with(
amount=5.26315789,
amount=31.57894736,
pair='ETH/USDT',
order_types=freqtrade.strategy.order_types,
stop_price=4.4 * 0.96,
@ -2576,7 +2576,7 @@ def test_check_handle_timedout_exception(default_conf_usdt, ticker_usdt, open_tr
caplog.clear()
freqtrade.check_handle_timedout()
assert log_has_re(r"Cannot query order for Trade\(id=1, pair=ADA/USDT, amount=30.00000000, "
f"is_short=False, leverage=1.0, "
r"is_short=False, leverage=1.0, "
r"open_rate=2.00000000, open_since="
f"{open_trade_usdt.open_date.strftime('%Y-%m-%d %H:%M:%S')}"
r"\) due to Traceback \(most recent call last\):\n*",
@ -2805,11 +2805,11 @@ def test_execute_trade_exit_up(default_conf_usdt, ticker_usdt, fee, ticker_usdt_
'pair': 'ETH/USDT',
'gain': 'profit',
'limit': 2.2,
'amount': 5.0,
'amount': 30.0,
'order_type': 'limit',
'open_rate': 2.0,
'current_rate': 2.3,
'profit_amount': 0.9475,
'profit_amount': 5.685,
'profit_ratio': 0.09451372,
'stake_currency': 'USDT',
'fiat_currency': 'USD',
@ -2859,11 +2859,11 @@ def test_execute_trade_exit_down(default_conf_usdt, ticker_usdt, fee, ticker_usd
'pair': 'ETH/USDT',
'gain': 'loss',
'limit': 2.01,
'amount': 5.0,
'amount': 30.0,
'order_type': 'limit',
'open_rate': 2.0,
'current_rate': 2.0,
'profit_amount': -0.000125,
'profit_amount': -0.00075,
'profit_ratio': -1.247e-05,
'stake_currency': 'USDT',
'fiat_currency': 'USD',
@ -2927,11 +2927,11 @@ def test_execute_trade_exit_custom_exit_price(default_conf_usdt, ticker_usdt, fe
'pair': 'ETH/USDT',
'gain': 'profit',
'limit': 2.25,
'amount': 5.0,
'amount': 30.0,
'order_type': 'limit',
'open_rate': 2.0,
'current_rate': 2.3,
'profit_amount': 1.196875,
'profit_amount': 7.18125,
'profit_ratio': 0.11938903,
'stake_currency': 'USDT',
'fiat_currency': 'USD',
@ -2988,11 +2988,11 @@ def test_execute_trade_exit_down_stoploss_on_exchange_dry_run(
'pair': 'ETH/USDT',
'gain': 'loss',
'limit': 1.98,
'amount': 5.0,
'amount': 30.0,
'order_type': 'limit',
'open_rate': 2.0,
'current_rate': 2.0,
'profit_amount': -0.14975,
'profit_amount': -0.8985,
'profit_ratio': -0.01493766,
'stake_currency': 'USDT',
'fiat_currency': 'USD',
@ -3210,11 +3210,11 @@ def test_execute_trade_exit_market_order(default_conf_usdt, ticker_usdt, fee, is
'pair': 'ETH/USDT',
'gain': 'profit',
'limit': 2.2,
'amount': 5.0,
'amount': 30.0,
'order_type': 'market',
'open_rate': 2.0,
'current_rate': 2.3,
'profit_amount': 0.9475,
'profit_amount': 5.685,
'profit_ratio': 0.09451372,
'stake_currency': 'USDT',
'fiat_currency': 'USD',
@ -3999,7 +3999,7 @@ def test_order_book_depth_of_market(
assert trade is None
else:
assert trade is not None
assert trade.stake_amount == 10.0
assert trade.stake_amount == 60.0
assert trade.is_open
assert trade.open_date is not None
assert trade.exchange == 'binance'
@ -4148,7 +4148,7 @@ def test_sync_wallet_dry_run(mocker, default_conf_usdt, ticker_usdt, fee, limit_
caplog):
default_conf_usdt['dry_run'] = True
# Initialize to 2 times stake amount
default_conf_usdt['dry_run_wallet'] = 20.0
default_conf_usdt['dry_run_wallet'] = 120.0
default_conf_usdt['max_open_trades'] = 2
default_conf_usdt['tradable_balance_ratio'] = 1.0
patch_exchange(mocker)
@ -4161,7 +4161,7 @@ def test_sync_wallet_dry_run(mocker, default_conf_usdt, ticker_usdt, fee, limit_
bot = get_patched_freqtradebot(mocker, default_conf_usdt)
patch_get_signal(bot)
assert bot.wallets.get_free('USDT') == 20.0
assert bot.wallets.get_free('USDT') == 120.0
n = bot.enter_positions()
assert n == 2
@ -4172,7 +4172,7 @@ def test_sync_wallet_dry_run(mocker, default_conf_usdt, ticker_usdt, fee, limit_
n = bot.enter_positions()
assert n == 0
assert log_has_re(r"Unable to create trade for XRP/USDT: "
r"Available balance \(0.0 USDT\) is lower than stake amount \(10.0 USDT\)",
r"Available balance \(0.0 USDT\) is lower than stake amount \(60.0 USDT\)",
caplog)