Advertisement
aaahopper

strategy of RSI with trendlines and S/R with HiLoPositions

Dec 23rd, 2019
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.54 KB | None | 0 0
  1. //@version=4
  2. //
  3. study("V01. RSI American Line with trendlines with HiLoPositions", shorttitle = "V01. strategy of RSI with trendlines and S/R with HiLoPositions", overlay = false)
  4.  
  5. // Chart Display Criteria ════════════════════════════════════════════════════════
  6. // ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  7.  
  8. display1=input(true, title="RSI display format section ══════════════════")
  9. Trd_All = input("Both", title ="+++ Show trade locations", options = ["Both", "S/R", "Ob/Os"])
  10. ShowTrendCandles = input("Yes",title = "+++ Highlight Trend Candles", options = ["No", "Yes"])
  11. std_bar = input("bar", title = "+++ Show RSI in Line or Bar format", options = ["bar", "std"])
  12.  
  13. log_chart = input("No",title = "+++ Use Log Chart", options = ["No", "Yes"])
  14. inner = input("dynamic", title = "+++ Inside form", options = ["dynamic", "fixed", "disabled"])
  15. inner_type = input("standard deviation", title = "+++ dynamic inner calculation method", options = ["standard deviation", "smooth moving average"])
  16. a_Color_Type = input(defval="Colored", title = "+++ Trendlines color Scheme", options=["Colored", "Monochrome"])
  17.  
  18. // RSI Input Criteria ════════════════════════════════════════════════════════
  19. // ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  20.  
  21. display02=input(true, title="RSI Data Entry section ════════════════════")
  22. len = input( 9, title = "+++ RSI length parameter", minval = 2, maxval = 100)
  23. src = input(close, title = "+++ RSI source")
  24. Hline= input(60,title='+++ Overbought RSI Line')
  25. Lline= input(40,title='+++ Oversold RSI Line')
  26. inner_mult = input(1.0, title = "+++ RSI inner Channel width", minval = 0.0, maxval = 4.0, step = 0.1)
  27.  
  28. //==calculation ==//
  29. norm = if std_bar=="std"
  30. 1
  31. else
  32. avg(src, src[1])
  33.  
  34. gain_loss = change(src) / norm
  35.  
  36. RSI = 50 + 50 * rma(gain_loss, len) / rma(abs(gain_loss), len)
  37.  
  38. RSI_ch = if inner != "disabled"
  39. if inner_type == "standard deviation"
  40. stdev(abs(change(RSI)), len)
  41. else
  42. rma(abs(change(RSI)), len)
  43.  
  44. //==Draw RSIB==//
  45. o = std_bar=="bar" ? RSI[1] : RSI
  46. h = std_bar=="bar" ? RSI : RSI
  47. l = std_bar=="bar" ? RSI[1] : RSI
  48. c = std_bar=="bar" ? RSI : RSI
  49.  
  50. plotbar(o, h, l, c, title = "RSIB", color = src[1] < src ? color.blue : color.red)
  51.  
  52. //==Draw RSI==//
  53. plot(std_bar=="bar" ? na : RSI, color = #A64D79FF, title = "RSI")
  54.  
  55. //==Draw a channel==//
  56. b1 = hline(Lline,color = color.red,linestyle = hline.style_dotted,linewidth = 1,title = "oversold")
  57. b2 = hline(Hline,color = color.blue,linestyle = hline.style_dotted,linewidth = 1,title = "overbought")
  58. fill(b1, b2, color = #AB47BC, title = "band background")
  59.  
  60. //Inner Lines
  61. Inner_hi = inner == "disabled" ? na : 50 + inner_mult * (inner == "dynamic" ? RSI_ch : 5.0)
  62. Inner_lo = inner == "disabled" ? na : 50 - inner_mult * (inner == "dynamic" ? RSI_ch : 5.0)
  63.  
  64. inner_h = plot(Inner_hi,color = #A64D7933, title = "with inner edge")
  65. inner_l = plot(Inner_lo,color = #A64D7933, title = "inside lower edge")
  66. fill(inner_h, inner_l, color = #A64D7922, title = "with inner background")
  67.  
  68. Long_rsi01 = crossover(RSI, Lline) and src[1] < src
  69. Short_rsi01= crossunder(RSI, Hline) and src[1] > src
  70. plotshape(Trd_All=="Both" or Trd_All=="Ob/Os" ? Short_rsi01: na, text="Dn", title="Condition Short-Trade", style=shape.triangledown, size=size.tiny, location=location.top, color=color.fuchsia, transp=0)
  71. plotshape(Trd_All=="Both" or Trd_All=="Ob/Os" ? Long_rsi01: na, text="Up", title="Condition Long-Trade", style=shape.triangleup, size=size.tiny, location=location.bottom, color=color.aqua, transp=0)
  72.  
  73. // Bollinger Bands S/R ════════════════════════════════════════════════════════
  74. // ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  75. show_dbbl = input(true, title='Display Bollinger Bands S/R ═════════════════')
  76. length10 = input(title="+++ Bollinger Length", type=input.integer, defval=38, minval=1)
  77. multiplier = input(title="+++ Bollinger Deviation", type=input.float, defval=2, step=0.1, minval=1)
  78. overbought = 1 //input(title="=> Overbought", type=input.integer, defval=1, minval=1)
  79. oversold = 0 //input(title="=> Oversold", type=input.integer, defval=0, minval=0)
  80.  
  81.  
  82. smabasis = sma(c, length10)
  83. stdev = stdev(c, length10)
  84. cierre = c
  85. alta = h
  86. baja = l
  87. basis1 = smabasis
  88. stdevb = stdev
  89. dev5 = multiplier * stdevb
  90. upper = basis1 + dev5
  91. lower5 = basis1 - dev5
  92.  
  93. bbr = (cierre - lower5) / (upper - lower5)
  94.  
  95. plot(bbr)
  96.  
  97. // // Top Resistance Band
  98. pintarojo = 0.0
  99. pintarojo := nz(pintarojo[1])
  100. pintarojo := bbr[1] > overbought and bbr < overbought ? alta[1] : nz(pintarojo[1])
  101. p = plot(show_dbbl ? pintarojo : na, color=#d904c4, style=plot.style_circles, linewidth=2, transp=0)
  102.  
  103. // // Bottom Support Band
  104. pintaverde = 0.0
  105. pintaverde := nz(pintaverde[1])
  106. pintaverde := bbr[1] < oversold and bbr > oversold ? baja[1] : nz(pintaverde[1])
  107. g = plot(show_dbbl ? pintaverde : na, color=#0959e3, style=plot.style_circles, linewidth=2, transp=0)
  108. //
  109. Long_bb = cross(RSI, pintaverde)
  110. Short_bb = cross(RSI, pintarojo)
  111.  
  112. plotshape(Trd_All=="Both" or Trd_All=="S/R" ? Short_bb and src[1] > src: na, text="Dn", title="Condition Short-Trade", style=shape.triangledown, size=size.tiny, location=location.top, color=color.fuchsia, transp=0)
  113. plotshape(Trd_All=="Both" or Trd_All=="S/R" ? Long_bb and src[1] < src: na, text="Up", title="Condition Long-Trade", style=shape.triangleup, size=size.tiny, location=location.bottom, color=color.aqua, transp=0)
  114.  
  115. //Trendlines 2 lots ════════════════════════════════════════════════════════
  116. // ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  117.  
  118. // Input variables
  119. show_text= input(true, title='══════════════════════════════════')
  120.  
  121. a_Show_Primary = input(true, title = "1 ═> Display Primary Trendlines ═════════════")
  122. a_len = input(20, title = "+++ Primary Lookback Length")
  123. a_Extensions = input( title = "+++ Primary Trendline Extensions", defval=" 50", options=["Infinate", " 25", " 50", " 75", " 100", " 150", " 200", " 300", " 400", " 500", " 750", "1000"])
  124. a_width = input(2, title = "+++ Primary Line Width", minval = 0, maxval = 10)
  125. a_Show_Breaks = "n/a"
  126. a_trendline_nr = 0
  127. a_Rising_Upper_Falling_Lower = false
  128.  
  129.  
  130. b_Show_Secondary = input(true, title = "2 ═> Display Secondary Trendlines ════════════")
  131. b_len = input(10, title = "+++ Secondary Lookback Length")
  132. b_Extensions = input( title = "+++ Secondary Trendline Extensions", defval=" 25", options=["Infinate", " 25", " 50", " 75", " 100", " 150", " 200", " 300", " 400", " 500", " 750", "1000"])
  133. b_width = input(1, title = "+++ Secondary Line Width", minval = 0, maxval = 10)
  134. b_Show_Breaks = "n/a"
  135. b_trendline_nr = 0
  136. b_Rising_Upper_Falling_Lower = false
  137.  
  138. a_bar_time = time - time[1]
  139. b_bar_time = time - time[1]
  140.  
  141.  
  142. ///// Primary Trendlines /////
  143. // Trendline Extensions
  144.  
  145. a_Extension_Multiplier=
  146. a_Extensions==" 25"? 1 :
  147. a_Extensions==" 50"? 2 :
  148. a_Extensions==" 75"? 3 :
  149. a_Extensions==" 100"? 4 :
  150. a_Extensions==" 150"? 6 :
  151. a_Extensions==" 200"? 8 :
  152. a_Extensions==" 300"? 12 :
  153. a_Extensions==" 400"? 16 :
  154. a_Extensions==" 500"? 20 :
  155. a_Extensions==" 750"? 30 :
  156. a_Extensions=="1000"? 40 :
  157. a_Extensions=="Infinate"? 0 : na
  158.  
  159.  
  160. // Declaration of trendline function
  161. a_f_trendline(a__input_function, a__delay, a__only_up, a__extend) =>
  162. // Calculate line coordinates (Ax,Ay) - (Bx,By)
  163. var int a_Ax = 1
  164. var int a_Bx = 1
  165. var float a_By = 0
  166. var float a_slope = 0
  167. a_Ay = fixnan(a__input_function)
  168. if change(a_Ay)!=0
  169. a_Ax := time[a__delay]
  170. a_By := a_Ay[1]
  171. a_Bx := a_Ax[1]
  172. a_slope:= log_chart=="Yes"? ((log(a_Ay) - log(a_By)) / (a_Ax - a_Bx)) : ((a_Ay - a_By) / (a_Ax - a_Bx))
  173. else
  174. a_Ax := a_Ax[1]
  175. a_Bx := a_Bx[1]
  176. a_By := a_By[1]
  177. // Draw trendlines
  178. var line a_trendline = na
  179. var int a_Axbis = 0
  180. var float a_Aybis = 0
  181. var bool a__xtend = true
  182. a_extension_time = a_Extension_Multiplier * a_bar_time * 25
  183. a_Axbis := a_Ax + a_extension_time
  184. a_Aybis := log_chart=="Yes"? (a_Ay * exp(a_extension_time * a_slope)) : (a_Ay + a_extension_time * a_slope)
  185. if a_Extension_Multiplier != 0
  186. a__xtend := false
  187. if change(a_Ay) != 0
  188.  
  189. a_line_color_Rising_Falling = a_slope * time < 0? (a__only_up ? a_Rising_Upper_Falling_Lower ? (a_Color_Type=="Colored" ? color.gray : color.teal) : na : (a_Color_Type=="Colored" ? #cf0a83 : color.teal)) : (a__only_up? (a_Color_Type=="Colored"? #027521 : color.teal) : a_Rising_Upper_Falling_Lower ? (a_Color_Type=="Colored"? color.gray : color.teal) : na)
  190. a_line_color_Not_Rising_Falling = a_slope * time < 0? (a__only_up ? na :(a_Color_Type=="Colored" ? #cf0a83 : color.teal)) : (a__only_up? (a_Color_Type=="Colored"? #027521 : color.teal) : na)
  191.  
  192. a_line_color = a_Show_Primary and not a_Rising_Upper_Falling_Lower ? a_line_color_Not_Rising_Falling : a_Show_Primary and a_Rising_Upper_Falling_Lower ? a_line_color_Rising_Falling : na
  193.  
  194. if not na(a_line_color)
  195. a_trendline = line.new(a_Bx, a_By, a_Axbis, a_Aybis, xloc.bar_time, extend = a__xtend? extend.right : extend.none, color = a_line_color, style = line.style_solid, width = a_width)
  196. [a_Bx, a_By, a_Axbis, a_Aybis, a_slope]
  197.  
  198. // Calculate pivot points
  199. a_high_point = pivothigh((c > o? c : o), a_len, a_len / 2)
  200. a_low_point = pivotlow((c > o? o : c), a_len, a_len / 2)
  201.  
  202. // Call trendline function for high and low pivot points
  203. [a_phx1, a_phy1, a_phx2, a_phy2, a_slope_high] = a_f_trendline(a_high_point, a_len / 2, false, true)
  204. [a_plx1, a_ply1, a_plx2, a_ply2, a_slope_low] = a_f_trendline(a_low_point, a_len / 2, true, true)
  205.  
  206. // ///// Secondary Trendlines /////
  207.  
  208. // // Trendline Extensions
  209.  
  210. b_Extension_Multiplier=
  211. b_Extensions==" 25"? 1 :
  212. b_Extensions==" 50"? 2 :
  213. b_Extensions==" 75"? 3 :
  214. b_Extensions==" 100"? 4 :
  215. b_Extensions==" 150"? 6 :
  216. b_Extensions==" 200"? 8 :
  217. b_Extensions==" 300"? 12 :
  218. b_Extensions==" 400"? 16 :
  219. b_Extensions==" 500"? 20 :
  220. b_Extensions==" 750"? 30 :
  221. b_Extensions=="1000"? 40 :
  222. b_Extensions=="Infinate"? 0 : na
  223.  
  224.  
  225. // Declaration of trendline function
  226. b_f_trendline(b__input_function, b__delay, b__only_up, b__extend) =>
  227. // Calculate line coordinates (Ax,Ay) - (Bx,By)
  228. var int b_Ax = 1
  229. var int b_Bx = 1
  230. var float b_By = 0
  231. var float b_slope = 0
  232. b_Ay = fixnan(b__input_function)
  233. if change(b_Ay)!=0
  234. b_Ax := time[b__delay]
  235. b_By := b_Ay[1]
  236. b_Bx := b_Ax[1]
  237. b_slope:= log_chart=="Yes"? ((log(b_Ay) - log(b_By)) / (b_Ax - b_Bx)) : ((b_Ay - b_By) / (b_Ax - b_Bx))
  238. else
  239. b_Ax := b_Ax[1]
  240. b_Bx := b_Bx[1]
  241. b_By := b_By[1]
  242. // Draw trendlines
  243. var line b_trendline = na
  244. var int b_Axbis = 0
  245. var float b_Aybis = 0
  246. var bool b__xtend = true
  247. b_extension_time = b_Extension_Multiplier * b_bar_time * 25
  248. b_Axbis := b_Ax + b_extension_time
  249. b_Aybis := log_chart=="Yes"? (b_Ay * exp(b_extension_time * b_slope)) : (b_Ay + b_extension_time * b_slope)
  250. if b_Extension_Multiplier != 0
  251. b__xtend := false
  252. if change(b_Ay) != 0
  253.  
  254. b_line_color_Rising_Falling = b_slope * time < 0? (b__only_up ? b_Rising_Upper_Falling_Lower ? (a_Color_Type=="Colored" ? color.gray : color.teal) : na : (a_Color_Type=="Colored" ? color.red : color.teal)) : (b__only_up? (a_Color_Type=="Colored"? color.green : color.teal) : b_Rising_Upper_Falling_Lower ? (a_Color_Type=="Colored"? color.gray : color.teal) : na)
  255. b_line_color_Not_Rising_Falling = b_slope * time < 0? (b__only_up ? na :(a_Color_Type=="Colored" ? color.red : color.teal)) : (b__only_up? (a_Color_Type=="Colored"? color.green : color.teal) : na)
  256.  
  257. b_line_color = b_Show_Secondary and not b_Rising_Upper_Falling_Lower ? b_line_color_Not_Rising_Falling : b_Show_Secondary and b_Rising_Upper_Falling_Lower ? b_line_color_Rising_Falling : na
  258.  
  259. if not na(b_line_color)
  260. b_trendline = line.new(b_Bx, b_By, b_Axbis, b_Aybis, xloc.bar_time, extend = b__xtend? extend.right : extend.none, color = b_line_color, style = line.style_dashed, width = b_width)
  261. [b_Bx, b_By, b_Axbis, b_Aybis, b_slope]
  262.  
  263.  
  264. // Calculate pivot points
  265. b_high_point = pivothigh((c > o? c : o), b_len, b_len / 2)
  266. b_low_point = pivotlow((c > o? o : c), b_len, b_len / 2)
  267.  
  268. // Call trendline function for high and low pivot points
  269. [b_phx1, b_phy1, b_phx2, b_phy2, b_slope_high] = b_f_trendline(b_high_point, b_len / 2, false, true)
  270. [b_plx1, b_ply1, b_plx2, b_ply2, b_slope_low] = b_f_trendline(b_low_point, b_len / 2, true, true)
  271.  
  272. // Plot and connect pivot points
  273. b_color_high = b_slope_high * time < 0 ? color.green : na
  274. b_color_low = b_slope_low * time > 0 ? color.red : na
  275.  
  276. // Trend Candles //
  277. //UCS_Trend by ucsgears copy Trend Candles
  278. //Interpretation of TTM Trend bars. It is really close to the actual.
  279.  
  280. haclose = c
  281. haopen = 0.0
  282. haopen := na(haopen[1]) ? (o + c) / 2 : (haopen[1] + haclose[1]) / 2
  283.  
  284. ccolor = haclose - haopen > 0 ? 1 : 0
  285. inside6 = haopen <= max(haopen[6],haclose[6]) and haopen>=min(haopen[6],haclose[6]) and haclose <= max(haopen[6],haclose[6]) and haclose >= min(haopen[6],haclose[6]) ? 1 : 0
  286. inside5 = haopen <= max(haopen[5],haclose[5]) and haopen>=min(haopen[5],haclose[5]) and haclose <= max(haopen[5],haclose[5]) and haclose >= min(haopen[5],haclose[5]) ? 1 : 0
  287. inside4 = haopen <= max(haopen[4],haclose[4]) and haopen>=min(haopen[4],haclose[4]) and haclose <= max(haopen[4],haclose[4]) and haclose >= min(haopen[4],haclose[4]) ? 1 : 0
  288. inside3 = haopen <= max(haopen[3],haclose[3]) and haopen>=min(haopen[3],haclose[3]) and haclose <= max(haopen[3],haclose[3]) and haclose >= min(haopen[3],haclose[3]) ? 1 : 0
  289. inside2 = haopen <= max(haopen[2],haclose[2]) and haopen>=min(haopen[2],haclose[2]) and haclose <= max(haopen[2],haclose[2]) and haclose >= min(haopen[2],haclose[2]) ? 1 : 0
  290. inside1 = haopen <= max(haopen[1],haclose[1]) and haopen>=min(haopen[1],haclose[1]) and haclose <= max(haopen[1],haclose[1]) and haclose >= min(haopen[1],haclose[1]) ? 1 : 0
  291.  
  292. colorvalue = inside6 ? ccolor[6] : inside5 ? ccolor[5] : inside4 ? ccolor[4] : inside3 ? ccolor[3] : inside2 ? ccolor[2] : inside1 ? ccolor[1] : ccolor
  293. Trend_Candle_Color = colorvalue ? color.lime : color.fuchsia
  294.  
  295. // Bar Color according to Trend Candles
  296. barcolor(ShowTrendCandles=="Yes"? Trend_Candle_Color : na, title = "Trend Candles")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement