xmd79

RHS - Final Chapter

May 23rd, 2023
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.13 KB | None | 0 0
  1. //@version=5
  2.  
  3. indicator('RHS - Final Chapter', overlay=true, shorttitle='RHS - Final Chapter', max_bars_back=1000)
  4. //
  5. //====================channel======================
  6. len = input.int(8, minval=1)
  7. src = input(close, title='Source')
  8. out = ta.sma(src, len)
  9. last8h = ta.highest(close, 13)
  10. lastl8 = ta.lowest(close, 13)
  11. bearish = ta.cross(close, out) == 1 and close[1] > close
  12. bullish = ta.cross(close, out) == 1 and close[1] < close
  13. channel2 = input(false, title='Bar Channel On/Off')
  14. ul2 = plot(channel2 ? last8h : last8h == nz(last8h[1]) ? last8h : na, color=color.new(color.black, 0), linewidth=1, style=plot.style_linebr, title='Candle body resistance level top', offset=0)
  15. ll2 = plot(channel2 ? lastl8 : lastl8 == nz(lastl8[1]) ? lastl8 : na, color=color.new(color.black, 0), linewidth=1, style=plot.style_linebr, title='Candle body resistance level bottom', offset=0)
  16. //
  17. //--------------------Trend colour ema------------------------------------------------// ema trend direction trigger For Singals 1 & 2
  18. src0 = close
  19. len0 = input.int(13, minval=1, title='Trend Change EMA')
  20. ema0 = ta.ema(src0, len0)
  21. plot_color = ema0 >= ema0[2] ? color.lime : ema0 < ema0[2] ? color.red : na
  22. plot(ema0, title='EMA', style=plot.style_line, linewidth=1, color=plot_color)
  23.  
  24.  
  25. //
  26. // Create non-repainting security function
  27. //rp_security(_symbol, _res, _src) => security(_symbol, _res, _src[barstate.isrealtime ? 1 : 0])
  28. //f_secureSecurity(_symbol, _res, _src) => security(_symbol, _res, _src[1], lookahead = barmerge.lookahead_on)
  29. f_security(_symbol, _res, _src, _repaint) =>
  30. request.security(_symbol, _res, _src[_repaint ? 0 : barstate.isrealtime ? 1 : 0])[_repaint ? 0 : barstate.isrealtime ? 0 : 1]
  31. //
  32. //
  33. //--Modified vyacheslav.shindin-------------------------------------------------// Signal 1
  34. //Configured ema signal output
  35. slow = 8
  36. fast = 5
  37. vh1 = ta.ema(ta.highest(math.avg(low, close), fast), 5)
  38. vl1 = ta.ema(ta.lowest(math.avg(high, close), slow), 8)
  39. //
  40. e_ema1 = ta.ema(close, 1)
  41. e_ema2 = ta.ema(e_ema1, 1)
  42. e_ema3 = ta.ema(e_ema2, 1)
  43. tema = 1 * (e_ema1 - e_ema2) + e_ema3
  44. //
  45. e_e1 = ta.ema(close, 8)
  46. e_e2 = ta.ema(e_e1, 5)
  47. dema = 2 * e_e1 - e_e2
  48. signal = tema > dema ? math.max(vh1, vl1) : math.min(vh1, vl1)
  49. is_call = tema > dema and signal > low and signal - signal[1] > signal[1] - signal[2]
  50. is_put = tema < dema and signal < high and signal[1] - signal > signal[2] - signal[1]
  51.  
  52. plotshape(is_call ? 1 : na, title='BUY ARROW', color=color.new(color.green, 0), text='BUY', textcolor=color.new(color.white, 0), size=size.auto, style=shape.labelup, location=location.belowbar)
  53. plotshape(is_put ? -1 : na, title='SELL ARROW', color=color.new(color.red, 0), text='SELL', textcolor=color.new(color.white, 0), size=size.auto, style=shape.labeldown)
  54. //
  55. //Modified - Rajandran R Supertrend----------------------------------------------------- // Signal 2
  56. Factor = input.int(1, minval=1, defval=2, maxval=1000, title='Trend Transition Signal')
  57. Pd = input.int(1, minval=1, maxval=100)
  58. Up = hl2 - Factor * ta.atr(Pd)
  59. Dn = hl2 + Factor * ta.atr(Pd)
  60. TrendUp = Up
  61. TrendUp := close[1] > TrendUp[1] ? math.max(Up, TrendUp[1]) : Up
  62. TrendDown = Dn
  63. TrendDown := close[1] < TrendDown[1] ? math.min(Dn, TrendDown[1]) : Dn
  64. Trend = TrendUp
  65. Trend := close > TrendDown[1] ? 1 : close < TrendUp[1] ? -1 : nz(Trend[1], 0)
  66.  
  67. plotshape(Trend == 1 and Trend[1] == -1 ? Trend : na, title='BUY ARROW', color=color.new(color.green, 0), text='BREAK THE LINE\n(BUY)', textcolor=color.new(color.white, 0), size=size.auto, style=shape.labelup, location=location.belowbar)
  68. plotshape(Trend == -1 and Trend[1] == 1 ? Trend : na, title='SELL ARROW', color=color.new(color.red, 0), text='BREAK THE LINE\n(SELL)', textcolor=color.new(color.white, 0), size=size.auto, style=shape.labeldown)
  69.  
  70. //plotarrow(Trend == 1 and Trend[1] == -1 ? Trend : na, title="Strong Buy", colorup=lime, maxheight=1000, minheight=50, transp=80)
  71. //plotarrow(Trend == -1 and Trend[1] == 1 ? Trend : na, title="Strong Sell", colordown=red, maxheight=1000, minheight=50, transp=80)
  72. // Moddified [RS]Support and Resistance V0
  73. RST = input(title='Support / Resistance length:', defval=21)
  74. RSTT = ta.valuewhen(high >= ta.highest(high, RST), high, 0)
  75. RSTB = ta.valuewhen(low <= ta.lowest(low, RST), low, 0)
  76. RT2 = plot(RSTT, color=RSTT != RSTT[1] ? na : color.red, linewidth=1, offset=0)
  77. RB2 = plot(RSTB, color=RSTB != RSTB[1] ? na : color.green, linewidth=1, offset=0)
  78. //
  79. //===============================Directional Projection=======================================//
  80. tf2 = input('1', title='Trend Projection TF / Mins/D/W')
  81. M2 = input('ATR')
  82. P2 = input(13.00)
  83. W2 = input(1)
  84. pf2 = ticker.pointfigure(syminfo.tickerid, 'close', M2, P2, W2)
  85. spfc2 = request.security(pf2, tf2, close)
  86. channel3 = input(false, title='Connect Projection High/Low')
  87. p22 = plot(channel3 ? spfc2 : spfc2 == nz(spfc2[1]) ? spfc2 : na, color=color.new(color.white, 100), linewidth=1, style=plot.style_linebr, title='Directional Projection', offset=0)
  88. //----------------------------------------------------------------------//
  89.  
  90.  
  91. // ~~ Tooltips {
  92. t1 = "The space between the levels can be adjusted with a percentage step. 1% means that each level is located 1% above/under the previous one."
  93. t2 = "Set the number of levels you want to display."
  94. t3 = "If a level got 0 % likelihood of being hit, the level is not displayed as default. Enable the option if you want to see all levels regardless of their values."
  95. t4 = "Enable this option if you want to display the backtest statistics for that a new high or low is made."
  96. string [] tbl_tips = array.from("Number of times price has reached the first highest percentage level",
  97. "Number of times price failed to reach the first highest percentage level",
  98. "Win/Loss ratio")
  99. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  100.  
  101. // ~~ Inputs {
  102. perc = input.float(1.0,title="Percentage Step",step=.1,minval=0,group="Settings",tooltip=t1)
  103. nbr = input.int(5, title="Number of Lines",maxval=5,minval=1,group="Settings",tooltip=t2)
  104. upCol = input.color(color.new(color.green,0),title=" ",inline="col"),dnCol=input.color(color.new(color.red,0),title="",inline="col"),fill=input.bool(true,title="BG Color",inline="col")
  105. var bool [] bools = array.from(input.bool(true,title="Disable 0.00%",group="Settings",tooltip=t3),input.bool(true, title="Show Statistic Panel",group="Settings",tooltip=t4))
  106. var bool [] alert_bool = array.from(
  107. input.bool(true,title="Ticker ID",group="Any alert() function call"),
  108. input.bool(true,title="High/Low Price",group="Any alert() function call"),
  109. input.bool(true,title="Bullish/Bearish Bias",group="Any alert() function call"),
  110. input.bool(true,title="Bullish/Bearish Percentage",group="Any alert() function call"))
  111. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  112.  
  113. // ~~ Variables {
  114. b = bar_index
  115. o = open
  116. h = high
  117. l = low
  118. c = close
  119. step = c*(perc/100)
  120. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  121.  
  122. // ~~ Save Values In Matrix {
  123. var total = matrix.new<int>(7,4,0)
  124. var vals = matrix.new<float>(5,4,0.0)
  125. var lines = matrix.new<line>(1,10,line(na))
  126. var labels = matrix.new<label>(1,10,label(na))
  127. var tbl = matrix.new<table>(1,1,table.new(position.top_right,2,3,
  128. frame_color=color.new(color.gray,50),frame_width=3,
  129. border_color=chart.bg_color,border_width=-2))
  130. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  131.  
  132. // ~~ Save Number Of Green & Red Candles {
  133. green = c[1]>o[1]
  134. red = c[1]<o[1]
  135. if green
  136. prev = matrix.get(total,5,0)
  137. matrix.set(total,5,0,prev+1)
  138. if red
  139. prev = matrix.get(total,5,1)
  140. matrix.set(total,5,1,prev+1)
  141. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  142.  
  143. // ~~ Functions {
  144. //Lines
  145. CreateLine(p,i,c)=>
  146. prevLine = matrix.get(lines,0,i)
  147. line.delete(prevLine)
  148. li = line.new(b[1],p,b,p,color=c,width=2)
  149. matrix.set(lines,0,i,li)
  150. //Labels
  151. CreateLabel(p,i,c,r,v)=>
  152. prevLabel = matrix.get(labels,0,i)
  153. label.delete(prevLabel)
  154. la = label.new(b+1,p,text=str.tostring(matrix.get(vals,r,v),format.percent),
  155. style=label.style_label_left,color=color.new(color.black,100),textcolor=c)
  156. matrix.set(labels,0,i,la)
  157. //Score Calculation
  158. Score(x,i)=>
  159. ghh = matrix.get(total,i,0)
  160. gll = matrix.get(total,i,1)
  161. rhh = matrix.get(total,i,2)
  162. rll = matrix.get(total,i,3)
  163. gtotal = matrix.get(total,5,0)
  164. rtotal = matrix.get(total,5,1)
  165. hh = h>=h[1] + x
  166. ll = l<=l[1] - x
  167. if green and hh
  168. matrix.set(total,i,0,ghh+1)
  169. matrix.set(vals,i,0,math.round(((ghh+1)/gtotal)*100,2))
  170. if green and ll
  171. matrix.set(total,i,1,gll+1)
  172. matrix.set(vals,i,1,math.round(((gll+1)/gtotal)*100,2))
  173. if red and hh
  174. matrix.set(total,i,2,rhh+1)
  175. matrix.set(vals,i,2,math.round(((rhh+1)/rtotal)*100,2))
  176. if red and ll
  177. matrix.set(total,i,3,rll+1)
  178. matrix.set(vals,i,3,math.round(((rll+1)/rtotal)*100,2))
  179. //Backtest
  180. Backtest(v)=>
  181. p1 = matrix.get(total,6,0)
  182. p2 = matrix.get(total,6,1)
  183. if v==h[1]
  184. if h>=v
  185. matrix.set(total,6,0,p1+1)
  186. else
  187. matrix.set(total,6,1,p2+1)
  188. else
  189. if l<=v
  190. matrix.set(total,6,0,p1+1)
  191. else
  192. matrix.set(total,6,1,p2+1)
  193. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  194.  
  195.  
  196. // ~~ Code {
  197. //Run Score Function
  198. Score(0,0)
  199. Score(step,1)
  200. Score(step*2,2)
  201. Score(step*3,3)
  202. Score(step*4,4)
  203.  
  204. //Fetch Score Values
  205. a1 = matrix.get(vals,0,0)
  206. b1 = matrix.get(vals,0,1)
  207. a2 = matrix.get(vals,0,2)
  208. b2 = matrix.get(vals,0,3)
  209.  
  210. //Lines & Labels & Alerts
  211. for i=0 to nbr-1
  212. hide = array.get(bools,0)
  213. if not hide or (hide and (green?math.min(matrix.get(vals,i,0),
  214. matrix.get(vals,i,1))>0:
  215. math.min(matrix.get(vals,i,2),
  216. matrix.get(vals,i,3))>0))
  217. hi = h[1]+(step*i)
  218. lo = l[1]-(step*i)
  219. //Plot Lines
  220. CreateLine(hi,i,upCol)
  221. CreateLine(lo,5+i,dnCol)
  222. //Plot Labels
  223. if green
  224. CreateLabel(hi,i,upCol,i,0)
  225. CreateLabel(lo,5+i,dnCol,i,1)
  226. else
  227. CreateLabel(hi,i,upCol,i,2)
  228. CreateLabel(lo,5+i,dnCol,i,3)
  229. //Create Alert
  230. if array.includes(alert_bool, true)
  231. s1 = str.tostring(syminfo.ticker)
  232. s2 = "High Price: "+str.tostring(math.round_to_mintick(h[1]))+
  233. " | Low Price: "+str.tostring(math.round_to_mintick(l[1]))
  234. s3 = green?(math.max(a1,b1)==a1?"BULLISH":"BEARISH"):
  235. (math.max(a2,b2)==a2?"BULLISH":"BEARISH")
  236. s4 = green?(math.max(a1,b1)==a1?a1:b1):(math.min(a2,b2)==a2?a2:b2)
  237. s5 = red ?(math.max(a2,b2)==a2?a2:b2):(math.min(a1,b1)==a1?a1:b1)
  238. string [] str_vals = array.from(s1,s2,"BIAS: "+s3,
  239. "Percentage: High: "+str.tostring(s4,format.percent)
  240. +" | Low: "+str.tostring(s5,format.percent))
  241. output = array.new_string()
  242. for x=0 to array.size(alert_bool)-1
  243. if array.get(alert_bool,x)
  244. array.push(output,array.get(str_vals,x))
  245. //Alert Is Triggered On Every Bar Open With Bias And Percentage Ratio
  246. alert(array.join(output,'\n'),alert.freq_once_per_bar)
  247. else
  248. //Delete Old Lines & Labels
  249. line.delete(matrix.get(lines,0,i))
  250. line.delete(matrix.get(lines,0,5+i))
  251. label.delete(matrix.get(labels,0,i))
  252. label.delete(matrix.get(labels,0,5+i))
  253.  
  254. //Run Backtest Function
  255. Backtest(green?(math.max(a1,b1)==a1?h[1]:l[1]):(math.max(a2,b2)==a2?h[1]:l[1]))
  256. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  257.  
  258. // ~~ Linefill {
  259. if fill
  260. var filler = linefill(na)
  261. for i=0 to 8
  262. get = matrix.get(lines,0,i)
  263. get1= matrix.get(lines,0,i+1)
  264. col = i>4?color.new(dnCol,80) : i==4?color.new(color.gray,100) : color.new(upCol,80)
  265. filler := linefill.new(get,get1,color=col)
  266. linefill.delete(filler[1])
  267. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  268.  
Advertisement
Add Comment
Please, Sign In to add comment