Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This Pine Scriptβ’ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // Β© ChartPrime
- // 1. CHANGE VERSION TO V6
- // 2. INITIALIZE BOOLEANS AS FALSE IN PINESCRIPT V6 breakout_resistance, breakout_sup, resistance_holds, support_holds
- // 3. CHANGE brekout to breakout with ctrl + f
- // 4. CHANGE res to resistance and sup to support
- // 5. CHANGE library to strategy
- // 6. If support holds: Buy, If resistance holds: sell
- //@version=6
- strategy("Support and Resistance (High Volume Boxes) [ChartPrime]", shorttitle = "SR Breaks and Retests [ChartPrime]", overlay=true, max_boxes_count = 50)
- // ---------------------------------------------------------------------------------------------------------------------}
- // ππππ ππππππ
- // ---------------------------------------------------------------------------------------------------------------------{
- int lookbackPeriod = input.int(20, "Lookback Period", minval=1, group = "Settings")
- int vol_len = input.int(2, "Delta Volume Filter Length", tooltip = "Higher input, will filter low volume boxes"
- , group = "Settings")
- float box_withd = input.float(1, "Adjust Box Width", maxval = 1000, minval = 0, step = 0.1)
- // ---------------------------------------------------------------------------------------------------------------------}
- // πππΏππΎπΌπππ πΎπΌππΎπππΌπππππ
- // ---------------------------------------------------------------------------------------------------------------------{
- // Delta Volume Function
- upAndDownVolume() =>
- posVol = 0.0
- negVol = 0.0
- var isBuyVolume = true
- switch
- close > open => isBuyVolume := true
- close < open => isBuyVolume := false
- if isBuyVolume
- posVol += volume
- else
- negVol -= volume
- posVol + negVol
- // Function to identify support and resistance boxes
- calcSupportResistance(src, lookbackPeriod) =>
- // Volume
- Vol = upAndDownVolume()
- vol_hi = ta.highest(Vol/2.5, vol_len)
- vol_lo = ta.lowest(Vol/2.5, vol_len)
- var float supportLevel = na
- var float supportLevel_1 = na
- var float resistanceLevel = na
- var float resistanceLevel_1 = na
- var box support = na
- var box resistance = na
- var color resistance_color = na
- var color support_color = na
- var float multi = na
- var bool breakout_resistance = false
- var bool breakout_support = false
- var bool resistance_holds = false
- var bool support_holds = false
- // Find pivot points
- pivotHigh = ta.pivothigh(src, lookbackPeriod, lookbackPeriod)
- pivotLow = ta.pivotlow (src, lookbackPeriod, lookbackPeriod)
- // Box width
- atr = ta.atr(200)
- withd = atr * box_withd
- // Find support levels with Positive Volume
- if (not na(pivotLow)) and Vol > vol_hi
- supportLevel := pivotLow
- supportLevel_1 := supportLevel-withd
- topLeft = chart.point.from_index(bar_index-lookbackPeriod, supportLevel)
- bottomRight = chart.point.from_index(bar_index, supportLevel_1)
- support_color := color.from_gradient(Vol, 0, ta.highest(Vol, 25), color(na), color.new(color.green, 30))
- support := box.new(
- top_left = topLeft,
- bottom_right = bottomRight,
- border_color = color.green,
- border_width = 1,
- bgcolor = support_color,
- text = "Vol: "+str.tostring(math.round(Vol,2)),
- text_color = chart.fg_color,
- text_size = size.small
- )
- // Find resistance levels with Negative Volume
- if (not na(pivotHigh)) and Vol < vol_lo
- resistanceLevel := pivotHigh
- resistanceLevel_1 := resistanceLevel+withd
- topLeft = chart.point.from_index(bar_index-lookbackPeriod, resistanceLevel)
- bottomRight = chart.point.from_index(bar_index, resistanceLevel_1)
- resistance_color := color.from_gradient(Vol, ta.lowest(Vol, 25), 0, color.new(color.red, 30), color(na))
- resistance := box.new(
- top_left = topLeft,
- bottom_right = bottomRight,
- border_color = color.red,
- border_width = 1,
- bgcolor = resistance_color,
- text = "Vol: "+str.tostring(math.round(Vol,2)),
- text_color = chart.fg_color,
- text_size = size.small
- )
- // Adaptive Box Len
- support.set_right(bar_index+1)
- resistance.set_right(bar_index+1)
- // Break of support or resistance conditions
- breakout_resistance := ta.crossover(low, resistanceLevel_1)
- resistance_holds := ta.crossunder(high, resistanceLevel)
- support_holds := ta.crossover(low, supportLevel)
- breakout_support := ta.crossunder(high, supportLevel_1)
- // Change Color of Support to red if it was break, change color of resistance to green if it was break
- if breakout_support
- support.set_bgcolor(color.new(color.red, 80))
- support.set_border_color(color.red)
- support.set_border_style(line.style_dashed)
- if support_holds
- support.set_bgcolor(support_color)
- support.set_border_color(color.green)
- support.set_border_style(line.style_solid)
- if breakout_resistance
- resistance.set_bgcolor(color.new(color.green, 80))
- resistance.set_border_color(color.new(color.green, 0))
- resistance.set_border_style(line.style_dashed)
- if resistance_holds
- resistance.set_bgcolor(resistance_color)
- resistance.set_border_color(color.new(color.red, 0))
- resistance.set_border_style(line.style_solid)
- [supportLevel, resistanceLevel, breakout_resistance, resistance_holds, support_holds, breakout_support]
- // Calculate support and resistance levels and their breakouts
- [supportLevel,
- resistanceLevel,
- breakout_resistance,
- resistance_holds,
- support_holds,
- breakout_support] = calcSupportResistance(close, lookbackPeriod)
- // Check if Resistance become Support or Support Become Resistance
- var bool resistance_is_support = false
- var bool support_is_resistance = false
- switch
- breakout_resistance => resistance_is_support := true
- resistance_holds => resistance_is_support := false
- switch
- breakout_support => support_is_resistance := true
- support_holds => support_is_resistance := false
- // ---------------------------------------------------------------------------------------------------------------------}
- // πππππΌππππΌππππ
- // ---------------------------------------------------------------------------------------------------------------------{
- // Plot resistance and support breakouts and holds
- plotchar(resistance_holds, "Resistance Holds", "β",
- color = #e92929, size = size.tiny, location = location.abovebar, offset = -1)
- plotchar(support_holds, "Support Holds", "β",
- color = #20ca26, size = size.tiny, location = location.belowbar, offset = -1)
- plotchar(breakout_resistance and resistance_is_support[1], "Resistance as Support Holds", "β",
- color = #20ca26, size = size.tiny, location = location.belowbar, offset = -1)
- plotchar(breakout_support and support_is_resistance[1], "Support as Resistance Holds", "β",
- color = #e92929, size = size.tiny, location = location.abovebar, offset = -1)
- // Break Out Labels
- if breakout_support and not support_is_resistance[1]
- label.new(
- bar_index[1], supportLevel[1],
- text = "Break Sup",
- style = label.style_label_down,
- color = #7e1e1e,
- textcolor = chart.fg_color,
- size = size.small
- )
- if breakout_resistance and not resistance_is_support[1]
- label.new(
- bar_index[1], resistanceLevel[1],
- text = "Break Res",
- style = label.style_label_up,
- color = #2b6d2d,
- textcolor = chart.fg_color,
- size = size.small
- )
- // Define risk-reward ratio
- riskRewardRatio = 2
- // If resistance Hold: short
- // If support Holds: Long
- longCondition = (supportLevel[3] != supportLevel[1]) and (resistanceLevel > supportLevel)
- shortCondition = false
- //// Uncomment for mean reverting strategy
- // longCondition = false
- // shortCondition = (supportLevel[1] != supportLevel[0]) and (resistanceLevel > supportLevel)
- plot(supportLevel, color=color.green, linewidth=2)
- plot(resistanceLevel, color=color.red, linewidth=2)
- if strategy.position_size == 0
- if longCondition
- strategy.entry("Long", strategy.long)
- stop = supportLevel
- limit = close + (close - supportLevel) * riskRewardRatio
- strategy.exit("TP / SL", "Long", limit = limit, stop = stop)
- if shortCondition
- strategy.entry("Short", strategy.short)
- stop = resistanceLevel
- limit = close - (resistanceLevel - close) * riskRewardRatio
- strategy.exit("TP / SL", "Short", limit = limit, stop = stop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement