交易员日志

 找回密码
 受邀注册
查看: 6869|回复: 2

3 MA w price slope

[复制链接]

256

主题

887

帖子

898

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
898
发表于 2020-9-23 10:44:24 | 显示全部楼层 |阅读模式
https://www.tradingview.com/script/H0jMdstt-3-MA-w-price-slope/
3 MA w price slope.png
// 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[x]) ? line.style_arrow_left: line.style_arrow_right
yArrow = (close>close[y]) ? line.style_arrow_left: line.style_arrow_right
zArrow = (close>close[z]) ? line.style_arrow_left: line.style_arrow_right


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




//price label

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

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

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




//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[x]:na,color=color.black,linewidth=5,offset=-x,style=plot.style_circles,transp=0)
dot2 = plot(cond?close[y]:na,color=color.red,linewidth=5,offset=-y,style=plot.style_circles,transp=0)
dot3 = plot(cond?close[z]: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: [2 ÷ (number of observations + 1)]. For a 20-day moving average, the multiplier would be [2/(20+1)]= 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[x]
smayUp = close>close[y]
smazUp = close>close[z]
smaxDown = close<close[x]
smayDown = close<close[y]
smazDown = close<close[z]

emaxUp = close>ema(close[1],x)
emayUp = close>ema(close[1],y)
emazUp = close>ema(close[1],z)
emaxDown = close<ema(close[1],x)
emayDown = close<ema(close[1],y)
emazDown = close<ema(close[1],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[1]) and (emaxDown or emaxDown[1])     //Black and Aqua is a short term indicator
ConAqua  = not ConBlack and (smaxUp or smaxUp[1]) and (emaxUp or emaxUp[1])

//ConBlack = smaxDown and (emaxDown or emaxDown[1])
//ConBlack =  close<close[x] and close[1]<ema(close[1],x)
//ConBlack =  low<max(high[x],high[x-1]) and close<(ema(low,x))
//ConBlack =  low<max(high[x],high[x-1]) and (close[1]<ema(close[1],x) or close<ema(close,x))
//ConBlack =  close<close[x] and (close[1]<ema(close[1],x) or close<ema(close,x))
//ConAqua  =  close[1]>close[x+1] and close>ema(close,x)
//ConAqua  =  high>min(low[x-1],low[x]) and close>ema(high,x)
//ConAqua  =  not ConBlack and (emaxUp[1] or emaxUp) and (smaxUp or smaxUp[1])
//ConAqua  =  not ConBlack and (smaxUp[1] or smaxUp) and emaxUp
//ConAqua  =  not ConBlack and (close[1]>close[x+1] or close>close[x]) and high>ema(close,x)
//ConAqua  =  not ConBlack and (close[1]>close[x+1] or close>close[y]) and high>ema(close,x)
//ConAqua  =  not ConBlack and ((close[1]>close[x+1] and close<sma(close,y)) or ((close>close[y])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[1] and SwitchGap>0) ?line.new(bar_index,low,bar_index[1],high[1],extend=extend.none,color=color.navy,width=3):na
Up2 = (low>high[1] and SwitchGap>0) ?line.new(bar_index,high[1],bar_index[1],low,extend=extend.none,color=color.navy,width=3):na
Down1 = (high<low[1] and SwitchGap>0) ?line.new(bar_index,high,bar_index[1],low[1],extend=extend.none,color=color.navy,width=3):na
Down2 = (high<low[1] and SwitchGap>0) ?line.new(bar_index,low[1],bar_index[1],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])
回复

使用道具 举报

256

主题

887

帖子

