*Chandelier Exit Pro w/Extensions*

MYNAMEISBRANDON · study · 653 行 · 点赞 252 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MYNAMEISBRANDON
//I've been working on this script for quite a while and the concepts within it and help and ideas on bits of the code have come from a few sources, namely 
//➡️SimpleCryptoLife
//➡️bfr_
//➡️Everget (for the main inspiration)
//You're free to manipulate the code at your discretion but please give proper reference 🙏
//I've included calc_bars_count which is a relatively new addition to the indicator function that allows you to limit the past calculations. Very useful and can help to speed up efficiency of the indicator.
 
 
//@version=5
indicator("*Chandelier Exit Pro w/Extensions*", "*CE_Pro*", overlay=true, calc_bars_count=1000)
 
 
 
var string maGroup0 = '⚡--------------------------Global Options--------------------------⚡'
awaitBarConfirmation = input.bool(title="Bar Confirmation-For Alerts", defval=true, group=maGroup0)
var int in_labelPadding_ = input.int(title="Label Padding-Active Levels", minval=0, maxval=200, defval=5, group=maGroup0)
var int in_labelPadding_ext = input.int(title="Label Padding-Inactive Extension Levels", minval=0, maxval=200, defval=10, group=maGroup0)
 
 
 
 
resInputMA2 = input.timeframe(title='Time Frame', defval='', group=maGroup0, inline='')
 
_getMany(res, src1, src2, src3, src4, src5) =>
    data1 = src1//[1]
    data2 = src2//[1]
    data3 = src3//[1]
    data4 = src4//[1]
    data5 = src5//[1]
 
    if res == timeframe.period
        data1 := src1
        data2 := src2
        data3 := src3
        data4 := src4
        data5 := src5
 
    [out1, out2, out3, out4, out5] = request.security(syminfo.tickerid, res, [data1, data2, data3, data4, data5], lookahead=barmerge.lookahead_on)
    [out1, out2, out3, out4, out5]
 
 
 
var string ceGroup = '⚡--------------------------------Level 1--------------------------------⚡'
ceATRLength = input(title='ATR Period', defval=14, group=ceGroup)
ceATRMult = input.float(title='ATR Multiplier', step=0.1, defval=2.5, group=ceGroup)
ceShowLabels = input(title='Show Labels @ L', defval=true, group=ceGroup)
ceShowLabels_ATRIGHT_ORG_22 = input(title='Show Active Lvl Labels @ R ', defval=true, group=ceGroup)
ceUseClose = input(title='Use Close Price', defval=true, group=ceGroup, tooltip='This changes the calculations on past candles. If you want to wait until the candle closes, keep this checked. In real time though, if price passes above/below a level, the Chandelier Exit plot and associated labels will change axis. In a perfect world the real time changes do not occur until the bar closes. Consider this a work-in-progress.')  //#a7f389
highlightState1 = input.bool(title='Highlight Fill', defval=true, group=ceGroup)
 
 
// Convert ceATRMult to a string to use it dynamically in the label
string atrMultiplierText = str.tostring(ceATRMult, "#.#")  // This will convert 2.5, 4, or any value into a string like "2.5", "4", etc.
 
_ce(_atrMult, _atrLength, _useClose) =>
    _atr = _atrMult * ta.atr(_atrLength)
    _longStop = (_useClose ? ta.highest(close, _atrLength) : ta.highest(_atrLength)) - _atr
    _longStopPrev = nz(_longStop[1], _longStop)
    _longStop := close[1] > _longStopPrev ? math.max(_longStop, _longStopPrev) : _longStop
    _shortStop = (_useClose ? ta.lowest(close, _atrLength) : ta.lowest(_atrLength)) + _atr
    _shortStopPrev = nz(_shortStop[1], _shortStop)
    _shortStop := close[1] < _shortStopPrev ? math.min(_shortStop, _shortStopPrev) : _shortStop
    var int _dir = 1
    _dir := close > _shortStopPrev ? 1 : close < _longStopPrev ? -1 : _dir
    _buySignal = _dir == 1 and _dir[1] == -1
    _sellSignal = _dir == -1 and _dir[1] == 1
 
    [_dirMTF, _longStopMTF, _shortStopMTF, _buySignalMTF, _sellSignalMTF] = _getMany(resInputMA2, _dir, _longStop, _shortStop, _buySignal, _sellSignal)
    [_dirMTF, _longStopMTF, _shortStopMTF, _buySignalMTF, _sellSignalMTF]
 
var color longColor = color.yellow   //#a7f389
var color shortColor = color.gray //color.rgb(249, 135, 135)
var color longFillColor = color.new(color.yellow, 90)
var color shortFillColor = color.new(color.gray, 90)
 
[ceDir, ceLongStop, ceShortStop, ceBuySignal, ceSellSignal] = _ce(ceATRMult, ceATRLength, ceUseClose)
 
 
 
 
ceLongStopPlot = plot(ceDir == 1 ? ceLongStop : na, title='Bull Lvl 1-Active', style=plot.style_linebr, linewidth=4, color=color.new(longColor,25))
ceShortStopPlot = plot(ceDir == 1 ? na : ceShortStop, title='Bear Lvl 1-Active', style=plot.style_linebr, linewidth=4, color=color.new(shortColor, 25))
//plotshape(ceBuySignal ? ceLongStop : na, title='Long Stop Start', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(longColor, 0))
plotshape(ceBuySignal and ceShowLabels ? ceLongStop : na, title='Bull Lvl 1-Label', text='2.5', location=location.absolute, style=shape.labelup, size=size.tiny, color=na, textcolor=color.new(longColor, 0))
plotshape(ceSellSignal and ceShowLabels ? ceShortStop : na, title='Bear Lvl 1-Label', text='2.5', location=location.absolute, style=shape.labeldown, size=size.tiny, color=na, textcolor=color.new(shortColor, 0))
midPricePlot = plot(ohlc4, title='', style=plot.style_circles, linewidth=0, display=display.none, editable=false)
 
longStateFillColor = highlightState1 ? ceDir == 1 ? longFillColor : na : na
shortStateFillColor = highlightState1 ? ceDir == -1 ? shortFillColor : na : na
fill(midPricePlot, ceLongStopPlot, title='Bull Lvl 1 Fill', color=longStateFillColor)
fill(midPricePlot, ceShortStopPlot, title='Bear Lvl 1 Fill', color=shortStateFillColor)
 
invisibleColor=color.rgb(0, 0, 0, 100)
 
f_spacer(int _number) =>  // Add this many em spaces to a string
    var string emSpace = " "  // Em space, Unicode U+2003
    string _spacer = ""
    for i = 1 to _number
        _spacer += emSpace
    _spacer
 
 
var label ceExtLabel_3A = na
var label ceLabel_3A = na
var label ceExtLabel_2a = na  // Global label to track the extension   
var label ceExtLabel_2A = na  // Global label to track the extension
var label ceLabel = na  // Global label to track the last formed level
var label ceLabel_2A = na  // Global label to track the last formed level
var label ceLabel_2a = na
var line atrLine = na   // Global line to track the ATR level
var float lastLevelPrice = na  // To store the last level's price
var int lastBarIndex = na  // Track the last bar index where the level was updated
 
f_ATRLabels_(int _padNumber, float _yValue, string _text) =>
    string _pad = f_spacer(_padNumber)
    string _textUpdated = _pad + _text
    _textUpdated
 
f_labelPrinter_(_label, _condition, _yValue, _text, _textcolor, _barIndex) =>
    var label tempLabel = _label
    if _condition
        if na(tempLabel)
            // Create a new label if it doesn't exist
            tempLabel := label.new(x=_barIndex, y=_yValue, text=_text, xloc=xloc.bar_index, yloc=yloc.price, color=invisibleColor, style=label.style_label_center, textcolor=_textcolor, size=size.normal)
        else
            // Update the label's position and text without recreating it
            label.set_xy(tempLabel, _barIndex, _yValue)
            label.set_text(tempLabel, _text)
            label.set_textcolor(tempLabel, _textcolor)
    tempLabel  // Return the updated label
 
if ceBuySignal or ceSellSignal
    // Delete old label and line if they exist
    if not na(ceLabel)
        label.delete(ceLabel)
    if not na(atrLine)
        line.delete(atrLine)
 
    // Record the start of the level
    lastBarIndex := bar_index
 
    // Determine new level properties based on the signal
    if ceBuySignal
        lastLevelPrice := ceLongStop
 
        // Create the label and set it to follow the ATR plot dynamically
        ceLabel := f_labelPrinter_(ceLabel, ceShowLabels_ATRIGHT_ORG_22, lastLevelPrice, f_ATRLabels_(in_labelPadding_, lastLevelPrice, ""), longColor , lastBarIndex)
    
    if ceSellSignal
        lastLevelPrice := ceShortStop
 
        // Create the label and set it to follow the ATR plot dynamically
        ceLabel := f_labelPrinter_(ceLabel, ceShowLabels_ATRIGHT_ORG_22, lastLevelPrice, f_ATRLabels_(in_labelPadding_, lastLevelPrice, ""), shortColor, lastBarIndex)
 
// Continuously update the label's position to follow the most recent ATR level
if not na(ceLabel)
    lastLevelPrice := (ceDir == 1) ? ceLongStop : ceShortStop
    label.set_xy(ceLabel, bar_index, lastLevelPrice)
//longStateFillColor = ceHighlightState ? ceDir == 1 ? longFillColor : na : na
//shortStateFillColor = ceHighlightState ? ceDir == -1 ? shortFillColor : na : na
//fill(midPricePlot, ceLongStopPlot, title='Long State Filling', color=longStateFillColor)
//fill(midPricePlot, ceShortStopPlot, title='Short State Filling', color=shortStateFillColor)
 
//alertcondition(ceDir != ceDir[1], title='Alert: CE Direction Change', message='Chandelier Exit has changed direction!')
//alertcondition(ceBuySignal, title='Alert: CE Buy', message='Chandelier Exit Buy!')
//alertcondition(ceSellSignal, title='Alert: CE Sell', message='Chandelier Exit Sell!')
 
 
 
 
 
 
 
// Chandelier Exit
var string ceGroup1a = '⚡-------------------------Level 1 Extension-------------------------⚡'
 
ceATRLength1a = input(title='ATR Period', defval=14, group=ceGroup1a)
ceATRMult1a = input.float(title='ATR Multiplier', step=0.1, defval=2.5, group=ceGroup1a)
ceShowLabels_ATRIGHT_5AA = input(title='Show Extension Lvl Labels @ R', defval=true, group=ceGroup1a)
ceUseClose1a = input(title='Use Close Price', defval=true, group=ceGroup1a)
 
 
 
string atrMultiplierText_1a = str.tostring(ceATRMult1a, "#.#")  // This will convert 2.5, 4, or any value into a string like "2.5", "4", etc.
 
 
_ce1a(_atrMult1a, _atrLength1a, _useClose1a) =>
    _atr1a = _atrMult1a * ta.atr(_atrLength1a)
    _longStop1a = (_useClose1a ? ta.highest(close, _atrLength1a) : ta.highest(_atrLength1a)) - _atr1a
    _longStopPrev1a = nz(_longStop1a[1], _longStop1a)
    _longStop1a := close[1] > _longStopPrev1a ? math.max(_longStop1a, _longStopPrev1a) : _longStop1a
    _shortStop1a = (_useClose1a ? ta.lowest(close, _atrLength1a) : ta.lowest(_atrLength1a)) + _atr1a
    _shortStopPrev1a = nz(_shortStop1a[1], _shortStop1a)
    _shortStop1a := close[1] < _shortStopPrev1a ? math.min(_shortStop1a, _shortStopPrev1a) : _shortStop1a
    var int _dir1a = 1
    _dir1a := close > _shortStopPrev1a ? 1 : close < _longStopPrev1a ? -1 : _dir1a
    _buySignal1a= _dir1a == 1 and _dir1a[1] == -1
    _sellSignal1a = _dir1a == -1 and _dir1a[1] == 1
 
    [_dirMTF1a, _longStopMTF1a, _shortStopMTF1a, _buySignalMTF1a, _sellSignalMTF1a] = _getMany(resInputMA2, _dir1a, _longStop1a, _shortStop1a, _buySignal1a, _sellSignal1a)
    [_dirMTF1a, _longStopMTF1a, _shortStopMTF1a, _buySignalMTF1a, _sellSignalMTF1a]
 
