freqtrade_origin/tests/conftest_trades.py

519 lines
14 KiB
Python
Raw Permalink Normal View History

2020-12-09 19:26:11 +00:00
from datetime import datetime, timedelta, timezone
from freqtrade.persistence.models import Order, Trade
2021-07-04 06:11:59 +00:00
MOCK_TRADE_COUNT = 6
2020-09-10 05:40:19 +00:00
2022-04-06 01:02:13 +00:00
def entry_side(is_short: bool):
2021-09-15 03:08:15 +00:00
return "sell" if is_short else "buy"
def exit_side(is_short: bool):
return "buy" if is_short else "sell"
def direc(is_short: bool):
return "short" if is_short else "long"
def mock_order_1(is_short: bool):
2020-09-07 04:48:34 +00:00
return {
2021-09-15 03:08:15 +00:00
'id': f'1234_{direc(is_short)}',
2020-09-07 04:48:34 +00:00
'symbol': 'ETH/BTC',
'status': 'closed',
2022-04-06 01:02:13 +00:00
'side': entry_side(is_short),
2020-09-07 04:48:34 +00:00
'type': 'limit',
'price': 0.123,
'average': 0.123,
2020-09-07 04:48:34 +00:00
'amount': 123.0,
'filled': 123.0,
2022-06-18 14:53:25 +00:00
'cost': 15.129,
2020-09-07 04:48:34 +00:00
'remaining': 0.0,
}
2021-09-15 03:08:15 +00:00
def mock_trade_1(fee, is_short: bool):
trade = Trade(
pair='ETH/BTC',
stake_amount=0.001,
amount=123.0,
amount_requested=123.0,
fee_open=fee.return_value,
fee_close=fee.return_value,
is_open=True,
2021-03-13 14:46:20 +00:00
open_date=datetime.now(tz=timezone.utc) - timedelta(minutes=17),
open_rate=0.123,
2021-04-20 10:54:22 +00:00
exchange='binance',
2021-09-15 03:08:15 +00:00
open_order_id=f'dry_run_buy_{direc(is_short)}_12345',
strategy='StrategyTestV3',
2021-01-01 18:38:28 +00:00
timeframe=5,
2021-09-15 03:08:15 +00:00
is_short=is_short
)
2022-04-06 01:02:13 +00:00
o = Order.parse_from_ccxt_object(mock_order_1(is_short), 'ETH/BTC', entry_side(is_short))
2020-09-07 04:48:34 +00:00
trade.orders.append(o)
return trade
2021-09-15 03:08:15 +00:00
def mock_order_2(is_short: bool):
2020-09-07 04:48:34 +00:00
return {
2021-09-15 03:08:15 +00:00
'id': f'1235_{direc(is_short)}',
2020-09-07 04:48:34 +00:00
'symbol': 'ETC/BTC',
'status': 'closed',
2022-04-06 01:02:13 +00:00
'side': entry_side(is_short),
2020-09-07 04:48:34 +00:00
'type': 'limit',
'price': 0.123,
'amount': 123.0,
'filled': 123.0,
2022-06-18 14:53:25 +00:00
'cost': 15.129,
'remaining': 0.0,
2020-09-07 04:48:34 +00:00
}
2021-09-15 03:08:15 +00:00
def mock_order_2_sell(is_short: bool):
2020-09-07 04:48:34 +00:00
return {
2021-09-15 03:08:15 +00:00
'id': f'12366_{direc(is_short)}',
2020-09-07 04:48:34 +00:00
'symbol': 'ETC/BTC',
'status': 'closed',
2021-09-15 03:08:15 +00:00
'side': exit_side(is_short),
2020-09-07 04:48:34 +00:00
'type': 'limit',
'price': 0.128,
'amount': 123.0,
'filled': 123.0,
2022-06-18 14:53:25 +00:00
'cost': 15.129,
2020-09-07 04:48:34 +00:00
'remaining': 0.0,
}
2021-09-15 03:08:15 +00:00
def mock_trade_2(fee, is_short: bool):
"""
Closed trade...
"""
trade = Trade(
pair='ETC/BTC',
stake_amount=0.001,
amount=123.0,
amount_requested=123.0,
fee_open=fee.return_value,
fee_close=fee.return_value,
open_rate=0.123,
close_rate=0.128,
2021-09-15 03:08:15 +00:00
close_profit=-0.005 if is_short else 0.005,
close_profit_abs=-0.005584127 if is_short else 0.000584127,
2021-04-20 10:54:22 +00:00
exchange='binance',
is_open=False,
2021-09-15 03:08:15 +00:00
open_order_id=f'dry_run_sell_{direc(is_short)}_12345',
strategy='StrategyTestV3',
2021-01-01 18:38:28 +00:00
timeframe=5,
2021-11-21 08:51:16 +00:00
enter_tag='TEST1',
2022-03-24 19:33:47 +00:00
exit_reason='sell_signal',
2020-12-09 19:26:11 +00:00
open_date=datetime.now(tz=timezone.utc) - timedelta(minutes=20),
close_date=datetime.now(tz=timezone.utc) - timedelta(minutes=2),
2021-09-15 03:08:15 +00:00
is_short=is_short
)
2022-04-06 01:02:13 +00:00
o = Order.parse_from_ccxt_object(mock_order_2(is_short), 'ETC/BTC', entry_side(is_short))
2020-09-07 04:48:34 +00:00
trade.orders.append(o)
2021-09-15 03:08:15 +00:00
o = Order.parse_from_ccxt_object(mock_order_2_sell(is_short), 'ETC/BTC', exit_side(is_short))
2020-09-07 04:48:34 +00:00
trade.orders.append(o)
return trade
2021-09-15 03:08:15 +00:00
def mock_order_3(is_short: bool):
2020-09-07 04:48:34 +00:00
return {
2021-09-15 03:08:15 +00:00
'id': f'41231a12a_{direc(is_short)}',
2020-09-07 04:48:34 +00:00
'symbol': 'XRP/BTC',
'status': 'closed',
2022-04-06 01:02:13 +00:00
'side': entry_side(is_short),
2020-09-07 04:48:34 +00:00
'type': 'limit',
'price': 0.05,
'amount': 123.0,
'filled': 123.0,
2022-06-18 14:53:25 +00:00
'cost': 15.129,
'remaining': 0.0,
2020-09-07 04:48:34 +00:00
}
2021-09-15 03:08:15 +00:00
def mock_order_3_sell(is_short: bool):
2020-09-07 04:48:34 +00:00
return {
2021-09-15 03:08:15 +00:00
'id': f'41231a666a_{direc(is_short)}',
2020-09-07 04:48:34 +00:00
'symbol': 'XRP/BTC',
'status': 'closed',
2021-09-15 03:08:15 +00:00
'side': exit_side(is_short),
2020-09-07 05:41:58 +00:00
'type': 'stop_loss_limit',
2020-09-07 04:48:34 +00:00
'price': 0.06,
2020-09-07 05:41:58 +00:00
'average': 0.06,
'amount': 123.0,
'filled': 123.0,
2022-06-18 14:53:25 +00:00
'cost': 15.129,
'remaining': 0.0,
2020-09-07 04:48:34 +00:00
}
2021-09-15 03:08:15 +00:00
def mock_trade_3(fee, is_short: bool):
"""
Closed trade
"""
trade = Trade(
pair='XRP/BTC',
stake_amount=0.001,
amount=123.0,
2021-01-01 18:38:28 +00:00
amount_requested=123.0,
fee_open=fee.return_value,
fee_close=fee.return_value,
open_rate=0.05,
close_rate=0.06,
2021-09-15 03:08:15 +00:00
close_profit=-0.01 if is_short else 0.01,
close_profit_abs=-0.001155 if is_short else 0.000155,
2021-04-20 10:54:22 +00:00
exchange='binance',
is_open=False,
strategy='StrategyTestV3',
2021-01-01 18:38:28 +00:00
timeframe=5,
2022-03-24 19:33:47 +00:00
exit_reason='roi',
2020-12-09 19:26:11 +00:00
open_date=datetime.now(tz=timezone.utc) - timedelta(minutes=20),
close_date=datetime.now(tz=timezone.utc),
2021-09-15 03:08:15 +00:00
is_short=is_short
)
2022-04-06 01:02:13 +00:00
o = Order.parse_from_ccxt_object(mock_order_3(is_short), 'XRP/BTC', entry_side(is_short))
trade.orders.append(o)
2021-09-15 03:08:15 +00:00
o = Order.parse_from_ccxt_object(mock_order_3_sell(is_short), 'XRP/BTC', exit_side(is_short))
trade.orders.append(o)
return trade
2021-09-15 03:08:15 +00:00
def mock_order_4(is_short: bool):
2020-09-07 04:48:34 +00:00
return {
2021-09-15 03:08:15 +00:00
'id': f'prod_buy_{direc(is_short)}_12345',
2020-09-07 04:48:34 +00:00
'symbol': 'ETC/BTC',
'status': 'open',
2022-04-06 01:02:13 +00:00
'side': entry_side(is_short),
2020-09-07 04:48:34 +00:00
'type': 'limit',
'price': 0.123,
'amount': 123.0,
'filled': 0.0,
2022-06-18 14:53:25 +00:00
'cost': 15.129,
2020-09-07 04:48:34 +00:00
'remaining': 123.0,
}
2021-09-15 03:08:15 +00:00
def mock_trade_4(fee, is_short: bool):
"""
Simulate prod entry
"""
trade = Trade(
pair='ETC/BTC',
stake_amount=0.001,
amount=123.0,
amount_requested=124.0,
fee_open=fee.return_value,
fee_close=fee.return_value,
2021-03-13 14:46:20 +00:00
open_date=datetime.now(tz=timezone.utc) - timedelta(minutes=14),
is_open=True,
open_rate=0.123,
2021-04-20 10:54:22 +00:00
exchange='binance',
2021-09-15 03:08:15 +00:00
open_order_id=f'prod_buy_{direc(is_short)}_12345',
strategy='StrategyTestV3',
2021-01-01 18:38:28 +00:00
timeframe=5,
2022-08-04 14:28:36 +00:00
is_short=is_short,
stop_loss_pct=0.10
)
2022-04-06 01:02:13 +00:00
o = Order.parse_from_ccxt_object(mock_order_4(is_short), 'ETC/BTC', entry_side(is_short))
trade.orders.append(o)
2020-09-07 04:48:34 +00:00
return trade
2020-09-09 05:01:43 +00:00
2021-09-15 03:08:15 +00:00
def mock_order_5(is_short: bool):
2020-09-09 05:01:43 +00:00
return {
2021-09-15 03:08:15 +00:00
'id': f'prod_buy_{direc(is_short)}_3455',
2020-09-09 05:01:43 +00:00
'symbol': 'XRP/BTC',
'status': 'closed',
2022-04-06 01:02:13 +00:00
'side': entry_side(is_short),
2020-09-09 05:01:43 +00:00
'type': 'limit',
'price': 0.123,
'amount': 123.0,
'filled': 123.0,
2022-06-18 14:53:25 +00:00
'cost': 15.129,
2020-09-09 05:01:43 +00:00
'remaining': 0.0,
}
2021-09-15 03:08:15 +00:00
def mock_order_5_stoploss(is_short: bool):
2020-09-09 05:01:43 +00:00
return {
2021-09-15 03:08:15 +00:00
'id': f'prod_stoploss_{direc(is_short)}_3455',
2020-09-09 05:01:43 +00:00
'symbol': 'XRP/BTC',
'status': 'open',
2021-09-15 03:08:15 +00:00
'side': exit_side(is_short),
2020-09-09 05:01:43 +00:00
'type': 'stop_loss_limit',
'price': 0.123,
'amount': 123.0,
'filled': 0.0,
2022-06-18 14:53:25 +00:00
'cost': 0.0,
2020-09-09 05:01:43 +00:00
'remaining': 123.0,
}
2021-09-15 03:08:15 +00:00
def mock_trade_5(fee, is_short: bool):
2020-09-09 05:01:43 +00:00
"""
Simulate prod entry with stoploss
"""
trade = Trade(
pair='XRP/BTC',
stake_amount=0.001,
amount=123.0,
amount_requested=124.0,
fee_open=fee.return_value,
fee_close=fee.return_value,
2021-03-13 14:46:20 +00:00
open_date=datetime.now(tz=timezone.utc) - timedelta(minutes=12),
is_open=True,
2020-09-09 05:01:43 +00:00
open_rate=0.123,
2021-04-20 10:54:22 +00:00
exchange='binance',
2020-09-09 05:01:43 +00:00
strategy='SampleStrategy',
2021-11-21 08:51:16 +00:00
enter_tag='TEST1',
2021-09-15 03:08:15 +00:00
stoploss_order_id=f'prod_stoploss_{direc(is_short)}_3455',
2021-04-07 15:10:20 +00:00
timeframe=5,
2022-08-04 14:28:36 +00:00
is_short=is_short,
stop_loss_pct=0.10,
2020-09-09 05:01:43 +00:00
)
2022-04-06 01:02:13 +00:00
o = Order.parse_from_ccxt_object(mock_order_5(is_short), 'XRP/BTC', entry_side(is_short))
2020-09-10 05:40:19 +00:00
trade.orders.append(o)
2021-09-15 03:08:15 +00:00
o = Order.parse_from_ccxt_object(mock_order_5_stoploss(is_short), 'XRP/BTC', 'stoploss')
2020-09-10 05:40:19 +00:00
trade.orders.append(o)
return trade
2021-09-15 03:08:15 +00:00
def mock_order_6(is_short: bool):
2020-09-10 05:40:19 +00:00
return {
2021-09-15 03:08:15 +00:00
'id': f'prod_buy_{direc(is_short)}_6',
2020-09-10 05:40:19 +00:00
'symbol': 'LTC/BTC',
'status': 'closed',
2022-04-06 01:02:13 +00:00
'side': entry_side(is_short),
2020-09-10 05:40:19 +00:00
'type': 'limit',
'price': 0.15,
'amount': 2.0,
'filled': 2.0,
2022-06-18 14:53:25 +00:00
'cost': 0.3,
2020-09-10 05:40:19 +00:00
'remaining': 0.0,
}
2021-09-15 03:08:15 +00:00
def mock_order_6_sell(is_short: bool):
2020-09-10 05:40:19 +00:00
return {
2021-09-15 03:08:15 +00:00
'id': f'prod_sell_{direc(is_short)}_6',
2020-09-10 05:40:19 +00:00
'symbol': 'LTC/BTC',
'status': 'open',
2021-09-15 03:08:15 +00:00
'side': exit_side(is_short),
2020-09-10 05:40:19 +00:00
'type': 'limit',
2021-09-15 03:08:15 +00:00
'price': 0.15 if is_short else 0.20,
2020-09-10 05:40:19 +00:00
'amount': 2.0,
'filled': 0.0,
2022-06-18 14:53:25 +00:00
'cost': 0.0,
2020-09-10 05:40:19 +00:00
'remaining': 2.0,
}
2021-09-15 03:08:15 +00:00
def mock_trade_6(fee, is_short: bool):
2020-09-10 05:40:19 +00:00
"""
2021-09-15 03:08:15 +00:00
Simulate prod entry with open exit order
2020-09-10 05:40:19 +00:00
"""
trade = Trade(
pair='LTC/BTC',
stake_amount=0.001,
amount=2.0,
amount_requested=2.0,
2021-03-13 14:46:20 +00:00
open_date=datetime.now(tz=timezone.utc) - timedelta(minutes=5),
2020-09-10 05:40:19 +00:00
fee_open=fee.return_value,
fee_close=fee.return_value,
is_open=True,
2020-09-10 05:40:19 +00:00
open_rate=0.15,
2021-04-20 10:54:22 +00:00
exchange='binance',
2020-09-10 05:40:19 +00:00
strategy='SampleStrategy',
2021-11-21 08:51:16 +00:00
enter_tag='TEST2',
2021-09-15 03:08:15 +00:00
open_order_id=f"prod_sell_{direc(is_short)}_6",
2021-04-07 15:10:20 +00:00
timeframe=5,
2021-11-14 08:52:38 +00:00
is_short=is_short
2020-09-10 05:40:19 +00:00
)
2022-04-06 01:02:13 +00:00
o = Order.parse_from_ccxt_object(mock_order_6(is_short), 'LTC/BTC', entry_side(is_short))
2020-09-09 05:01:43 +00:00
trade.orders.append(o)
2021-09-15 03:08:15 +00:00
o = Order.parse_from_ccxt_object(mock_order_6_sell(is_short), 'LTC/BTC', exit_side(is_short))
2020-09-09 05:01:43 +00:00
trade.orders.append(o)
return trade
def short_order():
return {
2021-07-02 08:48:30 +00:00
'id': '1236',
'symbol': 'ETC/BTC',
'status': 'closed',
'side': 'sell',
'type': 'limit',
'price': 0.123,
'amount': 123.0,
'filled': 123.0,
2022-06-18 14:53:25 +00:00
'cost': 15.129,
'remaining': 0.0,
}
def exit_short_order():
return {
2021-07-02 08:48:30 +00:00
'id': '12367',
'symbol': 'ETC/BTC',
'status': 'closed',
'side': 'buy',
'type': 'limit',
'price': 0.128,
'amount': 123.0,
'filled': 123.0,
2022-06-18 14:53:25 +00:00
'cost': 15.744,
'remaining': 0.0,
}
def short_trade(fee):
"""
2021-07-02 08:48:30 +00:00
10 minute short limit trade on binance
Short trade
fee: 0.25% base
interest_rate: 0.05% per day
open_rate: 0.123 base
close_rate: 0.128 base
amount: 123.0 crypto
stake_amount: 15.129 base
borrowed: 123.0 crypto
time-periods: 10 minutes(rounds up to 1/24 time-period of 1 day)
interest: borrowed * interest_rate * time-periods
= 123.0 * 0.0005 * 1/24 = 0.0025625 crypto
open_value: (amount * open_rate) - (amount * open_rate * fee)
= (123 * 0.123) - (123 * 0.123 * 0.0025)
= 15.091177499999999
amount_closed: amount + interest = 123 + 0.0025625 = 123.0025625
close_value: (amount_closed * close_rate) + (amount_closed * close_rate * fee)
= (123.0025625 * 0.128) + (123.0025625 * 0.128 * 0.0025)
= 15.78368882
total_profit = open_value - close_value
= 15.091177499999999 - 15.78368882
= -0.6925113200000013
total_profit_percentage = total_profit / stake_amount
= -0.6925113200000013 / 15.129
= -0.04577376693766946
"""
trade = Trade(
pair='ETC/BTC',
2021-07-02 08:48:30 +00:00
stake_amount=15.129,
amount=123.0,
amount_requested=123.0,
fee_open=fee.return_value,
fee_close=fee.return_value,
open_rate=0.123,
2021-07-02 08:48:30 +00:00
# close_rate=0.128,
# close_profit=-0.04577376693766946,
# close_profit_abs=-0.6925113200000013,
exchange='binance',
2021-07-02 08:48:30 +00:00
is_open=True,
open_order_id='dry_run_exit_short_12345',
strategy='DefaultStrategy',
timeframe=5,
2022-03-24 19:33:47 +00:00
exit_reason='sell_signal',
open_date=datetime.now(tz=timezone.utc) - timedelta(minutes=20),
2021-07-02 08:48:30 +00:00
# close_date=datetime.now(tz=timezone.utc) - timedelta(minutes=2),
2021-08-06 07:15:18 +00:00
is_short=True
)
o = Order.parse_from_ccxt_object(short_order(), 'ETC/BTC', 'sell')
trade.orders.append(o)
o = Order.parse_from_ccxt_object(exit_short_order(), 'ETC/BTC', 'sell')
trade.orders.append(o)
return trade
def leverage_order():
return {
2021-07-02 08:48:30 +00:00
'id': '1237',
'symbol': 'DOGE/BTC',
'status': 'closed',
'side': 'buy',
'type': 'limit',
'price': 0.123,
'amount': 123.0,
'filled': 123.0,
'remaining': 0.0,
2022-06-18 14:53:25 +00:00
'cost': 15.129,
'leverage': 5.0
}
def leverage_order_sell():
return {
2021-07-02 08:48:30 +00:00
'id': '12368',
'symbol': 'DOGE/BTC',
'status': 'closed',
'side': 'sell',
'type': 'limit',
'price': 0.128,
'amount': 123.0,
'filled': 123.0,
'remaining': 0.0,
2022-06-18 14:53:25 +00:00
'cost': 15.744,
'leverage': 5.0
}
def leverage_trade(fee):
"""
2021-07-02 08:48:30 +00:00
5 hour short limit trade on kraken
Short trade
fee: 0.25% base
interest_rate: 0.05% per day
open_rate: 0.123 base
close_rate: 0.128 base
2021-07-04 06:11:59 +00:00
amount: 615 crypto
2021-07-02 08:48:30 +00:00
stake_amount: 15.129 base
borrowed: 60.516 base
leverage: 5
hours: 5
interest: borrowed * interest_rate * ceil(1 + hours/4)
= 60.516 * 0.0005 * ceil(1 + 5/4) = 0.090774 base
open_value: (amount * open_rate) + (amount * open_rate * fee)
= (615.0 * 0.123) + (615.0 * 0.123 * 0.0025)
= 75.83411249999999
close_value: (amount_closed * close_rate) - (amount_closed * close_rate * fee) - interest
= (615.0 * 0.128) - (615.0 * 0.128 * 0.0025) - 0.090774
= 78.432426
total_profit = close_value - open_value
= 78.432426 - 75.83411249999999
= 2.5983135000000175
total_profit_percentage = ((close_value/open_value)-1) * leverage
= ((78.432426/75.83411249999999)-1) * 5
= 0.1713156134055116
"""
trade = Trade(
pair='DOGE/BTC',
2021-07-02 08:48:30 +00:00
stake_amount=15.129,
amount=615.0,
leverage=5.0,
amount_requested=615.0,
fee_open=fee.return_value,
fee_close=fee.return_value,
open_rate=0.123,
close_rate=0.128,
close_profit=0.1713156134055116,
close_profit_abs=2.5983135000000175,
2021-07-02 08:48:30 +00:00
exchange='kraken',
is_open=False,
open_order_id='dry_run_leverage_buy_12368',
strategy='DefaultStrategy',
timeframe=5,
2022-03-24 19:33:47 +00:00
exit_reason='sell_signal',
2021-07-02 08:48:30 +00:00
open_date=datetime.now(tz=timezone.utc) - timedelta(minutes=300),
close_date=datetime.now(tz=timezone.utc),
2021-08-06 07:15:18 +00:00
interest_rate=0.0005
)
o = Order.parse_from_ccxt_object(leverage_order(), 'DOGE/BTC', 'sell')
trade.orders.append(o)
o = Order.parse_from_ccxt_object(leverage_order_sell(), 'DOGE/BTC', 'sell')
trade.orders.append(o)
return trade