Keltner Channel Alerts [QuantNomad]

QuantNomad · study · 94 行 · 点赞 469 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=4
study("Keltner Channel Alerts [QuantNomad]", shorttitle = "Keltner Alerts [QN]", overlay = true)
 
// 
// author: QuantNomad
// date: 2019-12-22
// https://www.tradingview.com/u/QuantNomad/
// 
 
////////////
// INPUTS //
////////////
 
source       = close
useTrueRange = input(true)
length       = input(20, minval=1)
mult         = input(1.0)
 
//////////////////
// CALCULATIONS //
//////////////////
 
// Calculate Keltner Channel
ma      = sma(source, length)
range   = useTrueRange ? tr : high - low
rangema = sma(range, length)
upper = ma + rangema * mult
lower = ma - rangema * mult
 
// Signal for crossing Keltner Channer
crossUpper = crossover( source, upper)
crossLower = crossunder(source, lower)
 
// Buying price - first high afther the cross
bprice = 0.0
bprice := crossUpper ? high+syminfo.mintick : nz(bprice[1])
 
// Selling price - first low afther the cross
sprice = 0.0
sprice := crossLower ? low -syminfo.mintick : nz(sprice[1]) 
 
 
crossBcond = false
crossBcond := crossUpper ? true 
 : na(crossBcond[1]) ? false : crossBcond[1]
 
crossScond = false
crossScond := crossLower ? true 
 : na(crossScond[1]) ? false : crossScond[1]
 
// Condition to cancel orders
cancelBcond = crossBcond and (source < ma or high >= bprice )
cancelScond = crossScond and (source > ma or low <= sprice )
 
position = 0
 
// Active stop long order
waitingB = false
waitingB := position[1] == 1 ? false : crossUpper ? true : cancelBcond ? false : nz(waitingB[1])
 
// Active stop short order
waitingS = false
waitingS := position[1] == -1 ? false : crossLower ? true : cancelScond ? false : nz(waitingS[1])
 
// Long/Short signals
long  = waitingB[1] and high >= bprice
short = waitingS[1] and low <=  sprice
 
// Keeping track of current position
position := long ? 1 : short ? -1 : nz(position[1])
 
//////////
// PLOT //
//////////
 
plot(ma, color = color.blue, title="Basis")
 
u = plot(upper, color=color.blue, title="Upper")
l = plot(lower, color=color.blue, title="Lower")
 
fill(u, l, color=color.blue)
 
plotshape(waitingB ? bprice : na, color = color.green, style = shape.cross, title = "Buy Price",  location = location.absolute)
plotshape(waitingS ? sprice : na, color = color.red,   style = shape.cross, title = "Sell Price", location = location.absolute)
 
plotshape(long,  location = location.abovebar, color = color.green, style = shape.arrowup,   text = "Long",  title = "Long")
plotshape(short, location = location.belowbar, color = color.red,   style = shape.arrowdown, text = "Short", title = "Short")
 
////////////
// ALERTS //
////////////
 
alertcondition(long,  "Keltner Long",  "Keltner Long")
alertcondition(short, "Keltner Short", "Keltner Short")

← 返回列表