本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=3
study(title = "Donchian Channel Alerts R1 by JustUncleL", shorttitle="DONALERT", overlay=true)
//
// Author: JustUncleL
//
// Description:
// This idea is based on the Donchain Channel centre line Price action. When price moves from the highest/lowest
// point, the price will move to the center line first. At this point, the center line acts as dynamic
// support/resistance and often price will bounce back up. However, if price successfully breaks the center line,
// primary entry condition, then prices will tend to catch up to the bottom channel line and even make further
// moves in that direction, secondary entry condition.
//
// This script idea is designed to be used with Renko (10pip brick recommended) Renko or
// Heikin Ashi (1 hour recommended) charts. It combines the price action with a directional
// coloured EMA (default length 8) and a Donchian Channel to provide entry and exit signals.
//
// There are three options to exit trade:
// - MA Cross (default exit) = exit occur when price breaks EMA in opposite direction.
// - Centre Cross = exit occurs when price breaks back passed the centre line in opposite direction.
// - Brick Colour = exit when a brick/bar paints in the opposite colour to trade direction.
//
// Each Entry and Exit signal creates an Alertcondition that can be picked up by the TradingView Alarm system.
//
// TIP: Remember this type of Trading technique only works well in a trending market. Do not try to trade
// this technique in a ranging/flat market.
//
// TIP: To get 10pip Bricks set Renko to "Traditional" type bricks and 0.001 for non-JPY currency pairs, and
// 0.1 for JPY currency pairs. Also set chart Time frame to 5min or 15mins.
//
// References:
// - CM_Donchian Channels V3 revised by JustUncleL
//
// Modifications:
// R1 15-Sep-2017 Original
//
// === INPUTS
// MA - type, source, length
ma_type = input(defval="EMA", title="MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"])
ma_len = input(defval=8, title="MA - Length", minval=1)
ma_src = input(close, title="MA - Source")
//
donlength = input(30, minval=1, title="Donchian Channel Lookback Length")
exitcond = input("Cross MA",title="Exit Trade when Price ", options=["Cross MA", "Cross Centre", "Brick Colour"])
//
// === /INPUTS
//
// === FUNCTIONS
// - variant(type, src, len)
// Returns MA input selection variant, default to SMA if blank or typo.
// SuperSmoother filter
// © 2013 John F. Ehlers
variant_supersmoother(src,len) =>
a1 = exp(-1.414*3.14159 / len)
b1 = 2*a1*cos(1.414*3.14159 / len)
c2 = b1
c3 = (-a1)*a1
c1 = 1 - c2 - c3
v9 = 0.0
v9 := c1*(src + nz(src[1])) / 2 + c2*nz(v9[1]) + c3*nz(v9[2])
v9
variant_smoothed(src,len) =>
v5 = 0.0
v5 := na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len
v5
variant_zerolagema(src,len) =>
ema1 = ema(src, len)
ema2 = ema(ema1, len)
v10 = ema1+(ema1-ema2)
v10
variant_doubleema(src,len) =>
v2 = ema(src, len)
v6 = 2 * v2 - ema(v2, len)
v6
variant_tripleema(src,len) =>
v2 = ema(src, len)
v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
v7
// return variant, defaults to SMA
variant(type, src, len) =>
type=="EMA" ? ema(src,len) :
type=="WMA" ? wma(src,len):
type=="VWMA" ? vwma(src,len) :
type=="SMMA" ? variant_smoothed(src,len) :
type=="DEMA" ? variant_doubleema(src,len):
type=="TEMA" ? variant_tripleema(src,len):
type=="HullMA"? wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) :
type=="SSMA" ? variant_supersmoother(src,len) :
type=="ZEMA" ? variant_zerolagema(src,len) :
type=="TMA" ? sma(sma(src,len),len) : sma(src,len)
// - /variant
// === /FUNCTIONS
dodgerblue = #1E90FF
// === Moving Average
ma_series = variant(ma_type,ma_src,ma_len)
// Get direction based on MA
direction = 0
direction := rising(ma_series,3) ? 1 : falling(ma_series,3) ? -1 : nz(direction[1])
// Plot MA series and color it according too direction
pcol = direction>0 ? lime : direction<0 ? red : na
plot(ma_series, title="MA Plot", color=pcol, linewidth=2,style=line,join=true, transp=10)
// === /Moving Average
// === Donchian Channel
upper = highest(donlength)
lower = lowest(donlength)
basis = avg(upper, lower)
break_Above = high > upper[1]?1:0
break_Below = low < lower[1]?1:0
break_Above_B = close > basis[1]?1:0
break_Below_B = close < basis[1]?1:0
// Plot channel
u = plot(upper, title="Upper DC Band", style=line, linewidth=1, color=blue,offset=0)
l = plot(lower, title="Lower DC Band", style=line, linewidth=1, color=blue,offset=0)
plot(basis, title="Mid-Line", color=orange, style=line, linewidth=2, offset=0)
fill(u, l, color=blue, transp=95, title="Fill")
//
higherhigh = 0
higherhigh := break_Above ? nz(higherhigh[1])>0?higherhigh[1]+1:1 : 0
// Count the lower breaks.
lowerlow = 0
lowerlow := break_Below ? nz(lowerlow[1])>0?lowerlow[1]+1:1 : 0
//plotshape(ShowHHLL ? higherhigh : na, title='HH', style=shape.square, location=location.abovebar, color=black, text="HH", offset=0,transp=0)
//plotshape(ShowHHLL ? lowerlow : na, title='LL', style=shape.square, location=location.belowbar, color=black, text="LL", offset=0,transp=0)
// === /Donchian Channel
// === Calculate Alerts
// Entry conditions
PA = (close>open) and ((break_Above_B==1 and (break_Above_B[1]==0 or break_Above_B[2]==0)) or (higherhigh>1))? 1 :
(close<open) and ((break_Below_B==1 and (break_Below_B[1]==0 or break_Below_B[2]==0)) or (lowerlow>1))? -1 : 0
MA = close>ma_series? 1 : close<ma_series? -1 : 0
long = PA==1 and MA ==1 //and ma_series>=basis
short = PA==-1 and MA ==-1 //and ma_series<=basis
// Exit conditions
exitlong = exitcond=="Cross MA"? MA<0 ? 1 : MA>0 ? 0 : 0 :
exitcond=="Cross Centre"? break_Below_B ? 1 : 0 :
exitcond=="Brick Colour"? (close<open and close[1]>open[1]) ? 1 : 0 : 0
exitshort= exitcond=="Cross MA"? MA>0 ? 1 : MA<0 ? 0 : 0 :
exitcond=="Cross Centre"? break_Above_B ? 1 : 0 :
exitcond=="Brick Colour"? (close>open and close[1]<open[1]) ? 1 : 0 : 0
// Alert State
olong = 0
olong := nz(olong[1])>0? exitlong==1? 0 : olong[1]+1 : long? 1 : 0
oshort = 0
oshort := nz(oshort[1])>0? exitshort==1? 0 : oshort[1]+1 : short? 1 : 0
// - Plot Alerts
plotarrow(olong==1?1:na, title="BUY Arrow", colorup=lime, maxheight=60, minheight=50, transp=20,offset=0)
plotarrow(oshort==1?-1:na, title="SELL Arrow", colordown=red, maxheight=60, minheight=50, transp=20,offset=0)
//
plotshape(olong[1]>0 and olong==0, title='BUY Exit', style=shape.xcross, location=location.belowbar, color=gray, text="Exit\nBuy", offset=0,transp=0)
plotshape(oshort[1]>0 and oshort==0, title='Sell Exit', style=shape.xcross, location=location.abovebar, color=gray, text="Exit\nSell", offset=0,transp=0)
// - Signal Alerts to TV Alarm system
alertcondition(olong==1,title="Break BUY Alert",message="BUY")
alertcondition(oshort==1,title="Break SELL Alert",message="SELL")
alertcondition(olong[1]>0 and olong==0,title="Break BUY Exit Alert",message="BUY Exit")
alertcondition(oshort[1]>0 and oshort==0,title="Break SELL Exit Alert",message="SELL Exit")
// === /Calculate Alerts
// --- debugs
//plotshape(higherhigh,location=location.bottom)
//plotshape(lowerlow,location=location.bottom)
//plotshape(break_Above_B,location=location.bottom)
//plotshape(break_Below_B,location=location.bottom)
//plotshape(PA,location=location.bottom)
//plotshape(MA,location=location.bottom)
//plotshape(olong,location=location.bottom)
//plotshape(oshort,location=location.bottom)
// --- /debugs
//EOF