Chandelier Exit Strategy with 200 EMA Filter

PakunFX · strategy · 51 行 · 点赞 1,199 · TradingView 原页

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

Pine Script

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © PakunFX
 
//@version=5
// Copyright (c) 2019-present, Alex Orekhov (everget)
// Chandelier Exit script may be freely distributed under the terms of the GPL-3.0 license.
strategy('Chandelier Exit Strategy with 200 EMA Filter', shorttitle='CES', overlay=true)
 
var string calcGroup = 'Calculation'
length = input.int(title='ATR Period', defval=22, group=calcGroup)
mult = input.float(title='ATR Multiplier', step=0.1, defval=3.0, group=calcGroup)
useClose = input.bool(title='Use Close Price for Extremums', defval=true, group=calcGroup)
 
var string visualGroup = 'Visuals'
showLabels = input.bool(title='Show Buy/Sell Labels', defval=true, group=visualGroup)
highlightState = input.bool(title='Highlight State', defval=true, group=visualGroup)
 
var string alertGroup = 'Alerts'
awaitBarConfirmation = input.bool(title="Await Bar Confirmation", defval=true, group=alertGroup)
 
atr = mult * ta.atr(length)
ema200 = ta.ema(close, 200)
 
longStop = (useClose ? ta.highest(close, length) : ta.highest(length)) - atr
longStopPrev = nz(longStop[1], longStop)
longStop := close[1] > longStopPrev ? math.max(longStop, longStopPrev) : longStop
 
shortStop = (useClose ? ta.lowest(close, length) : ta.lowest(length)) + 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
 
await = awaitBarConfirmation ? barstate.isconfirmed : true
 
// Trading logic
if (buySignal and await and close > ema200)
    strategy.entry("Long", strategy.long, stop = low - atr * 0.5)
 
if (sellSignal and await and close < ema200)
    strategy.entry("Short", strategy.short, stop = high + atr * 0.5)
 
if (sellSignal and await)
    strategy.close("Long")
 
if (buySignal and await)
    strategy.close("Short")

← 返回列表