var color longColor1a = color.yellow // #a7f389
var color shortColor1a = color.gray  // color.rgb(249, 135, 135)
 
[ceDir1a, ceLongStop1a, ceShortStop1a, ceBuySignal1a, ceSellSignal1a] = _ce1a(ceATRMult1a, ceATRLength1a, ceUseClose1a)
 
 
 
ceLongStopPlot1a = plot(true ? ceLongStop1a : na, title='Bull Lvl 1-Ext', style=plot.style_linebr, linewidth=1, color=color.new(longColor1a, 75))
ceShortStopPlot1a = plot(true  ? ceShortStop1a : na, title='Bear Lvl 1-Ext', style=plot.style_linebr, linewidth=1, color=color.new(shortColor1a, 75))
 
// Label code added below: Placing labels for active and extension levels
 
//var label ceLabel = na  // Global label to track the last formed level
var label ceExtLabel = na  // Global label to track the extension
 
 
// Main Labels for the active levels
if ceDir1a == 1  // If ceLongStop1a is active, show "2.5" on the green line
    ceLabel := f_labelPrinter_(ceLabel, ceShowLabels_ATRIGHT_ORG_22, ceLongStop1a, f_ATRLabels_(in_labelPadding_, ceLongStop1a, atrMultiplierText), longColor1a, bar_index)
else  // If ceShortStop1a is active, show "2.5" on the pink line
    ceLabel := f_labelPrinter_(ceLabel, ceShowLabels_ATRIGHT_ORG_22, ceShortStop1a, f_ATRLabels_(in_labelPadding_, ceShortStop1a, atrMultiplierText), shortColor1a, bar_index)
 
// Extension Labels for the inactive levels
if ceDir1a == -1 and not na(ceLongStop1a)  // If ceShortStop1a is active, ceLongStop1a should show "2.5ext" on the thinner green line
    ceExtLabel := f_labelPrinter_(ceExtLabel, ceShowLabels_ATRIGHT_5AA, ceLongStop1a, f_ATRLabels_(in_labelPadding_ext, ceLongStop1a, atrMultiplierText_1a+"x"), longColor1a, bar_index)
else if ceDir1a == 1 and not na(ceShortStop1a)  // If ceLongStop1a is active, ceShortStop1a should show "2.5ext" on the thinner pink line
    ceExtLabel := f_labelPrinter_(ceExtLabel, ceShowLabels_ATRIGHT_5AA, ceShortStop1a, f_ATRLabels_(in_labelPadding_ext, ceShortStop1a, atrMultiplierText_1a+"x"), shortColor1a, bar_index)
 
//plotshape(ceBuySignal1a and ceShowLabels1a ? ceLongStop1a : na, title='2.5 Buy Label', text='2.5', location=location.absolute, style=shape.labelup, size=size.tiny, color=na, textcolor=color.new(longColor, 0))
//plotshape(ceSellSignal1a and ceShowLabels1a ? ceShortStop1a : na, title='2.5 Sell Label', text='2.5', location=location.absolute, style=shape.labeldown, size=size.tiny, color=na, textcolor=color.new(shortColor, 0))
 
 
 
 
 
var string ceGroup1 = '⚡--------------------------------Level 2--------------------------------⚡'
ceATRLength_1 = input(title='ATR Period', defval=14, group=ceGroup1)
ceATRMult_1 = input.float(title='ATR Multiplier', step=0.1, defval=5.0, group=ceGroup1)
ceShowLabels_1 = input(title='Show Labels @ L', defval=true, group=ceGroup1)
ceShowLabels_ATRIGHT_ORG_11 = input(title='Show Active Lvl Labels @ R', defval=true, group=ceGroup1)
ceUseClose_1 = input(title='Use Close Price', defval=true, group=ceGroup1)
highlightState2 = input.bool(title='Highlight Fill', defval=true, group=ceGroup1)
 
 
string atrMultiplierText_l = str.tostring(ceATRMult_1, "#.#")  // This will convert 2.5, 4, or any value into a string like "2.5", "4", etc.
 
var color longColor_1 = color.yellow // #45f300
var color shortColor_1 = color.gray // color.rgb(179, 45, 45)
var color longFillColor_1 = color.new(color.yellow, 90)
var color shortFillColor_1 = color.new(color.gray, 90)
 
[ceDir_1, ceLongStop_1, ceShortStop_1, ceBuySignal_1, ceSellSignal_1] = _ce(ceATRMult_1, ceATRLength_1, ceUseClose_1)
 
ceLongStopPlot_1 = plot( true and ceDir_1 == 1 ? ceLongStop_1 : na, title='Bull Lvl 2-Active', style=plot.style_linebr, linewidth=4, color=color.new(longColor_1, 50))
ceShortStopPlot_1 = plot( true and ceDir_1 == 1 ? na : ceShortStop_1, title='Bear Lvl 2-Active', style=plot.style_linebr, linewidth=4, color=color.new(shortColor_1, 50))
//plotshape(ceBuySignal ? ceLongStop : na, title='Long Stop Start', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(longColor, 0))
plotshape(ceBuySignal_1 and ceShowLabels_1 ? ceLongStop_1 : na, title='Bull Lvl 2-Label', text='5', location=location.absolute, style=shape.labelup, size=size.tiny, color=na, textcolor=color.new(longColor_1, 0))
plotshape(ceSellSignal_1 and ceShowLabels_1 ? ceShortStop_1 : na, title='Bear Lvl 2-Label', text='5', location=location.absolute, style=shape.labeldown, size=size.tiny, color=na, textcolor=color.new(shortColor_1, 0))
 
longStateFillColor1 = highlightState2 ? ceDir_1 == 1 ? longFillColor_1 : na : na
shortStateFillColor1 = highlightState2 ? ceDir_1 == -1 ? shortFillColor_1 : na : na
fill(midPricePlot, ceLongStopPlot_1, title='Bull Lvl 2 Fill', color=longStateFillColor1)
fill(midPricePlot, ceShortStopPlot_1, title='Bear Lvl 2 Fill', color=shortStateFillColor1)
 
 
 
