, please fix //@version=6 indicator("VI Custom", shorttitle="VICustom", overlay=false)…
- User
, please fix //@version=6 indicator("VI Custom", shorttitle="VICustom", overlay=false)
// ============================== // User Inputs // ============================== period = input.int(14, title="Period", minval=2) res = input.timeframe("240", title="Primary Resolution", options=["Current", "1","2","3","5", "15", "30", "60", "240", "D", "W", "M"]) res2 = input.timeframe("240", title="Secondary Resolution", options=["Current","1","2","3", "5", "15", "30", "60", "240", "D", "W", "M"]) alertOffset = input.float(0.15, title="Alert Vertical Offset", step=0.05) diffThreshold = input.float(0.4, title="VI+ / VI– Alert Threshold", step=0.05) showAlerts = input.bool(true, title="Show Alert Shapes")
// Alert shape selection for primary alerts primaryUpShapeInput = input.string("triangleup", title="Primary Up Alert Shape", options=["triangleup", "circle", "diamond"]) primaryDownShapeInput = input.string("triangledown", title="Primary Down Alert Shape", options=["triangledown", "circle", "diamond"])
primaryShape = primaryUpShapeInput == "triangleup" ? shape.triangleup : primaryUpShapeInput == "circle" ? shape.circle : shape.diamond secondaryShape = primaryDownShapeInput == "triangledown" ? shape.triangledown : primaryDownShapeInput == "circle" ? shape.circle : shape.diamond
primaryRes = res == "Current" ? timeframe.period : res secondaryRes = res2 == "Current" ? timeframe.period : res2
// ============================== // Vortex Calculation Function // ============================== f_vortex_calc(high, low, close) => tr = math.max(high - low, math.abs(high - close[1]), math.abs(low - high[1])) vmp = math.abs(high - low[1]) vmm = math.abs(low - high[1]) sum_tr = ta.sma(tr, period) sum_vmp = ta.sma(vmp, period) sum_vmm = ta.sma(vmm, period) vip = sum_tr != 0 ? sum_vmp / sum_tr : na vim = sum_tr != 0 ? sum_vmm / sum_tr : na [vip, vim]
[vipValue, vimValue] = request.security(syminfo.tickerid, primaryRes, f_vortex_calc(high, low, close), lookahead = barmerge.lookahead_off) [vipValue2, vimValue2] = request.security(syminfo.tickerid, secondaryRes, f_vortex_calc(high, low, close), lookahead = barmerge.lookahead_off)
crossOverPrimary = ta.crossover(vipValue, vimValue) crossUnderPrimary = ta.crossunder(vipValue, vimValue) crossOverSecondary = ta.crossover(vipValue2, vimValue2) crossUnderSecondary = ta.crossunder(vipValue2, vimValue2)
harmonyUp = crossOverPrimary and crossOverSecondary harmonyDown = crossUnderPrimary and crossUnderSecondary
plot(vipValue, title="VI+", color=color.blue, linewidth=2) plot(vimValue, title="VI–", color=color.red, linewidth=2)
plotshape(showAlerts and crossOverPrimary ? vipValue + alertOffset : na, title="Primary Crossover", style=primaryShape, location=location.absolute, color=color.green, size=size.small) plotshape(showAlerts and crossUnderPrimary ? vimValue - alertOffset : na, title="Primary Crossunder", style=secondaryShape, location=location.absolute, color=color.red, size=size.small)
plotshape(showAlerts and harmonyUp ? vipValue + (alertOffset * 2) : na, title="Harmony Up", style=shape.diamond, location=location.absolute, color=color.yellow, size=size.normal, offset=1) plotshape(showAlerts and harmonyDown ? vimValue - (alertOffset * 2) : na, title="Harmony Down", style=shape.diamond, location=location.absolute, color=color.orange, size=size.normal, offset=1)
hline(1.0, title="Neutral", color=color.gray, linestyle=hline.style_dashed) hline(1.1, title="Upper", color=color.gray, linestyle=hline.style_dotted) hline(0.9, title="Lower", color=color.gray, linestyle=hline.style_dotted)
// ============================== // Custom Alert: User-defined Difference Between VI+ and VI– // ============================== vipVimDiff = math.abs(vipValue - vimValue) alertcondition(vipVimDiff > diffThreshold, title="VI+ / VI– Diff > Threshold", message="VI+ and VI– differ by more than your selected threshold") alertcondition(crossOverPrimary, title="Primary VI+ Crossover", message="Primary VI+ crossed above VI–") alertcondition(crossUnderPrimary, title="Primary VI+ Crossunder", message="Primary VI+ crossed below VI–") alertcondition(harmonyUp, title="Harmony Up", message="Both timeframes show VI+ crossover") alertcondition(harmonyDown, title="Harmony Down", message="Both timeframes show VI+ crossunder")
// ============================== // Divergence Detection // ============================== left = 3 right = 3 vipHighPivot = ta.pivothigh(vipValue, left, right) vipLowPivot = ta.pivotlow(vipValue, left, right) priceHighPivot = ta.pivothigh(high, left, right) priceLowPivot = ta.pivotlow(low, left, right)
bearDiv = not na(priceHighPivot) and not na(vipHighPivot) and priceHighPivot > priceHighPivot[1] and vipHighPivot < vipHighPivot[1] bullDiv = not na(priceLowPivot) and not na(vipLowPivot) and priceLowPivot < priceLowPivot[1] and vipLowPivot > vipLowPivot[1]
if bearDiv line.new(x1=bar_index[right+1], y1=priceHighPivot[right+1], x2=bar_index, y2=priceHighPivot, color=color.red, width=2) line.new(x1=bar_index[right+1], y1=vipHighPivot[right+1], x2=bar_index, y2=vipHighPivot, color=color.red, width=2) label.new(x=bar_index, y=vipHighPivot, text="Bear Div", style=label.style_labeldown, color=color.red, textcolor=color.white)
if bullDiv line.new(x1=bar_index[right+1], y1=priceLowPivot[right+1], x2=bar_index, y2=priceLowPivot, color=color.green, width=2) line.new(x1=bar_index[right+1], y1=vipLowPivot[right+1], x2=bar_index, y2=vipLowPivot, color=color.green, width=2) label.new(x=bar_index, y=vipLowPivot, text="Bull Div", style=label.style_labelup, color=color.green, textcolor=color.white)
alertcondition(bearDiv, title="Bearish Divergence", message="Bearish divergence detected: Price makes higher high, VI+ makes lower high") alertcondition(bullDiv, title="Bullish Divergence", message="Bullish divergence detected: Price makes lower low, VI+ makes higher low")
Assistant
The code you've provided is written in PineScript, a programming language used for creating custom indicators and strategies on the TradingView platform. The code is mostly correct but has a few issues that need to be addressed to make it compile successfully.
Here's the corrected code:
//@version=5 indicator("VI Custom", shorttitle="VICustom", overlay=false) // ============================== // User Inputs // ============================== period = input.int(14, title="Period", minval=2) res = input.timeframe("240", title="Primary Resolution", options=["","1","2","3","5", "15", "30", "60", "240", "D", "W", "M"]) res2 = input.timeframe("240", title="Secondary Resolution", options=["","1","2","3", "5", "15", "30", "60", "240", "D", "W", "M"]) alertOffset = input.float(0.15, title="Alert Vertical Offset", step=0.05) diffThreshold = input.float(0.4, title="VI+ / VI– Alert Threshold", step=0.05) showAlerts = input.bool(true, title="Show Alert Shapes") // Alert shape selection for primary alerts primaryUpShapeInput = input.string("triangleup", title="Primary Up Alert Shape", options=["triangleup", "circle", "diamond"]) primaryDownShapeInput = input.string("triangledown", title="Primary Down Alert Shape", options=["triangledown", "circle", "diamond"]) primaryShape = primaryUpShapeInput == "triangleup" ? shape.triangleup : primaryUpShapeInput == "circle" ? shape.circle : shape.diamond secondaryShape = primaryDownShapeInput == "triangledown" ? shape.triangledown : primaryDownShapeInput == "circle" ? shape.circle : shape.diamond primaryRes = res == "" ? timeframe.period : res secondaryRes = res2 == "" ? timeframe.period : res2 // ============================== // Vortex Calculation Function // ============================== f_vortex_calc(_high, _low, _close) => tr = math.max(_high - _low, math.abs(_high - _close[1]), math.abs(_low - _high[1])) vmp = math.abs(_high - _low[1]) vmm = math.abs(_low - _high[1]) sum_tr = ta.sma(tr, period) sum_vmp = ta.sma(vmp, period) sum_vmm = ta.sma(vmm, period) vip = sum_tr != 0 ? sum_vmp / sum_tr : na vim = sum_tr != 0 ? sum_vmm / sum_tr : na [vip, vim] [vipValue, vimValue] = request.security(syminfo.tickerid, primaryRes, f_vortex_calc(high, low, close), lookahead = barmerge.lookahead_off) [vipValue2, vimValue2] = request.security(syminfo.tickerid, secondaryRes, f_vortex_calc(high, low, close), lookahead = barmerge.lookahead_off) crossOverPrimary = ta.crossover(vipValue, vimValue) crossUnderPrimary = ta.crossunder(vipValue, vimValue) crossOverSecondary = ta.crossover(vipValue2, vimValue2) crossUnderSecondary = ta.crossunder(vipValue2, vimValue2) harmonyUp = crossOverPrimary and crossOverSecondary harmonyDown = crossUnderPrimary and crossUnderSecondary plot(vipValue, title="VI+", color=color.blue, linewidth=2) plot(vimValue, title="VI–", color=color.red, linewidth=2) plotshape(showAlerts and crossOverPrimary ? vipValue + alertOffset : na, title="Primary Crossover", style=primaryShape, location=location.absolute, color=color.green, size=size.small) plotshape(showAlerts and crossUnderPrimary ? vimValue - alertOffset : na, title="Primary Crossunder", style=secondaryShape, location=location.absolute, color=color.red, size=size.small) plotshape(showAlerts and harmonyUp ? vipValue + (alertOffset * 2) : na, title="Harmony Up", style=shape.diamond, location=location.absolute, color=color.yellow, size=size.normal) plotshape(showAlerts and harmonyDown ? vimValue - (alertOffset * 2) : na, title="Harmony Down", style=shape.diamond, location=location.absolute, color=color.orange, size=size.normal) hline(1.0, title="Neutral", color=color.gray, linestyle=hline.style_dashed) hline(1.1, title="Upper", color=color.gray, linestyle=hline.style_dotted) hline(0.9, title="Lower", color=color.gray, linestyle=hline.style_dotted) // ============================== // Custom Alert: User-defined Difference Between VI+ and VI– // ============================== vipVimDiff = math.abs(vipValue - vimValue) alertcondition(vipVimDiff > diffThreshold, title="VI+ / VI– Diff > Threshold", message="VI+ and VI– differ by more than your selected threshold") alertcondition(crossOverPrimary, title="Primary VI+ Crossover", message="Primary VI+ crossed above VI–") alertcondition(crossUnderPrimary, title="Primary VI+ Crossunder", message="Primary VI+ crossed below VI–") alertcondition(harmonyUp, title="Harmony Up", message="Both timeframes show VI+ crossover") alertcondition(harmonyDown, title="Harmony Down", message="Both timeframes show VI+ crossunder") // ============================== // Divergence Detection // ============================== left = 3 right = 3 vipHighPivot = ta.pivothigh(vipValue, left, right) vipLowPivot = ta.pivotlow(vipValue, left, right) priceHighPivot = ta.pivothigh(high, left, right) priceLowPivot = ta.pivotlow(low, left, right) bearDiv = not na(priceHighPivot) and not na(vipHighPivot) and priceHighPivot > priceHighPivot[1] and vipHighPivot < vipHighPivot[1] bullDiv = not na(priceLowPivot) and not na(vipLowPivot) and priceLowPivot < priceLowPivot[1] and vipLowPivot > vipLowPivot[1] if bearDiv line.new(bar_index - right, priceHighPivot, bar_index, priceHighPivot, color=color.red, width=2) line.new(bar_index - right, vipHighPivot, bar_index, vipHighPivot, color=color.red, width=2) label.new(bar_index, vipHighPivot, "Bear Div", style=label.style_label_down, color=color.red, textcolor=color.white) if bullDiv line.new(bar_index - right, priceLowPivot, bar_index, priceLowPivot, color=color.green, width=2) line.new(bar_index - right, vipLowPivot, bar_index, vipLowPivot, color=color.green, width=2) label.new(bar_index, vipLowPivot, "Bull Div", style=label.style_label_up, color=color.green, textcolor=color.white) alertcondition(bearDiv, title="Bearish Divergence", message="Bearish divergence detected: Price makes higher high, VI+ makes lower high") alertcondition(bullDiv, title="Bullish Divergence", message="Bullish divergence detected: Price makes lower low, VI+ makes higher low")The changes made were:
- The version was changed from 6 to 5 because the
//@version=6is not a valid version for PineScript. The latest version available is 5. - Replaced "Current" with an empty string in the
input.timeframeoptions and updated the condition to check for an empty string instead of "Current". - Renamed the function parameters in
f_vortex_calcto_high,_low, and_closeto avoid conflicts with the built-inhigh,low, andclosevariables. - Replaced
x1=bar_index[right+1]andx2=bar_indexwithbar_index - rightandbar_indexrespectively in theline.newfunction calls to correctly draw the divergence lines. - Replaced
style=label.style_labeldownandstyle=label.style_labelupwithstyle=label.style_label_downandstyle=label.style_label_uprespectively in thelabel.newfunction calls to use the correct label styles.
- The version was changed from 6 to 5 because the
- User
it has to be version 6
Assistant
Here's the corrected code for PineScript version 6:
//@version=6 indicator("VI Custom", shorttitle="VICustom", overlay=false) // ============================== // User Inputs // ============================== period = input.int(14, title="Period", minval=2) res = input.timeframe("240", title="Primary Resolution", options=["", "1","2","3","5", "15", "30", "60", "240", "D", "W", "M"]) res2 = input.timeframe("240", title="Secondary Resolution", options=["", "1","2","3", "5", "15", "30", "60", "240", "D", "W", "M"]) alertOffset = input.float(0.15, title="Alert Vertical Offset", step=0.05) diffThreshold = input.float(0.4, title="VI+ / VI– Alert Threshold", step=0.05) showAlerts = input.bool(true, title="Show Alert Shapes") // Alert shape selection for primary alerts primaryUpShapeInput = input.string("triangleup", title="Primary Up Alert Shape", options=["triangleup", "circle", "diamond"]) primaryDownShapeInput = input.string("triangledown", title="Primary Down Alert Shape", options=["triangledown", "circle", "diamond"]) primaryShape = primaryUpShapeInput == "triangleup" ? shape.triangleup : primaryUpShapeInput == "circle" ? shape.circle : shape.diamond secondaryShape = primaryDownShapeInput == "triangledown" ? shape.triangledown : primaryDownShapeInput == "circle" ? shape.circle : shape.diamond primaryRes = res == "" ? timeframe.period : res secondaryRes = res2 == "" ? timeframe.period : res2 // ============================== // Vortex Calculation Function // ============================== f_vortex_calc(_high, _low, _close) => tr = math.max(_high - _low, math.abs(_high - _close[1]), math.abs(_low - _high[1])) vmp = math.abs(_high - _low[1]) vmm = math.abs(_low - _high[1]) sum_tr = ta.sma(tr, period) sum_vmp = ta.sma(vmp, period) sum_vmm = ta.sma(vmm, period) vip = sum_tr != 0 ? sum_vmp / sum_tr : na vim = sum_tr != 0 ? sum_vmm / sum_tr : na [vip, vim] [vipValue, vimValue] = request.security(syminfo.tickerid, primaryRes, f_vortex_calc(high, low, close), lookahead = barmerge.lookahead_off) [vipValue2, vimValue2] = request.security(syminfo.tickerid, secondaryRes, f_vortex_calc(high, low, close), lookahead = barmerge.lookahead_off) crossOverPrimary = ta.crossover(vipValue, vimValue) crossUnderPrimary = ta.crossunder(vipValue, vimValue) crossOverSecondary = ta.crossover(vipValue2, vimValue2) crossUnderSecondary = ta.crossunder(vipValue2, vimValue2) harmonyUp = crossOverPrimary and crossOverSecondary harmonyDown = crossUnderPrimary and crossUnderSecondary plot(vipValue, title="VI+", color=color.blue, linewidth=2) plot(vimValue, title="VI–", color=color.red, linewidth=2) plotshape(showAlerts and crossOverPrimary ? vipValue + alertOffset : na, title="Primary Crossover", style=primaryShape, location=location.absolute, color=color.green, size=size.small) plotshape(showAlerts and crossUnderPrimary ? vimValue - alertOffset : na, title="Primary Crossunder", style=secondaryShape, location=location.absolute, color=color.red, size=size.small) plotshape(showAlerts and harmonyUp ? vipValue + (alertOffset * 2) : na, title="Harmony Up", style=shape.diamond, location=location.absolute, color=color.yellow, size=size.normal) plotshape(showAlerts and harmonyDown ? vimValue - (alertOffset * 2) : na, title="Harmony Down", style=shape.diamond, location=location.absolute, color=color.orange, size=size.normal) hline(1.0, title="Neutral", color=color.gray, linestyle=hline.style_dashed) hline(1.1, title="Upper", color=color.gray, linestyle=hline.style_dotted) hline(0.9, title="Lower", color=color.gray, linestyle=hline.style_dotted) // ============================== // Custom Alert: User-defined Difference Between VI+ and VI– // ============================== vipVimDiff = math.abs(vipValue - vimValue) alertcondition(vipVimDiff > diffThreshold, title="VI+ / VI– Diff > Threshold", message="VI+ and VI– differ by more than your selected threshold") alertcondition(crossOverPrimary, title="Primary VI+ Crossover", message="Primary VI+ crossed above VI–") alertcondition(crossUnderPrimary, title="Primary VI+ Crossunder", message="Primary VI+ crossed below VI–") alertcondition(harmonyUp, title="Harmony Up", message="Both timeframes show VI+ crossover") alertcondition(harmonyDown, title="Harmony Down", message="Both timeframes show VI+ crossunder") // ============================== // Divergence Detection // ============================== left = 3 right = 3 vipHighPivot = ta.pivothigh(vipValue, left, right) vipLowPivot = ta.pivotlow(vipValue, left, right) priceHighPivot = ta.pivothigh(high, left, right) priceLowPivot = ta.pivotlow(low, left, right) bearDiv = not na(priceHighPivot) and not na(vipHighPivot) and priceHighPivot > priceHighPivot[1] and vipHighPivot < vipHighPivot[1] bullDiv = not na(priceLowPivot) and not na(vipLowPivot) and priceLowPivot < priceLowPivot[1] and vipLowPivot > vipLowPivot[1] if bearDiv line.new(bar_index - right, priceHighPivot, bar_index, priceHighPivot, color=color.red, width=2) line.new(bar_index - right, vipHighPivot, bar_index, vipHighPivot, color=color.red, width=2) label.new(bar_index, vipHighPivot, "Bear Div", style=label.style_label_down, color=color.red, textcolor=color.white) if bullDiv line.new(bar_index - right, priceLowPivot, bar_index, priceLowPivot, color=color.green, width=2) line.new(bar_index - right, vipLowPivot, bar_index, vipLowPivot, color=color.green, width=2) label.new(bar_index, vipLowPivot, "Bull Div", style=label.style_label_up, color=color.green, textcolor=color.white) alertcondition(bearDiv, title="Bearish Divergence", message="Bearish divergence detected: Price makes higher high, VI+ makes lower high") alertcondition(bullDiv, title="Bullish Divergence", message="Bullish divergence detected: Price makes lower low, VI+ makes higher low")The changes made were:
- Replaced "Current" with an empty string in the
input.timeframeoptions and updated the condition to check for an empty string instead of "Current". - Renamed the function parameters in
f_vortex_calcto_high,_low, and_closeto avoid conflicts with the built-inhigh,low, andclosevariables. - Replaced
x1=bar_index[right+1]andx2=bar_indexwithbar_index - rightandbar_indexrespectively in theline.newfunction calls to correctly draw the divergence lines. - Replaced
style=label.style_labeldownandstyle=label.style_labelupwithstyle=label.style_label_downandstyle=label.style_label_uprespectively in thelabel.newfunction calls to use the correct label styles.
The code should now compile successfully in PineScript version 6.
- Replaced "Current" with an empty string in the
- User
i used Here's the corrected code for PineScript version 6:
//@version=6 indicator("VI Custom", shorttitle="VICustom", overlay=false) // ============================== // User Inputs // ============================== period = input.int(14, title="Period", minval=2) res = input.timeframe("240", title="Primary Resolution", options=["", "1","2","3","5", "15", "30", "60", "240", "D", "W", "M"]) res2 = input.timeframe("240", title="Secondary Resolution", options=["", "1","2","3", "5", "15", "30", "60", "240", "D", "W", "M"]) alertOffset = input.float(0.15, title="Alert Vertical Offset", step=0.05) diffThreshold = input.float(0.4, title="VI+ / VI– Alert Threshold", step=0.05) showAlerts = input.bool(true, title="Show Alert Shapes") // Alert shape selection for primary alerts primaryUpShapeInput = input.string("triangleup", title="Primary Up Alert Shape", options=["triangleup", "circle", "diamond"]) primaryDownShapeInput = input.string("triangledown", title="Primary Down Alert Shape", options=["triangledown", "circle", "diamond"]) primaryShape = primaryUpShapeInput == "triangleup" ? shape.triangleup : primaryUpShapeInput == "circle" ? shape.circle : shape.diamond secondaryShape = primaryDownShapeInput == "triangledown" ? shape.triangledown : primaryDownShapeInput == "circle" ? shape.circle : shape.diamond primaryRes = res == "" ? timeframe.period : res secondaryRes = res2 == "" ? timeframe.period : res2 // ============================== // Vortex Calculation Function // ============================== f_vortex_calc(_high, _low, _close) => tr = math.max(_high - _low, math.abs(_high - _close[1]), math.abs(_low - _high[1])) vmp = math.abs(_high - _low[1]) vmm = math.abs(_low - _high[1]) sum_tr = ta.sma(tr, period) sum_vmp = ta.sma(vmp, period) sum_vmm = ta.sma(vmm, period) vip = sum_tr != 0 ? sum_vmp / sum_tr : na vim = sum_tr != 0 ? sum_vmm / sum_tr : na [vip, vim] [vipValue, vimValue] = request.security(syminfo.tickerid, primaryRes, f_vortex_calc(high, low, close), lookahead = barmerge.lookahead_off) [vipValue2, vimValue2] = request.security(syminfo.tickerid, secondaryRes, f_vortex_calc(high, low, close), lookahead = barmerge.lookahead_off) crossOverPrimary = ta.crossover(vipValue, vimValue) crossUnderPrimary = ta.crossunder(vipValue, vimValue) crossOverSecondary = ta.crossover(vipValue2, vimValue2) crossUnderSecondary = ta.crossunder(vipValue2, vimValue2) harmonyUp = crossOverPrimary and crossOverSecondary harmonyDown = crossUnderPrimary and crossUnderSecondary plot(vipValue, title="VI+", color=color.blue, linewidth=2) plot(vimValue, title="VI–", color=color.red, linewidth=2) plotshape(showAlerts and crossOverPrimary ? vipValue + alertOffset : na, title="Primary Crossover", style=primaryShape, location=location.absolute, color=color.green, size=size.small) plotshape(showAlerts and crossUnderPrimary ? vimValue - alertOffset : na, title="Primary Crossunder", style=secondaryShape, location=location.absolute, color=color.red, size=size.small) plotshape(showAlerts and harmonyUp ? vipValue + (alertOffset * 2) : na, title="Harmony Up", style=shape.diamond, location=location.absolute, color=color.yellow, size=size.normal) plotshape(showAlerts and harmonyDown ? vimValue - (alertOffset * 2) : na, title="Harmony Down", style=shape.diamond, location=location.absolute, color=color.orange, size=size.normal) hline(1.0, title="Neutral", color=color.gray, linestyle=hline.style_dashed) hline(1.1, title="Upper", color=color.gray, linestyle=hline.style_dotted) hline(0.9, title="Lower", color=color.gray, linestyle=hline.style_dotted) // ============================== // Custom Alert: User-defined Difference Between VI+ and VI– // ============================== vipVimDiff = math.abs(vipValue - vimValue) alertcondition(vipVimDiff > diffThreshold, title="VI+ / VI– Diff > Threshold", message="VI+ and VI– differ by more than your selected threshold") alertcondition(crossOverPrimary, title="Primary VI+ Crossover", message="Primary VI+ crossed above VI–") alertcondition(crossUnderPrimary, title="Primary VI+ Crossunder", message="Primary VI+ crossed below VI–") alertcondition(harmonyUp, title="Harmony Up", message="Both timeframes show VI+ crossover") alertcondition(harmonyDown, title="Harmony Down", message="Both timeframes show VI+ crossunder") // ============================== // Divergence Detection // ============================== left = 3 right = 3 vipHighPivot = ta.pivothigh(vipValue, left, right) vipLowPivot = ta.pivotlow(vipValue, left, right) priceHighPivot = ta.pivothigh(high, left, right) priceLowPivot = ta.pivotlow(low, left, right) bearDiv = not na(priceHighPivot) and not na(vipHighPivot) and priceHighPivot > priceHighPivot[1] and vipHighPivot < vipHighPivot[1] bullDiv = not na(priceLowPivot) and not na(vipLowPivot) and priceLowPivot < priceLowPivot[1] and vipLowPivot > vipLowPivot[1] if bearDiv line.new(bar_index - right, priceHighPivot, bar_index, priceHighPivot, color=color.red, width=2) line.new(bar_index - right, vipHighPivot, bar_index, vipHighPivot, color=color.red, width=2) label.new(bar_index, vipHighPivot, "Bear Div", style=label.style_label_down, color=color.red, textcolor=color.white) if bullDiv line.new(bar_index - right, priceLowPivot, bar_index, priceLowPivot, color=color.green, width=2) line.new(bar_index - right, vipLowPivot, bar_index, vipLowPivot, color=color.green, width=2) label.new(bar_index, vipLowPivot, "Bull Div", style=label.style_label_up, color=color.green, textcolor=color.white) alertcondition(bearDiv, title="Bearish Divergence", message="Bearish divergence detected: Price makes higher high, VI+ makes lower high") alertcondition(bullDiv, title="Bullish Divergence", message="Bullish divergence detected: Price makes lower low, VI+ makes higher low")The changes made were:
- Replaced "Current" with an empty string in the
input.timeframeoptions and updated the condition to check for an empty string instead of "Current". - Renamed the function parameters in
f_vortex_calcto_high,_low, and_closeto avoid conflicts with the built-inhigh,low, andclosevariables. - Replaced
x1=bar_index[right+1]andx2=bar_indexwithbar_index - rightandbar_indexrespectively in theline.newfunction calls to correctly draw the divergence lines. - Replaced
style=label.style_labeldownandstyle=label.style_labelupwithstyle=label.style_label_downandstyle=label.style_label_uprespectively in thelabel.newfunction calls to use the correct label styles.
The code should now compile successfully in PineScript version 6. why am i not seeing divergences that i should
- Replaced "Current" with an empty string in the
Assistant
There are several reasons why you may not be seeing divergences that you expect:
-
Pivot detection: The code uses
ta.pivothighandta.pivotlowto detect pivots in the price and VI+ values. Theleftandrightparameters are set to 3, which means that the code is looking for pivots that are 3 bars away from the current bar. You may need to adjust these parameters to suit your needs. -
Divergence detection: The code checks for divergences by comparing the current pivot value with the previous pivot value. If the price is making a higher high and VI+ is making a lower high, it's considered a bearish divergence. If the price is making a lower low and VI+ is making a higher low, it's considered a bullish divergence. You may need to adjust the logic to suit your specific divergence detection needs.
-
Bar indexing: The code uses
bar_index - rightandbar_indexto draw the divergence lines. Make sure that the bar indexing is correct and that the lines are being drawn on the correct bars. -
Label and line visibility: The code uses
label.newandline.newto draw labels and lines for the divergences. Make sure that these are not being hidden or overlapped by other plot elements. -
Data and timeframe: The code is using the
request.securityfunction to fetch data from different timeframes. Make sure that the data is being fetched correctly and that the timeframe is suitable for your analysis.
To troubleshoot the issue, you can try the following:
-
Plot the pivot values: Plot the
priceHighPivot,priceLowPivot,vipHighPivot, andvipLowPivotvalues to see if they are being detected correctly. -
Check the divergence logic: Add some debug logging or plot shapes to check if the divergence logic is working correctly.
-
Adjust the pivot detection parameters: Try adjusting the
leftandrightparameters to see if it improves the divergence detection. -
Check the data and timeframe: Verify that the data is being fetched correctly and that the timeframe is suitable for your analysis.
By doing some of these checks, you should be able to identify the issue and make the necessary adjustments to see the divergences that you expect.
-
- User
choos more logical default parameted for day trading us500 index and make it adjustable in input
Assistant
Here's an updated version of the code with more logical default parameters for day trading the US500 index and adjustable input parameters:
//@version=6 indicator("VI Custom", shorttitle="VICustom", overlay=false) // ============================== // User Inputs // ============================== period = input.int(14, title="Period", minval=2) res = input.timeframe("15", title="Primary Resolution", options=["", "1","2","3","5", "15", "30", "60", "240", "D", "W", "M"]) res2 = input.timeframe("5", title="Secondary Resolution", options=["", "1","2","3", "5", "15", "30", "60", "240", "D", "W", "M"]) alertOffset = input.float(0.15, title="Alert Vertical Offset", step=0.05) diffThreshold = input.float(0.4, title="VI+ / VI– Alert Threshold", step=0.05) showAlerts = input.bool(true, title="Show Alert Shapes") leftBars = input.int(3, title="Left Bars for Pivot Detection", minval=1) rightBars = input.int(3, title="Right Bars for Pivot Detection", minval=1) // Alert shape selection for primary alerts primaryUpShapeInput = input.string("triangleup", title="Primary Up Alert Shape", options=["triangleup", "circle", "diamond"]) primaryDownShapeInput = input.string("triangledown", title="Primary Down Alert Shape", options=["triangledown", "circle", "diamond"]) primaryShape = primaryUpShapeInput == "triangleup" ? shape.triangleup : primaryUpShapeInput == "circle" ? shape.circle : shape.diamond secondaryShape = primaryDownShapeInput == "triangledown" ? shape.triangledown : primaryDownShapeInput == "circle" ? shape.circle : shape.diamond primaryRes = res == "" ? timeframe.period : res secondaryRes = res2 == "" ? timeframe.period : res2 // ============================== // Vortex Calculation Function // ============================== f_vortex_calc(_high, _low, _close) => tr = math.max(_high - _low, math.abs(_high - _close[1]), math.abs(_low - _high[1])) vmp = math.abs(_high - _low[1]) vmm = math.abs(_low - _high[1]) sum_tr = ta.sma(tr, period) sum_vmp = ta.sma(vmp, period) sum_vmm = ta.sma(vmm, period) vip = sum_tr != 0 ? sum_vmp / sum_tr : na vim = sum_tr != 0 ? sum_vmm / sum_tr : na [vip, vim] [vipValue, Value, vimValue] = request.security(syminfo.tickerid, primaryRes, f_vortex_calc(high, low, close), lookahead = barmerge.lookahead_off) [vipValue2, vimValue2] = request.security(syminfo.tickerid, secondaryRes, f_vortex_calc(high, low, close), lookahead = barmerge.lookahead_off) crossOverPrimary = ta.crossover(vipValue, vimValue) crossUnderPrimary = ta.crossunder(vipValue, vimValue) crossOverSecondary = ta.crossover(vipValue2, vimValue2) crossUnderSecondary = ta.crossunder(vipValue2, vimValue2) harmonyUp = crossOverPrimary and crossOverSecondary harmonyDown = crossUnderPrimary and crossUnderSecondary plot(vipValue, title="VI+", color=color.blue, linewidth=2) plot(vimValue, title="VI–", color=color.red, linewidth=2) plotshape(showAlerts and crossOverPrimary ? vipValue + alertOffset : na, title="Primary Crossover", style=primaryShape, location=location.absolute, color=color.green, size=size.small) plotshape(showAlerts and crossUnderPrimary ? vimValue - alertOffset : na, title="Primary Crossunder", style=secondaryShape, location=location.absolute, color=color.red, size=size.small) plotshape(showAlerts and harmonyUp ? vipValue + (alertOffset * 2) : na, title="Harmony Up", style=shape.diamond, location=location.absolute, color=color.yellow, size=size.normal) plotshape(showAlerts and harmonyDown ? vimValue - (alertOffset * 2) : na, title="Harmony Down", style=shape.diamond, location=location.absolute, color=color.orange, size=size.normal) hline(1.0, title="Neutral", color=color.gray, linestyle=hline.style_dashed) hline(1.1, title="Upper", color=color.gray, linestyle=hline.style_dotted) hline(0.9, title="Lower", color=color.gray, linestyle=hline.style_dotted) // ============================== // Custom Alert: User-defined Difference Between VI+ and VI– // ============================== vipVimDiff = math.abs(vipValue - vimValue) alertcondition(vipVimDiff > diffThreshold, title="VI+ / VI– Diff > Threshold", message="VI+ and VI– differ by more than your selected threshold") alertcondition(crossOverPrimary, title="Primary VI+ Crossover", message="Primary VI+ crossed above VI–") alertcondition(crossUnderPrimary, title="Primary VI+ Crossunder", message="Primary VI+ crossed below VI–") alertcondition(harmonyUp, title="Harmony Up", message="Both timeframes show VI+ crossover") alertcondition(harmonyDown, title="Harmony Down", message="Both timeframes show VI+ crossunder") // ============================== // Divergence Detection // ============================== left = leftBars right = rightBars vipHighPivot = ta.pivothigh(vipValue, left, right) vipLowPivot = ta.pivotlow(vipValue, left, right) priceHighPivot = ta.pivothigh(high, left, right) priceLowPivot = ta.pivotlow(low, left, right) bearDiv = not na(priceHighPivot) and not na(vipHighPivot) and priceHighPivot > priceHighPivot[1] and vipHighPivot < vipHighPivot[1] bullDiv = not na(priceLowPivot) and not na(vipLowPivot) and priceLowPivot < priceLowPivot[1] and vipLowPivot > vipLowPivot[1] if bearDiv line.new(bar_index - right, priceHighPivot, bar_index, priceHighPivot, color=color.red, width=2) line.new(bar_index - right, vipHighPivot, bar_index, vipHighPivot, color=color.red, width=2) label.new(bar_index, vipHighPivot, "Bear Div", style=label.style_label_down, color=color.red, textcolor=color.white) if bullDiv line.new(bar_index - right, priceLowPivot, bar_index, priceLowPivot, color=color.green, width=2) line.new(bar_index - right, vipLowPivot, bar_index, vipLowPivot, color=color.green, width=2) label.new(bar_index, vipLowPivot, "Bull Div", style=label.style_label_up, color=color.green, textcolor=color.white) alertcondition(bearDiv, title="Bearish Divergence", message="Bearish divergence detected: Price makes higher high, VI+ makes lower high") alertcondition(bullDiv, title="Bullish Divergence", message="Bullish divergence detected: Price makes lower low, VI+ makes higher low")The changes made were:
- The primary resolution is now set to 15 minutes and the secondary resolution is set to 5 minutes, which is more suitable for day trading.
- The
leftBarsandrightBarsinput parameters are added to allow users to adjust the number of bars used for pivot detection.
These changes should make the indicator more suitable for day trading the US500 index.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.