Search code examples
pine-scripttradingview-apitrendline

Tradingview - Plot higher timeframe trend line in lower time frame


Is it possible to plot trend line base on higher time frame in lower time frame?

For example: Plot Week level trend line in Day level chart. Trend line set from Week[2] high to Week1 high And show on Day time frame.

- Extend Question:

How do I only keep Week[2] high to Week1 high lines(Blue cirle).

And skip other wrong connect line (Red cross in pic)

Wrong connect from Monday to High of the week


Solution

  • The problem with this is, you cannot really use the security() function because you want to draw your trendline from wick to wick. With security(), you cannot tell where the HTF wick was formed.

    Therefore, you need to keep track of events yourself.

    Something like below should get you going:

    var float weekly_high_price = na
    var int weekly_high_idx = na
    
    is_new_week = timeframe.change("W")
    
    if (is_new_week)
        weekly_high_price := high
        weekly_high_idx := bar_index
    else
        if (high > weekly_high_price)
            weekly_high_price := high
            weekly_high_idx := bar_index
    

    This is how you can figure out an HTF high on your lower timeframe.

    Create two other variable sets for W[1] and W[2] and then draw your trendline with line.new().