var label ceLabel_5p0 = na  // Global label to track the last formed level
var line atrLine_5p0 = na   // Global line to track the ATR level
var float lastLevelPrice_5p0 = na  // To store the last level's price
var int lastBarIndex_5p0 = na  // Track the last bar index where the level was updated
 
 
if ceBuySignal_1 or ceSellSignal_1
    // Delete old label and line if they exist
    if not na(ceLabel_5p0)
        label.delete(ceLabel_5p0)
    if not na(atrLine_5p0)
        line.delete(atrLine_5p0)
 
    // Record the start of the level
    lastBarIndex_5p0 := bar_index
 
    // Determine new level properties based on the signal
    if ceBuySignal_1
        lastLevelPrice_5p0 := ceLongStop_1
 
        // Create the label and set it to follow the ATR plot dynamically
        ceLabel_5p0 := f_labelPrinter_(ceLabel_5p0, ceShowLabels_ATRIGHT_ORG_11, lastLevelPrice_5p0, f_ATRLabels_(in_labelPadding_, lastLevelPrice_5p0, atrMultiplierText_l), color.new(longColor_1, 0), lastBarIndex_5p0)
    
    if ceSellSignal_1
        lastLevelPrice_5p0 := ceShortStop_1
 
        // Create the label and set it to follow the ATR plot dynamically
        ceLabel_5p0 := f_labelPrinter_(ceLabel_5p0, ceShowLabels_ATRIGHT_ORG_11, lastLevelPrice_5p0, f_ATRLabels_(in_labelPadding_, lastLevelPrice_5p0, atrMultiplierText_l), color.new(shortColor_1, 0), lastBarIndex_5p0)
 
// Continuously update the label's position to follow the most recent ATR level
if not na(ceLabel_5p0)
    lastLevelPrice_5p0 := (ceDir_1 == 1) ? ceLongStop_1 : ceShortStop_1
    label.set_xy(ceLabel_5p0, bar_index, lastLevelPrice_5p0)
 
 
 
 
var string ceGroup2A = '⚡-------------------------Level 2 Extension-------------------------⚡'
 
ceATRLength2A = input(title='ATR Period', defval=14, group=ceGroup2A)
ceATRMult2A = input.float(title='ATR Multiplier', step=0.1, defval=5.0, group=ceGroup2A)
ceShowLabels_ATRIGHT_3AA = input(title='Show Extension Labels @ R', defval=true, group=ceGroup2A)
ceUseClose2A = input(title='Use Close Price', defval=true, group=ceGroup2A)
 
 
string atrMultiplierText_11 = str.tostring(ceATRMult2A, "#.#")  // This will convert 2.5, 4, or any value into a string like "2.5", "4", etc.
 
_ce2A(_atrMult2A, _atrLength2A, _useClose2A) =>
    _atr2A = _atrMult2A * ta.atr(_atrLength2A)
    _longStop2A = (_useClose2A ? ta.highest(close, _atrLength2A) : ta.highest(_atrLength2A)) - _atr2A
    _longStopPrev2A = nz(_longStop2A[1], _longStop2A)
    _longStop2A := close[1] > _longStopPrev2A ? math.max(_longStop2A, _longStopPrev2A) : _longStop2A
    _shortStop2A = (_useClose2A ? ta.lowest(close, _atrLength2A) : ta.lowest(_atrLength2A)) + _atr2A
    _shortStopPrev2A = nz(_shortStop2A[1], _shortStop2A)
    _shortStop2A := close[1] < _shortStopPrev2A ? math.min(_shortStop2A, _shortStopPrev2A) : _shortStop2A
    var int _dir2A = 1
    _dir2A := close > _shortStopPrev2A ? 1 : close < _longStopPrev2A ? -1 : _dir2A
    _buySignal2A= _dir2A == 1 and _dir2A[1] == -1
    _sellSignal2A = _dir2A == -1 and _dir2A[1] == 1
 
    [_dirMTF2A, _longStopMTF2A, _shortStopMTF2A, _buySignalMTF2A, _sellSignalMTF2A] = _getMany(resInputMA2, _dir2A, _longStop2A, _shortStop2A, _buySignal2A, _sellSignal2A)
    [_dirMTF2A, _longStopMTF2A, _shortStopMTF2A, _buySignalMTF2A, _sellSignalMTF2A]
 
var color longColor2A = color.yellow // #45f300
var color shortColor2A = color.gray // color.rgb(179, 45, 45)
 
[ceDir2A, ceLongStop2A, ceShortStop2A, ceBuySignal2A, ceSellSignal2A] = _ce2A(ceATRMult2A, ceATRLength2A, ceUseClose2A)
 
 
 
ceLongStopPlot2A = plot(true  ? ceLongStop2A : na, title='Bull Lvl 2-Ext', style=plot.style_linebr, linewidth=1, color=color.new(longColor2A, 0))
ceShortStopPlot2A = plot(true  ? ceShortStop2A : na, title='Bear Lvl 2-Ext', style=plot.style_linebr, linewidth=1, color=color.new(shortColor2A, 0))
 
// Label code added below: Placing labels for active and extension levels
 
//var label ceLabel = na  // Global label to track the last formed level
 
 
 
// Main Labels for the active levels
if ceDir2A == 1  // If ceLongStop2A is active, show "2.5" on the green line
    ceLabel_2A := f_labelPrinter_(ceLabel_2A, ceShowLabels_ATRIGHT_ORG_11, ceLongStop2A, f_ATRLabels_(in_labelPadding_, ceLongStop2A, atrMultiplierText_l), longColor2A, bar_index)
else  // If ceShortStop2A is active, show "2.5" on the pink line
    ceLabel_2A := f_labelPrinter_(ceLabel_2A, ceShowLabels_ATRIGHT_ORG_11, ceShortStop2A, f_ATRLabels_(in_labelPadding_, ceShortStop2A, atrMultiplierText_l), shortColor2A, bar_index)
 
// Extension Labels for the inactive levels
if ceDir2A == -1 and not na(ceLongStop2A)  // If ceShortStop2A is active, ceLongStop2A should show "2.5ext" on the thinner green line
    ceExtLabel_2A := f_labelPrinter_(ceExtLabel_2A, ceShowLabels_ATRIGHT_3AA, ceLongStop2A, f_ATRLabels_(in_labelPadding_ext, ceLongStop2A, atrMultiplierText_11 + "x"), longColor2A, bar_index)
else if ceDir2A == 1 and not na(ceShortStop2A)  // If ceLongStop2A is active, ceShortStop2A should show "2.5ext" on the thinner pink line
    ceExtLabel_2A := f_labelPrinter_(ceExtLabel_2A, ceShowLabels_ATRIGHT_3AA, ceShortStop2A, f_ATRLabels_(in_labelPadding_ext, ceShortStop2A, atrMultiplierText_11 + "x"), shortColor2A, bar_index)
 
 
 
var string ceGroup2 = '⚡--------------------------------Level 3--------------------------------⚡'
ceATRLength_2 = input(title='ATR Period', defval=14, group=ceGroup2)
ceATRMult_2 = input.float(title='ATR Multiplier', step=0.1, defval=7.5, group=ceGroup2)
ceShowLabels_2 = input(title='Show Labels @ L', defval=true, group=ceGroup2)
ceShowLabels_ATRIGHT_ORG_1 = input(title='Show Active Lvl Labels @ R', defval=true, group=ceGroup2)
ceUseClose_2 = input(title='Use Close Price', defval=true, group=ceGroup2)
highlightState3 = input.bool(title='Highlight Fill', defval=true, group=ceGroup2)
 
 
string atrMultiplierText_ceATRMult_2 = str.tostring(ceATRMult_2, "#.#")  // This will convert 2.5, 4, or any value into a string like "2.5", "4", etc.
 
var color longColor_2 = color.yellow //#e9f500
var color shortColor_2 = color.gray // color.rgb(225, 22, 22)
var color longFillColor_2 = color.new(color.yellow, 90)
var color shortFillColor_2 = color.new(color.gray, 90)
 
[ceDir_2, ceLongStop_2, ceShortStop_2, ceBuySignal_2, ceSellSignal_2] = _ce(ceATRMult_2, ceATRLength_2, ceUseClose_2)
 
ceLongStopPlot_2 = plot(true and ceDir_2 == 1 ? ceLongStop_2 : na, title='Bull Lvl 3-Active', style=plot.style_linebr, linewidth=4, color=color.new(longColor_2, 50))
ceShortStopPlot_2 = plot(true and ceDir_2 == 1 ? na : ceShortStop_2, title='Bear Lvl 3-Active', style=plot.style_linebr, linewidth=4, color=color.new(shortColor_2, 50))
 
plotshape(ceBuySignal_2 and ceShowLabels_2 ? ceLongStop_2 : na, title='Bull Lvl 3-Label', text='7.5', location=location.absolute, style=shape.labelup, size=size.tiny, color=na, textcolor=color.new(longColor_2, 0))
plotshape(ceSellSignal_2 and ceShowLabels_2 ? ceShortStop_2 : na, title='Bear Lvl 3-Label', text='7.5', location=location.absolute, style=shape.labeldown, size=size.tiny, color=na, textcolor=color.new(shortColor_2, 0))
 
longStateFillColor2 = highlightState3 ? ceDir_2 == 1 ? longFillColor_2 : na : na
shortStateFillColor2 = highlightState3 ? ceDir_2 == -1 ? shortFillColor_2 : na : na
fill(midPricePlot, ceLongStopPlot_2, title='Lvl 3 Bull Fill', color=longStateFillColor2)
fill(midPricePlot, ceShortStopPlot_2, title='Lvl 3 Bear Fill', color=shortStateFillColor2)
 
 
 
var label ceLabel_7p5 = na  // Global label to track the last formed level
var line atrLine_7p5 = na   // Global line to track the ATR level
var float lastLevelPrice_7p5 = na  // To store the last level's price
var int lastBarIndex_7p5 = na  // Track the last bar index where the level was updated
 
 
if ceBuySignal_2 or ceSellSignal_2
    // Delete old label and line if they exist
    if not na(ceLabel_7p5)
        label.delete(ceLabel_7p5)
    if not na(atrLine_7p5)
        line.delete(atrLine_7p5)
 
    // Record the start of the level
    lastBarIndex_7p5 := bar_index
 
    // Determine new level properties based on the signal
    if ceBuySignal_2
        lastLevelPrice_7p5 := ceLongStop_2
 
        // Create the label and set it to follow the ATR plot dynamically
        ceLabel_7p5 := f_labelPrinter_(ceLabel_7p5, ceShowLabels_ATRIGHT_ORG_1, lastLevelPrice_7p5, f_ATRLabels_(in_labelPadding_, lastLevelPrice_7p5, atrMultiplierText_ceATRMult_2), color.new(longColor_2, 0), lastBarIndex_7p5)
    
    if ceSellSignal_2
        lastLevelPrice_7p5 := ceShortStop_2
 
        // Create the label and set it to follow the ATR plot dynamically
        ceLabel_7p5 := f_labelPrinter_(ceLabel_7p5, ceShowLabels_ATRIGHT_ORG_1, lastLevelPrice_7p5, f_ATRLabels_(in_labelPadding_, lastLevelPrice_7p5, atrMultiplierText_ceATRMult_2), color.new(shortColor_2, 0), lastBarIndex_7p5)
 
// Continuously update the label's position to follow the most recent ATR level
if not na(ceLabel_7p5)
    lastLevelPrice_7p5 := (ceDir_2 == 1) ? ceLongStop_2 : ceShortStop_2
    label.set_xy(ceLabel_7p5, bar_index, lastLevelPrice_7p5)
 
 
 
 
 
 
var string ceGroup3A = '⚡-------------------------Level 3 Extension-------------------------⚡'
 
ceATRLength3A = input(title='ATR Period', defval=14, group=ceGroup3A)
ceATRMult3A = input.float(title='ATR Multiplier', step=0.1, defval=7.5, group=ceGroup3A)
ceShowLabels_ATRIGHT_3A = input(title='Show Extension Lvl Labels @ R', defval=true, group=ceGroup3A)
ceUseClose3A = input(title='Use Close Price', defval=true, group=ceGroup3A)
 
 
string atrMultiplierText_ceATRMult3A = str.tostring(ceATRMult3A, "#.#")  // This will convert 2.5, 4, or any value into a string like "2.5", "4", etc.
 
 
//var int in_labelPadding_3A = input.int(title="Label Padding 5.0 ext level", minval=0, maxval=200, defval=7, group=ceGroup3A)
 
_ce3A(_atrMult3A, _atrLength3A, _useClose3A) =>
    _atr3A = _atrMult3A * ta.atr(_atrLength3A)
    _longStop3A = (_useClose3A ? ta.highest(close, _atrLength3A) : ta.highest(_atrLength3A)) - _atr3A
    _longStopPrev3A = nz(_longStop3A[1], _longStop3A)
    _longStop3A := close[1] > _longStopPrev3A ? math.max(_longStop3A, _longStopPrev3A) : _longStop3A
    _shortStop3A = (_useClose3A ? ta.lowest(close, _atrLength3A) : ta.lowest(_atrLength3A)) + _atr3A
    _shortStopPrev3A = nz(_shortStop3A[1], _shortStop3A)
    _shortStop3A := close[1] < _shortStopPrev3A ? math.min(_shortStop3A, _shortStopPrev3A) : _shortStop3A
    var int _dir3A = 1
    _dir3A := close > _shortStopPrev3A ? 1 : close < _longStopPrev3A ? -1 : _dir3A
    _buySignal3A= _dir3A == 1 and _dir3A[1] == -1
    _sellSignal3A = _dir3A == -1 and _dir3A[1] == 1
 
    [_dirMTF3A, _longStopMTF3A, _shortStopMTF3A, _buySignalMTF3A, _sellSignalMTF3A] = _getMany(resInputMA2, _dir3A, _longStop3A, _shortStop3A, _buySignal3A, _sellSignal3A)
    [_dirMTF3A, _longStopMTF3A, _shortStopMTF3A, _buySignalMTF3A, _sellSignalMTF3A]
 
//var color longColor3A = #a7f389
//var color shortColor3A = color.rgb(249, 135, 135)
 
[ceDir3A, ceLongStop3A, ceShortStop3A, ceBuySignal3A, ceSellSignal3A] = _ce3A(ceATRMult3A, ceATRLength3A, ceUseClose3A)
 
 
 
ceLongStopPlot3A = plot(true  ? ceLongStop3A : na, title='Bull Lvl 3-Ext', style=plot.style_linebr, linewidth=1, color=color.new(longColor_2, 0))
ceShortStopPlot3A = plot(true  ? ceShortStop3A : na, title='Bear Lvl 3-Ext', style=plot.style_linebr, linewidth=1, color=color.new(shortColor_2, 0))
 
// Label code added below: Placing labels for active and extension levels
 
//var label ceLabel = na  // Global label to track the last formed level
 
 
 
// Main Labels for the active levels
if ceDir3A == 1  // If ceLongStop3A is active, show "2.5" on the green line
    ceLabel_3A := f_labelPrinter_(ceLabel_3A, ceShowLabels_ATRIGHT_ORG_1, ceLongStop3A, f_ATRLabels_(in_labelPadding_, ceLongStop3A, atrMultiplierText_ceATRMult_2), longColor_2, bar_index)
else  // If ceShortStop3A is active, show "2.5" on the pink line
    ceLabel_3A := f_labelPrinter_(ceLabel_3A, ceShowLabels_ATRIGHT_ORG_1, ceShortStop3A, f_ATRLabels_(in_labelPadding_, ceShortStop3A, atrMultiplierText_ceATRMult_2), shortColor_2, bar_index)
 
// Extension Labels for the inactive levels
if ceDir3A == -1 and not na(ceLongStop3A)  // If ceShortStop3A is active, ceLongStop3A should show "2.5ext" on the thinner green line
    ceExtLabel_3A := f_labelPrinter_(ceExtLabel_3A, ceShowLabels_ATRIGHT_3A, ceLongStop3A, f_ATRLabels_(in_labelPadding_ext, ceLongStop3A, atrMultiplierText_ceATRMult3A+"x"), longColor_2, bar_index)
else if ceDir3A == 1 and not na(ceShortStop3A)  // If ceLongStop3A is active, ceShortStop3A should show "2.5ext" on the thinner pink line
    ceExtLabel_3A := f_labelPrinter_(ceExtLabel_3A, ceShowLabels_ATRIGHT_3A, ceShortStop3A, f_ATRLabels_(in_labelPadding_ext, ceShortStop3A, atrMultiplierText_ceATRMult3A+"x"), shortColor_2, bar_index)
 
 
 
 
 
 
var string ceGroup3 = '⚡--------------------------------Level 4--------------------------------⚡'
ceATRLength_3 = input(title='ATR Period', defval=14, group=ceGroup3)
ceATRMult_3 = input.float(title='ATR Multiplier', step=0.1, defval=10.0, group=ceGroup3)
ceShowLabels_3 = input(title='Show Labels @ L', defval=true, group=ceGroup3)
ceShowLabels_ATRIGHT_ORG = input(title='Show Active Lvl Labels @ R', defval=true, group=ceGroup3)
ceUseClose_3 = input(title='Use Close Price', defval=true, group=ceGroup3)
highlightState4 = input.bool(title='Highlight Fill', defval=true, group=ceGroup3)
 
string atrMultiplierText_ceATRMult_3 = str.tostring(ceATRMult_3, "#.#")  // This will convert 2.5, 4, or any value into a string like "2.5", "4", etc.
 
var color longColor_3 = color.yellow // #ff7b00
var color shortColor_3 = color.gray // color.rgb(225, 22, 134)
var color longFillColor_3 = color.new(color.yellow, 90)
var color shortFillColor_3 = color.new(color.gray, 90)
 
[ceDir_3, ceLongStop_3, ceShortStop_3, ceBuySignal_3, ceSellSignal_3] = _ce(ceATRMult_3, ceATRLength_3, ceUseClose_3)
 
ceLongStopPlot_3 = plot( true and ceDir_3 == 1 ? ceLongStop_3 : na, title='Bull Lvl 4-Active', style=plot.style_linebr, linewidth=4, color=color.new(longColor_3, 35))
ceShortStopPlot_3 = plot( true and ceDir_3 == 1 ? na : ceShortStop_3, title='Bear Lvl 4-Active', style=plot.style_linebr, linewidth=4, color=color.new(shortColor_3, 35))
//plotshape(ceBuySignal ? ceLongStop : na, title='Long Stop Start', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(longColor, 0))
plotshape(ceBuySignal_3 and ceShowLabels_3 ? ceLongStop_3 : na, title='Bull Lvl 4-Label', text='10.0', location=location.absolute, style=shape.labelup, size=size.tiny, color=na, textcolor=color.new(longColor_3, 0))
plotshape(ceSellSignal_3 and ceShowLabels_3 ? ceShortStop_3 : na, title='Bear Lvl 4-Label', text='10.0', location=location.absolute, style=shape.labeldown, size=size.tiny, color=na, textcolor=color.new(shortColor_3, 0))
 