898

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
898
 楼主| 发表于 2020-9-23 10:46:48 | 显示全部楼层
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © seedof

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



  9. //slope line with arrow
  10. SwitchArrow=input(defval=true,title="Arrow for Deduction vs Close",type=input.bool)
  11. SwitchY=SwitchArrow?1:-1
  12. xArrow = (close>close[x]) ? line.style_arrow_left: line.style_arrow_right
  13. yArrow = (close>close[y]) ? line.style_arrow_left: line.style_arrow_right
  14. zArrow = (close>close[z]) ? line.style_arrow_left: line.style_arrow_right


  15. slopeS = (SwitchY>0)? line.new(bar_index, close, bar_index[x], close[x], width = 4,color=color.black, style = xArrow) : line.new(bar_index, close, bar_index[x], close[x], width = 4,color=color.black)
  16. line.delete(slopeS[1])
  17. slopeM = (SwitchY>0)? line.new(bar_index, close, bar_index[y], close[y], width = 4,color=color.orange, style = yArrow): line.new(bar_index, close, bar_index[y], close[y], width = 4,color=color.orange)
  18. line.delete(slopeM[1])
  19. slopeL = (SwitchY>0)? line.new(bar_index, close, bar_index[z], close[z], width = 4,color=color.blue, style = zArrow): line.new(bar_index, close, bar_index[z], close[z], width = 4,color=color.blue)
  20. line.delete(slopeL[1])




  21. //price label

  22. l1 = label.new(bar_index-x, na, tostring(x)+'MA '+tostring(close[x],'#.##'),
  23.   color=color.black,
  24.   size=size.huge,
  25.   textcolor=color.white,
  26.   style=close[x] > open[x] ? label.style_labeldown : label.style_labelup,
  27.   yloc=close[x] > open[x] ? yloc.abovebar : yloc.belowbar
  28.   )
  29. label.delete(l1[1])

  30. l2 = label.new(bar_index-y, na, tostring(y)+'MA '+tostring(close[y],'#.##'),
  31.   color=color.orange,
  32.   size=size.huge,
  33.   textcolor=color.white,
  34.    style=close[y] > open[y] ? label.style_labeldown : label.style_labelup,
  35.   yloc=close[y] > open[y] ? yloc.abovebar : yloc.belowbar
  36.   )
  37. label.delete(l2[1])

  38. l3 = label.new(bar_index-z, na, tostring(z) +'MA '+tostring(close[z],'#.##'),
  39.   color=color.blue,
  40.   size=size.huge,
  41.   textcolor=color.white,
  42.   style=close[z] > open[z] ? label.style_labeldown : label.style_labelup,
  43.   yloc=close[z] > open[z] ? yloc.abovebar : yloc.belowbar
  44.   )
  45. label.delete(l3[1])




  46. //MA line

  47. e1=ema(close,x)
  48. c1=sma(close,x)
  49. e2=ema(close,y)
  50. c2=sma(close,y)
  51. e3=ema(close,z)
  52. c3=sma(close,z)

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


  59. //color dot for price

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



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

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

  73. //calculate the multiplier for smoothing (weighting) the EMA, which typically follows the formula: [2 ÷ (number of observations + 1)]. For a 20-day moving average, the multiplier would be [2/(20+1)]= 0.0952.
  74. //Finally, the following formula is used to calculate the current EMA:EMA = Closing price x multiplier + EMA (previous day) x (1-multiplier)
  75. //So if EMA(today) > EMA (previous day) then you can consider       EMA(today) = EMA (previous day) * (1+x) and x>0
  76. //EMA(today) = Closing price * multiplier + EMA (previous day) * (1-multiplier)
  77. //EMA (previous day) * (1+x) = Closing price * multiplier + EMA (previous day) * (1-multiplier)
  78. //EMA (previous day) *(x+multiplier) = Closing price* multiplier
  79. //if Closing price> EMA (previous day) then Closing price = EMA (previous day) * (1+y) and y>0
  80. //EMA (previous day) *(x+multiplier) = EMA (previous day) * (1+y)* multiplier
  81. //x+multiplier = multiplier + y*multiplier
  82. //x/multiplier (+1) = (1+) y
  83. //x>0 <==> y>0
  84. // EMA(today) > EMA (previous day)   <==>  Closing price> EMA (previous day)

  85. smaxUp = close>close[x]
  86. smayUp = close>close[y]
  87. smazUp = close>close[z]
  88. smaxDown = close<close[x]
  89. smayDown = close<close[y]
  90. smazDown = close<close[z]

  91. emaxUp = close>ema(close[1],x)
  92. emayUp = close>ema(close[1],y)
  93. emazUp = close>ema(close[1],z)
  94. emaxDown = close<ema(close[1],x)
  95. emayDown = close<ema(close[1],y)
  96. emazDown = close<ema(close[1],z)

  97. cs=(close-ema(close,x))/ema(close,x)*100          // divergence use ema
  98. sm=(ema(close,x)-ema(close,y))/ema(close,y)*100
  99. ml=(ema(close,y)-ema(close,z))/ema(close,z)*100
  100. cm=(close-ema(close,y))/ema(close,y)*100
  101. cl=(close-ema(close,z))/ema(close,z)*100
  102. sl=(ema(close,x)-ema(close,z))/ema(close,z)*100



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

  106. shortlinerowsm = ema(close,x)<ema(close,y)
  107. shortlinerowml = ema(close,y)<ema(close,z)
  108. shortlinerowsml = shortlinerowsm and shortlinerowml



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


  113. ConBlack = (smaxDown or smaxDown[1]) and (emaxDown or emaxDown[1])     //Black and Aqua is a short term indicator
  114. ConAqua  = not ConBlack and (smaxUp or smaxUp[1]) and (emaxUp or emaxUp[1])

  115. //ConBlack = smaxDown and (emaxDown or emaxDown[1])
  116. //ConBlack =  close<close[x] and close[1]<ema(close[1],x)
  117. //ConBlack =  low<max(high[x],high[x-1]) and close<(ema(low,x))
  118. //ConBlack =  low<max(high[x],high[x-1]) and (close[1]<ema(close[1],x) or close<ema(close,x))
  119. //ConBlack =  close<close[x] and (close[1]<ema(close[1],x) or close<ema(close,x))
  120. //ConAqua  =  close[1]>close[x+1] and close>ema(close,x)
  121. //ConAqua  =  high>min(low[x-1],low[x]) and close>ema(high,x)
  122. //ConAqua  =  not ConBlack and (emaxUp[1] or emaxUp) and (smaxUp or smaxUp[1])
  123. //ConAqua  =  not ConBlack and (smaxUp[1] or smaxUp) and emaxUp
  124. //ConAqua  =  not ConBlack and (close[1]>close[x+1] or close>close[x]) and high>ema(close,x)
  125. //ConAqua  =  not ConBlack and (close[1]>close[x+1] or close>close[y]) and high>ema(close,x)
  126. //ConAqua  =  not ConBlack and ((close[1]>close[x+1] and close<sma(close,y)) or ((close>close[y])and close>sma(close,y))) and high>ema(close,x)

  127. ConGray  =  not (ConAqua or ConBlack)

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


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

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


  141. // Credit
  142. GLlabel=label.new(x=bar_index,y=close,text="均线系统\n\n  "+"感谢LEI & LoneCapital\n\n"+"感谢jchang274" ,
  143.      style=label.style_label_left,
  144.      color=color.new(color.yellow,transp=100),
  145.      textcolor=color.new(color.silver,transp=50),
  146.      size=size.large)
  147. label.delete(GLlabel[1])
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 受邀注册

本版积分规则

Archiver|手机版|小黑屋|交易员日志

GMT+8, 2025-5-4 00:35 , Processed in 0.058403 second(s), 25 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表