Trend Projection Lasers by MoneyK

Money-K · study · 949 行 · 点赞 13 · 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/
 
// © Money-K
 
//@version=6
 
indicator("Trend Projection Lasers by MoneyK", overlay=true, max_lines_count=10, max_labels_count=10)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
// TREND PROJECTION LASERS v1.1.4 · DEFAULT PALETTE
 
// Visual context only — no entries, trade plans or alerts.
 
//
 
// TRIPLE-TF SET
 
// 1M  → 1M solid + 5M dashed
 
// 5M  → 1M dotted + 5M solid + 15M dashed
 
// 15M → 5M dotted + 15M solid + 1H dashed
 
// 30M → 15M dotted + 30M solid + 1H dashed
 
// 1H  → 15M dotted + 1H solid + 4H dashed
 
//
 
// Each laser keeps its own regression slope.
 
// All lasers use the same chart-space display window.
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
// USER CONTROLS
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
showLowerProjectionInput = input.bool(
 
     true,
 
     "Show Lower TF Laser / Mostrar láser TF menor",
 
     tooltip="Shows the lower-timeframe companion laser when available. It is drawn with a dotted line. / Muestra el láser del marco temporal inferior cuando está disponible. Se dibuja con línea punteada.")
 
showCurrentProjectionInput = input.bool(
 
     true,
 
     "Show Current TF Laser / Mostrar láser TF actual",
 
     tooltip="Shows the projection laser for the current chart timeframe. It is the only solid line. / Muestra el láser de proyección del marco temporal actual. Es la única línea sólida.")
 
showHigherProjectionInput = input.bool(
 
     true,
 
     "Show Higher TF Laser / Mostrar láser TF mayor",
 
     tooltip="Shows the higher-timeframe companion laser. It is drawn with a dashed line. / Muestra el láser del marco temporal superior. Se dibuja con línea entrecortada.")
 
lowerLaserWidthInput = input.int(
 
     1,
 
     "Lower TF Laser Width / Grosor láser TF menor",
 
     minval=1,
 
     maxval=5)
 
currentLaserWidthInput = input.int(
 
     2,
 
     "Current TF Laser Width / Grosor láser TF actual",
 
     minval=1,
 
     maxval=5)
 
higherLaserWidthInput = input.int(
 
     1,
 
     "Higher TF Laser Width / Grosor láser TF mayor",
 
     minval=1,
 
     maxval=5)
 
bullLaserColorInput = input.color(
 
     color.rgb(56, 142, 60),
 
     "Bullish Laser Color / Color láser alcista",
 
     tooltip="Color used when the projection slope is bullish. / Color usado cuando la pendiente de proyección es alcista.")
 
bearLaserColorInput = input.color(
 
     color.rgb(182, 7, 7),
 
     "Bearish Laser Color / Color láser bajista",
 
     tooltip="Color used when the projection slope is bearish. / Color usado cuando la pendiente de proyección es bajista.")
 
flatLaserColorInput = input.color(
 
     color.new(color.white, 55),
 
     "Flat Laser Color / Color láser neutral",
 
     tooltip="Color used when the projection is flat or neutral. / Color usado cuando la proyección está plana o neutral.")
 
// Fixed calibration.
 
projectionLen = 34
 
projectionBars = 28
 
flatAtrMult = 0.02
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
// VISUAL PALETTE
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
projectionBullColor = bullLaserColorInput
 
projectionBearColor = bearLaserColorInput
 
projectionFlatColor = flatLaserColorInput
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
// HELPERS
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
f_chartTfLabel() =>
 
    string result = timeframe.period
 
    if timeframe.isseconds
 
        result := str.tostring(timeframe.multiplier) + "S"
 
    else if timeframe.isminutes
 
        result := timeframe.multiplier >= 60 and timeframe.multiplier % 60 == 0
 
             ? str.tostring(int(timeframe.multiplier / 60)) + "H"
 
             : str.tostring(timeframe.multiplier) + "M"
 
    else if timeframe.isdaily
 
        result := str.tostring(timeframe.multiplier) + "D"
 
    else if timeframe.isweekly
 
        result := str.tostring(timeframe.multiplier) + "W"
 
    else if timeframe.ismonthly
 
        result := str.tostring(timeframe.multiplier) + "MO"
 
    result
 
f_addTransparency(color baseColor, int extraTransparency) =>
 
    float baseTransparency = color.t(baseColor)
 
    float finalTransparency = math.min(
 
         100.0,
 
         baseTransparency +
 
         (100.0 - baseTransparency) *
 
         (extraTransparency / 100.0))
 
    color.new(baseColor, finalTransparency)
 
f_projectionColor(float slope, float sourceAtr, int transparency) =>
 
    bool isFlat = not na(sourceAtr) and math.abs(slope) <= sourceAtr * flatAtrMult
 
    color baseColor = isFlat
 
         ? projectionFlatColor
 
         : slope > 0
 
         ? projectionBullColor
 
         : projectionBearColor
 
    f_addTransparency(baseColor, transparency)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
// CURRENT TF DATA
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
currentNow = ta.linreg(close, projectionLen, 0)
 
currentPrev = ta.linreg(close, projectionLen, 1)
 
currentStart = ta.linreg(close, projectionLen, projectionLen - 1)
 
currentAtr = ta.atr(14)
 
currentSlope = currentNow - currentPrev
 
currentFuture =
 
     currentNow +
 
     currentSlope * projectionBars
 
currentColor =
 
     f_projectionColor(
 
         currentSlope,
 
         currentAtr,
 
         0)
 
currentTfText = f_chartTfLabel()
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
// FIXED MTF DATA
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
[projection1Now, projection1Prev, projection1Atr] = request.security(
 
     syminfo.tickerid,
 
     "1",
 
     [
 
         ta.linreg(close, projectionLen, 0),
 
         ta.linreg(close, projectionLen, 1),
 
         ta.atr(14)
 
     ],
 
     barmerge.gaps_off,
 
     barmerge.lookahead_off)
 
[projection5Now, projection5Prev, projection5Atr] = request.security(
 
     syminfo.tickerid,
 
     "5",
 
     [
 
         ta.linreg(close, projectionLen, 0),
 
         ta.linreg(close, projectionLen, 1),
 
         ta.atr(14)
 
     ],
 
     barmerge.gaps_off,
 
     barmerge.lookahead_off)
 
[projection15Now, projection15Prev, projection15Atr] = request.security(
 
     syminfo.tickerid,
 
     "15",
 
     [
 
         ta.linreg(close, projectionLen, 0),
 
         ta.linreg(close, projectionLen, 1),
 
         ta.atr(14)
 
     ],
 
     barmerge.gaps_off,
 
     barmerge.lookahead_off)
 
[projection1hNow, projection1hPrev, projection1hAtr] = request.security(
 
     syminfo.tickerid,
 
     "60",
 
     [
 
         ta.linreg(close, projectionLen, 0),
 
         ta.linreg(close, projectionLen, 1),
 
         ta.atr(14)
 
     ],
 
     barmerge.gaps_off,
 
     barmerge.lookahead_off)
 
[projection4hNow, projection4hPrev, projection4hAtr] = request.security(
 
     syminfo.tickerid,
 
     "240",
 
     [
 
         ta.linreg(close, projectionLen, 0),
 
         ta.linreg(close, projectionLen, 1),
 
         ta.atr(14)
 
     ],
 
     barmerge.gaps_off,
 
     barmerge.lookahead_off)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
// TRIPLE-TF MAPPING
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
float lowerNow = na
 
float lowerPrev = na
 
float lowerAtr = na
 
float lowerSourceSeconds = na
 
string lowerTfText = ""
 
float higherNow = na
 
float higherPrev = na
 
float higherAtr = na
 
float higherSourceSeconds = na
 
string higherTfText = ""
 
if timeframe.isminutes
 
    if timeframe.multiplier == 1
 
        higherNow := projection5Now
 
        higherPrev := projection5Prev
 
        higherAtr := projection5Atr
 
        higherSourceSeconds := 300.0
 
        higherTfText := "5M"
 
    else if timeframe.multiplier == 5
 
        lowerNow := projection1Now
 
        lowerPrev := projection1Prev
 
        lowerAtr := projection1Atr
 
        lowerSourceSeconds := 60.0
 
        lowerTfText := "1M"
 
        higherNow := projection15Now
 
        higherPrev := projection15Prev
 
        higherAtr := projection15Atr
 
        higherSourceSeconds := 900.0
 
        higherTfText := "15M"
 
    else if timeframe.multiplier == 15
 
        lowerNow := projection5Now
 
        lowerPrev := projection5Prev
 
        lowerAtr := projection5Atr
 
        lowerSourceSeconds := 300.0
 
        lowerTfText := "5M"
 
        higherNow := projection1hNow
 
        higherPrev := projection1hPrev
 
        higherAtr := projection1hAtr
 
        higherSourceSeconds := 3600.0
 
        higherTfText := "1H"
 
    else if timeframe.multiplier == 30
 
        lowerNow := projection15Now
 
        lowerPrev := projection15Prev
 
        lowerAtr := projection15Atr
 
        lowerSourceSeconds := 900.0
 
        lowerTfText := "15M"
 
        higherNow := projection1hNow
 
        higherPrev := projection1hPrev
 
        higherAtr := projection1hAtr
 
        higherSourceSeconds := 3600.0
 
        higherTfText := "1H"
 
    else if timeframe.multiplier == 60
 
        lowerNow := projection15Now
 
        lowerPrev := projection15Prev
 
        lowerAtr := projection15Atr
 
        lowerSourceSeconds := 900.0
 
        lowerTfText := "15M"
 
        higherNow := projection4hNow
 
        higherPrev := projection4hPrev
 
        higherAtr := projection4hAtr
 
        higherSourceSeconds := 14400.0
 
        higherTfText := "4H"
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
// MTF VISUAL GEOMETRY
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
float chartSeconds = timeframe.in_seconds()
 
lowerSlope =
 
     lowerNow -
 
     lowerPrev
 
lowerSlopePerChartBar =
 
     not na(lowerSourceSeconds) and
 
     not na(chartSeconds)
 
     ? lowerSlope *
 
       (chartSeconds / lowerSourceSeconds)
 
     : na
 
lowerVisualStart =
 
     not na(lowerSlopePerChartBar)
 
     ? lowerNow -
 
       lowerSlopePerChartBar *
 
       (projectionLen - 1)
 
     : na
 
lowerFuture =
 
     not na(lowerSlopePerChartBar)
 
     ? lowerNow +
 
       lowerSlopePerChartBar *
 
       projectionBars
 
     : na
 
lowerColor =
 
     f_projectionColor(
 
         lowerSlope,
 
         lowerAtr,
 
         20)
 
higherSlope =
 
     higherNow -
 
     higherPrev
 
higherSlopePerChartBar =
 
     not na(higherSourceSeconds) and
 
     not na(chartSeconds)
 
     ? higherSlope *
 
       (chartSeconds / higherSourceSeconds)
 
     : na
 
higherVisualStart =
 
     not na(higherSlopePerChartBar)
 
     ? higherNow -
 
       higherSlopePerChartBar *
 
       (projectionLen - 1)
 
     : na
 
higherFuture =
 
     not na(higherSlopePerChartBar)
 
     ? higherNow +
 
       higherSlopePerChartBar *
 
       projectionBars
 
     : na
 
higherColor =
 
     f_projectionColor(
 
         higherSlope,
 
         higherAtr,
 
         20)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
// DRAWING ENGINE
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
var line lowerProjectionLine = na
 
var line currentProjectionLine = na
 
var line higherProjectionLine = na
 
var label lowerProjectionTfLabel = na
 
var label currentProjectionTfLabel = na
 
var label higherProjectionTfLabel = na
 