longStateFillColor3 = highlightState4 ? ceDir_3 == 1 ? longFillColor_3 : na : na
shortStateFillColor3 = highlightState4 ? ceDir_3 == -1 ? shortFillColor_3 : na : na
fill(midPricePlot, ceLongStopPlot_3, title='Lvl 4 Bull Fill', color=longStateFillColor3)
fill(midPricePlot, ceShortStopPlot_3, title='Lvl 4 Bear Fill', color=shortStateFillColor3)
 
 
 
var label ceLabel_10p0 = na  // Global label to track the last formed level
var line atrLine_10p0 = na   // Global line to track the ATR level
var float lastLevelPrice_10p0 = na  // To store the last level's price
var int lastBarIndex_10p0 = na  // Track the last bar index where the level was updated
 
 
if ceBuySignal_3 or ceSellSignal_3
    // Delete old label and line if they exist
    if not na(ceLabel_10p0)
        label.delete(ceLabel_10p0)
    if not na(atrLine_10p0)
        line.delete(atrLine_10p0)
 
    // Record the start of the level
    lastBarIndex_10p0 := bar_index
 
    // Determine new level properties based on the signal
    if ceBuySignal_3
        lastLevelPrice_10p0 := ceLongStop_3
 
        // Create the label and set it to follow the ATR plot dynamically
        ceLabel_10p0 := f_labelPrinter_(ceLabel_10p0, ceShowLabels_3, lastLevelPrice_10p0, f_ATRLabels_(in_labelPadding_, lastLevelPrice_10p0, atrMultiplierText_ceATRMult_3), color.new(longColor_3, 0), lastBarIndex_10p0)
    
    if ceSellSignal_3
        lastLevelPrice_10p0 := ceShortStop_3
 
        // Create the label and set it to follow the ATR plot dynamically
        ceLabel_10p0 := f_labelPrinter_(ceLabel_10p0, ceShowLabels_3, lastLevelPrice_10p0, f_ATRLabels_(in_labelPadding_, lastLevelPrice_10p0, atrMultiplierText_ceATRMult_3), color.new(shortColor_3, 0), lastBarIndex_10p0)
 
// Continuously update the label's position to follow the most recent ATR level
if not na(ceLabel_10p0)
    lastLevelPrice_10p0 := (ceDir_3 == 1) ? ceLongStop_3 : ceShortStop_3
    label.set_xy(ceLabel_10p0, bar_index, lastLevelPrice_10p0)
 
 
 
// Chandelier Exit
var string ceGroup2a = '⚡-------------------------Level 4 Extension-------------------------⚡'
ceATRLength2a = input(title='ATR Period', defval=14, group=ceGroup2a)
ceATRMult2a = input.float(title='ATR Multiplier', step=0.1, defval=10.0, group=ceGroup2a)
ceShowLabels_ATRIGHT = input(title='Show Extension Lvl Labels @ R', defval=true, group=ceGroup2a)
ceUseClose2a = input(title='Use Close Price', defval=true, group=ceGroup2a)
highlightState_10x = input.bool(title='Highlight Fill', defval=true, group=ceGroup3)
 
string atrMultiplierText_ceATRMult2a = str.tostring(ceATRMult2a, "#.#")  // This will convert 2.5, 4, or any value into a string like "2.5", "4", etc
 
_ce2a(_atrMult2a, _atrLength2a, _useClose2a) =>
    _atr2a = _atrMult2a * ta.atr(_atrLength2a)
    _longStop2a = (_useClose2a ? ta.highest(close, _atrLength2a) : ta.highest(_atrLength2a)) - _atr2a
    _longStopPrev2a = nz(_longStop2a[1], _longStop2a)
    _longStop2a := close[1] > _longStopPrev2a ? math.max(_longStop2a, _longStopPrev2a) : _longStop2a
    _shortStop2a = (_useClose2a ? ta.lowest(close, _atrLength2a) : ta.lowest(_atrLength2a)) + _atr2a
    _shortStopPrev2a = nz(_shortStop2a[1], _shortStop2a)
    _shortStop2a := close[1] < _shortStopPrev2a ? math.min(_shortStop2a, _shortStopPrev2a) : _shortStop2a
    var int _dir2a = 1
    _dir2a := close > _shortStopPrev2a ? 1 : close < _longStopPrev2a ? -1 : _dir2a
    _buySignal2a= _dir2a == 1 and _dir2a[1] == -1
    _sellSignal2a = _dir2a == -1 and _dir2a[1] == 1
 
    [_dirMTF2a, _longStopMTF2a, _shortStopMTF2a, _buySignalMTF2a, _sellSignalMTF2a] = _getMany(resInputMA2, _dir2a, _longStop2a, _shortStop2a, _buySignal2a, _sellSignal2a)
    [_dirMTF2a, _longStopMTF2a, _shortStopMTF2a, _buySignalMTF2a, _sellSignalMTF2a]
 
var color longColor_2a = color.yellow // #ff7b00
var color shortColor_2a = color.gray  // color.rgb(225, 22, 134)
var color longFillColor_2a = color.new(color.yellow, 90)
var color shortFillColor_2a = color.new(color.gray, 90)
 
[ceDir2a, ceLongStop2a, ceShortStop2a, ceBuySignal2a, ceSellSignal2a] = _ce2a(ceATRMult2a, ceATRLength2a, ceUseClose2a)
 
 
 
 
ceLongStopPlot2a = plot(true  ? ceLongStop2a : na, title='Bull Lvl 4-Ext', style=plot.style_linebr, linewidth=1, color=color.new(longColor_2a, 0))
ceShortStopPlot2a = plot(true  ? ceShortStop2a : na, title='Bear Lvl 4-Ext', style=plot.style_linebr, linewidth=1, color=color.new(shortColor_2a, 0))
 
 
//longStateFillColor5 = highlightState_10x ? ceDir2a == 1 ? longFillColor_2a : na : na
//shortStateFillColor5 = highlightState_10x ? ceDir2a == -1 ? shortFillColor_2a : na : na
//fill(midPricePlot, ceLongStopPlot2a, title='Long State Fill', color=longStateFillColor5)
//fill(midPricePlot, ceShortStopPlot2a, title='Short State Fill', color=shortStateFillColor5)
 
 
 
