TradingView Pine Script Renko Call to HTF Alerts 1 Bar Late?

Summary

The issue at hand is a delayed alert in a Pine Script strategy on TradingView. The script uses a higher timeframe (HTF) filter to determine the direction of the 5M Renko chart, but the alert is sent one bar late. This problem only occurs when the HTF filter code is added to the script.

Root Cause

The root cause of this issue is likely due to the way request.security functions in Pine Script. When using request.security to fetch data from a higher timeframe, the data is only available after the higher timeframe bar has closed and confirmed. This can cause a delay in the alert being sent, as the script needs to wait for the higher timeframe bar to confirm before it can trigger the alert. Some possible causes include:

  • Lookahead settings in the request.security function
  • Barmerge settings in the request.security function
  • Confirmation of higher timeframe bars

Why This Happens in Real Systems

This issue can occur in real systems due to the way Pine Script handles higher timeframe data. When using request.security to fetch data from a higher timeframe, the script needs to wait for the higher timeframe bar to close and confirm before it can use the data. This can cause delays in alerts being sent, especially when using Renko charts which can have variable bar lengths. Some key factors that contribute to this issue include:

  • Higher timeframe data availability
  • Bar confirmation settings
  • Lookahead settings

Real-World Impact

The real-world impact of this issue is that traders may miss opportunities due to delayed alerts. If the alert is sent one bar late, the trader may miss the optimal entry point for a trade, resulting in reduced profits or even losses. Some potential impacts include:

  • Reduced trading performance
  • Missed trading opportunities
  • Increased risk

Example or Code

htfOpen = request.security(syminfo.tickerid, "5", open[1], lookahead=barmerge.lookahead_off)
htfClose = request.security(syminfo.tickerid, "5", close[1], lookahead=barmerge.lookahead_off)
htfConfirmed = request.security(syminfo.tickerid, "5", barstate.isconfirmed)

var bool htfBull = false
var bool htfBear = false

if htfConfirmed
    htfBull := htfClose > htfOpen
    htfBear := htfClose < htfOpen

htfGreen = htfBull
htfRed = htfBear

How Senior Engineers Fix It

Senior engineers can fix this issue by optimizing the HTF filter code to reduce the delay in alert sending. Some possible solutions include:

  • Using a lower lookahead setting in the request.security function
  • Using a different barmerge setting in the request.security function
  • Confirming higher timeframe bars earlier
  • Using a different approach to determine the direction of the higher timeframe chart

Why Juniors Miss It

Junior engineers may miss this issue due to a lack of understanding of how Pine Script handles higher timeframe data. They may not be aware of the delays that can occur when using request.security to fetch data from a higher timeframe, or they may not know how to optimize the HTF filter code to reduce the delay. Some key areas where juniors may need improvement include:

  • Understanding of Pine Script fundamentals
  • Knowledge of higher timeframe data handling
  • Experience with optimizing HTF filter code
  • Familiarity with Renko charts and their characteristics

Leave a Comment