Donchian Channels [Gu5]

gu5tavo71 · study · 172 行 · 点赞 1,117 · TradingView 原页

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

Pine Script

// This source code is subject to these terms:
// Attribution-NonCommercial 4.0 International (CC BY-NC 4.0)
// https://creativecommons.org/licenses/by-nc/4.0/
// You are free to Share, copy and redistribute. Adapt, transform and re-build on this script.
// Under the following terms: Non-commercial & Attribution.
// © gu5tavo71 (Gustavo Cardelle)
 
//@version=5
VERSION                  = 'v1.4.42'
indicator(
  'Donchian Channels [Gu5]',
  shorttitle             = 'DC ' + VERSION,
  overlay                = true,
  explicit_plot_zorder   = true)
 
// Donchian Channels developed by Richard Donchian
// Actual Study Version: @gu5tavo71
// 2023.05.12
// Project #687
// This script reuses open source code from another authors:
// @PineCoders, Built-in Library, and Community Scripts
// Disclaimer: I am not a financial advisor.
//             For purpose educate only. Use at your own risk.
 
 
//#region ——————————— <constant_declarations> {
//<my colors>
C_GREEN      =                   #006400    //Green
C_GREENLIGHT =                   #388e3c    //Green Light
C_RED        =                   #8B0000    //Red
C_REDLIGHT   =                   #b71c1c    //Red Light
//#endregion }
//#region ——————————— <inputs> {
i_dcLen      = input.int       (20,        'Length'                        , minval = 1)
//Display
G_DISPLAY    = 'Display'
i_showBasis  = input.bool      (true,      '■ Basis On/Off'                , group   = G_DISPLAY)
i_alertOn    = input.bool      (false,     '■ Alert On/Off'                , group   = G_DISPLAY)
i_showFill   = input.bool      (false,     '■ Fill On/Off'                 , group   = G_DISPLAY)
i_showBarCol = input.bool      (true,      '■ Bar Color On/Off'            , group   = G_DISPLAY)
i_showClose  = input.bool      (false,     '■ Close Alert On/Off'          , group   = G_DISPLAY)
//#endregion }
//#region ——————————— <calculations> {
//<set initial values>
condition    = 0.0
dc           = 0.0
dcDirCond    = 0.0
dcTopDir     = 0.0
dcBotDir     = 0.0
 
dcTop        = ta.highest(i_dcLen)
dcBot        = ta.lowest (i_dcLen)
dcBasis      = math.avg(dcTop, dcBot)
 
dc          := dc[1] !=  1 and dcTop > dcTop[1] ?  1 :
               dc[1] != -1 and dcBot < dcBot[1] ? -1 : nz(dc[1])
 
dcDirUp      = dcTop > dcTop[1]
dcDirDn      = dcBot < dcBot[1]
dcDirZero    = ta.crossunder(close, dcBasis) or ta.crossover(close, dcBasis)
dcDirCond   := dcDirCond[1] !=  1 and dcDirUp   ?  1 :
               dcDirCond[1] != -1 and dcDirDn   ? -1 :
               dcDirCond[1] !=  0 and dcDirZero ?  0 : nz(dcDirCond[1])
dcDir        = dcDirCond == 0 ? 0 :
               dc        == 1 ? 1 : -1
dcTopDir    := dcTop < dcTop[1] ?  0 :
               dcTop > dcTop[1] ?  1 : nz(dcTopDir[1])
dcBotDir    := dcBot > dcBot[1] ?  0 :
               dcBot < dcBot[1] ? -1 : nz(dcBotDir[1])
dcRange      = dcTopDir == 0 and dcBotDir == 0
//<rules>
longErule    = dcTop > dcTop[1]
shortErule   = dcBot < dcBot[1]
longXrule    = i_showClose ? ta.crossunder(close, dcBasis) : false
shortXrule   = i_showClose ? ta.crossover (close, dcBasis) : false
//<conditions>
condition   := condition[1] !=  1 and longErule  ?  1 :
               condition[1] != -1 and shortErule ? -1 :
               condition[1] !=  0 and longXrule  ?  0 :
               condition[1] !=  0 and shortXrule ?  0 : nz(condition[1])
longE        = condition[1] !=  1 and condition ==  1
shortE       = condition[1] != -1 and condition == -1
longX        = condition[1] ==  1 and longXrule
shortX       = condition[1] == -1 and shortXrule
 
// <color>
c_range      = dcRange or condition ==  0
c_dcTop      = c_range                           ? color.new(color.gray,   75) :
               condition ==  1                   ? color.new(color.green,   0) :
               condition ==  0                   ? color.new(color.orange,  0) :
               dcRange                           ? color.new(color.orange,  0) :
               color.new(color.gray,   75)
c_dcBot      = c_range                           ? color.new(color.gray,   75) :
               condition == -1                   ? color.new(color.red,     0) :
               condition ==  0                   ? color.new(color.orange,  0) :
               color.new(color.gray,   75)
c_dcBasis    = dcRange                           ? color.new(color.orange,  0) :
               dc ==  1                          ? color.new(color.green,  25) :
               dc == -1                          ? color.new(color.red,    25) : na
c_dcFill     = dcRange                           ? color.new(color.orange, 80) :
               dc ==  1                          ? color.new(color.green,  80) :
               dc == -1                          ? color.new(color.red,    80) : na
c_barCol     = c_range         and open <= close ? color.new(color.orange,  0) :
               c_range         and open >  close ? color.new(color.yellow,  0) :
               condition ==  1 and open <= close ? color.new(C_GREEN,       0) :
               condition ==  1 and open >  close ? color.new(color.green,   0) :
               condition == -1 and open >= close ? color.new(C_RED,         0) :
               condition == -1 and open <  close ? color.new(color.red,     0) :
               color.new(color.gray, 0)
//#endregion }
//#region ——————————— <visuals> {
plot(
  i_showBasis ? dcBasis : na,
  title      = 'Basis',
  linewidth  = 2,
  color      = c_dcBasis)
p_dcTop      = plot(
  dcTop,
  title      = 'Top',
  linewidth  = 1,
  color      = c_dcTop)
p_dcBot      = plot(
  dcBot,
  title      = 'Bottom',
  linewidth  = 1,
  color      = c_dcBot)
fill(
  p_dcTop, p_dcBot,
  title      = 'Background',
  color      = i_showFill ? c_dcFill : na)
//<alerts labels>
plotshape(
  i_alertOn and longE  ? close : na,
  title      = 'Long',
  text       = 'Long',
  textcolor  = color.white,
  color      = color.new(color.green, 0),
  style      = shape.labelup,
  size       = size.normal,
  location   = location.belowbar)
plotshape(
  i_alertOn and shortE ? close : na,
  title      = 'Short',
  text       = 'Short',
  textcolor  = color.white,
  color      = color.new(color.red, 0),
  style      = shape.labeldown,
  size       = size.normal,
  location   = location.abovebar)
plotshape(
  i_alertOn and (longX or shortX) ? close : na,
  title      = 'Close',
  text       = 'Close',
  textcolor  = color.new(color.yellow, 0),
  color      = color.new(color.yellow, 0),
  style      = shape.xcross,
  size       = size.small,
  location   = location.absolute)
barcolor(i_showBarCol ? c_barCol : na)
//#endregion }
//#region ——————————— <alerts> {
if longE or shortE or longX or shortX
    alert(message = 'Any Alert',   freq = alert.freq_once_per_bar_close)
if longE
    alert(message = 'Long Entry',  freq = alert.freq_once_per_bar_close)
if shortE
    alert(message = 'Short Entry', freq = alert.freq_once_per_bar_close)
if longX
    alert(message = 'Long Exit',   freq = alert.freq_once_per_bar_close)
if shortX
    alert(message = 'Short Exit',  freq = alert.freq_once_per_bar_close)
//#endregion }

← 返回列表