Advertisement
Maurizio-Ciullo

Study Analyzing Afternoon vs Morning Ranges

Jan 27th, 2024
1,016
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This Pine Scriptβ„’ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // Β© ZenAndTheArtOfTrading
  3. // https://www.youtube.com/watch?v=MbC1kgJFtvE
  4.                                                                 // QUESTO TEST E' STATO CREATO PRINCIPALMENTE PER IL MERCATO AZIONARIO AMERICANO //
  5. // @version=5
  6. indicator("Study RANGE CONTINUATION TEST", overlay=true)
  7.  
  8. // Get user input
  9. Session1    = input.session(title="First Session", defval="0930-1200", display=display.none)
  10. Session2    = input.session(title="Second Session", defval="1200-1600", display=display.none)
  11. StartDate   = input.time(timestamp("1 January 2000"))
  12. EndDate     = input.time(timestamp("1 January 2030"))
  13.  
  14. // InSession() determines if a price bar falls inside the specified session on the given weekdays
  15. InSession(sess) => na(time(timeframe.period, sess + ":1234567")) == false
  16.  
  17. // Highlight sessions
  18. bgcolor(InSession(Session1) ? color.new(color.green, 90) : na)
  19. bgcolor(InSession(Session2) ? color.new(color.red,   90) : na)
  20.  
  21. // Get higher timeframe data
  22. float yesterdayHigh  = request.security(syminfo.tickerid, "D", high [barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1]
  23. float yesterdayLow   = request.security(syminfo.tickerid, "D", low  [barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1]
  24.  
  25. // Detect session start
  26. bool startSession1 = not InSession(Session1)[1] and InSession(Session1) and time >= StartDate
  27. bool startSession2 = not InSession(Session2)[1] and InSession(Session2) and time >= StartDate
  28.  
  29. // Analysis variables
  30. var int bullishRangeExpansionCount  = 0
  31. var int bearishRangeExpansionCount  = 0
  32. var int bullishRangeContinuation    = 0
  33. var int bearishRangeContinuation    = 0
  34.  
  35. // First session variables
  36. var int   sessionIndex1         = na
  37. var float dayOpenPrice          = na
  38. var float sessionLow1           = na
  39. var float sessionHigh1          = na
  40.  
  41. // Second session variables
  42. var int   sessionIndex2         = na
  43. var float sessionLow2           = na
  44. var float sessionHigh2          = na
  45. var bool  rangeExpansion        = false
  46. var bool  checkContinuation     = false
  47.  
  48. // Analysis variables
  49. var bool  yesterdayWasBullish   = false
  50. var bool  monitoring1stSession  = false
  51.  
  52. // 1. Detect start of day's first session
  53. // 2. Save previously monitored session's range
  54. // 3. Save yesterday's close and today's open
  55. // 4. Reset and start monitoring today's first session
  56. // 5. Start of day's first session is also end of yesterday's second session, so analyze data
  57. if startSession1
  58.     bool bullishClose = close[1] > dayOpenPrice
  59.     line.new(sessionIndex1, dayOpenPrice, bar_index - 1, close[1], color=bullishClose ? color.green : color.red)
  60.     line.new(sessionIndex2, sessionHigh2 + (sessionHigh1 - sessionHigh2), sessionIndex2, sessionLow2 + (sessionHigh1 - sessionHigh2), color=color.red, width=2)
  61.     if (sessionHigh2 - sessionLow2) >= (sessionHigh1 - sessionLow1)
  62.         rangeExpansion := true
  63.         label.new(bar_index - 1, high[1], (bullishClose ? "BULLISH" : "BEARISH") + " RANGE\nEXPANSION!", textcolor=color.white)
  64.     else
  65.         rangeExpansion := false
  66.  
  67.     sessionIndex1           := bar_index
  68.     sessionLow1             := na
  69.     sessionHigh1            := na
  70.     monitoring1stSession    := true
  71.     dayOpenPrice            := open
  72.  
  73.     if checkContinuation // We had range expansion yesterday - check if we continued in trend
  74.         if yesterdayWasBullish // Yesterday was a bullish day - check for bullish continuation
  75.             bool bullishContinuation = close[1] > yesterdayHigh
  76.             bullishRangeExpansionCount := bullishRangeExpansionCount + 1
  77.             line.new(bar_index, yesterdayHigh, bar_index - 20, yesterdayHigh, color=bullishContinuation ? color.green : color.gray)
  78.             if bullishContinuation
  79.                 bullishRangeContinuation := bullishRangeContinuation + 1
  80.                 label.new(bar_index - 1, high[1], "BULLISH CONTINUATION!", color=color.green, textcolor=color.white)
  81.         else
  82.             bool bearishContinuation = close[1] < yesterdayLow
  83.             bearishRangeExpansionCount := bearishRangeExpansionCount + 1
  84.             line.new(bar_index, yesterdayLow, bar_index - 20, yesterdayLow, color=bearishContinuation ? color.red : color.gray)
  85.             if bearishContinuation
  86.                 bearishRangeContinuation := bearishRangeContinuation + 1
  87.                 label.new(bar_index - 1, high[1], "BEARISH CONTINUATION!", color=color.red, textcolor=color.white)
  88.  
  89.     checkContinuation       := false
  90.     yesterdayWasBullish     := close[1] > dayOpenPrice[1]
  91.  
  92. // Track first session's range
  93. if monitoring1stSession
  94.     if na(sessionLow1) or low < sessionLow1
  95.         sessionLow1 := low
  96.     if na(sessionHigh1) or high > sessionHigh1
  97.         sessionHigh1 := high
  98.  
  99. // 1. Detect start of day's second session
  100. // 2. Reset and start monitoring today's second session
  101. if startSession2
  102.     line.new(bar_index - 1, sessionHigh1, bar_index - 1, sessionLow1, color=color.green, width=2)
  103.     monitoring1stSession    := false
  104.     sessionLow2             := na
  105.     sessionHigh2            := na
  106.     sessionIndex2           := bar_index
  107.     checkContinuation       := rangeExpansion
  108.  
  109. // Track second session's range
  110. if not monitoring1stSession
  111.     if na(sessionLow2) or low < sessionLow2
  112.         sessionLow2 := low
  113.     if na(sessionHigh2) or high > sessionHigh2
  114.         sessionHigh2 := high
  115.  
  116. // Analysis of range expansion data
  117. int totalRangeExpansionCount    = bullishRangeExpansionCount + bearishRangeExpansionCount
  118. float totalExpansionChance      = ((bullishRangeContinuation + bearishRangeContinuation) / totalRangeExpansionCount) * 100
  119. float bullishExpansionChance    = (bullishRangeContinuation / bullishRangeExpansionCount) * 100
  120. float bearishExpansionChance    = (bearishRangeContinuation / bearishRangeExpansionCount) * 100
  121.  
  122. // Debug data window plots
  123. plot(yesterdayWasBullish ? 1 : 0, title="BULLISH DAY?", display=display.data_window)
  124. plotshape(rangeExpansion ? 1 : na, "PREVIOUS EXPANSION?", shape.square, location.top, color=yesterdayWasBullish ? color.green : color.red)
  125. plot(na, title="-----------", display=display.data_window)
  126. plot(totalRangeExpansionCount, title="TOTAL RANGE EXPANSION", display=display.data_window)
  127. plot(bullishRangeExpansionCount, title="TOTAL BULLISH RANGE EXP", display=display.data_window)
  128. plot(bullishRangeContinuation, title="TOTAL BULLISH RANGE CNT", display=display.data_window)
  129. plot(bearishRangeExpansionCount, title="TOTAL BEARISH RANGE EXP", display=display.data_window)
  130. plot(bearishRangeContinuation, title="TOTAL BEARISH RANGE CNT", display=display.data_window)
  131. plot(na, title="-----------", display=display.data_window)
  132. plot(totalExpansionChance, title="Total Continuation Chance", display=display.data_window)
  133. plot(bullishExpansionChance, title="Bullish Continuation Chance", display=display.data_window)
  134. plot(bearishExpansionChance, title="Bearish Continuation Chance", display=display.data_window)
  135.  
  136. // Create stat display table
  137. if barstate.islast
  138.     var table myTable = table.new(position.top_right, 5, 2, border_width=1)
  139.  
  140.     txt1 = "Total Range Expansion\n"        + str.tostring(totalRangeExpansionCount)
  141.     txt2 = "Total Continuation Chance\n"    + str.tostring(totalExpansionChance, "#.##") + "%"
  142.     txt3 = "Bullish Days w/ Expansion\n"    + str.tostring(bullishRangeExpansionCount)
  143.     txt4 = "Bullish Continuation Chance\n"  + str.tostring(bullishExpansionChance, "#.##") + "%"
  144.     txt5 = "Bearish Days w/ Expansion\n"    + str.tostring(bearishRangeExpansionCount)
  145.     txt6 = "Bearish Continuation Chance\n"  + str.tostring(bearishExpansionChance, "#.##") + "%"
  146.  
  147.     table.cell(myTable, 0, 0, text=txt1, bgcolor=color.black, text_color=color.white)
  148.     table.cell(myTable, 0, 1, text=txt2, bgcolor=color.black, text_color=color.white)
  149.     table.cell(myTable, 1, 0, text=txt3, bgcolor=color.black, text_color=color.white)
  150.     table.cell(myTable, 1, 1, text=txt4, bgcolor=color.black, text_color=color.white)
  151.     table.cell(myTable, 2, 0, text=txt5, bgcolor=color.black, text_color=color.white)
  152.     table.cell(myTable, 2, 1, text=txt6, bgcolor=color.black, text_color=color.white)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement