xmd79

Crypto Paul Breakout Probability

Sep 24th, 2022
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. // This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
  2. // © Zeiierman
  3.  
  4. //@version=5
  5. indicator("Crypto Paul Breakout Probability",overlay=true,max_bars_back=5000)
  6.  
  7. // ~~ Tooltips {
  8. 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."
  9. t2 = "Set the number of levels you want to display."
  10. 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."
  11. t4 = "Enable this option if you want to display the backtest statistics for that a new high or low is made."
  12. string [] tbl_tips = array.from("Number of times price has reached the first highest percentage level",
  13. "Number of times price failed to reach the first highest percentage level",
  14. "Win/Loss ratio")
  15. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  16.  
  17. // ~~ Inputs {
  18. perc = input.float(1.0,title="Percentage Step",step=.1,minval=0,group="Settings",tooltip=t1)
  19. nbr = input.int(5, title="Number of Lines",maxval=5,minval=1,group="Settings",tooltip=t2)
  20. upCol = input.color(color.new(color.green,0),title="",inline="col"),dnCol=input.color(color.new(color.red,0),title="",inline="col")
  21. 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))
  22. var bool [] alert_bool = array.from(
  23. input.bool(true,title="Ticker ID",group="Any alert() function call"),
  24. input.bool(true,title="High/Low Price",group="Any alert() function call"),
  25. input.bool(true,title="Bullish/Bearish Bias",group="Any alert() function call"),
  26. input.bool(true,title="Bullish/Bearish Percentage",group="Any alert() function call"))
  27. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  28.  
  29. // ~~ Variables {
  30. b = bar_index
  31. o = open
  32. h = high
  33. l = low
  34. c = close
  35. step = c*(perc/100)
  36. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  37.  
  38. // ~~ Save Values In Matrix {
  39. var total = matrix.new<int>(7,4,0)
  40. var vals = matrix.new<float>(5,4,0.0)
  41. var lines = matrix.new<line>(1,10,line(na))
  42. var labels = matrix.new<label>(1,10,label(na))
  43. var tbl = matrix.new<table>(1,1,table.new(position.top_right,2,3,
  44. frame_color=color.new(color.gray,50),frame_width=3,
  45. border_color=chart.bg_color,border_width=-2))
  46. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  47.  
  48. // ~~ Save Number Of Green & Red Candles {
  49. green = c[1]>o[1]
  50. red = c[1]<o[1]
  51. if green
  52. prev = matrix.get(total,5,0)
  53. matrix.set(total,5,0,prev+1)
  54. if red
  55. prev = matrix.get(total,5,1)
  56. matrix.set(total,5,1,prev+1)
  57. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  58.  
  59. // ~~ Functions {
  60. //Lines
  61. CreateLine(p,i,c)=>
  62. prevLine = matrix.get(lines,0,i)
  63. line.delete(prevLine)
  64. li = line.new(b[1],p,b,p,color=c,width=2)
  65. matrix.set(lines,0,i,li)
  66. //Labels
  67. CreateLabel(p,i,c,r,v)=>
  68. prevLabel = matrix.get(labels,0,i)
  69. label.delete(prevLabel)
  70. la = label.new(b+1,p,text=str.tostring(matrix.get(vals,r,v),format.percent),
  71. style=label.style_label_left,color=color.new(color.black,100),textcolor=c)
  72. matrix.set(labels,0,i,la)
  73. //Score Calculation
  74. Score(x,i)=>
  75. ghh = matrix.get(total,i,0)
  76. gll = matrix.get(total,i,1)
  77. rhh = matrix.get(total,i,2)
  78. rll = matrix.get(total,i,3)
  79. gtotal = matrix.get(total,5,0)
  80. rtotal = matrix.get(total,5,1)
  81. hh = h>=h[1] + x
  82. ll = l<=l[1] - x
  83. if green and hh
  84. matrix.set(total,i,0,ghh+1)
  85. matrix.set(vals,i,0,math.round(((ghh+1)/gtotal)*100,2))
  86. if green and ll
  87. matrix.set(total,i,1,gll+1)
  88. matrix.set(vals,i,1,math.round(((gll+1)/gtotal)*100,2))
  89. if red and hh
  90. matrix.set(total,i,2,rhh+1)
  91. matrix.set(vals,i,2,math.round(((rhh+1)/rtotal)*100,2))
  92. if red and ll
  93. matrix.set(total,i,3,rll+1)
  94. matrix.set(vals,i,3,math.round(((rll+1)/rtotal)*100,2))
  95. //Backtest
  96. Backtest(v)=>
  97. p1 = matrix.get(total,6,0)
  98. p2 = matrix.get(total,6,1)
  99. if v==h[1]
  100. if h>=v
  101. matrix.set(total,6,0,p1+1)
  102. else
  103. matrix.set(total,6,1,p2+1)
  104. else
  105. if l<=v
  106. matrix.set(total,6,0,p1+1)
  107. else
  108. matrix.set(total,6,1,p2+1)
  109. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  110.  
  111. // ~~ Code {
  112. //Run Score Function
  113. Score(0,0)
  114. Score(step,1)
  115. Score(step*2,2)
  116. Score(step*3,3)
  117. Score(step*4,4)
  118.  
  119. //Fetch Score Values
  120. a1 = matrix.get(vals,0,0)
  121. b1 = matrix.get(vals,0,1)
  122. a2 = matrix.get(vals,0,2)
  123. b2 = matrix.get(vals,0,3)
  124.  
  125. //Lines & Labels & Alerts
  126. for i=0 to nbr-1
  127. hide = array.get(bools,0)
  128. if not hide or (hide and (green?math.min(matrix.get(vals,i,0),
  129. matrix.get(vals,i,1))>0:
  130. math.min(matrix.get(vals,i,2),
  131. matrix.get(vals,i,3))>0))
  132. hi = h[1]+(step*i)
  133. lo = l[1]-(step*i)
  134. //Plot Lines
  135. CreateLine(hi,i,upCol)
  136. CreateLine(lo,5+i,dnCol)
  137. //Plot Labels
  138. if green
  139. CreateLabel(hi,i,upCol,i,0)
  140. CreateLabel(lo,5+i,dnCol,i,1)
  141. else
  142. CreateLabel(hi,i,upCol,i,2)
  143. CreateLabel(lo,5+i,dnCol,i,3)
  144. //Create Alert
  145. if array.includes(alert_bool, true)
  146. s1 = str.tostring(syminfo.ticker)
  147. s2 = "High Price: "+str.tostring(math.round_to_mintick(h[1]))+
  148. " | Low Price: "+str.tostring(math.round_to_mintick(l[1]))
  149. s3 = green?(math.max(a1,b1)==a1?"BULLISH":"BEARISH"):
  150. (math.max(a2,b2)==a2?"BULLISH":"BEARISH")
  151. s4 = green?(math.max(a1,b1)==a1?a1:b1):(math.min(a2,b2)==a2?a2:b2)
  152. s5 = red ?(math.max(a2,b2)==a2?a2:b2):(math.min(a1,b1)==a1?a1:b1)
  153. string [] str_vals = array.from(s1,s2,"BIAS: "+s3,
  154. "Percentage: High: "+str.tostring(s4,format.percent)
  155. +" | Low: "+str.tostring(s5,format.percent))
  156. output = array.new_string()
  157. for x=0 to array.size(alert_bool)-1
  158. if array.get(alert_bool,x)
  159. array.push(output,array.get(str_vals,x))
  160. //Alert Is Triggered On Every Bar Open With Bias And Percentage Ratio
  161. alert(array.join(output,'\n'),alert.freq_once_per_bar)
  162. else
  163. //Delete Old Lines & Labels
  164. line.delete(matrix.get(lines,0,i))
  165. line.delete(matrix.get(lines,0,5+i))
  166. label.delete(matrix.get(labels,0,i))
  167. label.delete(matrix.get(labels,0,5+i))
  168.  
  169. //Run Backtest Function
  170. Backtest(green?(math.max(a1,b1)==a1?h[1]:l[1]):(math.max(a2,b2)==a2?h[1]:l[1]))
  171. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  172.  
  173. // ~~ Table {
  174. if barstate.islast and array.get(bools,1)
  175. //Calulate WinRatio
  176. W = matrix.get(total,6,0)
  177. L = matrix.get(total,6,1)
  178. WR = math.round(W/(W+L)*100,2)
  179. string [] tbl_vals = array.from("WIN: "+str.tostring(W),
  180. "LOSS: "+str.tostring(L),
  181. "Profitability: "+str.tostring(WR,format.percent))
  182. color [] tbl_col = array.from(color.green,color.red,chart.fg_color)
  183. for i=0 to 2
  184. table.cell(matrix.get(tbl,0,0),0,i,array.get(tbl_vals,i),
  185. text_halign=text.align_center,bgcolor=chart.bg_color,
  186. text_color=array.get(tbl_col,i),text_size=size.auto,
  187. tooltip=array.get(tbl_tips,i))
  188. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
Advertisement
Add Comment
Please, Sign In to add comment