// Main Labels for the active levels
if ceDir2a == 1  // If ceLongStop2a is active, show "2.5" on the green line
    ceLabel_2a := f_labelPrinter_(ceLabel_2a, ceShowLabels_ATRIGHT_ORG, ceLongStop2a, f_ATRLabels_(in_labelPadding_, ceLongStop2a, atrMultiplierText_ceATRMult_3), longColor_2a, bar_index)
else  // If ceShortStop2a is active, show "2.5" on the pink line
    ceLabel_2a := f_labelPrinter_(ceLabel_2a, ceShowLabels_ATRIGHT_ORG, ceShortStop2a, f_ATRLabels_(in_labelPadding_, ceShortStop2a, atrMultiplierText_ceATRMult_3), shortColor_2a, bar_index)
 
// Extension Labels for the inactive levels
if ceDir2a == -1 and not na(ceLongStop2a)  // If ceShortStop2a is active, ceLongStop2a should show "2.5ext" on the thinner green line
    ceExtLabel_2a := f_labelPrinter_(ceExtLabel_2a, ceShowLabels_ATRIGHT, ceLongStop2a, f_ATRLabels_(in_labelPadding_ext, ceLongStop2a, atrMultiplierText_ceATRMult2a+"x"), longColor_2a, bar_index)
else if ceDir2a == 1 and not na(ceShortStop2a)  // If ceLongStop2a is active, ceShortStop2a should show "2.5ext" on the thinner pink line
    ceExtLabel_2a := f_labelPrinter_(ceExtLabel_2a, ceShowLabels_ATRIGHT, ceShortStop2a, f_ATRLabels_(in_labelPadding_ext, ceShortStop2a, atrMultiplierText_ceATRMult2a+"x"), shortColor_2a, bar_index)
 
 
 
 
 
 
 
await = awaitBarConfirmation ? barstate.isconfirmed : true
//alertcondition(dir != dir[1] and await, title='Alert: CE Direction Change', message='Chandelier Exit has changed direction!')
 
 
alertcondition(ceBuySignal and await, title='2.5 Bull Cross Up-Active Lvl 1', message='💥PRICE CROSSED UP 2.5 ACTIVE BULL LVL💥\n({{interval}}) {{close}}')
alertcondition(ceSellSignal and await, title='2.5 Bear Cross Down-Active Lvl 1', message='🔻PRICE CROSSED BELOW 2.5 ACTIVE BEAR LVL🔻\n({{interval}}) {{close}}')
 
alertcondition(ceBuySignal1a and await, title='2.5x Bull Cross Up-Ext Lvl 1', message='💥PRICE CROSSED UP 2.5x BULL LVL💥\n({{interval}}) {{close}}')
alertcondition(ceSellSignal1a and await, title='2.5x Bear Cross Down-Ext Lvl 1', message='🔻PRICE CROSSED BELOW 2.5x BEAR LVL🔻\n({{interval}}) {{close}}')
 
alertcondition(ceBuySignal_1 and await, title='5.0 Bull Cross Up-Active Lvl 2', message='💥PRICE CROSSED UP 5.0 ACTIVE BULL LVL💥\n({{interval}}) {{close}}')
alertcondition(ceSellSignal_1 and await, title='5.0 Bear Cross Down-Active Lvl 2', message='🔻PRICE CROSSED BELOW 5.0 ACTIVE BEAR LVL🔻\n({{interval}}) {{close}}')
 
alertcondition(ceBuySignal2A and await, title='5.0x Bull Cross Up-Ext Lvl 2', message='💥PRICE CROSSED UP 5.0x BULL LVL💥\n({{interval}}) {{close}}')
alertcondition(ceSellSignal2A and await, title='5.0x Bear Cross Down-Ext Lvl 2', message='🔻PRICE CROSSED BELOW 5.0x BEAR LVL🔻\n({{interval}}) {{close}}')
 
alertcondition(ceBuySignal_2 and await, title='7.5 Bull Cross Up-Active Lvl 3', message='💥PRICE CROSSED UP 7.5 ACTIVE BULL LVL💥\n({{interval}}) {{close}}')
alertcondition(ceSellSignal_2 and await, title='7.5 Bear Cross Down-Active Lvl 3', message='🔻PRICE CROSSED BELOW 7.5 ACTIVE BEAR LVL🔻\n({{interval}}) {{close}}')
 
alertcondition(ceBuySignal3A and await, title='7.5x Bull Cross Up-Ext Lvl 3', message='💥PRICE CROSSED UP 7.5x BULL LVL💥\n({{interval}}) {{close}}')
alertcondition(ceSellSignal3A and await, title='7.5x Bear Cross Down-Ext Lvl 3', message='🔻PRICE CROSSED BELOW 7.5x BEAR LVL🔻\n({{interval}}) {{close}}')
 
alertcondition(ceBuySignal_3 and await, title='10.0 Bull Cross Up-Active Lvl 4', message='💥PRICE CROSSED UP 10.0 ACTIVE BULL LVL💥\n({{interval}}) {{close}}')
alertcondition(ceSellSignal_3 and await, title='10.0 Bear Cross Down-Active Lvl 4', message='🔻PRICE CROSSED BELOW 10.0 ACTIVE BEAR LVL🔻\n({{interval}}) {{close}}')
 
alertcondition(ceBuySignal2a and await, title='10x Bull Cross Up-Ext Lvl 4', message='💥PRICE CROSSED UP 10.0x BULL LVL💥\n({{interval}}) {{close}}')
alertcondition(ceSellSignal2a and await, title='10x Bear Cross Down-Ext Lvl 4', message='🔻PRICE CROSSED BELOW 10.0x BEAR LVL🔻\n({{interval}}) {{close}}')

← 返回列表