xmd79

Camarilla Levels Trading

Oct 26th, 2022
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.24 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // Some source borrowed from © LonesomeTheBlue's Expanded Camarilla script
  3.  
  4. //@version=5
  5. indicator('Camarilla Levels Trading', overlay=true)
  6. mode = input.string(title='HTF Method', defval='Auto', options=['Auto', 'User Defined'])
  7. HTFm = input.timeframe('D', title='Time Frame (if HTF Method=User Defined)')
  8. showlast = input(title='Show Only Last Period', defval=true)
  9. lstyle = input.string(title='Line Style', options=['Solid', 'Circles', 'Cross'], defval='Circles')
  10. showCPROnly = input(defval=false, title='Show CPR Only')
  11. showCPR = input(defval=false, title="Show Central Pivot Range")
  12. showhl1 = input(defval=true, title='Show R1/S1')
  13. showhl2 = input(defval=true, title='Show R2/S2')
  14. showPriorScenarios = input(defval=false, title='Show Prior Scenario')
  15. noChangePCT_Threshold = input(defval=0.005, title='No Change Percentage')
  16.  
  17. //New Day
  18. is_newbar(res, sess) =>
  19. t = time(res, sess)
  20. na(t[1]) and not na(t) or t[1] < t
  21. // Detect New Day
  22.  
  23.  
  24.  
  25.  
  26. //auto higher time frame
  27. HTFo = timeframe.period == '1' ? '30' :
  28. timeframe.period == '3' ? '60' :
  29. timeframe.period == '5' ? '240' :
  30. timeframe.period == '15' ? 'D' :
  31. timeframe.period == '30' ? 'D' :
  32. timeframe.period == '45' ? 'D' :
  33. timeframe.period == '60' ? 'D' :
  34. timeframe.period == '120' ? 'D' :
  35. timeframe.period == '180' ? 'D' :
  36. timeframe.period == '240' ? 'D' :
  37. timeframe.period == 'D' ? 'W' :
  38. timeframe.period == 'W' ? '4W' : 'D'
  39.  
  40. HTF = mode == 'Auto' ? HTFo : HTFm
  41.  
  42. new_bar = is_newbar(HTF, '1700-1659')
  43.  
  44.  
  45.  
  46. var openValue = 0.00
  47. var longTrigger = false
  48. var shortTrigger = false
  49. var inTrade = false
  50. if new_bar
  51. longTrigger := false
  52. shortTrigger := false
  53. inTrade := false
  54. openValue := new_bar ? open : openValue[1]
  55.  
  56.  
  57. highhtf = request.security(syminfo.tickerid, HTF, high[1], lookahead=barmerge.lookahead_on)
  58. lowhtf = request.security(syminfo.tickerid, HTF, low[1], lookahead=barmerge.lookahead_on)
  59. closehtf = request.security(syminfo.tickerid, HTF, close[1], lookahead=barmerge.lookahead_on)
  60.  
  61. RANGE = highhtf - lowhtf
  62.  
  63. centralPivot = (highhtf + lowhtf + closehtf) / 3
  64. tempBottomPivot = (highhtf + lowhtf) / 2
  65. tempTopPivot = (centralPivot - tempBottomPivot) + centralPivot
  66.  
  67. bottomPivot = (tempBottomPivot > tempTopPivot) ? tempTopPivot : tempBottomPivot
  68. topPivot = bottomPivot == tempBottomPivot ? tempTopPivot : tempBottomPivot
  69.  
  70. cprRange = topPivot - bottomPivot
  71.  
  72.  
  73.  
  74. // is this last bar for HTF?
  75. islast = showlast ? request.security(syminfo.tickerid, HTF, barstate.islast, lookahead=barmerge.lookahead_on) : true
  76.  
  77. // Line Style
  78. linestyle = lstyle == 'Solid' ? plot.style_line : lstyle == 'Circle' ? plot.style_circles : plot.style_cross
  79.  
  80. R5 = highhtf / lowhtf * closehtf
  81. R4 = closehtf + RANGE * 1.1 / 2
  82. R3 = closehtf + RANGE * 1.1 / 4
  83. R2 = closehtf + RANGE * 1.1 / 6
  84. R1 = closehtf + RANGE * 1.1 / 12
  85. S1 = closehtf - RANGE * 1.1 / 12
  86. S2 = closehtf - RANGE * 1.1 / 6
  87. S3 = closehtf - RANGE * 1.1 / 4
  88. S4 = closehtf - RANGE * 1.1 / 2
  89. S5 = closehtf - (R5 - closehtf)
  90.  
  91. //plot(islast ? H5 : na, title='H5', color=color.new(color.red, 0), linewidth=1, style=linestyle)
  92. plot(islast and not showCPROnly ? R4 : na, title='R4', color=color.new(color.red, 0), linewidth=1, style=linestyle)
  93. plotR3 = plot(islast and not showCPROnly ? R3 : na, title='R3', color=color.new(color.red, 0), linewidth=1, style=linestyle)
  94. plot(islast and showhl2 and not showCPROnly ? R2 : na, title='R2', color=color.new(color.red, 70), linewidth=1, style=linestyle)
  95. plot(islast and showhl1 and not showCPROnly ? R1 : na, title='R1', color=color.new(color.red, 80), linewidth=1, style=linestyle)
  96.  
  97. plotTP = plot(showCPR ? topPivot : na, "Top Pivot", style=plot.style_cross, color=color.purple)
  98. plotCP = plot(showCPR ? centralPivot : na, "Central Pivot", style=plot.style_cross, color=color.orange)
  99. plotBP = plot(showCPR ? bottomPivot : na, "Bottom Pivot", style=plot.style_cross, color=color.purple)
  100.  
  101. plot(islast and showhl1 and not showCPROnly ? S1 : na, title='S1', color=color.new(color.lime, 70), linewidth=1, style=linestyle)
  102. plot(islast and showhl2 and not showCPROnly ? S2 : na, title='S2', color=color.new(color.lime, 80), linewidth=1, style=linestyle)
  103. plotS3 = plot(islast and not showCPROnly ? S3 : na, title='S3', color=color.new(color.lime, 0), linewidth=1, style=linestyle)
  104. plot(islast and not showCPROnly ? S4 : na, title='S4', color=color.new(color.lime, 0), linewidth=1, style=linestyle)
  105. //plot(islast ? L5 : na, title='L5', color=color.new(color.lime, 0), linewidth=1, style=linestyle)
  106.  
  107. //Trade
  108. int scenario = switch
  109. (R3 > openValue) and (openValue > S3) => 1
  110. (R3 < openValue) and (openValue < R4) => 2
  111. (S3 > openValue) and (openValue > S4) => 3
  112. (openValue > R4) => 4
  113. (openValue < S4) => 5
  114.  
  115. longTrigger := ((scenario == 1) and (close[1] < S3) and (high > S3))
  116. or (scenario == 2 and (close[1] < R3) and (high > R3))
  117. or (scenario == 3 and (high > S3))
  118. or (scenario == 5 and (high > S3))
  119.  
  120. shortTrigger := (scenario == 1 and (close[1] > R3) and (low < R3))
  121. or (scenario == 2 and (close[1] > S3) and (low < S3))
  122. or (scenario == 3 and (low < S4))
  123. or (scenario == 4 and (low < R3))
  124.  
  125. float entryValue = switch
  126. (longTrigger and scenario == 1) => S3
  127. (longTrigger and scenario == 2) => R3
  128. (longTrigger and scenario == 3) => S3
  129. (longTrigger and scenario == 5) => S3
  130. (shortTrigger and scenario == 1) => R3
  131. (shortTrigger and scenario == 2) => S3
  132. (shortTrigger and scenario == 3) => S4
  133. (shortTrigger and scenario == 4) => R3
  134.  
  135. float tpValue = switch
  136. (longTrigger and scenario == 1) => R1
  137. (longTrigger and scenario == 2) => R5
  138. (longTrigger and scenario == 3) => R1
  139. (longTrigger and scenario == 5) => R1
  140. (shortTrigger and scenario == 1) => S1
  141. (shortTrigger and scenario == 2) => S5
  142. (shortTrigger and scenario == 3) => S1
  143. (shortTrigger and scenario == 4) => S1
  144.  
  145. float stopValue = switch
  146. (longTrigger and scenario == 1) => S4
  147. (longTrigger and scenario == 2) => R3
  148. (longTrigger and scenario == 3) => S4
  149. (longTrigger and scenario == 5) => (S4 + S3)/2
  150. (shortTrigger and scenario == 1) => R4
  151. (shortTrigger and scenario == 2) => R4
  152. (shortTrigger and scenario == 3) => S3
  153. (shortTrigger and scenario == 4) => (R4 + R3)/2
  154.  
  155.  
  156. plotshape(longTrigger and not inTrade, style=shape.triangleup,
  157. location=location.abovebar, color=color.green)
  158. plotshape(shortTrigger and not inTrade, style=shape.triangledown,
  159. location=location.belowbar, color=color.red)
  160.  
  161. if (longTrigger or shortTrigger) and not inTrade[1]
  162. inTrade := true
  163. alertText = (longTrigger ? "Go Long\n" : "Go Short\n") +
  164. "Entry: " + str.tostring(entryValue, "#.##") + "\n" +
  165. "TP: " + str.tostring(tpValue, "#.##") + "\n" +
  166. "SL: " + str.tostring(stopValue, "#.##")
  167. labelStyle = longTrigger ? label.style_label_up : label.style_label_down
  168. yPlacement = longTrigger ? S4 : R4
  169. labelColor = longTrigger ? color.new(color.green, 50) : color.new(color.red, 50)
  170. label.new(x=bar_index, y=yPlacement, color=labelColor,
  171. textcolor=color.white, size=size.small,
  172. style=labelStyle,
  173. text=alertText)
  174.  
  175.  
  176.  
  177. //Status
  178. priorCentralPivot = request.security(syminfo.tickerid, HTF, centralPivot[1], lookahead=barmerge.lookahead_on)
  179. priorTopPivot = request.security(syminfo.tickerid, HTF, topPivot[1], lookahead=barmerge.lookahead_on)
  180. priorBottomPivot = request.security(syminfo.tickerid, HTF, bottomPivot[1], lookahead=barmerge.lookahead_on)
  181. priorCPRRange = request.security(syminfo.tickerid, HTF, cprRange[1], lookahead=barmerge.lookahead_on)
  182. priorR4 = request.security(syminfo.tickerid, HTF, R4[1], lookahead=barmerge.lookahead_on)
  183. priorS4 = request.security(syminfo.tickerid, HTF, S4[1], lookahead=barmerge.lookahead_on)
  184.  
  185. //Highlight pressure zones when present
  186. fill(plotR3, plotTP, color=R3 <= topPivot ? color.new(color.red,50) : na)
  187. fill(plotS3, plotBP, color=S3 >= bottomPivot ? color.new(color.green,50) : na)
  188.  
  189.  
  190. //CPR Range Percentile
  191. cprRangePercentile = cprRange/math.max(cprRange, 22) * 100
  192.  
  193. //2-Day CPR Movement
  194. cprStatus = if (bottomPivot > topPivot)
  195. -1
  196. else if (bottomPivot > priorTopPivot)
  197. 1
  198. else if (topPivot < priorBottomPivot)
  199. 2
  200. else if (centralPivot <= priorCentralPivot * (1 + noChangePCT_Threshold)
  201. and centralPivot >= priorCentralPivot * (1 - noChangePCT_Threshold))
  202. 3
  203. else if (topPivot < priorTopPivot and bottomPivot > priorBottomPivot)
  204. 4
  205. else if (topPivot > priorTopPivot and bottomPivot < priorBottomPivot)
  206. 5
  207. else if (centralPivot > priorCentralPivot)
  208. 6
  209. else
  210. 7
  211.  
  212.  
  213. string cprBias = switch cprStatus
  214. 1 => "Bullish | Higher CPR"
  215. 2 => "Bearish | Lower CPR"
  216. 3 => "Sideways | Unchanged CPR"
  217. 4 => "Trending/Breakout Inside CPR"
  218. 5 => "Sideways | Engulfing CPR"
  219. 6 => "Moderately Bullish | Overlapping CPR"
  220. 7 => "Moderately Bearish | Overlapping CPR"
  221.  
  222. string cprRangeStatus = switch
  223. cprRangePercentile >= 90 => "Wide | Sideways Trend"
  224. cprRangePercentile >= 80 => "Somewhat Wide | Sideways Trend"
  225. cprRangePercentile >= 20 => "Mid-Range | No Bias"
  226. cprRangePercentile >= 10 => "Somewhat Narrow | Trending"
  227. cprRangePercentile >= 0 => "Narrow | Trending"
  228.  
  229. // We use `var` to only initialize the table on the first bar.
  230. var table cprDisplay = table.new(position.bottom_right, 1, 4)
  231. if barstate.islast
  232. // We only populate the table on the last bar.
  233. table.cell(cprDisplay, 0, 0, str.tostring(cprBias), text_color=color.white)
  234. table.cell(cprDisplay, 0, 1, 'Range Width Percentile:' +str.tostring(cprRangePercentile, '#.#'), text_color=color.white)
  235. table.cell(cprDisplay, 0, 2, str.tostring(cprRangeStatus) , text_color=color.white)
  236. table.cell(cprDisplay, 0, 3, "Trading Scenario " + str.tostring(scenario, '#') , text_color=color.white)
  237.  
  238.  
  239.  
  240. if new_bar and showPriorScenarios
  241. labelText = 'Scenario ' + str.tostring(scenario, '#')
  242. label.new(x=bar_index, y=high,
  243. text=labelText,
  244. size = size.normal,
  245. style=label.style_label_lower_left,
  246. textcolor=color.white)
  247.  
  248.  
  249.  
  250.  
  251.  
Advertisement
Add Comment
Please, Sign In to add comment