Add New Logic for Up Trend and Down Trend along with old logic, keep old logic as it,…
- User
Add New Logic for Up Trend and Down Trend along with old logic, keep old logic as it, new logic, if there are continually more than two high then trend will UP and If there are more than two low continually then trend will be down: //@version=5 indicator("Trend Indicator with Early Trend Detection", overlay=true)
// User inputs htf = input.timeframe('1D', 'Higher Time Frame') // Default higher time frame is 1 Day left = input.int(4, 'Pivot Left Bars', minval=1) // Number of left bars for pivot detection right = input.int(2, 'Pivot Right Bars', minval=1) // Number of right bars for pivot detection
// Request higher time frame data htf_high = request.security(syminfo.tickerid, htf, high) htf_low = request.security(syminfo.tickerid, htf, low) htf_close = request.security(syminfo.tickerid, htf, close)
// Calculate pivot highs and lows on the higher time frame htf_pivotHigh = request.security(syminfo.tickerid, htf, ta.pivothigh(htf_high, left, right), lookahead=barmerge.lookahead_on) htf_pivotLow = request.security(syminfo.tickerid, htf, ta.pivotlow(htf_low, left, right), lookahead=barmerge.lookahead_on)
// Plot the pivot points at the exact bars where they occurred plotshape(not na(htf_pivotHigh), title="Pivot High", style=shape.triangledown, location=location.abovebar, offset=-right, color=color.red, size=size.tiny, text="PH", textcolor=color.white) plotshape(not na(htf_pivotLow), title="Pivot Low", style=shape.triangleup, location=location.belowbar, offset=-right, color=color.green, size=size.tiny, text="PL", textcolor=color.white)
// Initialize variables for trend detection var float[] pivotHighs = array.new_float() var float[] pivotLows = array.new_float() var string trend = 'Sideways' var string earlyTrend = 'Sideways'
// Collect pivot highs if not na(htf_pivotHigh) pivotHighTime = bar_index - right pivotHighValue = htf_pivotHigh array.unshift(pivotHighs, pivotHighValue) if array.size(pivotHighs) > 3 array.pop(pivotHighs)
// Collect pivot lows if not na(htf_pivotLow) pivotLowTime = bar_index - right pivotLowValue = htf_pivotLow array.unshift(pivotLows, pivotLowValue) if array.size(pivotLows) > 3 array.pop(pivotLows)
// Early trend detection logic using last three pivots if array.size(pivotHighs) >= 3 and array.size(pivotLows) >= 3 recentHigh = array.get(pivotHighs, 0) recentLow = array.get(pivotLows, 0)
// Check if price crosses recent higher high if htf_close > recentHigh earlyTrend := 'Uptrend' // Check if price crosses recent lower low else if htf_close < recentLow earlyTrend := 'Downtrend' else earlyTrend := 'Sideways' // Set to 'Sideways' if conditions are not metelse earlyTrend := 'Sideways' // Set to 'Sideways' if insufficient data
// Existing trend detection logic (4 pivots) var float previousHigh = na var float previousLow = na var float currentHigh = na var float currentLow = na
// Update pivot highs and lows if not na(htf_pivotHigh) previousHigh := currentHigh currentHigh := htf_pivotHigh
- Assistant