Merge pull request #4941 from brookmiles/fix-stoploss-above-candle
prevent backtest stoploss trade price being set above candle high
This commit is contained in:
commit
ef4d1c24d7
@ -216,6 +216,12 @@ 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 trade.stop_loss > sell_row[HIGH_IDX]:
|
||||||
|
# our stoploss was already higher than candle high,
|
||||||
|
# possibly due to a cancelled trade exit.
|
||||||
|
# sell at open price.
|
||||||
|
return sell_row[OPEN_IDX]
|
||||||
|
|
||||||
# Set close_rate to stoploss
|
# Set close_rate to stoploss
|
||||||
return trade.stop_loss
|
return trade.stop_loss
|
||||||
elif sell.sell_type == (SellType.ROI):
|
elif sell.sell_type == (SellType.ROI):
|
||||||
|
@ -185,7 +185,7 @@ tc11 = BTContainer(data=[
|
|||||||
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
|
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
|
||||||
[1, 5000, 5050, 4950, 5100, 6172, 0, 0],
|
[1, 5000, 5050, 4950, 5100, 6172, 0, 0],
|
||||||
[2, 5100, 5251, 5100, 5100, 6172, 0, 0],
|
[2, 5100, 5251, 5100, 5100, 6172, 0, 0],
|
||||||
[3, 4850, 5050, 4650, 4750, 6172, 0, 0],
|
[3, 5000, 5150, 4650, 4750, 6172, 0, 0],
|
||||||
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
|
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
|
||||||
stop_loss=-0.10, roi={"0": 0.10}, profit_perc=0.019, trailing_stop=True,
|
stop_loss=-0.10, roi={"0": 0.10}, profit_perc=0.019, trailing_stop=True,
|
||||||
trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.05,
|
trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.05,
|
||||||
@ -440,6 +440,23 @@ tc27 = BTContainer(data=[
|
|||||||
trades=[BTrade(sell_reason=SellType.ROI, open_tick=1, close_tick=4)]
|
trades=[BTrade(sell_reason=SellType.ROI, open_tick=1, close_tick=4)]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Test 28: trailing_stop should raise so candle 3 causes a stoploss
|
||||||
|
# Same case than tc11 - but candle 3 "gaps down" - the stoploss will be above the candle,
|
||||||
|
# therefore "open" will be used
|
||||||
|
# stop-loss: 10%, ROI: 10% (should not apply), stoploss adjusted candle 2
|
||||||
|
tc28 = BTContainer(data=[
|
||||||
|
# D O H L C V B S
|
||||||
|
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
|
||||||
|
[1, 5000, 5050, 4950, 5100, 6172, 0, 0],
|
||||||
|
[2, 5100, 5251, 5100, 5100, 6172, 0, 0],
|
||||||
|
[3, 4850, 5050, 4650, 4750, 6172, 0, 0],
|
||||||
|
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
|
||||||
|
stop_loss=-0.10, roi={"0": 0.10}, profit_perc=-0.03, trailing_stop=True,
|
||||||
|
trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.05,
|
||||||
|
trailing_stop_positive=0.03,
|
||||||
|
trades=[BTrade(sell_reason=SellType.TRAILING_STOP_LOSS, open_tick=1, close_tick=3)]
|
||||||
|
)
|
||||||
|
|
||||||
TESTS = [
|
TESTS = [
|
||||||
tc0,
|
tc0,
|
||||||
tc1,
|
tc1,
|
||||||
@ -469,6 +486,7 @@ TESTS = [
|
|||||||
tc25,
|
tc25,
|
||||||
tc26,
|
tc26,
|
||||||
tc27,
|
tc27,
|
||||||
|
tc28,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user