Advertisement
PineCoders

Trend with S/R

Aug 12th, 2019
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.09 KB | None | 0 0
  1.  
  2. //@version=2
  3. strategy("Trend with S/R", overlay=true)
  4.  
  5.  
  6. //HA
  7. haclose = ((open + high + low + close)/4)//[smoothing]
  8. haopen = na(haopen[1]) ? (open + close)/2 : (haopen[1] + haclose[1]) / 2
  9.  
  10. o = haopen
  11. c = haclose
  12. src = haclose
  13. price = haclose
  14.  
  15. heikUpColor() => haclose > haopen
  16. heikDownColor() => haclose <= haopen
  17. barcolor(heikUpColor() ? green: heikDownColor() ? red : na)
  18.  
  19. //Hi-Lo Range
  20. therange=24
  21. lowbar=lowest(therange)
  22. highbar=highest(therange)
  23. halfrange=(highbar-lowbar)/2
  24. middlebar=lowbar+halfrange
  25. middlehighbar=middlebar+(halfrange*0.61803399)
  26. middlelowbar=middlebar-(halfrange*0.61803399)
  27. closelowbar=middlebar-(halfrange*0.15450849)
  28. closehighbar=middlebar+(halfrange*0.15450849)
  29.  
  30. //RSI
  31. up1 = rma(max(change(src), 0), 9)
  32. down1 = rma(-min(change(src), 0), 9)
  33. rsi = down1 == 0 ? 100 : up1 == 0 ? 0 : 100 - (100 / (1 + up1 / down1))
  34.  
  35. //HMA source for CMO
  36. n1=12
  37. n2ma=2*wma(haclose,round(n1/2))
  38. nma=wma(haclose,n1)
  39. diff=n2ma-nma
  40. sqn=round(sqrt(n1))
  41. C=5
  42. n2ma6=2*wma(haopen,round(C/2))
  43. nma6=wma(haopen,C)
  44. diff6=n2ma6-nma6
  45. sqn6=round(sqrt(C))
  46. a1=wma(diff6,sqn6)
  47. a=wma(diff,sqn)
  48.  
  49. //CMO
  50.  
  51. gains= sum(a1>a, 1)
  52. losses = sum(a1<a, 1)
  53. cmo = 100 * (gains - losses) / (gains + losses)
  54.  
  55. //Close Pivots
  56. len5 = 2
  57. h = highest(len5)
  58. h1 = dev(h, len5) ? na : h
  59. hpivot = fixnan(h1)
  60. l = lowest(len5)
  61. l1 = dev(l, len5) ? na : l
  62. lpivot = fixnan(l1)
  63.  
  64. //Calc Values
  65. sup = rsi<25 and cmo>50 and lpivot
  66. res3 = rsi>75 and cmo<-50 and hpivot
  67. xup = sup ? low : xup[1]
  68. xdown = res3 ? high : xdown[1]
  69. tf1 = security(tickerid, period, xup)
  70. tf2 = security(tickerid, period, xdown)
  71.  
  72. //Plot
  73. plot(tf1, "SR1", color=white, linewidth=1, style=circles, transp=20, join=false, editable=true)
  74. plot(tf2, "SR2", color=white, linewidth=1, style=circles, transp=20, join=false, editable=true)
  75.  
  76. //KDJ
  77.  
  78. hi = highest(high, 9)
  79. lo = lowest(low , 9)
  80. k = 100*((c-lo)/(hi-lo))
  81. pK = rma(k, 3)
  82. pD = rma(pK, 3)
  83. pJ = 3*pK-2*pD
  84. KD = avg(pK, pD)
  85.  
  86. // ICHIMOKU CLOUD
  87. ATRlength = 200
  88. ATRMult = 2.272
  89. ATR = rma(tr(true), ATRlength)
  90. out2 = ema(c, 14)
  91. emaup = out2+(ATR*ATRMult)
  92. emadw = out2-(ATR*ATRMult)
  93.  
  94. conversionPeriods = 5
  95. basePeriods = 26
  96. laggingSpan2Periods = 52
  97. displacement = 26
  98.  
  99. donchian(len2) => avg(lowest(26), highest(26))
  100.  
  101. conversionLine = donchian(conversionPeriods)
  102. baseLine = donchian(basePeriods)
  103. leadLine1 = avg(conversionLine, baseLine)
  104. leadLine2 = donchian(laggingSpan2Periods)
  105.  
  106. //Highest & Lowest
  107. hgst = highest(high,1)
  108. lwst = lowest(low,1)
  109. plot(lwst, color = white)
  110. plot(hgst, color = white)
  111.  
  112. //EMA
  113. ema5 = ema(c,5)
  114. ema10 = ema(c,10)
  115.  
  116. //SLOW RSI
  117. periods = 6
  118. smooth = 14
  119. calc_wima(src, length) =>
  120. MA_s=(src + nz(MA_s[1] * (length-1)))/length
  121. MA_s
  122.  
  123. r1 = ema( price, periods )
  124. r2 = iff( price > r1, price - r1, 0 )
  125. r3 = iff( price < r1, r1 - price, 0 )
  126. r4 = calc_wima( r2, smooth )
  127. r5 = calc_wima( r3, smooth )
  128. rr = iff( r5 == 0, 100, 100 - ( 100 / ( 1 + ( r4 / r5 ) ) ) )
  129.  
  130. //GANN Trend
  131. avghigh = ema(high, 10)
  132. avglow = ema(low, 10)
  133.  
  134. uptrend = high > avghigh
  135. downtrend = low < avglow
  136.  
  137. //GANN Swing
  138. Length = 5
  139. xHH = highest(Length)
  140. xLL = lowest(Length)
  141. xGSO = iff(xHH[2] > xHH[1] and xHH[0] > xHH[1], 1,
  142. iff(xLL[2] < xLL[1] and xLL[0] < xLL[1], -1, nz(xGSO[1],0)))
  143.  
  144. //TREND CONTINUE
  145. xChange = mom(close, 1)
  146. xPlusChange = iff(xChange > 0, xChange, 0)
  147. xMinusChange = iff(xChange < 0, (xChange * -1), 0)
  148. xPlusCF = iff(xPlusChange == 0, 0, xPlusChange + nz(xPlusCF[1], 1))
  149. xMinusCF = iff(xMinusChange == 0, 0, xMinusChange + nz(xMinusCF[1], 1))
  150. xPlus = xPlusChange - xMinusCF
  151. xMinus = xMinusChange - xPlusCF
  152. xPlusTCF = sum(xPlus, 35)
  153. nMinusTCF = sum(xMinus, 35)
  154.  
  155. //DTI
  156. xHMU = iff(high - high[1] > 0, high - high[1], 0)
  157. xLMD = iff(low - low[1] < 0, -(low - low[1]), 0)
  158. xPrice = xHMU - xLMD
  159. xPriceAbs = abs(xPrice)
  160. xuXA = ema(ema(ema(xPrice, 14),10),5)
  161. xuXAAbs = ema(ema(ema(xPriceAbs, 14),10),5)
  162. Val1 = 100 * xuXA
  163. Val2 = xuXAAbs
  164. DTI = iff(Val2 != 0, Val1 / Val2, 0)
  165.  
  166. //Ultimate MA
  167. useCurrentRes = input(true, title="Use Current Chart Resolution?")
  168. resCustom = input(title="Use Different Timeframe? Uncheck Box Above", type=resolution, defval="D")
  169. len = input(20, title="Moving Average Length - LookBack Period")
  170. //periodT3 = input(defval=7, title="Tilson T3 Period", minval=1)
  171. factorT3 = input(defval=7, title="Tilson T3 Factor - *.10 - so 7 = .7 etc.", minval=0)
  172. atype = input(1,minval=1,maxval=8,title="1=SMA, 2=EMA, 3=WMA, 4=HullMA, 5=VWMA, 6=RMA, 7=TEMA, 8=Tilson T3")
  173. spc=input(false, title="Show Price Crossing 1st Mov Avg - Highlight Bar?")
  174. cc = input(true,title="Change Color Based On Direction?")
  175. smoothe = input(2, minval=1, maxval=10, title="Color Smoothing - Setting 1 = No Smoothing")
  176. doma2 = input(false, title="Optional 2nd Moving Average")
  177. spc2=input(false, title="Show Price Crossing 2nd Mov Avg?")
  178. len2 = input(50, title="Moving Average Length - Optional 2nd MA")
  179. sfactorT3 = input(defval=7, title="Tilson T3 Factor - *.10 - so 7 = .7 etc.", minval=0)
  180. atype2 = input(1,minval=1,maxval=8,title="1=SMA, 2=EMA, 3=WMA, 4=HullMA, 5=VWMA, 6=RMA, 7=TEMA, 8=Tilson T3")
  181. cc2 = input(true,title="Change Color Based On Direction 2nd MA?")
  182. warn = input(false, title="***You Can Turn On The Show Dots Parameter Below Without Plotting 2nd MA to See Crosses***")
  183. warn2 = input(false, title="***If Using Cross Feature W/O Plotting 2ndMA - Make Sure 2ndMA Parameters are Set Correctly***")
  184. sd = input(false, title="Show Dots on Cross of Both MA's")
  185.  
  186. res = useCurrentRes ? period : resCustom
  187. //hull ma definition
  188. hullma = wma(2*wma(src, len/2)-wma(src, len), round(sqrt(len)))
  189. //TEMA definition
  190. ema1 = ema(src, len)
  191. ema2 = ema(ema1, len)
  192. ema3 = ema(ema2, len)
  193. tema = 3 * (ema1 - ema2) + ema3
  194.  
  195. //Tilson T3
  196. factor = factorT3 *.10
  197. gd(src, len, factor) => ema(src, len) * (1 + factor) - ema(ema(src, len), len) * factor
  198. t3(src, len, factor) => gd(gd(gd(src, len, factor), len, factor), len, factor)
  199. tilT3 = t3(src, len, factor)
  200.  
  201.  
  202. avg = atype == 1 ? sma(src,len) : atype == 2 ? ema(src,len) : atype == 3 ? wma(src,len) : atype == 4 ? hullma : atype == 5 ? vwma(src, len) : atype == 6 ? rma(src,len) : atype == 7 ? 3 * (ema1 - ema2) + ema3 : tilT3
  203. //2nd Ma - hull ma definition
  204. hullma2 = wma(2*wma(src, len2/2)-wma(src, len2), round(sqrt(len2)))
  205. //2nd MA TEMA definition
  206. sema1 = ema(src, len2)
  207. sema2 = ema(sema1, len2)
  208. sema3 = ema(sema2, len2)
  209. stema = 3 * (sema1 - sema2) + sema3
  210.  
  211. //2nd MA Tilson T3
  212. sfactor = sfactorT3 *.10
  213. sgd(src, len2, sfactor) => ema(src, len2) * (1 + sfactor) - ema(ema(src, len2), len2) * sfactor
  214. st3(src, len2, sfactor) => sgd(sgd(gd(src, len2, sfactor), len2, sfactor), len2, sfactor)
  215. stilT3 = st3(src, len2, sfactor)
  216.  
  217. avg2 = atype2 == 1 ? sma(src,len2) : atype2 == 2 ? ema(src,len2) : atype2 == 3 ? wma(src,len2) : atype2 == 4 ? hullma2 : atype2 == 5 ? vwma(src, len2) : atype2 == 6 ? rma(src,len2) : atype2 == 7 ? 3 * (ema1 - ema2) + ema3 : stilT3
  218.  
  219. out = avg
  220. out_two = avg2
  221.  
  222. out1 = security(tickerid, res, out)
  223. out3 = security(tickerid, res, out_two)
  224.  
  225. //Formula for Price Crossing Moving Average #1
  226. cr_up = open < out1 and close > out1
  227. cr_Down = open > out1 and close < out1
  228. //Formula for Price Crossing Moving Average #2
  229. cr_up2 = open < out3 and close > out3
  230. cr_Down2 = open > out3 and close < out3
  231. //barcolor Criteria for Price Crossing Moving Average #1
  232. iscrossUp() => cr_up
  233. iscrossDown() => cr_Down
  234. //barcolor Criteria for Price Crossing Moving Average #2
  235. iscrossUp2() => cr_up2
  236. iscrossDown2() => cr_Down2
  237.  
  238. ma_up = out1 >= out1[smoothe]
  239. ma_down = out1 < out1[smoothe]
  240.  
  241. //BUY AND SELL CONDITIONS
  242. Buy = rising(rr,1) and pK < pJ and rising(low,1) and uptrend
  243. Sell = falling(rr,1) and pK > pJ and falling(high,1) and downtrend
  244.  
  245. pos= 0.0
  246. pos:= Buy?1 : Sell?-1 : pos[1]
  247.  
  248. long = Buy and pos[1]!=1
  249. short= Sell and pos[1]!=-1
  250.  
  251. // Chart plot
  252. plotshape(long, color=lime, style=shape.triangleup, text="Buy",
  253. location=location.belowbar,transp=0,offset=0,size=size.small)
  254. plotshape(short, color=red,style=shape.triangledown,text="Sell",
  255. location=location.abovebar,transp=0,offset=0,size=size.small)
  256.  
  257. // Alerts
  258. alertcondition(long, title="Buy Alert")
  259. alertcondition(short, title="Sell Alert")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement