0% found this document useful (0 votes)
15 views6 pages

final script

The document is a Pine Script code for a trading indicator that includes various technical analysis tools such as Bollinger Bands, TEMA, LRC, and Donchian Channels. It allows users to input parameters for moving averages and alerts for buy/sell signals based on market conditions. Additionally, it incorporates features for visual representation of price action and breakout detection, along with currency conversion rates.

Uploaded by

Muneeba Qureshi
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)
15 views6 pages

final script

The document is a Pine Script code for a trading indicator that includes various technical analysis tools such as Bollinger Bands, TEMA, LRC, and Donchian Channels. It allows users to input parameters for moving averages and alerts for buy/sell signals based on market conditions. Additionally, it incorporates features for visual representation of price action and breakout detection, along with currency conversion rates.

Uploaded by

Muneeba Qureshi
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/ 6

//@version=5

indicator("Requesting chart points demo", "HTF Boxes", true, max_boxes_count = 500)


length = input.int(20, minval=1)
src_bb = input(close, title="Source")
mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev")
basis = ta.sma(src_bb, length)
dev_bb = mult * ta.stdev(src_bb, length)
upper = basis + dev_bb
lower = basis - dev_bb
offset = input.int(0, "Offset", minval = -500, maxval = 500)
plot(basis, "Basis", color=#FF6D00, offset = offset)
p1 = plot(upper, "Upper", color=#2962FF, offset = offset)
p2 = plot(lower, "Lower", color=#2962FF, offset = offset)
fill(p1, p2, title = "Background", color=color.rgb(33, 150, 243, 95))

// TEMA
len1 = input.int(10, minval=1, title="Length 1")
len2 = input.int(20, minval=2, title="Length 2")
ema1 = ta.ema(close, len1)
ema11 = ta.ema(ema1, len1)
ema111 = ta.ema(ema11, len1)
tema1 = 3 * (ema1 - ema11) + ema111
ema2 = ta.ema(close, len2)
ema22 = ta.ema(ema2, len2)
ema222 = ta.ema(ema22, len2)
tema2 = 3 * (ema2 - ema22) + ema222
color_tema1 = color.new(color.black, 20)
color_tema2 = color.new(color.maroon, 20)
plot(tema1, color=color_tema1)
plot(tema2, color=color_tema2)

//LRC
src = input.source(defval = close, title = "Source")
len = input.int(defval = 100, title = "Length", minval = 10)
devlen = input.float(defval = 2., title = "Deviation", minval = 0.1, step = 0.1)
extendit = input(defval = true, title = "Extend Lines")
showfibo = input(defval = false, title = "Show Fibonacci Levels")
showbroken = input(defval = true, title = "Show Broken Channel", inline = "brk")
brokencol = input(defval = color.blue, title = "", inline = "brk")
upcol = input(defval = color.lime, title = "Up/Down Trend Colors", inline =
"trcols")
dncol = input(defval = color.red, title = "", inline = "trcols")
widt = input.int(defval = 2, title = "Line Width")

var fibo_ratios = array.new_float(0)


var colors = array.new_color(2)
if barstate.isfirst
array.unshift(colors, upcol)
array.unshift(colors, dncol)
array.push(fibo_ratios, 0.236)
array.push(fibo_ratios, 0.382)
array.push(fibo_ratios, 0.618)
array.push(fibo_ratios, 0.786)

get_channel(src, len)=>
mid = math.sum(src, len) / len
slope = ta.linreg(src, len, 0) - ta.linreg(src, len, 1)
intercept = mid - slope * math.floor(len / 2) + ((1 - (len % 2)) / 2) * slope
endy = intercept + slope * (len - 1)
dev = 0.0
for x = 0 to len - 1
dev := dev + math.pow(src[x] - (slope * (len - x) + intercept), 2)
dev := math.sqrt(dev/len)
[intercept, endy, dev, slope]

[y1_, y2_, dev, slope] = get_channel(src, len)

outofchannel = (slope > 0 and close < y2_ - dev * devlen) ? 0 : (slope < 0 and
close > y2_ + dev * devlen) ? 2 : -1

var reglines = array.new_line(3)


var fibolines = array.new_line(4)
for x = 0 to 2
if not showbroken or outofchannel != x or nz(outofchannel[1], -1) != -1
line.delete(array.get(reglines, x))
else
line.set_color(array.get(reglines, x), color = brokencol)
line.set_width(array.get(reglines, x), width = 2)
line.set_style(array.get(reglines, x), style = line.style_dotted)
line.set_extend(array.get(reglines, x), extend = extend.none)

array.set(reglines, x,
line.new(x1 = bar_index - (len - 1),
y1 = y1_ + dev * devlen * (x - 1),
x2 = bar_index,
y2 = y2_ + dev * devlen * (x - 1),
color = array.get(colors,
math.round(math.max(math.sign(slope), 0))),
style = x % 2 == 1 ? line.style_solid : line.style_dashed,
width = widt,
extend = extendit ? extend.right : extend.none))
if showfibo
for x = 0 to 3
line.delete(array.get(fibolines, x))
array.set(fibolines, x,
line.new(x1 = bar_index - (len - 1),
y1 = y1_ - dev * devlen + dev * devlen * 2 *
array.get(fibo_ratios, x),
x2 = bar_index,
y2 = y2_ - dev * devlen + dev * devlen * 2 *
array.get(fibo_ratios, x),
color = array.get(colors,
math.round(math.max(math.sign(slope), 0))),
style = line.style_dotted,
width = widt,
extend = extendit ? extend.right : extend.none))

var label sidelab = label.new(x = bar_index - (len - 1), y = y1_, text = "S", size
= size.large)
txt = slope > 0 ? slope > slope[1] ? "⇑" : "⇗" : slope < 0 ? slope < slope[1] ? "⇓"
: "⇘" : "⇒"
stl = slope > 0 ? slope > slope[1] ? label.style_label_up :
label.style_label_upper_right : slope < 0 ? slope < slope[1] ?
label.style_label_down : label.style_label_lower_right : label.style_label_right
label.set_style(sidelab, stl)
label.set_text(sidelab, txt)
label.set_x(sidelab, bar_index - (len - 1))
label.set_y(sidelab, slope > 0 ? y1_ - dev * devlen : slope < 0 ? y1_ + dev *
devlen : y1_)
label.set_color(sidelab, slope > 0 ? upcol : slope < 0 ? dncol : color.blue)

alertcondition(outofchannel, title='Channel Broken', message='Channel Broken')

// direction
trendisup = math.sign(slope) != math.sign(slope[1]) and slope > 0
trendisdown = math.sign(slope) != math.sign(slope[1]) and slope < 0
alertcondition(trendisup, title='Up trend', message='Up trend')
alertcondition(trendisdown, title='Down trend', message='Down trend')

//INPUTS
MaFast_period = input.int(1,"Ma Fast period",1,1000,1)
MaSlow_period = input.int(34,"Ma Slow period",1,1000,1)
Signal_period = input.int(4,"Signal period",1,1000,1)
Show_gains = input(title="Show Wins/Losses ?", defval=true)

//CALCULATION
smaFast = ta.sma(hl2, MaFast_period)
smaSlow = ta.sma(hl2, MaSlow_period)

//SIGNAL
buffer1 = smaFast - smaSlow
buffer2 = ta.wma(buffer1, Signal_period)

//CONDITIONS
buyCondition = buffer1 > buffer2 and buffer1[1] < buffer2[1] and not (buffer1 <
buffer2 and buffer1[1] > buffer2[1])
sellCondition = buffer1 < buffer2 and buffer1[1] > buffer2[1] and not (buffer1 >
buffer2 and buffer1[1] < buffer2[1])

buy = buyCondition==1
sell = sellCondition==1
win = (buy[MaFast_period]==1 and close[MaFast_period]<close) or
(sell[MaFast_period]==1 and close[MaFast_period]>close)
loss = (buy[MaFast_period]==1 and close[MaFast_period]>=close) or
(sell[MaFast_period]==1 and close[MaFast_period]<=close)

//PLOTS
plotshape(series=buyCondition, title="Buy", style=shape.labelup,
location=location.belowbar, color=color.green, size=size.small, text="Buy",
textcolor=color.white)
plotshape(series=sellCondition, title="Sell", style=shape.labeldown,
location=location.abovebar, color=color.red, size=size.small, text="Sell",
textcolor=color.white)

//ALERTS
alertcondition(buyCondition, title='Alert: [SMT] BO1M: Buy Signal', message='Buy!')
alertcondition(sellCondition, title='Alert: [SMT] BO1M: Sell Signal',
message='Sell!')

// Channel settings
dcLen = input.int(20, title="Channel Length", minval=2)
// Visual settings
showMid = input.bool(false, title="Show Midpoint Line?")
fillDc = input.bool(false, title="Fill Donchian Channel?")

// Signal settings
brkOutFilter = input.int(0, title="Breakout Filter (Ticks)", minval=0) *
syminfo.mintick

showBreakouts = input.bool(true, title="Highlight Breakouts?")

// Step 2. Calculate indicator values


hiHighs = ta.highest(high, dcLen)[1]
loLows = ta.lowest(low, dcLen)[1]

midPoint = (hiHighs + loLows) / 2

// Step 3. Determine indicator signals


// See if prices crossed above/below Donchian Channel
upBrkOut = barstate.isconfirmed and
ta.crossover(close, hiHighs + brkOutFilter)

downBrkOut = barstate.isconfirmed and


ta.crossunder(close, loLows - brkOutFilter)

// Step 4. Output indicator data


// Plot Donchian Channel
ub = plot(hiHighs, color=color.green, title="DoCh UB")
lb = plot(loLows, color=color.red, title="DoCh LB")

// Fill background between Donchian bands


fill(plot1=ub, plot2=lb, color=fillDc ? color.new(#4169e1, 95) :
color.new(color.white, 100))

// Show midpoint line, when enabled


plot(showMid ? midPoint : na, color=color.orange, title="DoCh Mid")

// Highlight breakouts with a coloured background


highlightColour = not showBreakouts ? na :
upBrkOut ? color.new(color.green, 80) :
downBrkOut ? color.new(color.red, 80) :
na

bgcolor(highlightColour)

// Step 5. Create indicator alerts


alertcondition(condition=upBrkOut,
title="Long Entry",
message="Price closed above Donchian Channel")

alertcondition(condition=downBrkOut,
title="Short Entry",
message="Price closed below Donchian Channel")

// Calculate highest high and determine breakout

hiBreakout = barstate.isconfirmed and


close > hiHighs
// Plot highest high
plot(hiHighs, color=color.orange)

// Colour background when there's a breakout


bgcolor(hiBreakout ? color.green : na)
// LowerWickRange() returns the bar's lower wick range, which is the
// distance between the low and open (green bars) or close (red bars).
LowerWickRange() =>
math.min(open, close) - low

// Compute the 45-bar WMA of the lower wick size


avgDownWick = ta.wma(LowerWickRange(), 45)

// Figure out the plot colour for the lower wick columns
downWickColour = if LowerWickRange() >= avgDownWick
color.orange
else
color.teal

// Show the bar's down wick range with a column plot


plot(LowerWickRange(), style=plot.style_columns, color=downWickColour,
title="Lower Wick Size")

// Plot the average down wick size with a line


plot(avgDownWick, color=color.blue, linewidth=2,
title="Average Down Wick")
// Compute the 45-bar WMA of the lower wick size

// Figure out the plot colour for the lower wick columns

// Show the bar's down wick range with a column plot


plot(LowerWickRange(), style=plot.style_columns, color=downWickColour,
title="Lower Wick Size")
// Plot the average down wick size with a line
plot(avgDownWick, color=color.blue, linewidth=2,
title="Average Down Wick")

// UpperWickRange() returns the bar's upper wick range, which is the


// difference between the high and open (red bars) or close (green bars).
UpperWickRange() =>
high - math.max(open, close)

// Compute the 45-bar EMA of the bar's upper wick range


avgUpWick = ta.ema(UpperWickRange(), 45)

//@variable The timeframe to request data from.


string higherTimeframe = input.timeframe("1D", "Timeframe")

// Raise a runtime error if the `higherTimeframe` is smaller than the chart's


timeframe.
if timeframe.in_seconds(higherTimeframe) < timeframe.in_seconds(timeframe.period)
runtime.error("The selected timeframe is too small. Choose a higher
timeframe.")

//@variable A `chart.point` containing top-left coordinates from the last confirmed


bar.
topLeft = chart.point.now(high)[1]
//@variable A `chart.point` containing bottom-right coordinates from the last
confirmed bar.
bottomRight = chart.point.from_time(time_close, low)[1]

// Request the last confirmed `topLeft` and `bottomRight` chart points from the
`higherTimeframe`.
[requestedTopLeft, requestedBottomRight] = request.security(
syminfo.tickerid, higherTimeframe, [topLeft, bottomRight], lookahead =
barmerge.lookahead_on
)

// Draw a new box when a new `higherTimeframe` bar starts.


// The box uses the `time` fields from the `requestedTopLeft` and
`requestedBottomRight` as x-coordinates.
if timeframe.change(higherTimeframe)
box.new(
requestedTopLeft, requestedBottomRight, color.purple, 3,
xloc = xloc.bar_time, bgcolor = color.new(color.purple, 90)
)

//@variable The currency to convert.


simple string fromCurrency = currency.GBP
//@variable The resulting currency.
simple string toCurrency = currency.JPY

//@variable The spread symbol to request. Required in `request.security()` since no


direct "FX_IDC" rate exists.
simple string spreadSymbol = str.format("FX_IDC:{0}{2} * FX_IDC:{2}{1}",
fromCurrency, toCurrency, currency.USD)

//@variable The non-repainting conversion rate from `request.security()` using the


`spreadSymbol`.
float securityRequestedRate = request.security(spreadSymbol, "1D", close[1],
lookahead = barmerge.lookahead_on)
//@variable The non-repainting conversion rate from `request.currency_rate()`.
float nonSecurityRequestedRate = request.currency_rate(fromCurrency, toCurrency)

// Plot the requested rates. We can multiply TRY values by these rates to convert
them to KRW.
plot(securityRequestedRate, "`request.security()` value", color.purple, 5)
plot(nonSecurityRequestedRate, "`request.currency_rate()` value", color.yellow, 2)

//@variable The size of the table's text.


string tableSize = input.string(
size.large, "Table size", [size.auto, size.tiny, size.small, size.normal,
size.large, size.huge]
)

You might also like