本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=2
study(title="Index Adaptive Keltner Channels [DW]", shorttitle="IAKC [DW]", overlay=true)
//by Donovan Wall
//This study is an experiment in adaptive filtering. The process in this study was inspired by KAMA and ZLEMA filtering techniques.
//First, data is given an optional modification for lag reduction.
//Then, an adaptive filter of your choice is calculated. There are 6 different adaptive filters to choose from in this study:
// -Commodity Channel Index Adaptive Moving Average (CCIAMA)
// -Relative Strength Index Adaptive Moving Average (RSIAMA)
// -%R Adaptive Moving Average (%RAMA)
// -Klinger Volume Oscillator Adaptive Moving Average (KVOAMA)
// -Money Flow Index Adaptive Moving Average (MFIAMA)
// -Correlation Coefficient Adaptive Moving Average (CCAMA)
//Next, ATR is calculated using the specified adaptive filter.
//A set of ranges is calculated by multiplying ATR by the square root of the sampling period, then dividing it by 2 and 4.
//And Finally, the ranges are added to and subtracted from the adaptive filter to generate the channels.
//Custom bar colors are included. The formula for the color scheme is based on filter direction and price.
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Inputs
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Source
src = input(defval=hlc3, title="Source")
//Sampling Period
per = input(defval=144, minval=1, title="Sampling Period")
//Reduce Lag
lred = input(defval=false, title="Reduce Lag")
//Mode Selection
mode = input(defval=1, minval=1, maxval=6, title="Mode Selection (1 = CCIAMA, 2 = RSIAMA, 3 = %RAMA, 4 = KVOAMA, 5 = MFIAMA, 6 = CCAMA)")
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Definitions
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Lag Reduction
lag = (per - 1)/sqrt(per)
srcdata = lred ? 2*src - src[lag] : src
trdata = lred ? 2*tr - tr[lag] : tr
//CCIAMA
cciama(a, b)=>
ccicut = cci(a, b) > 100 ? 100 : cci(a, b) < 100 ? -100 : cci(a, b)
ccidata = abs(ccicut)/100
slow = sqrt(b)/(b/2)
fast = sqrt(b)/b
sc = pow(ccidata*(fast - slow) + slow, 2)
cciama = nz(cciama[1], a) + sc*(a - nz(cciama[1], a))
amacci = cciama(srcdata, per)
amaccitr = cciama(trdata, per)
//RSIAMA
rsiama(a, b)=>
rsi = rsi(a, b)/100
slow = sqrt(b)/(b/2)
fast = sqrt(b)/b
sc = pow(rsi*(fast - slow) + slow, 2)
rsiama = nz(rsiama[1], a) + sc*(a - nz(rsiama[1], a))
amarsi = rsiama(srcdata, per)
amarsitr = rsiama(trdata, per)
//%RAMA
rama(a, b)=>
r = ((highest(high, b) - close)/(highest(high, b) - lowest(low, b)))*-100
rdata = (r + 100)/100
slow = sqrt(b)/(b/2)
fast = sqrt(b)/b
sc = pow(rdata*(fast - slow) + slow, 2)
rama = nz(rama[1], a) + sc*(a - nz(rama[1], a))
amar = rama(srcdata, per)
amartr = rama(trdata, per)
//KVOAMA
kvoama(a, b)=>
sv = change(hlc3) >= 0 ? volume : -volume
kvoabs = abs(ema(sv, 34) - ema(sv, 55))
kvocut = kvoabs > 100 ? 100 : kvoabs
kvodata = kvocut/100
slow = sqrt(b)/(b/2)
fast = sqrt(b)/b
sc = pow(kvodata*(fast - slow) + slow, 2)
kvoama = nz(kvoama[1], a) + sc*(a - nz(kvoama[1], a))
amakvo = kvoama(srcdata, per)
amakvotr = kvoama(trdata, per)
//MFIAMA
mfiama(a, b)=>
pf = change(hlc3) >= 0 ? volume*a : 0
nf = change(hlc3) < 0 ? volume*a : 0
mratio = pf/nf
mfi = 100 - (100/(1 + mratio))
mfidata = mfi/100
slow = sqrt(b)/(b/2)
fast = sqrt(b)/b
sc = pow(mfidata*(fast - slow) + slow, 2)
mfiama = nz(mfiama[1], a) + sc*(a - nz(mfiama[1], a))
amamfi = mfiama(srcdata, per)
amamfitr = mfiama(trdata, per)
//CCAMA
ccama(a, b)=>
corr = correlation(time, a, b)
slow = sqrt(b)/(b/2)
fast = sqrt(b)/b
sc = pow(corr*(fast - slow) + slow, 2)
ccama = nz(ccama[1], a) + sc*(a - nz(ccama[1], a))
amacc = ccama(srcdata, per)
amacctr = ccama(trdata, per)
//AMA Mode Selection
ama = mode==1 ? amacci : mode==2 ? amarsi : mode==3 ? amar : mode==4 ? amakvo : mode==5 ? amamfi : amacc
//AMA True Range Selection
amatr = mode==1 ? amaccitr : mode==2 ? amarsitr : mode==3 ? amartr : mode==4 ? amakvotr : mode==5 ? amamfitr : amacctr
//Bands
hbandlg = ama + amatr*(sqrt(per)/2)
hbandsm = ama + amatr*(sqrt(per)/4)
lbandsm = ama - amatr*(sqrt(per)/4)
lbandlg = ama - amatr*(sqrt(per)/2)
//Colors
amacolor = ama > ama[1] ? lime : ama < ama[1] ? red : orange
barcolor = (ama > ama[1]) and (src > src[1]) and (src > ama) ? lime : (ama > ama[1]) and (src < src[1]) and (src > ama) ? green : (ama < ama[1]) and (src < src[1]) and (src < ama) ? red : (ama < ama[1]) and (src > src[1]) and (src < ama) ? maroon : orange
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Plots
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Adaptive Filter
amaplot = plot(ama, color=amacolor, linewidth=2, title="Index Adaptive Moving Average")
//Bands
hbandlgplot = plot(hbandlg, color=amacolor, linewidth=1, title="Adaptive Large High Band")
hbandsmplot = plot(hbandsm, color=amacolor, linewidth=1, title="Adaptive Small High Band")
lbandsmplot = plot(lbandsm, color=amacolor, linewidth=1, title="Adaptive Small Low Band")
lbandlgplot = plot(lbandlg, color=amacolor, linewidth=1, title="Adaptive Large Low Band")
//Band Fills
fill(hbandlgplot, hbandsmplot, color=lime, title="High Band Fill")
fill(lbandsmplot, lbandlgplot, color=red, title="Low Band Fill")
//Bar Color
barcolor(barcolor)