add short close rate calu
This commit is contained in:
parent
9facd5b52a
commit
82e0eca128
@ -362,6 +362,13 @@ class Backtesting:
|
|||||||
"""
|
"""
|
||||||
# Special handling if high or low hit STOP_LOSS or ROI
|
# Special handling if high or low hit STOP_LOSS or ROI
|
||||||
if sell.sell_type in (SellType.STOP_LOSS, SellType.TRAILING_STOP_LOSS):
|
if sell.sell_type in (SellType.STOP_LOSS, SellType.TRAILING_STOP_LOSS):
|
||||||
|
if is_short:
|
||||||
|
if trade.stop_loss < sell_row[LOW_IDX]:
|
||||||
|
# our stoploss was already lower than candle high,
|
||||||
|
# possibly due to a cancelled trade exit.
|
||||||
|
# sell at open price.
|
||||||
|
return sell_row[OPEN_IDX]
|
||||||
|
else:
|
||||||
if trade.stop_loss > sell_row[HIGH_IDX]:
|
if trade.stop_loss > sell_row[HIGH_IDX]:
|
||||||
# our stoploss was already higher than candle high,
|
# our stoploss was already higher than candle high,
|
||||||
# possibly due to a cancelled trade exit.
|
# possibly due to a cancelled trade exit.
|
||||||
@ -379,15 +386,28 @@ class Backtesting:
|
|||||||
and self.strategy.trailing_stop_positive
|
and self.strategy.trailing_stop_positive
|
||||||
):
|
):
|
||||||
# Worst case: price reaches stop_positive_offset and dives down.
|
# Worst case: price reaches stop_positive_offset and dives down.
|
||||||
|
if is_short:
|
||||||
|
stop_rate = (sell_row[OPEN_IDX] *
|
||||||
|
(1 - abs(self.strategy.trailing_stop_positive_offset) +
|
||||||
|
abs(self.strategy.trailing_stop_positive)))
|
||||||
|
else:
|
||||||
stop_rate = (sell_row[OPEN_IDX] *
|
stop_rate = (sell_row[OPEN_IDX] *
|
||||||
(1 + abs(self.strategy.trailing_stop_positive_offset) -
|
(1 + abs(self.strategy.trailing_stop_positive_offset) -
|
||||||
abs(self.strategy.trailing_stop_positive)))
|
abs(self.strategy.trailing_stop_positive)))
|
||||||
else:
|
else:
|
||||||
# Worst case: price ticks tiny bit above open and dives down.
|
# Worst case: price ticks tiny bit above open and dives down.
|
||||||
|
if is_short:
|
||||||
|
stop_rate = sell_row[OPEN_IDX] * (1 + abs(trade.stop_loss_pct / leverage))
|
||||||
|
assert stop_rate > sell_row[HIGH_IDX]
|
||||||
|
else:
|
||||||
stop_rate = sell_row[OPEN_IDX] * (1 - abs(trade.stop_loss_pct / leverage))
|
stop_rate = sell_row[OPEN_IDX] * (1 - abs(trade.stop_loss_pct / leverage))
|
||||||
assert stop_rate < sell_row[HIGH_IDX]
|
assert stop_rate < sell_row[HIGH_IDX]
|
||||||
|
|
||||||
# Limit lower-end to candle low to avoid sells below the low.
|
# Limit lower-end to candle low to avoid sells below the low.
|
||||||
# This still remains "worst case" - but "worst realistic case".
|
# This still remains "worst case" - but "worst realistic case".
|
||||||
|
if is_short:
|
||||||
|
return min(sell_row[HIGH_IDX], stop_rate)
|
||||||
|
else:
|
||||||
return max(sell_row[LOW_IDX], stop_rate)
|
return max(sell_row[LOW_IDX], stop_rate)
|
||||||
|
|
||||||
# Set close_rate to stoploss
|
# Set close_rate to stoploss
|
||||||
@ -402,6 +422,16 @@ class Backtesting:
|
|||||||
return sell_row[OPEN_IDX]
|
return sell_row[OPEN_IDX]
|
||||||
|
|
||||||
# - (Expected abs profit + open_rate + open_fee) / (fee_close -1)
|
# - (Expected abs profit + open_rate + open_fee) / (fee_close -1)
|
||||||
|
if is_short:
|
||||||
|
close_rate = (trade.open_rate *
|
||||||
|
(1 - trade.fee_open) - trade.open_rate * roi / leverage) / (trade.fee_close + 1)
|
||||||
|
if (trade_dur > 0 and trade_dur == roi_entry
|
||||||
|
and roi_entry % self.timeframe_min == 0
|
||||||
|
and sell_row[OPEN_IDX] < close_rate):
|
||||||
|
# new ROI entry came into effect.
|
||||||
|
# use Open rate if open_rate > calculated sell rate
|
||||||
|
return sell_row[OPEN_IDX]
|
||||||
|
else:
|
||||||
close_rate = - (trade.open_rate * roi / leverage + trade.open_rate *
|
close_rate = - (trade.open_rate * roi / leverage + trade.open_rate *
|
||||||
(1 + trade.fee_open)) / (trade.fee_close - 1)
|
(1 + trade.fee_open)) / (trade.fee_close - 1)
|
||||||
|
|
||||||
@ -412,6 +442,20 @@ class Backtesting:
|
|||||||
# use Open rate if open_rate > calculated sell rate
|
# use Open rate if open_rate > calculated sell rate
|
||||||
return sell_row[OPEN_IDX]
|
return sell_row[OPEN_IDX]
|
||||||
|
|
||||||
|
if is_short:
|
||||||
|
if (
|
||||||
|
trade_dur == 0
|
||||||
|
# Red candle (for longs), TODO: green candle (for shorts)
|
||||||
|
and sell_row[OPEN_IDX] < sell_row[CLOSE_IDX] # Red candle
|
||||||
|
and trade.open_rate > sell_row[OPEN_IDX] # trade-open below open_rate
|
||||||
|
and close_rate < sell_row[CLOSE_IDX]
|
||||||
|
):
|
||||||
|
# ROI on opening candles with custom pricing can only
|
||||||
|
# trigger if the entry was at Open or lower.
|
||||||
|
# details: https: // github.com/freqtrade/freqtrade/issues/6261
|
||||||
|
# If open_rate is < open, only allow sells below the close on red candles.
|
||||||
|
raise ValueError("Opening candle ROI on red candles.")
|
||||||
|
else:
|
||||||
if (
|
if (
|
||||||
trade_dur == 0
|
trade_dur == 0
|
||||||
# Red candle (for longs), TODO: green candle (for shorts)
|
# Red candle (for longs), TODO: green candle (for shorts)
|
||||||
@ -424,9 +468,13 @@ class Backtesting:
|
|||||||
# details: https: // github.com/freqtrade/freqtrade/issues/6261
|
# details: https: // github.com/freqtrade/freqtrade/issues/6261
|
||||||
# If open_rate is < open, only allow sells below the close on red candles.
|
# If open_rate is < open, only allow sells below the close on red candles.
|
||||||
raise ValueError("Opening candle ROI on red candles.")
|
raise ValueError("Opening candle ROI on red candles.")
|
||||||
|
|
||||||
# Use the maximum between close_rate and low as we
|
# Use the maximum between close_rate and low as we
|
||||||
# cannot sell outside of a candle.
|
# cannot sell outside of a candle.
|
||||||
# Applies when a new ROI setting comes in place and the whole candle is above that.
|
# Applies when a new ROI setting comes in place and the whole candle is above that.
|
||||||
|
if is_short:
|
||||||
|
return max(min(close_rate, sell_row[HIGH_IDX]), sell_row[LOW_IDX])
|
||||||
|
else:
|
||||||
return min(max(close_rate, sell_row[LOW_IDX]), sell_row[HIGH_IDX])
|
return min(max(close_rate, sell_row[LOW_IDX]), sell_row[HIGH_IDX])
|
||||||
|
|
||||||
else:
|
else:
|
||||||
@ -610,6 +658,9 @@ class Backtesting:
|
|||||||
proposed_rate=propose_rate, entry_tag=entry_tag) # default value is the open rate
|
proposed_rate=propose_rate, entry_tag=entry_tag) # default value is the open rate
|
||||||
# We can't place orders higher than current high (otherwise it'd be a stop limit buy)
|
# We can't place orders higher than current high (otherwise it'd be a stop limit buy)
|
||||||
# which freqtrade does not support in live.
|
# which freqtrade does not support in live.
|
||||||
|
if direction == "short":
|
||||||
|
propose_rate = max(propose_rate, row[LOW_IDX])
|
||||||
|
else:
|
||||||
propose_rate = min(propose_rate, row[HIGH_IDX])
|
propose_rate = min(propose_rate, row[HIGH_IDX])
|
||||||
|
|
||||||
min_stake_amount = self.exchange.get_min_pair_stake_amount(pair, propose_rate, -0.05) or 0
|
min_stake_amount = self.exchange.get_min_pair_stake_amount(pair, propose_rate, -0.05) or 0
|
||||||
@ -700,13 +751,13 @@ class Backtesting:
|
|||||||
|
|
||||||
trade.adjust_stop_loss(trade.open_rate, self.strategy.stoploss, initial=True)
|
trade.adjust_stop_loss(trade.open_rate, self.strategy.stoploss, initial=True)
|
||||||
|
|
||||||
trade.set_isolated_liq(self.exchange.get_liquidation_price(
|
# trade.set_isolated_liq(self.exchange.get_liquidation_price(
|
||||||
pair=pair,
|
# pair=pair,
|
||||||
open_rate=propose_rate,
|
# open_rate=propose_rate,
|
||||||
amount=amount,
|
# amount=amount,
|
||||||
leverage=leverage,
|
# leverage=leverage,
|
||||||
is_short=is_short,
|
# is_short=is_short,
|
||||||
))
|
# ))
|
||||||
|
|
||||||
order = Order(
|
order = Order(
|
||||||
id=self.order_id_counter,
|
id=self.order_id_counter,
|
||||||
|
Loading…
Reference in New Issue
Block a user