Max 发表于 2020-9-23 10:44:24

3 MA w price slope

https://www.tradingview.com/script/H0jMdstt-3-MA-w-price-slope/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © seedof

//@version=4
study("SMA EMA", overlay=true)
moveBar = input(defval=0,title="Adjust all MA range",type=input.integer)
x = input(defval=20,title="short MA",type=input.integer)+moveBar
y = input(defval=60,title="middle MA",type=input.integer)+moveBar
z = input(defval=120,title="long MA",type=input.integer)+moveBar



//slope line with arrow
SwitchArrow=input(defval=true,title="Arrow for Deduction vs Close",type=input.bool)
SwitchY=SwitchArrow?1:-1
xArrow = (close>close) ? line.style_arrow_left: line.style_arrow_right
yArrow = (close>close) ? line.style_arrow_left: line.style_arrow_right
zArrow = (close>close) ? line.style_arrow_left: line.style_arrow_right


slopeS = (SwitchY>0)? line.new(bar_index, close, bar_index, close, width = 4,color=color.black, style = xArrow) : line.new(bar_index, close, bar_index, close, width = 4,color=color.black)
line.delete(slopeS)
slopeM = (SwitchY>0)? line.new(bar_index, close, bar_index, close, width = 4,color=color.orange, style = yArrow): line.new(bar_index, close, bar_index, close, width = 4,color=color.orange)
line.delete(slopeM)
slopeL = (SwitchY>0)? line.new(bar_index, close, bar_index, close, width = 4,color=color.blue, style = zArrow): line.new(bar_index, close, bar_index, close, width = 4,color=color.blue)
line.delete(slopeL)




//price label

l1 = label.new(bar_index-x, na, tostring(x)+'MA '+tostring(close,'#.##'),
color=color.black,
size=size.huge,
textcolor=color.white,
style=close > open ? label.style_labeldown : label.style_labelup,
yloc=close > open ? yloc.abovebar : yloc.belowbar
)
label.delete(l1)

l2 = label.new(bar_index-y, na, tostring(y)+'MA '+tostring(close,'#.##'),
color=color.orange,
size=size.huge,
textcolor=color.white,
   style=close > open ? label.style_labeldown : label.style_labelup,
yloc=close > open ? yloc.abovebar : yloc.belowbar
)
label.delete(l2)

l3 = label.new(bar_index-z, na, tostring(z) +'MA '+tostring(close,'#.##'),
color=color.blue,
size=size.huge,
textcolor=color.white,
style=close > open ? label.style_labeldown : label.style_labelup,
yloc=close > open ? yloc.abovebar : yloc.belowbar
)
label.delete(l3)




//MA line

e1=ema(close,x)
c1=sma(close,x)
e2=ema(close,y)
c2=sma(close,y)
e3=ema(close,z)
c3=sma(close,z)

