NQ V39 | Dupa LRC DOWN + Upper Touch + QQE SHORT

shaditak · strategy · 790 行 · 点赞 1 · TradingView 原页

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

Pine Script

//@version=6
// Original Dupa LRC mathematics © kriv
// Mozilla Public License 2.0
// https://mozilla.org/MPL/2.0/
 
strategy("NQ V39 | Dupa LRC DOWN + Upper Touch + QQE SHORT",
     overlay=true,
     initial_capital=50000,
     pyramiding=0,
     default_qty_type=strategy.fixed,
     process_orders_on_close=true,
     commission_type=strategy.commission.cash_per_contract,
     commission_value=2.50,
     slippage=1,
     margin_short=1,
     max_lines_count=100,
     max_labels_count=500)
 
if timeframe.in_seconds() != 60
    runtime.error("Use the 1-minute NQ/MNQ chart.")
 
// ========================================
// INPUTS
// ========================================
 
groupLRC = "Original Dupa LRC"
 
lengthInput = input.int(
     100, "Length",
     minval=2, maxval=5000,
     group=groupLRC)
 
sourceInput = input.source(
     close, "Source",
     group=groupLRC)
 
transparencyValue = input.int(
     50, "Transparency (%)",
     minval=0, maxval=100,
     group=groupLRC)
 
showPearsonInput = input.bool(
     true, "Show Pearson's R",
     group=groupLRC)
 
extendLeftInput = input.bool(
     false, "Extend Lines Left",
     group=groupLRC)
 
extendRightInput = input.bool(
     true, "Extend Lines Right",
     group=groupLRC)
 
extendStyle = switch
    extendLeftInput and extendRightInput => extend.both
    extendLeftInput => extend.left
    extendRightInput => extend.right
    => extend.none
 
colorUpper = input.color(
     color.new(color.yellow, 85),
     "Upper color", group=groupLRC)
 
colorLower = input.color(
     color.new(color.green, 85),
     "Lower color", group=groupLRC)
 
colorOnePointZero = color.gray
colorOnePointFive = color.green
colorTwoPointZero = color.yellow
colorTwoPointFive = color.green
colorThreePointZero = color.red
 
// ========================================
// QQE SIGNALS 1-MINUTE
// ========================================
 
groupQQE = "QQE Signals 1m"
 
qqeRSI = input.int(
     14, "RSI period",
     minval=1, group=groupQQE)
 
qqeSmooth = input.int(
     5, "RSI smoothing",
     minval=1, group=groupQQE)
 
qqeFactor = input.float(
     4.238, "QQE factor",
     minval=0.1, group=groupQQE)
 
// ========================================
// TRADE SETTINGS
// ========================================
 
groupTrade = "Entry and Risk"
 
contracts = input.int(
     1, "Contracts",
     minval=1, group=groupTrade)
 
useYellow = input.bool(
     true, "Allow upper yellow +2 sigma",
     group=groupTrade)
 
useRed = input.bool(
     true, "Allow upper red +3 sigma",
     group=groupTrade)
 
useQQEMOD = input.bool(
     false, "Also require QQE MOD RED",
     group=groupTrade)
 
stopUSD = input.float(
     1500.0, "Fixed Stop Loss ($)",
     minval=1, group=groupTrade)
 
targetUSD = input.float(
     2000.0, "Fixed Profit Target ($)",
     minval=1, group=groupTrade)
 
useSessions = input.bool(
     true, "Use NY trading windows",
     group="Hours")
 
showEntryLabels = input.bool(
     true, "Show SHORT entry slope labels",
     group="Diagnostics")
 
showDiagnostics = input.bool(
     true, "Show filter diagnostics",
     group="Diagnostics")
 
// ========================================
// ORIGINAL DUPA calcSlope()
// © kriv
//
// Only change: calculate historical bars
// rather than only barstate.islast.
// Original regression math is preserved.
// ========================================
 
calcSlope(source, length) =>
    max_bars_back(source, 5000)
 
    if bar_index < length - 1 or length <= 1
        [float(na), float(na), float(na)]
    else
        sumX = 0.0
        sumY = 0.0
        sumXSqr = 0.0
        sumXY = 0.0
 
        for j = 0 to length - 1
            val = source[j]
            per = j + 1.0
 
            sumX := sumX + per
            sumY := sumY + val
            sumXSqr := sumXSqr + per * per
            sumXY := sumXY + val * per
 
        slope = (length * sumXY - sumX * sumY) /
             (length * sumXSqr - sumX * sumX)
 
        average = sumY / length
 
        intercept =
             average - slope * sumX / length + slope
 
        [slope, average, intercept]
 
// ========================================
// ORIGINAL DUPA calcDev()
// © kriv
// ========================================
 
calcDev(source, length, slope, average, intercept) =>
    upDev = 0.0
    dnDev = 0.0
    stdDevAcc = 0.0
    dsxx = 0.0
    dsyy = 0.0
    dsxy = 0.0
 
    periods = length - 1
    daY = intercept + slope * periods / 2
    val = intercept
 
    for j = 0 to periods
        price = high[j] - val
 
        if price > upDev
            upDev := price
 
        price := val - low[j]
 
        if price > dnDev
            dnDev := price
 
        price := source[j]
 
        dxt = price - average
        dyt = val - daY
        price := price - val
 
        stdDevAcc += price * price
        dsxx += dxt * dxt
        dsyy += dyt * dyt
        dsxy += dxt * dyt
 
        val += slope
 
    stdDev = math.sqrt(
         stdDevAcc / (periods == 0 ? 1 : periods))
 
    pearsonR = dsxx == 0 or dsyy == 0 ?
         0 : dsxy / math.sqrt(dsxx * dsyy)
 
    [stdDev, pearsonR, upDev, dnDev]
 
// ========================================
// ORIGINAL DUPA CHANNEL VALUES
// ========================================
 
[s, a, i] = calcSlope(sourceInput, lengthInput)
 
startPrice = i + s * (lengthInput - 1)
endPrice = i
 
float stdDev = na
float pearsonR = na
 
if not na(s)
    [devValue, pearsonValue, upDevValue, dnDevValue] =
         calcDev(sourceInput, lengthInput, s, a, i)
 
    stdDev := devValue
    pearsonR := pearsonValue
 
upperStartPrice2 = startPrice + 1.0 * stdDev
upperEndPrice2 = endPrice + 1.0 * stdDev
 
upperStartPrice3 = startPrice + 1.5 * stdDev
upperEndPrice3 = endPrice + 1.5 * stdDev
 
upperStartPrice4 = startPrice + 2.0 * stdDev
upperEndPrice4 = endPrice + 2.0 * stdDev
 
upperStartPrice5 = startPrice + 2.5 * stdDev
upperEndPrice5 = endPrice + 2.5 * stdDev
 
upperStartPrice6 = startPrice + 3.0 * stdDev
upperEndPrice6 = endPrice + 3.0 * stdDev
 
lowerStartPrice2 = startPrice - 1.0 * stdDev
lowerEndPrice2 = endPrice - 1.0 * stdDev
 
lowerStartPrice3 = startPrice - 1.5 * stdDev
lowerEndPrice3 = endPrice - 1.5 * stdDev
 
lowerStartPrice4 = startPrice - 2.0 * stdDev
lowerEndPrice4 = endPrice - 2.0 * stdDev
 
lowerStartPrice5 = startPrice - 2.5 * stdDev
lowerEndPrice5 = endPrice - 2.5 * stdDev
 
lowerStartPrice6 = startPrice - 3.0 * stdDev
lowerEndPrice6 = endPrice - 3.0 * stdDev
 
// ========================================
// ORIGINAL DUPA SLOPE DIRECTION
//
// The source indexes newest -> oldest.
// Therefore:
// s > 0 = line slopes DOWN left to right.
// s < 0 = line slopes UP left to right.
// ========================================
 
lrcDown = not na(s) and s > 0
 
// ========================================
// ORIGINAL DUPA DIAGONAL LINE OBJECTS
// NO ta.linreg() OR plot() REPLACEMENT
// ========================================
 
f_updateLine(
     line oldLine,
     float y1,
     float y2,
     color clr,
     bool dashed) =>
 
    line result = oldLine
 
    if na(result)
        result := line.new(
             bar_index - lengthInput + 1,
             y1,
             bar_index,
             y2,
             width=1,
             extend=extendStyle,
             color=color.new(clr, transparencyValue),
             style=dashed ?
                 line.style_dashed :
                 line.style_solid)
    else
        line.set_xy1(
             result,
             bar_index - lengthInput + 1,
             y1)
 
        line.set_xy2(result, bar_index, y2)
 
        line.set_extend(result, extendStyle)
        line.set_color(
             result,
             color.new(clr, transparencyValue))
 
    result
 
var line baseLine = na
 
var line upper2 = na
var line upper3 = na
var line upper4 = na
var line upper5 = na
var line upper6 = na
 
var line lower2 = na
var line lower3 = na
var line lower4 = na
var line lower5 = na
var line lower6 = na
 
if barstate.islast and
     not na(startPrice) and not na(stdDev)
 
    baseLine := f_updateLine(
         baseLine,
         startPrice,
         endPrice,
         colorLower,
         false)
 
    upper2 := f_updateLine(
         upper2,
         upperStartPrice2,
         upperEndPrice2,
         colorOnePointZero,
         false)
 
    upper3 := f_updateLine(
         upper3,
         upperStartPrice3,
         upperEndPrice3,
         colorOnePointFive,
         true)
 
    upper4 := f_updateLine(
         upper4,
         upperStartPrice4,
         upperEndPrice4,
         colorTwoPointZero,
         false)
 
    upper5 := f_updateLine(
         upper5,
         upperStartPrice5,
         upperEndPrice5,
         colorTwoPointFive,
         true)
 
    upper6 := f_updateLine(
         upper6,
         upperStartPrice6,
         upperEndPrice6,
         colorThreePointZero,
         false)
 
    lower2 := f_updateLine(
         lower2,
         lowerStartPrice2,
         lowerEndPrice2,
         colorOnePointZero,
         false)
 
    lower3 := f_updateLine(
         lower3,
         lowerStartPrice3,
         lowerEndPrice3,
         colorOnePointFive,
         true)
 
    lower4 := f_updateLine(
         lower4,
         lowerStartPrice4,
         lowerEndPrice4,
         colorTwoPointZero,
         false)
 
    lower5 := f_updateLine(
         lower5,
         lowerStartPrice5,
         lowerEndPrice5,
         colorTwoPointFive,
         true)
 
    lower6 := f_updateLine(
         lower6,
         lowerStartPrice6,
         lowerEndPrice6,
         colorThreePointZero,
         false)
 
// ========================================
// ORIGINAL PEARSON LABEL
// ========================================
 
var label pearsonLabel = na
 
if barstate.islast
    if not na(pearsonLabel)
        label.delete(pearsonLabel)
 
    if showPearsonInput and not na(pearsonR)
        pearsonLabel := label.new(
             bar_index - lengthInput + 1,
             lowerStartPrice4,
             str.tostring(
                 pearsonR, "#.################"),
             color=color.new(color.white, 100),
             textcolor=color.new(colorUpper, 0),
             style=label.style_label_up)
 
// ========================================
// QQE SIGNALS - 1 MINUTE ONLY
//
// Accept a new SHORT or already SHORT.
// ========================================
 
f_qqeShort() =>
    r = ta.ema(
         ta.rsi(close, qqeRSI),
         qqeSmooth)
 
    atrR = math.abs(r[1] - r)
 
    wilders = qqeRSI * 2 - 1
 
    dar = ta.ema(
         ta.ema(atrR, wilders),
         wilders) * qqeFactor
 
    float longBand = 0.0
    float shortBand = 0.0
 
    newLong = r - dar
    newShort = r + dar
 
    longBand :=
         r[1] > longBand[1] and
         r > longBand[1] ?
         math.max(longBand[1], newLong) :
         newLong
 
    shortBand :=
         r[1] < shortBand[1] and
         r < shortBand[1] ?
         math.min(shortBand[1], newShort) :
         newShort
 
    crossShort = ta.cross(r, shortBand[1])
    crossLong = ta.cross(longBand[1], r)
 
    int trend = 0
 
    trend :=
         crossShort ? 1 :
         crossLong ? -1 :
         nz(trend[1], 1)
 
    trail = trend == 1 ? longBand : shortBand
 
    trail > r
 
qqeShort1 = f_qqeShort()
 
// ========================================
// OPTIONAL QQE MOD RED - 1 MINUTE
// OFF BY DEFAULT
// ========================================
 
f_modPrimary() =>
    r = ta.ema(ta.rsi(close, 6), 5)
 
    atrR = math.abs(r[1] - r)
    dar = ta.ema(atrR, 11) * 3.0
 
    float longBand = 0.0
    float shortBand = 0.0
 
    newLong = r - dar
    newShort = r + dar
 
    longBand :=
         r[1] > longBand[1] and
         r > longBand[1] ?
         math.max(longBand[1], newLong) :
         newLong
 
    shortBand :=
         r[1] < shortBand[1] and
         r < shortBand[1] ?
         math.min(shortBand[1], newShort) :
         newShort
 
    crossShort = ta.cross(r, shortBand[1])
    crossLong = ta.cross(longBand[1], r)
 
    int trend = 0
 
    trend :=
         crossShort ? 1 :
         crossLong ? -1 :
         nz(trend[1], 0)
 
    trend == 1 ? longBand : shortBand
 
primaryR = ta.ema(ta.rsi(close, 6), 5)
secondaryR = ta.ema(ta.rsi(close, 6), 5)
 
modLine = f_modPrimary() - 50.0
 
bbBasis = ta.sma(modLine, 50)
bbLower = bbBasis - 0.35 * ta.stdev(modLine, 50)
 
qqeMODRed =
     secondaryR - 50.0 < -3.0 and
     primaryR - 50.0 < bbLower
 
modReady = not useQQEMOD or qqeMODRed
 
// ========================================
// NEW YORK TRADING HOURS
// ========================================
 
tz = "America/New_York"
 
night = not na(time(
     timeframe.period,
     "1900-0923:1234567", tz))
 
daySession = not na(time(
     timeframe.period,
     "0945-1540:1234567", tz))
 
entryHours =
     not useSessions or night or daySession
 
nyH = hour(time, tz)
nyM = minute(time, tz)
 
morningFlat = nyH == 9 and nyM == 25
afternoonFlat = nyH == 15 and nyM == 43
 
forceFlat =
     useSessions and
     (morningFlat or afternoonFlat)
 
// ========================================
// UPPER LRC TOUCH CONDITIONS
//
// A candle only needs to TOUCH/EXCEED
// an upper band with its HIGH.
//
// NO REQUIREMENT TO CLOSE ABOVE BAND.
//
// Original Dupa yellow = +2 sigma.
// Original Dupa red = +3 sigma.
// ========================================
 
upperYellow = upperEndPrice4
upperRed = upperEndPrice6
 
yellowTouched =
     useYellow and
     not na(upperYellow) and
     high >= upperYellow
 
redTouched =
     useRed and
     not na(upperRed) and
     high >= upperRed
 
// ========================================
// STRICT SHORT CONFIRMATION
//
// Check all requirements on the SAME
// confirmed 1-minute candle.
//
// NO resting orders.
// NO previous-candle entries.
// NO 5-minute filter.
// ========================================
 
baseReady =
     barstate.isconfirmed and
     entryHours and
     not forceFlat and
     strategy.position_size == 0 and
     lrcDown and
     qqeShort1 and
     modReady
 
shortRed =
     baseReady and
     redTouched
 
shortYellow =
     baseReady and
     not shortRed and
     yellowTouched
 
enterShort =
     shortRed or shortYellow
 
// ========================================
// ENTRY
//
// Market order at confirmed candle close.
// The HIGH touching the band is enough.
//
// No old sell-limit orders can remain
// pending after LRC changes direction.
// ========================================
 
if enterShort
 
    strategy.entry(
         "SHORT",
         strategy.short,
         qty=contracts,
         comment=shortRed ?
             "RED TOUCH | LRC DOWN" :
             "YELLOW TOUCH | LRC DOWN")
 
// ========================================
// FIXED $1500 STOP / $2000 TARGET
//
// NO PROFIT LOCK.
// NO TRAILING STOP.
// NO LOWER YELLOW EXIT.
// ========================================
 
tickValue =
     syminfo.pointvalue * syminfo.mintick
 
stopTicks = math.max(
     1,
     int(math.round(
         stopUSD /
         (contracts * tickValue))))
 
targetTicks = math.max(
     1,
     int(math.round(
         targetUSD /
         (contracts * tickValue))))
 
strategy.exit(
     "SHORT EXIT",
     from_entry="SHORT",
     loss=stopTicks,
     profit=targetTicks,
     comment_loss="SL",
     comment_profit="TP")
 
// ========================================
// EXISTING TIME-BASED FLATTEN
// ========================================
 
if barstate.isconfirmed and forceFlat
 
    strategy.cancel_all()
 
    if strategy.position_size != 0
        strategy.close_all(
             comment="NY TIME FLAT",
             immediately=true)
 
// ========================================
// ENTRY AUDIT LABEL
// ========================================
 
if showEntryLabels and enterShort
    label.new(
         bar_index,
         high,
         (shortRed ? "RED" : "YELLOW") +
         " TOUCH\nLRC DOWN\ns=" +
         str.tostring(s, "#.####"),
         color=color.new(color.green, 5),
         textcolor=color.white,
         style=label.style_label_down,
         size=size.tiny)
 
// ========================================
// FILTER DIAGNOSTICS
// Sequential counts on loaded chart.
// ========================================
 
var int nHours = 0
var int nDown = 0
var int nQQE = 0
var int nMOD = 0
var int nTouch = 0
var int nEntries = 0
 
if barstate.isconfirmed and entryHours and not forceFlat
    nHours += 1
 
    if lrcDown
        nDown += 1
 
        if qqeShort1
            nQQE += 1
 
            if modReady
                nMOD += 1
 
                if yellowTouched or redTouched
                    nTouch += 1
 
if enterShort
    nEntries += 1
 
var table diag = table.new(
     position.top_right, 2, 7,
     border_width=1,
     bgcolor=color.new(color.black, 15))
 
if showDiagnostics and barstate.islast
 
    table.cell(diag, 0, 0,
         "V39 SHORT FILTERS",
         text_color=color.white)
 
    table.cell(diag, 1, 0,
         "Bars", text_color=color.white)
 
    table.cell(diag, 0, 1,
         "Eligible hours", text_color=color.white)
    table.cell(diag, 1, 1,
         str.tostring(nHours), text_color=color.white)
 
    table.cell(diag, 0, 2,
         "+ LRC DOWN", text_color=color.white)
    table.cell(diag, 1, 2,
         str.tostring(nDown), text_color=color.white)
 
    table.cell(diag, 0, 3,
         "+ QQE SHORT 1m", text_color=color.white)
    table.cell(diag, 1, 3,
         str.tostring(nQQE), text_color=color.white)
 
    table.cell(diag, 0, 4,
         "+ Optional MOD", text_color=color.white)
    table.cell(diag, 1, 4,
         str.tostring(nMOD), text_color=color.white)
 
    table.cell(diag, 0, 5,
         "+ Upper LRC touch", text_color=color.white)
    table.cell(diag, 1, 5,
         str.tostring(nTouch), text_color=color.white)
 
    table.cell(diag, 0, 6,
         "SHORT orders", text_color=color.white)
    table.cell(diag, 1, 6,
         str.tostring(nEntries), text_color=color.white)
 
// ========================================
// ALERT
// ========================================
 
alertcondition(
     enterShort,
     title="V39 Upper Dupa LRC SHORT",
     message="Original Dupa LRC DOWN, upper yellow/red touched, QQE Signals 1m SHORT.")

← 返回列表