Keep Early Trend, High Or Low as it, Don't Convert it to Sideways until It Break 2nd…
- User
Keep Early Trend, High Or Low as it, Don't Convert it to Sideways until It Break 2nd Pivot: //@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' var string newTrend = 'Sideways' // For the new logic var int consecutivePivotHighs = 0 // Counter for consecutive pivot highs var int consecutivePivotLows = 0 // Counter for consecutive pivot lows
// 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) consecutivePivotHighs += 1 consecutivePivotLows := 0 // Reset low counter
// 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) consecutivePivotLows += 1 consecutivePivotHighs := 0 // Reset high counter
// New trend detection logic based on consecutive pivots if consecutivePivotHighs >= 3 newTrend := 'Uptrend' else if consecutivePivotLows >= 3 newTrend := 'Downtrend' else newTrend := 'Sideways' // Set to 'Sideways' if conditions are not met
// 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)
code - Assistant