plot(e1,"emaX",color=color.gray)
plot(c1,"maX",color=color.black, linewidth = 3)
plot(e2,"emaY",color=#FDBCB4)
plot(c2,"maY",color=color.red, linewidth = 3)
plot(e3,"emaZ",color=#ADD8E6)
plot(c3,"maZ",color=color.blue, linewidth = 3)


//color dot for price

cond=barstate.islast
dot1 = plot(cond?close:na,color=color.black,linewidth=5,offset=-x,style=plot.style_circles,transp=0)
dot2 = plot(cond?close:na,color=color.red,linewidth=5,offset=-y,style=plot.style_circles,transp=0)
dot3 = plot(cond?close:na,color=color.blue,linewidth=5,offset=-z,style=plot.style_circles,transp=0)



//////////////////////
///                ///
///短期趋势转折///
///                ///
////////////////////// 注:与雷公的显示不同,逻辑为 前一天或者当天,这两天的 EMA拐头 和 SMA拐头,任意一对判断组合互相验证了,就代表短期趋势出现,并以下跌为优先判断
Switch=input(defval=true,title="MA strategy Barcolor",type=input.bool)
SwitchX=Switch?1:-1

Switchbg=input(defval=true,title="Trend Background Color",type=input.bool)
Switchbackg=Switchbg?1:-1

//calculate the multiplier for smoothing (weighting) the EMA, which typically follows the formula: . For a 20-day moving average, the multiplier would be = 0.0952.
//Finally, the following formula is used to calculate the current EMA:EMA = Closing price x multiplier + EMA (previous day) x (1-multiplier)
//So if EMA(today) > EMA (previous day) then you can consider       EMA(today) = EMA (previous day) * (1+x) and x>0
//EMA(today) = Closing price * multiplier + EMA (previous day) * (1-multiplier)
//EMA (previous day) * (1+x) = Closing price * multiplier + EMA (previous day) * (1-multiplier)
//EMA (previous day) *(x+multiplier) = Closing price* multiplier
//if Closing price> EMA (previous day) then Closing price = EMA (previous day) * (1+y) and y>0
//EMA (previous day) *(x+multiplier) = EMA (previous day) * (1+y)* multiplier
//x+multiplier = multiplier + y*multiplier
//x/multiplier (+1) = (1+) y
//x>0 <==> y>0
// EMA(today) > EMA (previous day)   <==>Closing price> EMA (previous day)

smaxUp = close>close
smayUp = close>close
smazUp = close>close
smaxDown = close<close
smayDown = close<close
smazDown = close<close

emaxUp = close>ema(close,x)
emayUp = close>ema(close,y)
emazUp = close>ema(close,z)
emaxDown = close<ema(close,x)
emayDown = close<ema(close,y)
emazDown = close<ema(close,z)

cs=(close-ema(close,x))/ema(close,x)*100          // divergence use ema
sm=(ema(close,x)-ema(close,y))/ema(close,y)*100
ml=(ema(close,y)-ema(close,z))/ema(close,z)*100
cm=(close-ema(close,y))/ema(close,y)*100
cl=(close-ema(close,z))/ema(close,z)*100
sl=(ema(close,x)-ema(close,z))/ema(close,z)*100



longlinerowsm = ema(close,x)>ema(close,y)         // use ema response faster, sma delay too much
longlinerowml = ema(close,y)>ema(close,z)
longlinerowsml = longlinerowsm and longlinerowml

shortlinerowsm = ema(close,x)<ema(close,y)
shortlinerowml = ema(close,y)<ema(close,z)
shortlinerowsml = shortlinerowsm and shortlinerowml



bgcolor((longlinerowml and Switchbackg >0) ? color.aqua : na,transp=75)
bgcolor((longlinerowsml and Switchbackg >0) ? color.white : na,transp=55)// where top will occur
bgcolor((shortlinerowml and Switchbackg >0) ? color.black : na,transp=75)
bgcolor((shortlinerowsml and Switchbackg >0)? color.gray : na,transp=75)// where bottom will occur


ConBlack = (smaxDown or smaxDown) and (emaxDown or emaxDown)   //Black and Aqua is a short term indicator
ConAqua= not ConBlack and (smaxUp or smaxUp) and (emaxUp or emaxUp)

//ConBlack = smaxDown and (emaxDown or emaxDown)
//ConBlack =close<close and close<ema(close,x)
//ConBlack =low<max(high,high) and close<(ema(low,x))
//ConBlack =low<max(high,high) and (close<ema(close,x) or close<ema(close,x))
//ConBlack =close<close and (close<ema(close,x) or close<ema(close,x))
//ConAqua=close>close and close>ema(close,x)
//ConAqua=high>min(low,low) and close>ema(high,x)
//ConAqua=not ConBlack and (emaxUp or emaxUp) and (smaxUp or smaxUp)
//ConAqua=not ConBlack and (smaxUp or smaxUp) and emaxUp
//ConAqua=not ConBlack and (close>close or close>close) and high>ema(close,x)
//ConAqua=not ConBlack and (close>close or close>close) and high>ema(close,x)
//ConAqua=not ConBlack and ((close>close and close<sma(close,y)) or ((close>close)and close>sma(close,y))) and high>ema(close,x)

ConGray=not (ConAqua or ConBlack)

ChangeColorGray = (ConGray and SwitchX>0) ? color.silver:na
ChangeColorAqua = (ConAqua and SwitchX>0) ? color.aqua:na
ChangeColorBlack = (ConBlack and SwitchX>0) ? color.black:na
barcolor(ChangeColorAqua,editable=false)
barcolor(ChangeColorBlack,editable=false)
barcolor(ChangeColorGray,editable=false)


// Gap
SwitchG=input(defval=false,title="Gap",type=input.bool)
SwitchGap=SwitchG?1:-1

Up1 = (low>high and SwitchGap>0) ?line.new(bar_index,low,bar_index,high,extend=extend.none,color=color.navy,width=3):na
Up2 = (low>high and SwitchGap>0) ?line.new(bar_index,high,bar_index,low,extend=extend.none,color=color.navy,width=3):na
Down1 = (high<low and SwitchGap>0) ?line.new(bar_index,high,bar_index,low,extend=extend.none,color=color.navy,width=3):na
Down2 = (high<low and SwitchGap>0) ?line.new(bar_index,low,bar_index,high,extend=extend.none,color=color.navy,width=3):na


// Credit
GLlabel=label.new(x=bar_index,y=close,text="均线系统\n\n"+"感谢LEI & LoneCapital\n\n"+"感谢jchang274" ,
   style=label.style_label_left,
   color=color.new(color.yellow,transp=100),
   textcolor=color.new(color.silver,transp=50),
   size=size.large)
label.delete(GLlabel)

Max 发表于 2020-9-23 10:46:48

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © seedof

//@version=4
study("SMA EMA", overlay=true)
moveBar = input(defval=0,title="Adjust all MA range",type=input.integer)
x = input(defval=20,title="short MA",type=input.integer)+moveBar
y = input(defval=60,title="middle MA",type=input.integer)+moveBar
z = input(defval=120,title="long MA",type=input.integer)+moveBar



//slope line with arrow
SwitchArrow=input(defval=true,title="Arrow for Deduction vs Close",type=input.bool)
SwitchY=SwitchArrow?1:-1
xArrow = (close>close) ? line.style_arrow_left: line.style_arrow_right
yArrow = (close>close) ? line.style_arrow_left: line.style_arrow_right
zArrow = (close>close) ? line.style_arrow_left: line.style_arrow_right


slopeS = (SwitchY>0)? line.new(bar_index, close, bar_index, close, width = 4,color=color.black, style = xArrow) : line.new(bar_index, close, bar_index, close, width = 4,color=color.black)
line.delete(slopeS)
slopeM = (SwitchY>0)? line.new(bar_index, close, bar_index, close, width = 4,color=color.orange, style = yArrow): line.new(bar_index, close, bar_index, close, width = 4,color=color.orange)
line.delete(slopeM)
slopeL = (SwitchY>0)? line.new(bar_index, close, bar_index, close, width = 4,color=color.blue, style = zArrow): line.new(bar_index, close, bar_index, close, width = 4,color=color.blue)
line.delete(slopeL)




//price label

l1 = label.new(bar_index-x, na, tostring(x)+'MA '+tostring(close,'#.##'),
color=color.black,
size=size.huge,
textcolor=color.white,
style=close > open ? label.style_labeldown : label.style_labelup,
yloc=close > open ? yloc.abovebar : yloc.belowbar
)
label.delete(l1)

l2 = label.new(bar_index-y, na, tostring(y)+'MA '+tostring(close,'#.##'),
color=color.orange,
size=size.huge,
textcolor=color.white,
   style=close > open ? label.style_labeldown : label.style_labelup,
yloc=close > open ? yloc.abovebar : yloc.belowbar
)
label.delete(l2)

l3 = label.new(bar_index-z, na, tostring(z) +'MA '+tostring(close,'#.##'),
color=color.blue,
size=size.huge,
textcolor=color.white,
style=close > open ? label.style_labeldown : label.style_labelup,
yloc=close > open ? yloc.abovebar : yloc.belowbar
)
label.delete(l3)




//MA line

e1=ema(close,x)
c1=sma(close,x)
e2=ema(close,y)
c2=sma(close,y)
e3=ema(close,z)
c3=sma(close,z)

plot(e1,"emaX",color=color.gray)
plot(c1,"maX",color=color.black, linewidth = 3)
plot(e2,"emaY",color=#FDBCB4)
plot(c2,"maY",color=color.red, linewidth = 3)
plot(e3,"emaZ",color=#ADD8E6)
plot(c3,"maZ",color=color.blue, linewidth = 3)


//color dot for price

cond=barstate.islast
dot1 = plot(cond?close:na,color=color.black,linewidth=5,offset=-x,style=plot.style_circles,transp=0)
dot2 = plot(cond?close:na,color=color.red,linewidth=5,offset=-y,style=plot.style_circles,transp=0)
dot3 = plot(cond?close:na,color=color.blue,linewidth=5,offset=-z,style=plot.style_circles,transp=0)



//////////////////////
///                ///
///短期趋势转折///
///                ///
////////////////////// 注:与雷公的显示不同,逻辑为 前一天或者当天,这两天的 EMA拐头 和 SMA拐头,任意一对判断组合互相验证了,就代表短期趋势出现,并以下跌为优先判断
Switch=input(defval=true,title="MA strategy Barcolor",type=input.bool)
SwitchX=Switch?1:-1

Switchbg=input(defval=true,title="Trend Background Color",type=input.bool)
Switchbackg=Switchbg?1:-1

//calculate the multiplier for smoothing (weighting) the EMA, which typically follows the formula: . For a 20-day moving average, the multiplier would be = 0.0952.
//Finally, the following formula is used to calculate the current EMA:EMA = Closing price x multiplier + EMA (previous day) x (1-multiplier)
//So if EMA(today) > EMA (previous day) then you can consider       EMA(today) = EMA (previous day) * (1+x) and x>0
//EMA(today) = Closing price * multiplier + EMA (previous day) * (1-multiplier)
//EMA (previous day) * (1+x) = Closing price * multiplier + EMA (previous day) * (1-multiplier)
//EMA (previous day) *(x+multiplier) = Closing price* multiplier
//if Closing price> EMA (previous day) then Closing price = EMA (previous day) * (1+y) and y>0
//EMA (previous day) *(x+multiplier) = EMA (previous day) * (1+y)* multiplier
//x+multiplier = multiplier + y*multiplier
//x/multiplier (+1) = (1+) y
//x>0 <==> y>0
// EMA(today) > EMA (previous day)   <==>Closing price> EMA (previous day)

smaxUp = close>close
smayUp = close>close
smazUp = close>close
smaxDown = close<close
smayDown = close<close
smazDown = close<close

emaxUp = close>ema(close,x)
emayUp = close>ema(close,y)
emazUp = close>ema(close,z)
emaxDown = close<ema(close,x)
emayDown = close<ema(close,y)
emazDown = close<ema(close,z)

cs=(close-ema(close,x))/ema(close,x)*100          // divergence use ema
sm=(ema(close,x)-ema(close,y))/ema(close,y)*100
ml=(ema(close,y)-ema(close,z))/ema(close,z)*100
cm=(close-ema(close,y))/ema(close,y)*100
cl=(close-ema(close,z))/ema(close,z)*100
sl=(ema(close,x)-ema(close,z))/ema(close,z)*100



longlinerowsm = ema(close,x)>ema(close,y)         // use ema response faster, sma delay too much
longlinerowml = ema(close,y)>ema(close,z)
longlinerowsml = longlinerowsm and longlinerowml

shortlinerowsm = ema(close,x)<ema(close,y)
shortlinerowml = ema(close,y)<ema(close,z)
shortlinerowsml = shortlinerowsm and shortlinerowml



bgcolor((longlinerowml and Switchbackg >0) ? color.aqua : na,transp=75)
bgcolor((longlinerowsml and Switchbackg >0) ? color.white : na,transp=55)// where top will occur
bgcolor((shortlinerowml and Switchbackg >0) ? color.black : na,transp=75)
bgcolor((shortlinerowsml and Switchbackg >0)? color.gray : na,transp=75)// where bottom will occur


ConBlack = (smaxDown or smaxDown) and (emaxDown or emaxDown)   //Black and Aqua is a short term indicator
ConAqua= not ConBlack and (smaxUp or smaxUp) and (emaxUp or emaxUp)

//ConBlack = smaxDown and (emaxDown or emaxDown)
//ConBlack =close<close and close<ema(close,x)
//ConBlack =low<max(high,high) and close<(ema(low,x))
//ConBlack =low<max(high,high) and (close<ema(close,x) or close<ema(close,x))
//ConBlack =close<close and (close<ema(close,x) or close<ema(close,x))
//ConAqua=close>close and close>ema(close,x)
//ConAqua=high>min(low,low) and close>ema(high,x)
//ConAqua=not ConBlack and (emaxUp or emaxUp) and (smaxUp or smaxUp)
//ConAqua=not ConBlack and (smaxUp or smaxUp) and emaxUp
//ConAqua=not ConBlack and (close>close or close>close) and high>ema(close,x)
//ConAqua=not ConBlack and (close>close or close>close) and high>ema(close,x)
//ConAqua=not ConBlack and ((close>close and close<sma(close,y)) or ((close>close)and close>sma(close,y))) and high>ema(close,x)

ConGray=not (ConAqua or ConBlack)

ChangeColorGray = (ConGray and SwitchX>0) ? color.silver:na
ChangeColorAqua = (ConAqua and SwitchX>0) ? color.aqua:na
ChangeColorBlack = (ConBlack and SwitchX>0) ? color.black:na
barcolor(ChangeColorAqua,editable=false)
barcolor(ChangeColorBlack,editable=false)
barcolor(ChangeColorGray,editable=false)


// Gap
SwitchG=input(defval=false,title="Gap",type=input.bool)
SwitchGap=SwitchG?1:-1

Up1 = (low>high and SwitchGap>0) ?line.new(bar_index,low,bar_index,high,extend=extend.none,color=color.navy,width=3):na
Up2 = (low>high and SwitchGap>0) ?line.new(bar_index,high,bar_index,low,extend=extend.none,color=color.navy,width=3):na
Down1 = (high<low and SwitchGap>0) ?line.new(bar_index,high,bar_index,low,extend=extend.none,color=color.navy,width=3):na
Down2 = (high<low and SwitchGap>0) ?line.new(bar_index,low,bar_index,high,extend=extend.none,color=color.navy,width=3):na


// Credit
GLlabel=label.new(x=bar_index,y=close,text="均线系统\n\n"+"感谢LEI & LoneCapital\n\n"+"感谢jchang274" ,
   style=label.style_label_left,
   color=color.new(color.yellow,transp=100),
   textcolor=color.new(color.silver,transp=50),
   size=size.large)
label.delete(GLlabel)
页: [1]
查看完整版本: 3 MA w price slope