Certainly! The error you're encountering is often due to syntax issues in the code, such as incorrect indentation, misuse of assignment operators, or improper variable declarations. Additionally, to keep the early trend as "Uptrend" or "Downtrend" and not revert to "Sideways" until it breaks the second pivot, we'll need to adjust the logic accordingly.
Below is the corrected and properly formatted Pine Script code that addresses the issues:
//@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 earlyTrend = 'Sideways' // For the early trend logic
var string newTrend = 'Sideways' // For the new logic
var string trend = 'Sideways' // For the existing trend 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)
pivotHighValue = htf_pivotHigh
array.unshift(pivotHighs, pivotHighValue)
if array.size(pivotHighs) > 3
array.pop(pivotHighs)
consecutivePivotHighs := consecutivePivotHighs + 1
consecutivePivotLows := 0 // Reset low counter
// Collect pivot lows
if not na(htf_pivotLow)
pivotLowValue = htf_pivotLow
array.unshift(pivotLows, pivotLowValue)
if array.size(pivotLows) > 3
array.pop(pivotLows)
consecutivePivotLows := 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'
// Keep the newTrend as it is until conditions change
// Early trend detection logic
if array.size(pivotHighs) >= 2 and array.size(pivotLows) >= 2
recentHigh = array.get(pivotHighs, 0)
recentLow = array.get(pivotLows, 0)
priorHigh = array.get(pivotHighs, 1)
priorLow = array.get(pivotLows, 1)
// If earlyTrend is 'Sideways', check for trend initiation
if earlyTrend == 'Sideways'
if htf_close > recentHigh
earlyTrend := 'Uptrend'
else if htf_close < recentLow
earlyTrend := 'Downtrend'
// If earlyTrend is 'Uptrend', keep it until price breaks prior low
else if earlyTrend == 'Uptrend'
if htf_close < priorLow
earlyTrend := 'Sideways'
// If earlyTrend is 'Downtrend', keep it until price breaks prior high
else if earlyTrend == 'Downtrend'
if htf_close > priorHigh
earlyTrend := 'Sideways'
// Existing trend detection logic (based on four 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
if not na(htf_pivotLow)
previousLow := currentLow
currentLow := htf_pivotLow
// Determine the trend using the pivot values
if not na(previousHigh) and not na(previousLow) and not na(currentHigh) and not na(currentLow)
if currentHigh > previousHigh and currentLow > previousLow
trend := 'Uptrend'
else if currentHigh < previousHigh and currentLow < previousLow
trend := 'Downtrend'
// Keep the trend as it is until conditions change
// Combine trends for display
displayTrend = earlyTrend != 'Sideways' ? earlyTrend :
newTrend != 'Sideways' ? newTrend : trend
// Create a table to display the trend
var table trendTable = table.new(position.top_right, 1, 3)
// Determine the background color based on the trends
bgcolor_earlyTrend = earlyTrend == 'Uptrend' ? color.new(color.green, 0) :
earlyTrend == 'Downtrend' ? color.new(color.red, 0) :
color.new(color.gray, 0)
bgcolor_newTrend = newTrend == 'Uptrend' ? color.new(color.green, 0) :
newTrend == 'Downtrend' ? color.new(color.red, 0) :
color.new(color.gray, 0)
bgcolor_oldTrend = trend == 'Uptrend' ? color.new(color.green, 0) :
trend == 'Downtrend' ? color.new(color.red, 0) :
color.new(color.gray, 0)
// Update the table with the current trends
if barstate.islast
table.cell(trendTable, 0, 0, 'Early Trend: ' + earlyTrend + ' (' + htf + ')',
bgcolor=bgcolor_earlyTrend, text_color=color.white)
table.cell(trendTable, 0, 1, 'New Trend: ' + newTrend + ' (' + htf + ')',
bgcolor=bgcolor_newTrend, text_color=color.white)
table.cell(trendTable, 0, 2, 'Old Trend: ' + trend + ' (' + htf + ')',
bgcolor=bgcolor_oldTrend, text_color=color.white)
Explanation of Changes:
-
Fixed Syntax Errors:
- Replaced
+= operators with := for correct assignment, as Pine Script doesn't support +=.
- Ensured consistent use of the
:= operator for variable assignments.
- Corrected indentation to align with Pine Script's syntax rules.
- Ensured all
var declarations are at the top level and not nested within code blocks.
-
Adjusted Early Trend Logic:
- Modified the early trend detection logic to keep the trend as "Uptrend" or "Downtrend" until the price breaks the second pivot in the opposite direction.
- The early trend only reverts to "Sideways" when the price crosses the prior pivot in the opposite direction.
- This adjustment aligns with your requirement to "Keep Early Trend, High or Low as it, Don't Convert it to Sideways until It Breaks 2nd Pivot."
-
Consistency in Variable Usage:
- Made sure that variables like
currentHigh, previousHigh, currentLow, and previousLow are properly declared and used, avoiding indexing errors.
- Removed unnecessary shifting of pivot values to simplify the logic and prevent errors.
-
Proper Indentation and Structure:
- Corrected indentation throughout the code to ensure that blocks are correctly defined, which is crucial for Pine Script to parse the code properly.
- Ensured that
if-else blocks are properly indented and all conditions are correctly checked.
By making these changes, the code should now run without the "Mismatched input 'var' expecting 'end of line without line continuation'" error, and the trend detection logic should function as intended, keeping the early trend until the second pivot is broken.
Feel free to test this code and let me know if you have any further questions or need additional adjustments!