Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

take profit and stop loss behavior #242

Answered by kernc
nkami asked this question in Q&A
Discussion options

Hey, I was wondering what happens if you set both take profit and stop loss on an order, and the next candle fluctuates in a manner that should trigger both. What gets priority according to the backtest? (given the fact we cant know the order of events within the candle).
For example: you set a market order with tp=2 and sl=1, and the next candle has high=3 and low=0.

You must be logged in to vote

If you enter with a plain market order, SL has priority (the framework takes an adversarial, rather than an optimistic stance).

If you enter with a stop/limit order, a warning is emitted:

elif (low <= (order.sl or -np.inf) <= high or
low <= (order.tp or -np.inf) <= high):
warnings.warn(
f"({data.index[-1]}) A contingent SL/TP order would execute in the "
"same bar its parent stop/limit order was turned into a trade. "
"Since we can't assert the precise intra-candle "
"price movement, the affected SL/TP order will instead be executed on "
"the next (matching) price/bar, maki...

Replies: 1 comment 1 reply

Comment options

If you enter with a plain market order, SL has priority (the framework takes an adversarial, rather than an optimistic stance).

If you enter with a stop/limit order, a warning is emitted:

elif (low <= (order.sl or -np.inf) <= high or
low <= (order.tp or -np.inf) <= high):
warnings.warn(
f"({data.index[-1]}) A contingent SL/TP order would execute in the "
"same bar its parent stop/limit order was turned into a trade. "
"Since we can't assert the precise intra-candle "
"price movement, the affected SL/TP order will instead be executed on "
"the next (matching) price/bar, making the result (of this trade) "
"somewhat dubious. "
"See https://github.com/kernc/backtesting.py/issues/119",
UserWarning)
You must be logged in to vote
1 reply
Comment options

Thank you!

Answer selected by nkami
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /