本页源码来自 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/
// © piriya33
// Added Labels and alert conditions and other quality of life feature
// Updated compatability with pine script v4
// Based on improvements from "Kitti-Playbook Action Zone V.4.2.0.3 for Stock Market"
// Modified: Removed alert conditions and bar color painting
//@version=5
indicator('CDC ActionZone V3 2020 + ATR Trailing Stop', overlay=true, precision=6)
//****************************************************************************//
// CDC Action Zone is based on a simple EMA crossover
// between [default] EMA12 and EMA26
// The zones are defined by the relative position of
// price in relation to the two EMA lines
// Different zones can be use to activate / deactivate
// other trading strategies
// The strategy can also be used on its own with
// acceptable result, buy on the first green candle
// and sell on the first red candle
//****************************************************************************//
//****************************************************************************//
// Define User Input Variables
xsrc = input.source(title='Source Data', defval=close)
xprd1 = input.int(title='Fast EMA period', defval=12)
xprd2 = input.int(title='Slow EMA period', defval=26)
xsmooth = input.int(title='Smoothing period (1 = no smoothing)', defval=1)
fastSW = input.bool(title='Show fast moving average line', defval=true)
slowSW = input.bool(title='Show slow moving average line', defval=true)
plotRibsw = input.bool(title='Plot Buy/Sell Ribbon', defval=true)
plotRibbonPos = input.string(title='Ribbon Position', options=['Top', 'Bottom'], defval='Top')
xfixtf = input.bool(title='** Use Fixed time frame Mode (advanced) **', defval=false)
xtf = input.timeframe(title='** Fix chart to which time frame ? **)', defval='D')
//****************************************************************************//
// ATR Trailing Stop Inputs
atr_show = input.bool(title='Show ATR Trailing Stop', defval=true)
AP2 = input.int(title='Slow ATR Period', defval=10)
AF2 = input.float(title='Slow ATR Multiplier', defval=2.0, step=0.1)
//****************************************************************************//
// Calculate Indicators
f_secureSecurity(_symbol, _res, _src) =>
request.security(_symbol, _res, _src[1], lookahead=barmerge.lookahead_on)
xPrice = ta.ema(xsrc, xsmooth)
FastMA = xfixtf ?
ta.ema(f_secureSecurity(syminfo.tickerid, xtf, ta.ema(xsrc, xprd1)), xsmooth)
: ta.ema(xPrice, xprd1)
SlowMA = xfixtf ?
ta.ema(f_secureSecurity(syminfo.tickerid, xtf, ta.ema(xsrc, xprd2)), xsmooth)
: ta.ema(xPrice, xprd2)
Bull = FastMA > SlowMA
Bear = FastMA < SlowMA
//****************************************************************************//
// Define Color Zones (ยังคงไว้เพื่อใช้ในการคำนวณ signal)
Green = Bull and xPrice > FastMA
Blue = Bear and xPrice > FastMA and xPrice > SlowMA
LBlue = Bear and xPrice > FastMA and xPrice < SlowMA
Red = Bear and xPrice < FastMA
Orange = Bull and xPrice < FastMA and xPrice < SlowMA
Yellow = Bull and xPrice < FastMA and xPrice > SlowMA
//****************************************************************************//
// Display MA lines
FastL = plot(fastSW ? FastMA : na, 'Fast EMA', color=color.new(color.red, 0), style=xfixtf ? plot.style_stepline : plot.style_line)
SlowL = plot(slowSW ? SlowMA : na, 'Slow EMA', color=color.new(color.blue, 0), style=xfixtf ? plot.style_stepline : plot.style_line)
// Fill between EMA lines - green (bull) / red (bear)
fillcolor = Bull ? color.new(color.blue, 90) : color.new(color.orange, 90)
fill(FastL, SlowL, fillcolor)
//****************************************************************************//
// Define Buy and Sell condition
buycond = Green and Green[1] == 0
sellcond = Red and Red[1] == 0
bullish = ta.barssince(buycond) < ta.barssince(sellcond)
bearish = ta.barssince(sellcond) < ta.barssince(buycond)
buy = bearish[1] and buycond
sell = bullish[1] and sellcond
bColor_BullBear = bullish ? color.green : bearish ? color.red : color.gray
//****************************************************************************//
// Plot Buy and Sell point on chart
// Display Buy/Sell Ribbon
ribbonLoc = plotRibbonPos == 'Top' ? location.top : location.bottom
plotshape(plotRibsw ? close : na,
style=shape.square,
title='Buy/Sell Ribbon',
location=ribbonLoc,
color=bColor_BullBear)
//****************************************************************************//
// BUY / SELL Labels (large badge style)
if buy
label.new(bar_index, low,
text='Buy',
color=color.new(color.teal, 0),
textcolor=color.white,
style=label.style_label_up,
yloc=yloc.belowbar,
size=size.normal)
if sell
label.new(bar_index, high,
text='Sell',
color=color.new(color.orange, 0),
textcolor=color.white,
style=label.style_label_down,
yloc=yloc.abovebar,
size=size.normal)
//****************************************************************************
//****************************************************************************//
// ATR Trailing Stop Calculation (converted from v2 to v5)
SC = close
SL2 = AF2 * ta.atr(AP2)
Trail2 = 0.0
Trail2 := SC > nz(Trail2[1], 0) and SC[1] > nz(Trail2[1], 0) ? math.max(nz(Trail2[1], 0), SC - SL2) :
SC < nz(Trail2[1], 0) and SC[1] < nz(Trail2[1], 0) ? math.min(nz(Trail2[1], 0), SC + SL2) :
SC > nz(Trail2[1], 0) ? SC - SL2 : SC + SL2
// Plot ATR Trailing Stop (Slow only)
atrSlowColor = SC > Trail2 ? color.new(color.green, 0) : color.new(color.red, 0)
plot(atr_show ? Trail2 : na, 'Slow Trail (ATR)',
color=atrSlowColor, style=plot.style_line, linewidth=2)
//****************************************************************************//