Advertisement
xmd79

Session Breakout

Oct 21st, 2023
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.42 KB | None | 0 0
  1. //This source code is subject the terms of the Mozilla Public License at https://mozilla.org/MPL/2.0/
  2. // © hiimannshu
  3.  
  4. //@version=5
  5. indicator(title='Session Breakout', shorttitle='Sess Break', overlay=true, max_boxes_count=500, max_lines_count=500)
  6.  
  7. //--------------------------------- Inputs --------------------------------------------------------------//
  8.  
  9. show_session_box = input.bool(true, title='Show session time range box ?', group='Session Time Range')
  10. box_infil_color = input.color(color.new(color.red, 70), 'Box Infill', inline='box_prop', group='Session Time Range')
  11. box_border_color = input.color(color.new(color.black, 70), 'Box Border', inline='box_prop', group='Session Time Range')
  12. sess = input.session('1800-0101',"Session Timing", group='Session Time Range',tooltip = "Add one extra minute in session end time, otherwise it won't add last candle in time range. e.g. if your session timing is '12:00 - 13:00'\nset it as '12:00 - 13:01'")
  13. show_hl_levels = input.bool(title='Show breakout Levels?', defval=true, group='Breakout Levels')
  14. use_close = input.bool(title='Use close as breakout confirmation ?', defval=false, group='Breakout Levels')
  15. br_line_color = input.color(color.new(color.red, 70), 'Breakout Line Color', group='Breakout Levels')
  16. extend_upto = input.int(defval=5,title='Extend breakout lines (in hours) ',minval=2,maxval=10, group='Breakout Levels')
  17. ind_style = input.string("Fixed","Breakout indicator style",options = ["Don't show","Fixed","Moving"], group='Breakout Levels')
  18. use_method = input.string("Breakout", title = "Use as",options = ["Breakout","Liquidity sweep"], group='Breakout Levels')
  19.  
  20. alerts = input.string(defval = "No alerts",title="Alerts",options = ["No alerts","On first breakout","Every Time breakout happens"],group='Alerts')
  21.  
  22. show_day = input.bool(false,"Show previos day high low?",inline="day",group = "Previous Levels")
  23. day_col = input.color(color.new(color.blue, 70),"",inline="day",group = "Previous Levels")
  24.  
  25. show_week = input.bool(false,"Show previos week high low?",inline="week",group = "Previous Levels")
  26. week_col = input.color(color.new(color.green, 70),"",inline="week",group = "Previous Levels")
  27.  
  28. //--------------------------------- Functions --------------------------------------------------------------//
  29.  
  30. in_session(sess) =>
  31. not na(time(timeframe.period, sess))
  32.  
  33. start_time(sess) =>
  34. int startTime = na
  35. startTime := in_session(sess) and not in_session(sess)[1] ? time : startTime[1]
  36. startTime
  37.  
  38. is_new_session(res, sess) =>
  39. t = time(res, sess)
  40. na(t[1]) and not na(t) or t[1] < t
  41.  
  42.  
  43. candle_count(tf, hours) =>
  44. float candles = na
  45. in_min = hours * 60
  46. candles := in_min / tf
  47. candles
  48.  
  49.  
  50. //--------------------------------- Variavles --------------------------------------------------------------/
  51. var int count = na
  52. var float hv = na
  53. var float lv = na
  54.  
  55. // ---- Verify Timeframe-----//
  56.  
  57. current_tf = str.tonumber(timeframe.period)
  58. bool valid_tf = current_tf <= 45
  59.  
  60. // ---- Candle Count-----//
  61.  
  62. candles_far = int(candle_count(current_tf, extend_upto))
  63.  
  64.  
  65. // ---- Time Range High Low -----//
  66.  
  67. float sess_low = na
  68. float sess_high = na
  69. float confirmed_low = na
  70. float confirmed_high = na
  71.  
  72. session_started = is_new_session('1440', sess)
  73. inside_sess = in_session(sess)
  74. session_ended = inside_sess[1] and not(inside_sess)
  75.  
  76. sess_low := session_started ? low : in_session(sess) ? math.min(low, sess_low[1]) : na
  77. sess_high := session_started ? high : in_session(sess) ? math.max(high, sess_high[1]) : na
  78.  
  79. confirmed_low := session_ended ? math.min(low, sess_low)[1] : na
  80. confirmed_high := session_ended ? math.max(high, sess_high)[1] : na
  81.  
  82. dayHigh = request.security(syminfo.tickerid,"D",high[1])
  83. dayLow = request.security(syminfo.tickerid,"D",low[1])
  84.  
  85. weekHigh = request.security(syminfo.tickerid,"W",high[1])
  86. weekLow = request.security(syminfo.tickerid,"W",low[1])
  87.  
  88.  
  89.  
  90. // ---- Lines-----//
  91.  
  92. bullish_breakout_level = line(na)
  93. bearish_breakout_level = line(na)
  94. bull_br = line(na)
  95. bear_br = line(na)
  96.  
  97. day_high = line(na)
  98. day_low = line(na)
  99. week_high = line(na)
  100. week_low = line(na)
  101. month_high = line(na)
  102. month_low = line(na)
  103. // ---- Breakout Level Coordinates-----//
  104.  
  105. time_diffrence = time - time[1]
  106.  
  107. x1_val = time[1]
  108. x2_val = time + candles_far * time_diffrence
  109.  
  110. yl_val = confirmed_low
  111. yh_val = confirmed_high
  112.  
  113. // ---- Box -----//
  114. sess_box = box(na)
  115.  
  116. // ---- Label -----//
  117. message = label(na)
  118.  
  119.  
  120. //--------------------------------- Conditions --------------------------------------------------------------//
  121.  
  122. condition_1 = inside_sess and valid_tf
  123. condition_2 = session_ended and valid_tf
  124.  
  125. sess_start_time = start_time(sess)
  126.  
  127. //--------------------------------- Draw box and Levels --------------------------------------------------------------//
  128.  
  129.  
  130. if condition_1
  131. if in_session(sess)[1]
  132. box.delete(sess_box[1])
  133.  
  134. if low < sess_low
  135. sess_low := low
  136. confirmed_low := sess_low
  137. confirmed_low
  138.  
  139. if high > sess_high
  140. sess_high := high
  141. confirmed_high := sess_high
  142. confirmed_high
  143. if show_session_box
  144. sess_box := box.new(sess_start_time, sess_high, time, sess_low, xloc=xloc.bar_time, bgcolor= box_infil_color, border_color=box_border_color, border_style=line.style_solid)
  145. sess_box
  146.  
  147.  
  148. if barstate.islast and not valid_tf
  149. message := label.new(x=bar_index + 2, y=close, style=label.style_label_left, size=size.large, color=#ffeecc, textcolor=color.black, text='Shift to lower Timeframe ( < 45 min )')
  150. message
  151.  
  152.  
  153. if condition_2
  154.  
  155. if show_hl_levels
  156. bearish_breakout_level := line.new(x1_val, yl_val, x2_val, yl_val, xloc.bar_time, color=br_line_color)
  157. bullish_breakout_level := line.new(x1_val, yh_val, x2_val, yh_val, xloc.bar_time, color=br_line_color)
  158.  
  159. if show_day
  160. day_high := line.new(x1_val, dayHigh, x2_val, dayHigh, xloc.bar_time, color=day_col)
  161. day_low := line.new(x1_val, dayLow, x2_val, dayLow, xloc.bar_time, color=day_col)
  162.  
  163. if show_week
  164. week_high := line.new(x1_val, weekHigh, x2_val, weekHigh, xloc.bar_time, color=week_col)
  165. week_low := line.new(x1_val, weekLow, x2_val, weekLow, xloc.bar_time, color=week_col)
  166.  
  167.  
  168.  
  169.  
  170. if not(inside_sess) and inside_sess[1]
  171. count:=0
  172. hv:= yh_val
  173. lv:= yl_val
  174.  
  175. crossover = use_method =="Breakout"?(use_close?close[1]>hv : ((high[1]>hv and open[1]<hv) or close[1]>hv)):(low[1]<lv and close[1]>lv)
  176. crossunder =use_method =="Breakout"?( use_close? close[1]<lv :((low[1]<lv and open[1]>lv) or close[1]<lv)):(high[1]>hv and close[1]<hv)
  177.  
  178. if count!=0 and inside_sess
  179. hv:=0
  180. lv:=0
  181. count:=0
  182.  
  183. if count==0 and not(inside_sess)
  184. if crossover
  185. count:= 1
  186. if crossunder
  187. count:= 2
  188.  
  189. bullish_breakout = (count[1]==0 and count==1) and show_hl_levels and valid_tf
  190. bearish_breakout = (count[1]==0 and count==2) and show_hl_levels and valid_tf
  191.  
  192. br_st = ind_style == "Fixed"?1:(ind_style == "Moving"?2:0)
  193.  
  194. if bullish_breakout and br_st == 2
  195. bull_br:=line.new(bar_index[1], low[1], bar_index[1], high[1],extend=extend.left, color=color.green, style = line.style_dotted,width=1)
  196. if bearish_breakout and br_st == 2
  197. bull_br:=line.new(bar_index[1], low[1], bar_index[1], high[1],extend=extend.right, color=color.red, style = line.style_dotted,width=1)
  198.  
  199.  
  200. plotshape(bullish_breakout and br_st==2 ,"Bullish breakout",shape.labelup,location.bottom,text="⬆️",color = color.new(color.white,100),size=size.tiny,offset = -1)
  201. plotshape(bearish_breakout and br_st==2 ,"Bearishbreakout",shape.labeldown,location.top,text="⬇️",color = color.new(color.white,100),size=size.tiny,offset = -1)
  202.  
  203. plotshape(bullish_breakout and br_st==1 ,"Bullish breakout",shape.triangleup,location.belowbar,color = color.new(color.green,0),size=size.tiny,offset = -1)
  204. plotshape(bearish_breakout and br_st==1 ,"Bearish breakout",shape.triangledown,location.abovebar,color = color.new(color.red,0),size=size.tiny,offset = -1)
  205.  
  206.  
  207. bull_alert = alerts=="On first breakout"?bullish_breakout:(alerts=="Every Time breakout happens"?crossover:false)
  208. bear_alert = alerts=="On first breakout"?bearish_breakout:(alerts=="Every Time breakout happens"?crossunder:false)
  209.  
  210. if bull_alert
  211. alert("Bullish breakout on "+str.tostring(syminfo.tickerid), alert.freq_once_per_bar)
  212.  
  213. if bear_alert
  214. alert("Bearish breakout on "+str.tostring(syminfo.tickerid), alert.freq_once_per_bar)
  215.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement