Distance From EMA (HH/LL)

unmeshmulik · study · 102 行 · 点赞 5 · TradingView 原页

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

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Derived from "Distance From Moving Average" by Botnet101 (© Botnet101).
// Modified: Highest High / Lowest Low are now measured over a user-selected number of bars
// and drawn as horizontal lines across that window.
 
//@version=6
indicator("Distance From Moving Average (HH/LL over N bars)", shorttitle="Dist MA HH/LL", overlay=false, precision=2, max_bars_back=5000, max_labels_count=500, max_lines_count=50)
 
// ───────────────────────────────── INPUTS ─────────────────────────────────────
const string GM = "Moving Average"
const string GH = "Highest High / Lowest Low (selected period)"
const string GP = "Historic Highs / Lows (pivots)"
const string GL = "Current Value Labels"
 
src      = input.source(close, "Source", group=GM)
length   = input.int(20, "Length", minval=1, group=GM)
calcType = input.string("Percentage", "Calculation Type", options=["Spread", "Percentage"], group=GM)
maType   = input.string("EMA", "MA Type", options=["EMA", "SMA", "WMA", "VWMA", "HMA"], group=GM)
 
hhllBars = input.int(1000, "Lookback period (bars; days on a daily chart)", minval=2, maxval=5000, group=GH, tooltip="HH and LL of the distance are measured over the last N bars. If the chart has fewer bars, all available bars are used.")
showHH   = input.bool(true, "Show Highest High line", group=GH)
showLL   = input.bool(true, "Show Lowest Low line", group=GH)
extendR  = input.bool(true, "Extend lines to the right", group=GH)
hhColor  = input.color(color.red, "HH colour", group=GH)
llColor  = input.color(color.green, "LL colour", group=GH)
 
showPivLow  = input.bool(true, "Lows", group=GP, inline="piv")
showPivHigh = input.bool(true, "Highs", group=GP, inline="piv")
pivLen      = input.int(50, "Lookback Length", minval=1, step=1, group=GP)
pivTextCol  = input.color(color.white, "Text Colour", group=GP)
 
showLabels = input.bool(true, "Show current value labels", group=GL)
labelSizeIn = input.string("Normal", "Label Size", options=["Tiny", "Small", "Normal", "Large", "Huge"], group=GL)
labelOffset = input.int(5, "Label Position (bars to the right)", step=5, minval=0, group=GL)
 
// ─────────────────────────────── CALCULATION ──────────────────────────────────
float maEma  = ta.ema(src, length)
float maSma  = ta.sma(src, length)
float maWma  = ta.wma(src, length)
float maVwma = ta.vwma(src, length)
float maHma  = ta.hma(src, length)
float ma = maType == "EMA" ? maEma : maType == "SMA" ? maSma : maType == "WMA" ? maWma : maType == "VWMA" ? maVwma : maHma
 
float dist = calcType == "Spread" ? src - ma : 100 * (src - ma) / ma
 
f_fmt(float v) => str.tostring(v, "0.00") + (calcType == "Percentage" ? "%" : "")
 
lblSize = switch labelSizeIn
    "Tiny"  => size.tiny
    "Small" => size.small
    "Large" => size.large
    "Huge"  => size.huge
    => size.normal
 
// ───────────── HIGHEST HIGH / LOWEST LOW OVER THE SELECTED NUMBER OF BARS ──────
int nWin = math.min(hhllBars, bar_index + 1)
float hhV = na
float llV = na
if barstate.islast
    for i = 0 to nWin - 1
        float v = dist[i]
        if not na(v)
            hhV := na(hhV) ? v : math.max(hhV, v)
            llV := na(llV) ? v : math.min(llV, v)
 
var line  hhLine = na
var line  llLine = na
var label hhLbl  = na
var label llLbl  = na
var label dstLbl = na
 
if barstate.islast
    line.delete(hhLine)
    line.delete(llLine)
    label.delete(hhLbl)
    label.delete(llLbl)
    label.delete(dstLbl)
    int x1 = bar_index - nWin + 1
    if showHH and not na(hhV)
        hhLine := line.new(x1, hhV, bar_index, hhV, color=hhColor, width=2, extend=extendR ? extend.right : extend.none)
    if showLL and not na(llV)
        llLine := line.new(x1, llV, bar_index, llV, color=llColor, width=2, extend=extendR ? extend.right : extend.none)
    if showLabels
        if showHH and not na(hhV)
            hhLbl := label.new(bar_index + labelOffset, hhV, "HH " + str.tostring(nWin) + " bars (" + f_fmt(hhV) + ")", style=label.style_label_left, color=hhColor, textcolor=color.white, size=lblSize)
        if showLL and not na(llV)
            llLbl := label.new(bar_index + labelOffset, llV, "LL " + str.tostring(nWin) + " bars (" + f_fmt(llV) + ")", style=label.style_label_left, color=llColor, textcolor=color.white, size=lblSize)
        if not na(dist)
            dstLbl := label.new(bar_index + labelOffset, dist, "Dist (" + f_fmt(dist) + ")", style=label.style_label_left, color=color.blue, textcolor=color.white, size=lblSize)
 
// ───────────────────────────── HISTORIC PIVOTS ────────────────────────────────
float pl = ta.pivotlow(dist, pivLen, pivLen)
float ph = ta.pivothigh(dist, pivLen, pivLen)
 
if showPivLow and not na(pl)
    label.new(bar_index - pivLen, pl, f_fmt(pl), style=label.style_label_up, color=color.new(color.black, 100), textcolor=pivTextCol)
 
if showPivHigh and not na(ph)
    label.new(bar_index - pivLen, ph, f_fmt(ph), style=label.style_label_down, color=color.new(color.black, 100), textcolor=pivTextCol)
 
// ─────────────────────────────────── PLOT ─────────────────────────────────────
plot(dist, title="Distance", color=color.aqua, style=plot.style_columns)

← 返回列表