Suggestion: //@version=5 indicator("Multi Time Frame Trend Detector", overlay=true) //…
- User
Suggestion: //@version=5 indicator("Multi Time Frame Trend Detector", overlay=true)
// User Inputs higher_tf = input.timeframe("D", "Higher Time Frame") intermediate_tf = input.timeframe("60", "Intermediate Time Frame") pivot_left = input.int(5, "Pivot Left Bars", minval=1) pivot_right = input.int(5, "Pivot Right Bars", minval=1) ma_length = input.int(21, "MA Length")
// === Higher Time Frame Data === // Get Higher Time Frame Data htf_high = request.security(syminfo.tickerid, higher_tf, high) htf_low = request.security(syminfo.tickerid, higher_tf, low) htf_close = request.security(syminfo.tickerid, higher_tf, close)
// Detect Pivots in Higher Time Frame htf_pivot_high = ta.pivothigh(htf_high, pivot_left, pivot_right) htf_pivot_low = ta.pivotlow(htf_low, pivot_left, pivot_right)
// Shift pivots and data back by pivot_right bars to reduce lag htf_pivot_high_shifted = htf_pivot_high[pivot_right] htf_pivot_low_shifted = htf_pivot_low[pivot_right] htf_close_shifted = htf_close[pivot_right] htf_ma = ta.sma(htf_close, ma_length) htf_ma_shifted = htf_ma[pivot_right]
// Initialize Higher Time Frame Trend Variable var string htf_trend = na
// Determine Trend Based on Shifted Pivots for Higher Time Frame if not na(htf_pivot_high_shifted) htf_trend := "Downtrend" else if not na(htf_pivot_low_shifted) htf_trend := "Uptrend" else htf_trend := na(htf_trend[1]) ? "Sideways" : htf_trend[1]
// Use Moving Average to Supplement Trend Detection for Higher Time Frame if htf_close_shifted > htf_ma_shifted htf_trend := htf_trend == "Downtrend" ? "Sideways" : "Uptrend" else htf_trend := htf_trend == "Uptrend" ? "Sideways" : "Downtrend"
// === Intermediate Time Frame Data === // Get Intermediate Time Frame Data itf_high = request.security(syminfo.tickerid, intermediate_tf, high) itf_low = request.security(syminfo.tickerid, intermediate_tf, low) itf_close = request.security(syminfo.tickerid, intermediate_tf, close)
// Detect Pivots in Intermediate Time Frame itf_pivot_high = ta.pivothigh(itf_high, pivot_left, pivot_right) itf_pivot_low = ta.pivotlow(itf_low, pivot_left, pivot_right)
// Shift pivots and data back by pivot_right bars to reduce lag itf_pivot_high_shifted = itf_pivot_high[pivot_right] itf_pivot_low_shifted = itf_pivot_low[pivot_right] itf_close_shifted = itf_close[pivot_right] itf_ma = ta.sma(itf_close, ma_length) itf_ma_shifted = itf_ma[pivot_right]
// Initialize Intermediate Time Frame Trend Variable var string itf_trend = na
// Determine Trend Based on Shifted Pivots for Intermediate Time Frame if not na(itf_pivot_high_shifted) itf_trend := "Downtrend" else if not na(itf_pivot_low_shifted) itf_trend := "Uptrend" else itf_trend := na(itf_trend[1]) ? "Sideways" : itf_trend[1]
// Use Moving Average to Supplement Trend Detection for Intermediate Time Frame if itf_close_shifted > itf_ma_shifted itf_trend := itf_trend == "Downtrend" ? "Sideways" : "Uptrend" else itf_trend := itf_trend == "Uptrend" ? "Sideways" : "Downtrend"
- Assistant