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

Which come first: High or Low? #996

Unanswered
houseofai asked this question in Q&A
Discussion options

When processing a new bar, how does Backtesting decide which price from High/Low comes first?

TradingView's Strategy Tester uses the closest price to the Open price to decide which one comes first. Strategies that use small triggers (small TP/SL), can cause significant inaccuracies in the results.

To illustrate the issue, let's say that my strategy has a Long position with a Stoploss at 10$ and a Takeprofit at 20$. A new bar comes, with an Open price of 15$, a High price of 25$, and a Low price of 5$. The High price triggers the TP, and the Low price triggers the SL. So depending on which tick comes first, it can change the result.

You must be logged in to vote

Replies: 3 comments

Comment options

Just come by issue #119. I believe the comment made by @rokups makes things closer to reality.

This issue popup when Entry, SL, or TP are close enough to trigger on the same bar - and which happens more often using high time frames.

To add on these two comments:

  • If trade entry and take-profit prices are within wicks - close trade in-profit.
  • If trade entry and take-profit are within same candle - close trade in-profit only when candle matches trade direction (green candle for longs, red candle for shorts).

I would go for a more pessimistic approach and assume that:

  • If there is Entry and TP on the same bar: hit the Entry last and don't hit TP.
  • If there is Entry and SL on the same bar: hit the Entry and then the SL.
  • If there is Entry, TP, and SL on the same bar: hit the Entry and then the SL.
  • If there is TP and SL on the same bar: hit the SL
You must be logged in to vote
0 replies
Comment options

question SL/TP hit on the same bar as trade opened refers to the same problem

You must be logged in to vote
0 replies
Comment options

To illustrate the issue

Do you have code that triggers the issue?


See #119 (comment).

But in other regards, the framework's policy is already pessimistic, i.e. SL-first:

def _open_trade(self, price: float, size: int,
sl: Optional[float], tp: Optional[float], time_index: int, tag):
trade = Trade(self, size, price, time_index, tag)
self.trades.append(trade)
# Create SL/TP (bracket) orders.
# Make sure SL order is created first so it gets adversarially processed before TP order
# in case of an ambiguous tie (both hit within a single bar).
# Note, sl/tp orders are inserted at the front of the list, thus order reversed.
if tp:
trade.tp = tp
if sl:
trade.sl = sl
.
You must be logged in to vote
0 replies
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 によって変換されたページ (->オリジナル) /