if barstate.islast
 
    int x1 = bar_index - projectionLen + 1
 
    int x2 = bar_index + projectionBars
 
    //──────────────────────────────────
 
    // LOWER TF LASER · DOTTED
 
    //──────────────────────────────────
 
    if showLowerProjectionInput and
 
       not na(lowerVisualStart) and
 
       not na(lowerFuture) and
 
       lowerTfText != ""
 
        if na(lowerProjectionLine)
 
            lowerProjectionLine := line.new(
 
                 x1,
 
                 lowerVisualStart,
 
                 x2,
 
                 lowerFuture,
 
                 xloc=xloc.bar_index,
 
                 extend=extend.none,
 
                 color=lowerColor,
 
                 style=line.style_dotted,
 
                 width=lowerLaserWidthInput)
 
        else
 
            line.set_x1(lowerProjectionLine, x1)
 
            line.set_y1(lowerProjectionLine, lowerVisualStart)
 
            line.set_x2(lowerProjectionLine, x2)
 
            line.set_y2(lowerProjectionLine, lowerFuture)
 
            line.set_color(lowerProjectionLine, lowerColor)
 
            line.set_width(lowerProjectionLine, lowerLaserWidthInput)
 
            line.set_style(lowerProjectionLine, line.style_dotted)
 
        if na(lowerProjectionTfLabel)
 
            lowerProjectionTfLabel := label.new(
 
                 x2 + 1,
 
                 lowerFuture,
 
                 lowerTfText,
 
                 xloc=xloc.bar_index,
 
                 yloc=yloc.price,
 
                 style=label.style_none,
 
                 textcolor=lowerColor,
 
                 size=size.tiny)
 
        else
 
            label.set_x(lowerProjectionTfLabel, x2 + 1)
 
            label.set_y(lowerProjectionTfLabel, lowerFuture)
 
            label.set_text(lowerProjectionTfLabel, lowerTfText)
 
            label.set_textcolor(lowerProjectionTfLabel, lowerColor)
 
            label.set_size(lowerProjectionTfLabel, size.tiny)
 
            label.set_style(lowerProjectionTfLabel, label.style_none)
 
    else
 
        if not na(lowerProjectionLine)
 
            line.delete(lowerProjectionLine)
 
            lowerProjectionLine := na
 
        if not na(lowerProjectionTfLabel)
 
            label.delete(lowerProjectionTfLabel)
 
            lowerProjectionTfLabel := na
 
    //──────────────────────────────────
 
    // CURRENT TF LASER · SOLID
 
    //──────────────────────────────────
 
    if showCurrentProjectionInput and
 
       not na(currentStart) and
 
       not na(currentFuture)
 
        if na(currentProjectionLine)
 
            currentProjectionLine := line.new(
 
                 x1,
 
                 currentStart,
 
                 x2,
 
                 currentFuture,
 
                 xloc=xloc.bar_index,
 
                 extend=extend.none,
 
                 color=currentColor,
 
                 style=line.style_solid,
 
                 width=currentLaserWidthInput)
 
        else
 
            line.set_x1(currentProjectionLine, x1)
 
            line.set_y1(currentProjectionLine, currentStart)
 
            line.set_x2(currentProjectionLine, x2)
 
            line.set_y2(currentProjectionLine, currentFuture)
 
            line.set_color(currentProjectionLine, currentColor)
 
            line.set_width(currentProjectionLine, currentLaserWidthInput)
 
            line.set_style(currentProjectionLine, line.style_solid)
 
        if na(currentProjectionTfLabel)
 
            currentProjectionTfLabel := label.new(
 
                 x2 + 1,
 
                 currentFuture,
 
                 currentTfText,
 
                 xloc=xloc.bar_index,
 
                 yloc=yloc.price,
 
                 style=label.style_none,
 
                 textcolor=currentColor,
 
                 size=size.tiny)
 
        else
 
            label.set_x(currentProjectionTfLabel, x2 + 1)
 
            label.set_y(currentProjectionTfLabel, currentFuture)
 
            label.set_text(currentProjectionTfLabel, currentTfText)
 
            label.set_textcolor(currentProjectionTfLabel, currentColor)
 
            label.set_size(currentProjectionTfLabel, size.tiny)
 
            label.set_style(currentProjectionTfLabel, label.style_none)
 
    else
 
        if not na(currentProjectionLine)
 
            line.delete(currentProjectionLine)
 
            currentProjectionLine := na
 
        if not na(currentProjectionTfLabel)
 
            label.delete(currentProjectionTfLabel)
 
            currentProjectionTfLabel := na
 
    //──────────────────────────────────
 
    // HIGHER TF LASER · DASHED
 
    //──────────────────────────────────
 
    if showHigherProjectionInput and
 
       not na(higherVisualStart) and
 
       not na(higherFuture) and
 
       higherTfText != ""
 
        if na(higherProjectionLine)
 
            higherProjectionLine := line.new(
 
                 x1,
 
                 higherVisualStart,
 
                 x2,
 
                 higherFuture,
 
                 xloc=xloc.bar_index,
 
                 extend=extend.none,
 
                 color=higherColor,
 
                 style=line.style_dashed,
 
                 width=higherLaserWidthInput)
 
        else
 
            line.set_x1(higherProjectionLine, x1)
 
            line.set_y1(higherProjectionLine, higherVisualStart)
 
            line.set_x2(higherProjectionLine, x2)
 
            line.set_y2(higherProjectionLine, higherFuture)
 
            line.set_color(higherProjectionLine, higherColor)
 
            line.set_width(higherProjectionLine, higherLaserWidthInput)
 
            line.set_style(higherProjectionLine, line.style_dashed)
 
        if na(higherProjectionTfLabel)
 
            higherProjectionTfLabel := label.new(
 
                 x2 + 1,
 
                 higherFuture,
 
                 higherTfText,
 
                 xloc=xloc.bar_index,
 
                 yloc=yloc.price,
 
                 style=label.style_none,
 
                 textcolor=higherColor,
 
                 size=size.tiny)
 
        else
 
            label.set_x(higherProjectionTfLabel, x2 + 1)
 
            label.set_y(higherProjectionTfLabel, higherFuture)
 
            label.set_text(higherProjectionTfLabel, higherTfText)
 
            label.set_textcolor(higherProjectionTfLabel, higherColor)
 
            label.set_size(higherProjectionTfLabel, size.tiny)
 
            label.set_style(higherProjectionTfLabel, label.style_none)
 
    else
 
        if not na(higherProjectionLine)
 
            line.delete(higherProjectionLine)
 
            higherProjectionLine := na
 
        if not na(higherProjectionTfLabel)
 
            label.delete(higherProjectionTfLabel)
 
            higherProjectionTfLabel := na
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
// WATERMARK
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
var table watermarkTable = table.new(
 
     position.bottom_right,
 
     1,
 
     1,
 
     bgcolor=color.new(color.black, 100),
 
     frame_width=0,
 
     border_width=0)
 
if barstate.islast
 
    table.cell(
 
         watermarkTable,
 
         0,
 
         0,
 
         "Trading MoneyK's Way",
 
         text_color=color.new(color.white, 72),
 
         text_size=size.tiny,
 
         text_halign=text.align_right,
 
         bgcolor=color.new(color.black, 100))

← 返回列表