BUG: Preserve stop-loss value in stats._trades when SL is gapped through#1369
Open
nyxst4ck wants to merge 1 commit into
Open
BUG: Preserve stop-loss value in stats._trades when SL is gapped through#1369nyxst4ck wants to merge 1 commit into
nyxst4ck wants to merge 1 commit into
Conversation
When a stop-loss order triggers, its stop price is cleared as the stop becomes a market order. The value was only restored on stats._trades["SL"] when the fill price equalled the stop price exactly. If the bar gapped past the stop (fill price worse than the stop), the SL column was left empty (NaN). Restore the recorded SL whenever the executed order is the trade's own stop-loss order, regardless of the fill price. Adds a regression test. Fixes kernc#1340
Author
|
Marked ready for review. I do not see any checks reported on this branch yet. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a trade is closed by its stop-loss, the SL value is sometimes missing (
NaN) instats._trades["SL"], even though an SL was explicitly set. This happens specifically when the price gaps through the stop: the bar opens beyond the stop price, so the fill price is worse than the stop.This is the still-unfixed part of #1288 reported in #1340.
Root cause
When a stop order is hit, the stop becomes a market order and its
stop_priceis cleared:Since the trade reads its SL from
trade._sl_order.stop, clearing it drops the value from the trades data frame. The previous fix (#1288) restored it, but only when the fill price equalled the stop exactly:On a gap-through, the order fills at the (worse) open price, so
price != stop_priceand the SL is never restored.Fix
Restore the recorded SL whenever the executed order is the trade's own stop-loss order, regardless of the fill price. The
TP/trade.close()paths are unaffected (those orders carry no stop price).Test
Added
test_sl_value_in_trades_df_when_gapped_through, which enters a long position whose SL (99.5) is gapped through on the next bar's open (99.19). The exit price is below the stop, but the SL value must still be recorded.master:AssertionError: None != 99.5Existing SL/TP regression tests (
test_sl_tp_values_in_trades_df,test_sl_always_before_tp,test_stop_entry_and_tp_in_same_bar) still pass.Fixes #1340