本页源码来自 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
// MA - Moving Average
// MATR - Moving Average less Average True Range
// ChEx - Chandelier Exit
//@version=4
study("Moving Average | Moving Average less Average True Range | Chandelier Exit", shorttitle = "MA | MATR | ChEx", overlay = true)
maType = input(title = "MA | Type", type = input.string, defval = "EMA", options = ["EMA", "HMA", "RMA", "SMA", "WMA"])
Len1 = input(title = "MA | Period 1", type = input.integer, defval = 20)
Len2 = input(title = "MA | Period 2", type = input.integer, defval = 50)
Len3 = input(title = "MA | Period 3", type = input.integer, defval = 150)
Len4 = input(title = "MA | Period 4", type = input.integer, defval = 200)
slType = input(title = "MATR | Position", type = input.string, defval = "Long", options = ["Long", "Short"])
LenS = input(title = "MATR | Period", type = input.integer, defval = 20)
MultS = input(title = "MATR | ATR Multiplier", type = input.float, defval = 1.0)
LenC = input(title = "ChEx | Period", type = input.integer, defval = 20)
MultC = input(title = "ChEx | ATR Multiplier", type = input.float, defval = 4.0)
maX(T, N) =>
maX = 0.0
if T == "EMA"
maX := ema(close, N)
if T == "HMA"
maX := hma(close, N)
if T == "RMA"
maX := rma(close, N)
if T == "SMA"
maX := sma(close, N)
if T == "WMA"
maX := wma(close, N)
maX
// Moving Average
ma1 = maX(maType, Len1)
ma2 = maX(maType, Len2)
ma3 = maX(maType, Len3)
ma4 = maX(maType, Len4)
// Moving Average less Average True Ramge - Initial Stoploss
at = atr(LenS)
maS1 = maX(maType, LenS)
maSp = slType == "Long" ? maS1 - MultS * at : slType == "Short" ? maS1 + MultS * at : na
//Chandelier Exit - Trailing Stoploss
atC = atr(LenC)
sEx = lowest(low, LenC) + MultC * atC
lEx = highest(high, LenC) - MultC * atC
sSAR = 0.0
lSAR = 0.0
sSAR := na(sSAR[1]) ? sEx : iff(hl2 > sSAR[1], sEx, min(sEx, sSAR[1]))
lSAR := na(lSAR[1]) ? lEx : iff(hl2 < lSAR[1], lEx, max(lEx, lSAR[1]))
lSwitch = iff(hl2 >= sSAR[1] and hl2[1] < sSAR[1], 1, 0)
sSwitch = iff(hl2 <= lSAR[1] and hl2[1] > lSAR[1], 1, 0)
direction = 0
direction := iff(na(direction[1]), 0,
iff (direction[1] <= 0 and lSwitch, 1,
iff (direction[1] >= 0 and sSwitch, -1, direction[1])))
ChEx = direction > 0 ? lSAR : sSAR
// Charting
plot(ma1, title = "MA 1", color = #388e3c, transp = 0, linewidth = 1)
plot(ma2, title = "MA 2", color = #b71c1c, transp = 0, linewidth = 1)
plot(ma3, title = "MA 3", color = #f57c00, transp = 0, linewidth = 1, display = display.none)
plot(ma4, title = "MA 4", color = #0d47a1, transp = 0, linewidth = 1)
s1 = plot(maS1, title = "MATR - MA", color = #ffffff, transp = 100, linewidth = 1, display = display.none)
sp = plot(maSp, title = "MATR - Initial Stoploss", color = color.red, transp = 85, linewidth = 1)
plot(ChEx, title = "Chandelier Exit", color = direction > 0 ? color.aqua : color.fuchsia, linewidth = 2 )
fill(s1, sp, color = color.red, transp = 80)