Donchian Fibonacci Zones with Trading Features

eykpunter · study · 122 行 · 点赞 3,922 · TradingView 原页

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

Pine Script

//@version=3
study("Donchian Fibonacci Zones with Trading Features", shorttitle="DFT", overlay=true)
//INPUTS
per=input(24, type=integer, minval=15, maxval=60)
preunit=input('atr', title="prediction in atr or percent", options=['atr', 'percent'])
preamount=input(1, title="added amount of prediction", type=float, minval=0, maxval=6)
 
//CHANNEL
hb= highest(high,per) //High Border
lb = lowest(low,per)   //Low Border
dist = hb-lb          //range of the channel 
med = (hb+lb)/2        //median of the channel
 
hf = hb-dist*0.236    //Highest Fib
chf = hb-dist*0.382    //Center High Fib
clf = hb-dist*0.618    //Center Low Fib
lf = hb-dist*0.764     //Lowest Fib
 
//PULL BACK LEVELS
//initialise mutatables
leh=hb
lel=lb
hbtrue=true
 
evpup = high>hb[1] //push up event
evhbstart =  hb[3]==hb[2] and hb[2]==hb[1] and evpup //horizontal then up, part of evhb calculation
evpdown = low<lb[1] //push down event
evlbstart = lb[3]==lb[2] and lb[2]==lb[1] and evpdown //horizontal then down, part of evlb calculation
 
//two levels will be merged in the plot
evhb = evhbstart or high[1] == hb //event with high border
leh := evhb? hf :leh[1] //level high mutatable set to highest fib on event or kept on value
evlb = evlbstart or low[1] == lb  //event with low border
lel := evlb? lf :lel[1] //level low mutatable set to lowest fib on event or kept on value
hbtrue := evhb?true :evlb?false :hbtrue[1] //last event mutatable
 
//ENTRY MARKERS
tol=atr(per)*0.2 //tolerance for placing triangles and prediction candles at borders
//initialise mutatables
hftrue = true
lftrue = true
cftrue = true
evupin = crossover(close, hf) //market enters up trend
evupout = crossunder(close, hf) //market leaves up trend
hftrue := evupin? true :evupout? false :hftrue[1] //mutatable true if in up trend
evdownin = crossunder(close, lf) //market enters down trend
evdownout = crossover(close, lf) //market leaves down trend
lftrue := evdownin? true :evdownout? false :lftrue[1] //mutatable true if in down trend
cftrue := not hftrue and not lftrue? true : false    //mutatable true if instrument is ranging
 
//IDENTIFY LAST BAR
last= barstate.islast
last:= barstate.isrealtime and isintraday? not barstate.isconfirmed :barstate.islast 
//barstate.islast works also when all bars are confirmed and are history. However in real time and intradaycharts bars never lose this state.
//In that situation 'not barstate.isconfirmed' (like not ishistory) is the better alternative. if you use intraday charts after closing time of
//the markets "barstate.isrealtime and isintraday" will be false and the script will use barstate.islast.
 
//SITUATION 
hbdtrue = last and hftrue  //up trend
lbdtrue = last and lftrue  //down trend
chfdtrue = last and cftrue and hbtrue //high ranging
clfdtrue = last and cftrue and not hbtrue //low ranging
 
//expected levels
schigh = hb
schigh := hbdtrue? hb :lbdtrue? lf :chfdtrue? hf :clfdtrue? chf :na //situation candle high
sclow = lb
sclow := hbdtrue? hf :lbdtrue? lb :chfdtrue? clf :clfdtrue? lf :na //situation candle low
 
//prediction levels
pred= preunit=='atr'? preamount*atr(per) :preunit=='percent'? preamount*close/100 :0 
prcahigh = med
prcahigh := hbdtrue and (evpup or evpup[1])? hb+pred :na //prediction level higher than high border
prcalow = med
prcalow :=  lbdtrue and (evpdown or evpdown[1])? lb-pred :na //prediction level lower than low border
 
//HULL MOVING AVERAGE
hma = wma((2 * wma(close, per / 2)) - wma(close, per), round(sqrt(per)))
 
 
//PLOTTINGS
//plot channel and fibzones
phb=plot(hbdtrue? na :hb, title="high border", color=black)
phf = plot(hbdtrue or chfdtrue? na :hf, title="highest fib", color=gray, transp=80)
pchf = plot(last? na :chf, title="centhi fib", color=gray, transp=80)
pclf = plot(last? na :clf, title="centlo fib", color=gray, transp=80)
plf = plot(lbdtrue or clfdtrue? na :lf, title= "lowest fib", color= gray, transp= 80)
plb = plot(lbdtrue? na :lb, title="low border", color=black)
 
fill(phb,phf, color= hftrue? #00FFFF70 :#00FFFF10, title="up trend zone") //up trend zone, blue or transparant blue
fill(pchf,pclf, color= cftrue?#40404060 :#40404010, title="ranging zone")   // ranging zone, dark gray or light gray
fill(plf,plb, color= lftrue? #FF800099 :#FF800010, title="down trend zone")   //down trend zone, orange or transparant orange
 
//plot pull back levels
plot(last? na :hbtrue?leh:lel, title="swing level", color= hbtrue?#0000ff99:#ff000099, linewidth=3, style=line) //levels blue purple
 
//plot entry markers
plotshape(evupin?hb+tol :na, style=shape.triangleup, location=location.absolute, color=blue, size=size.tiny)
plotshape(evupout?hb+tol :na, style=shape.xcross, location=location.absolute, color=gray, size=size.tiny)
plotshape(evdownin?lb-tol :na, style=shape.triangledown, location=location.absolute, color=red, size=size.tiny)
plotshape(evdownout?lb-tol :na, style=shape.xcross, location=location.absolute, color=gray, size=size.tiny)
 
//plot future circle markers
plotshape(hbdtrue?hb:na, style=shape.circle, location=location.absolute, color=blue, offset=1, size=size.tiny)
plotshape(lbdtrue?lb:na, style=shape.circle, location=location.absolute, color=red, offset=1, size=size.tiny)
plotshape(chfdtrue?chf:na, style=shape.circle, location=location.absolute, color=green, offset=1, size=size.tiny)
plotshape(clfdtrue?clf:na, style=shape.circle, location=location.absolute, color=maroon, offset=1, size=size.tiny)
 
//plot expected range levels
plot(schigh, linewidth = 5, title=" expect", color=teal, offset=3)
plot(sclow, linewidth = 5, title=" expect", color=teal, offset=3)
 
//plot prediction levels 
plot(last and (evpup or evpup[1]) and preamount>0?prcahigh :na, style=cross, title=" predict", linewidth = 5, color = blue, offset=3)
plot(last and (evpdown or evpdown[1]) and preamount>0?prcalow :na, style=cross, title=" predict", linewidth = 5, color = red, offset=3)
 
//plot predictionmarkers
plotshape(hbdtrue and (evpup or evpup[1])? hb+tol :na, style=shape.diamond, location=location.absolute, color=blue, size=size.tiny, offset=3)
plotshape(lbdtrue and (evpdown or evpdown[1])? lb-tol :na, style=shape.diamond, location=location.absolute, color=red, size=size.tiny, offset=3)
 
//plot Hull moving average
plot(last? na :hma, title="Hull ma", linewidth=2, color=navy)

← 返回列表