1Copy the scriptPress Copy script above, or the copy button on the code below.
2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.
Source Code
181 lines of Pine Script v4 from [Order] Range + Trend Strategy.txt, published under MIT. Keep the author's credit and licence lines when you reuse it.
[Order] Range + Trend Strategy.txt
//@version=4
strategy("[Order] Range + Trend Strategy", shorttitle='RTI', overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=15)
//---INPUT-----------------------------------------------------------
src = input(defval=close, title="Source")
samplingPeriod = input(defval=100, minval=1, title="Sampling Period")
rangeMultiplier = input(defval=4.0, minval=0.1, title="Range Multiplier")
useTakeProfit = input(false, title="Use TakeProfit?", type=input.bool, group="TakeProfit")
takeProfitPercent = input(1.5, title='TakeProfit %(0-100)', type=input.float, step=0.1, group="TakeProfit") / 100
useStopLoss = input(false, title="Use StopLoss?", type=input.bool, group="StopLoss")
stopLossType = input("Percent", title="StopLoss type", options=["Percent", "Trailing"], type=input.string, group="StopLoss")
StopLossPercent = input(1.5, title="StopLoss %(0-100)", type=input.float, step=0.1, group="StopLoss") / 100
leftbars = input(100, minval=1, title="Pivot Detection: Left Bars", group="Trend lines")
rightbars = input(15, minval=1, title="Pivot Detection: Right Bars", group="Trend lines")
showTakeProfit = input(true, title="Show TakeProfit", group="Visual part")
showStopLoss = input(true, title="Show StopLoss", group="Visual part")
showTrend = input(true, title="Show Trend Lines", group="Visual part")
showPivotsConnection = input(true, title="Show Pivot Connections", group="Visual part")
//---Determine trend lines--------------------------------------------
var float takeProfit = 0.0
var float stopLoss = na
var float stopLoss_priceAction = na
var bool tookProfit = false
pivotHigh = pivothigh(high, leftbars, rightbars)
pivotLow = pivotlow(low, leftbars, rightbars)
pivotHighValue1 = valuewhen(pivotHigh, high[rightbars], 0)
pivotHighBar1 = valuewhen(pivotHigh, bar_index[rightbars], 0)
pivotHighValue2 = valuewhen(pivotHigh, high[rightbars], 1)
pivotHighBar2 = valuewhen(pivotHigh, bar_index[rightbars], 1)
pivotLowValue1 = valuewhen(pivotLow, low[rightbars], 0)
pivotLowBar1 = valuewhen(pivotLow, bar_index[rightbars], 0)
pivotLowValue2 = valuewhen(pivotLow, low[rightbars], 1)
pivotLowBar2 = valuewhen(pivotLow, bar_index[rightbars], 1)
plotshape(pivotHigh, style = shape.circle, location = location.abovebar, color = color.orange, title = 'Pivot High', offset = -rightbars)
plotshape(pivotLow, style = shape.circle, location = location.belowbar, color = color.blue, title = 'Pivot Low', offset = -rightbars)
plot(pivotHigh and showPivotsConnection ? high[rightbars] : na, color=color.orange, offset=-rightbars)
plot(pivotLow and showPivotsConnection ? low[rightbars] : na, color=color.purple, offset=-rightbars)
get_slope(x1,x2,y1,y2)=>
m = (y2-y1)/(x2-x1)
get_interceptY(m, x1, y1)=>
b=y1-m*x1
get_y(m, b, ts)=>
Y = m * ts + b
int resistance_x1 = na, int support_x1 = na
float resistance_y1 = na, float support_y1 = na
int resistance_x2 = na, int support_x2 = na
float resistance_y2 = na, float support_y2 = na
resistance_x1 := pivotHigh ? pivotHighBar1 : resistance_x1[1]
resistance_y1 := pivotHigh ? pivotHighValue1 : resistance_y1[1]
resistance_x2 := pivotHigh ? pivotHighBar2 : resistance_x2[1]
resistance_y2 := pivotHigh ? pivotHighValue2 : resistance_y2[1]
resistance_m = get_slope(resistance_x1, resistance_x2, resistance_y1, resistance_y2)
resistance_b = get_interceptY(resistance_m, resistance_x1, resistance_y1)
resistance_y = get_y(resistance_m, resistance_b, bar_index)
support_x1 := pivotLow ? pivotLowBar1 : support_x1[1]
support_y1 := pivotLow ? pivotLowValue1 : support_y1[1]
support_x2 := pivotLow ? pivotLowBar2 : support_x2[1]
support_y2 := pivotLow ? pivotLowValue2 : support_y2[1]
support_m = get_slope(support_x1, support_x2, support_y1, support_y2)
support_b = get_interceptY(support_m, support_x1, support_y1)
support_y = get_y(support_m, support_b, bar_index)
plot(showTrend ? resistance_y : na, color=color.red, title='Resistance line', linewidth=1, style=plot.style_circles, display = display.all)
plot(showTrend ? support_y : na, color=color.lime, title='Support line', linewidth=1, style=plot.style_circles, display = display.all)
if pivotHigh
line.new(showPivotsConnection ? pivotHighBar1 : na, pivotHighValue1, bar_index, resistance_y, style = line.style_dashed, color = color.blue)
if pivotLow
line.new(showPivotsConnection ? pivotLowBar1 : na, pivotLowValue1, bar_index, support_y, style = line.style_dashed, color = color.blue)
//---Determine range--------------------------------------------------
smoothRange = ema(ema(abs(src - src[1]), samplingPeriod), (samplingPeriod*2) - 1) * rangeMultiplier
rangeFilter(x, r)=>
rangeFilter = x
rangeFilter := x > nz(rangeFilter[1]) ? ((x - r) < nz(rangeFilter[1]) ? nz(rangeFilter[1]) : (x - r)) : ((x + r) > nz(rangeFilter[1]) ? nz(rangeFilter[1]) : (x + r))
rangeFilter
filter = rangeFilter(src, smoothRange)
up = 0.0
up := filter > filter[1] ? nz(up[1]) + 1 : filter < filter[1] ? 0 : nz(up[1])
down = 0.0
down := filter < filter[1] ? nz(down[1]) + 1 : filter > filter[1] ? 0 : nz(down[1])
longCond = src > filter and up > 0 and src != src[1]
shortCond = src < filter and down > 0 and src != src[1]
conds = 0
conds := longCond ? 1 : shortCond ? -1 : conds[1]
longCondition = longCond and conds[1] == -1
shortCondition = shortCond and conds[1] == 1
plotshape(longCondition, title = "Buy Signal", text ="L", textcolor = color.black, style=shape.triangleup, size = size.small, location=location.belowbar, color = color.green)
plotshape(shortCondition, title = "Sell Signal", text ="S", textcolor = color.black, style=shape.triangledown, size = size.small, location=location.abovebar, color = color.red)
filtplot = plot(filter, color=up > 0 ? color.green : down > 0 ? color.red : color.orange, linewidth=3, title="Range Filter")
[supertrend, marketSide] = supertrend(20, 7) // [супертренд линия, напрвление рынка]
takeProfitLongCondition = close > takeProfit and not tookProfit and longCond and takeProfitPercent != 0 and strategy.position_size > 0 and useTakeProfit
takeProfitShortCondition = close < takeProfit and not tookProfit and shortCond and takeProfitPercent != 0 and strategy.position_size < 0 and useTakeProfit
if takeProfitLongCondition or takeProfitShortCondition
tookProfit := true
takeProfit := na
strategy.close_all(comment="TakeProfit", alert_message="TAKEPROFIT")
stopLossLongCondition = close < stopLoss and strategy.position_size > 0 and useStopLoss
stopLossShortCondition = close > stopLoss and strategy.position_size < 0 and useStopLoss
if stopLossLongCondition or stopLossShortCondition
strategy.close_all(comment="StopLoss", alert_message="STOPLOSS")
stopLoss := na
stopLoss_priceAction := na
// Open orders
if longCondition and close > supertrend
strategy.entry("LONG", strategy.long, alert_message="🟢 LONG")
if shortCondition and close < supertrend
strategy.entry("SHORT", strategy.short, alert_message="🔴 SHORT")
// TakeProfit
if useTakeProfit
if longCondition
tookProfit := false
takeProfit := close + close * takeProfitPercent
if shortCondition
tookProfit := false
takeProfit := close - close * takeProfitPercent
//StopLoss
if useStopLoss
if longCondition
stopLoss := close - close * StopLossPercent
stopLoss_priceAction := close
if shortCondition
stopLoss := close + close * StopLossPercent
stopLoss_priceAction := close
if stopLossType == "Trailing"
if strategy.position_size > 0 and close > stopLoss_priceAction
stopLoss_priceAction := close
stopLoss := close - close * StopLossPercent
if strategy.position_size < 0 and close < stopLoss_priceAction
stopLoss_priceAction := close
stopLoss := close + close * StopLossPercent
// plot(stopLoss, color=color.white)
//---Determine signals------------------------------------------------
plotshape(takeProfitLongCondition and showTakeProfit, title = "TakeProfitLong", style = shape.circle, size = size.small, location = location.abovebar, color = color.yellow)
plotshape(takeProfitShortCondition and showTakeProfit, title = "TakeProfitShort", style = shape.circle, size = size.small, location = location.belowbar, color = color.yellow)
plotshape(stopLossLongCondition and showStopLoss, title="StopLossLong", style = shape.circle, size = size.small, location = location.abovebar, color = color.blue)
plotshape(stopLossShortCondition and showStopLoss, title="StopLossShort", style = shape.circle, size = size.small, location = location.abovebar, color = color.blue)
Overview
Range + Trend Strategy computes linear slope and intercept values to project dynamic trend lines on the main price chart. Optional take profit and stop loss (fixed percentage or trailing) can be toggled on. By default, both take profit and stop loss parameters are disabled. Pivot markers use a negative offset, which plots retroactively once confirmed.
Pivot high and pivot low detection
Dynamic resistance and support slope calculations
Configurable percentage take profit and trailing stop loss
Visual indicators for pivot points and connection lines
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
Source
source
close
src
Sampling Period
int
100
samplingPeriod
Range Multiplier
float
4.0
rangeMultiplier
Use TakeProfit?
bool
false
useTakeProfit
TakeProfit %(0-100)
float
1.5
takeProfitPercent
Use StopLoss?
bool
false
useStopLoss
StopLoss type
string
Percent
stopLossType
StopLoss %(0-100)
float
1.5
StopLossPercent
Pivot Detection: Left Bars
int
100
leftbars
Pivot Detection: Right Bars
int
15
rightbars
Show TakeProfit
bool
true
showTakeProfit
Show StopLoss
bool
true
showStopLoss
Show all 14 settings
Setting
Type
Default
Variable
Show Trend Lines
bool
true
showTrend
Show Pivot Connections
bool
true
showPivotsConnection
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.
This trend-following strategy executes a classic Donchian channel breakout system designed to cross-validate backtest results against external framework benchmarks. It triggers a long entry when the closing price breaks above the highest high of the prior entr
Omar Edited WF is a trading strategy overlay built around Williams Fractals and multiple Exponential Moving Averages. It detects high and low fractal pivot points over configurable lookback periods alongside three adjustable EMA trendlines. Long entries are ma
This strategy calculates dynamic support and resistance levels using pivot highs and lows alongside a custom smoothed trend filter and a 3-period EMA. It identifies entry opportunities when price breaks out relative to the trend filter and short-term EMA cross
The Chained Linear Regression Reversal Strategy plots linear regression channels with upper and lower standard deviation bands directly on the price chart across a user-defined time window. When price breaks out of the static channel for a confirmed number of
This overlay strategy plots a two-timeframe Ichimoku setup on the price chart, with Tenkan, Kijun, Chikou, and Kumo from a base higher timeframe and an optional highest-timeframe cloud. It allows two entries in the same direction and uses ATR-based trailing st
This strategy combines three Exponential Moving Averages with Williams Fractals to execute trend-following or mean-reverting trades. It analyzes price structure using short, medium, and long-term EMAs along with fractal highs and lows. Entries trigger when fra