0% found this document useful (0 votes)
23 views3 pages

Trend v2

Uploaded by

davidsonnjenga6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

Trend v2

Uploaded by

davidsonnjenga6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

I'll create a Pine Script that can automatically detect higher highs and lower lows

(to identify uptrends and downtrends) and draw the corresponding trendlines.
This script will include the following features:

Detection of Local Highs and Lows: It will identify local peaks (higher highs) for
uptrends and local troughs (lower lows) for downtrends.
Automatic Trendline Drawing: It will draw trendlines based on these highs and lows,
extending them dynamically.
Adjustable Parameters: It will allow for flexibility in the lookback period and the
detection of significant highs/lows.
Here's the full Pine Script to achieve this:\

//@version=5
indicator("Trendline Drawing with Highs/Lows", overlay=true)

// Adjustable parameters
lookback = input.int(50, title="Lookback Period for Trendline Detection", minval=1)
length = input.int(10, title="Local High/Low Length", minval=1)
trendline_width = input.int(2, title="Trendline Width")
min_distance = input.int(10, title="Minimum Bar Distance for Trendline", minval=1)

// Function to detect local high (higher high) for uptrend


localHighCondition = high == ta.highest(high, length)
localLowCondition = low == ta.lowest(low, length)

// Variables to store trendline points


var line upTrendLine = na
var line downTrendLine = na

// Arrays to store the highs and lows for trendline drawing


var float upTrendStart = na
var float downTrendStart = na
var float upTrendEnd = na
var float downTrendEnd = na

// Storing points for the trendlines


if localHighCondition
upTrendStart := high
upTrendEnd := high
// Draw an uptrend line if we have a higher high (and previous points exist)
if na(upTrendStart) == false
upTrendLine := line.new(bar_index[length], upTrendStart, bar_index,
upTrendEnd, color=color.green, width=trendline_width, extend=extend.right)

if localLowCondition
downTrendStart := low
downTrendEnd := low
// Draw a downtrend line if we have a lower low (and previous points exist)
if na(downTrendStart) == false
downTrendLine := line.new(bar_index[length], downTrendStart, bar_index,
downTrendEnd, color=color.red, width=trendline_width, extend=extend.right)

// Update the trendline endpoints for dynamic adjustment


if (localHighCondition and upTrendLine)
line.set_xy1(upTrendLine, bar_index[length], high)
line.set_xy2(upTrendLine, bar_index, high)
if (localLowCondition and downTrendLine)
line.set_xy1(downTrendLine, bar_index[length], low)
line.set_xy2(downTrendLine, bar_index, low)

// Optional: Plotting local highs and lows as markers


plotshape(localHighCondition, style=shape.triangleup, location=location.abovebar,
color=color.green, size=size.small)
plotshape(localLowCondition, style=shape.triangledown, location=location.belowbar,
color=color.red, size=size.small)

// Optional: Alerts for trend changes (if desired)


alertcondition(localHighCondition, title="Uptrend Detected", message="Local High
Detected: Possible Uptrend")
alertcondition(localLowCondition, title="Downtrend Detected", message="Local Low
Detected: Possible Downtrend")

Key Features of This Script:


Lookback & Length:

The lookback input determines how far back we analyze for trendlines (default is 50
bars).
The length input controls the length of the window for detecting local highs and
lows (default is 10 bars).
Trendline Detection:

Uptrend: If a higher high is detected (localHighCondition), a trendline is drawn


from the previous higher high to the current one.
Downtrend: If a lower low is detected (localLowCondition), a trendline is drawn
from the previous lower low to the current one.
Dynamic Trendline Updates:

The trendlines are updated dynamically as new highs or lows are detected. The
line.set_xy1 and line.set_xy2 functions adjust the start and end of the trendlines.
Optional Visual Cues:

Local Highs are marked with a green triangle above the bar.
Local Lows are marked with a red triangle below the bar.
Alerts:

The script includes alert conditions for detecting local highs and lows, which can
be used to notify the trader when potential trend changes are spotted.
How It Works:
As the chart progresses, the script checks for local highs and lows based on the
length parameter. Once a local high or low is identified, the trendline is drawn
from the previous point to the current one.
The trendline dynamically updates to extend rightward as the market moves.
Customization:
Trendline Width: Adjust the thickness of the trendlines with the trendline_width
parameter.
Minimum Bar Distance for Trendline: You can set a minimum distance between points
for the trendlines using the min_distance parameter to avoid drawing too many lines
if the price action is too close together.
Important Notes:
This is a simplified trendline script based on local highs and lows. More complex
trendline algorithms can be added by considering different aspects of price action,
like slope checks or more advanced pivot point techniques.
Always test on historical data before using it in live trading.
Let me know if you'd like any further modifications or additional features!

You might also like