本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © earnSmartAlgorithm
//@version=4
// Copyright @ 2020 earnSmartAlgorithmg. All rights reserved.
// Chandelier Exit can be formulated as a stop-and-reverse (SAR) or in as a traditional trailing stop-loss
// version shown by Stockcharts.com.
// The main different is that, in SAR, the indicator is usually formulated in a higher of previous or spot
// indicator (HPS) for long position and lower of previous and spot indicator (LPS).
// This below indicator is coded to show both the SAR version and the traditional one shown by Stockcharts.com
// (for long position).
study(title = "Chandelier Exit | SAR/Long Only (4CUP)", shorttitle = "Chandelier Exit", overlay = true)
// Getting inputs
Len = input(title = "Look back period", type = input.integer, defval = 20, minval = 1)
ATRLen = input(title = "ATR period", type = input.integer, defval = 20, minval = 1)
K = input(title = "Chandelier Exit Multiplier", type = input.float, defval = 4.0)
SChVersion = input(title = "StockCharts Version (Long)", type = input.bool, defval = false)
MP = hl2
// Chandelier Channel
ChanStopPlus = lowest(low, Len) + K * atr(ATRLen)
ChanStopMinus = highest(high, Len) - K * atr(ATRLen)
// SAR
ChanStop_SAR = 0.0
indChEx = MP > ChanStop_SAR[1] ? 1 : -1
maxChExSAR = max(nz(ChanStop_SAR[1]), ChanStopMinus)
minChExSAR = min(nz(ChanStop_SAR[1]), ChanStopPlus)
ChanStop_SAR := indChEx == 1 and indChEx[1] == 1 ?
maxChExSAR : indChEx == -1 and indChEx[1] == -1 ?
minChExSAR : indChEx == 1 ? ChanStopMinus : ChanStopPlus
// Chandelier Exit
ChEx = SChVersion ? ChanStopMinus : ChanStop_SAR
// Plot Colors
colGreen = #52ff4a
colRed = #f43636
// Chandelier Exit | ChEx
plot(ChEx, "Chandelier Exit", color = indChEx == 1 ? colGreen: colRed, transp = 15, style = plot.style_cross, linewidth = 1)