Advertisement
aaahopper

strategy of OBV with trendlines

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