Impulse Fade - Trade Ideas

IanColeman · study · 152 行 · 点赞 11 · TradingView 原页

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

Pine Script

//@version=6
// EUR/USD 4H Impulse Fade - Trade Ideas
// Apply to OANDA:EURUSD on the 4-hour chart.
//
// Rules
//   Impulse bar : true range > 2.25 x ATR(20) of the previous bar
//   Bullish     : close > open and close > bar midpoint  -> SELL at next bar open
//   Bearish     : close < open and close < bar midpoint  -> BUY  at next bar open
//   Stop loss   : 3.0 x ATR(20) of the signal bar from the entry price
//   Target      : 5.0 x ATR(20) of the signal bar from the entry price
//   Time exit   : close of the 18th bar after entry (72 hours)
//   One trade at a time. If a bar touches both stop and target, the stop counts.
indicator("Impulse Fade - Trade Ideas", overlay = true, max_labels_count = 500, max_boxes_count = 500)
 
// Inputs
atrLen     = input.int(20, "ATR period", minval = 1, group = "Signal")
impMult    = input.float(2.25, "Impulse threshold (x ATR)", step = 0.25, group = "Signal")
stopMult   = input.float(3.0, "Stop loss (x ATR)", step = 0.5, group = "Exits")
tgtMult    = input.float(5.0, "Profit target (x ATR)", step = 0.5, group = "Exits")
maxBars    = input.int(18, "Time exit (bars)", minval = 1, group = "Exits")
spreadPips = input.float(0.0, "Spread to deduct from each result (pips)", minval = 0, step = 0.1, group = "Results")
showZones  = input.bool(true, "Show stop and target zones", group = "Display")
showTable  = input.bool(true, "Show current trade idea table", group = "Display")
 
// Signal
float pip   = syminfo.mintick * (syminfo.type == "forex" ? 10 : 1)
float atr   = ta.atr(atrLen)
bool impulse = ta.tr(true) > impMult * atr[1]
bool bullImp = impulse and close > open and close > hl2
bool bearImp = impulse and close < open and close < hl2
 
// Trade state
var int   pos       = 0      // 1 = long, -1 = short, 0 = flat
var int   pending   = 0      // signal waiting to be entered at the next bar's open
var float pendAtr   = na
var float entryPx   = na
var float stopPx    = na
var float tgtPx     = na
var int   entryBar  = na
var box   stopBox   = na
var box   tgtBox    = na
var int   nTrades   = 0
var int   nWins     = 0
var float totalPips = 0.0
 
fmtPips(float p) =>
    (p >= 0 ? "+" : "-") + str.tostring(math.abs(p), "#,##0.0") + " pips"
 
// 1. Enter at this bar's open if the previous bar gave a signal
bool enteredNow = false
if pending != 0 and pos == 0
    pos      := pending
    pending  := 0
    entryPx  := open
    stopPx   := open - pos * stopMult * pendAtr
    tgtPx    := open + pos * tgtMult * pendAtr
    entryBar := bar_index
    enteredNow := true
    string eTxt = (pos > 0 ? "LONG " : "SHORT ") + str.tostring(entryPx, format.mintick) + "\nStop " + str.tostring(stopPx, format.mintick) + "\nTarget " + str.tostring(tgtPx, format.mintick) + "\nTime exit: close of bar " + str.tostring(maxBars)
    if pos > 0
        label.new(bar_index, low, eTxt, yloc = yloc.belowbar, style = label.style_label_up, color = color.new(color.teal, 10), textcolor = color.white, size = size.small, textalign = text.align_left)
    else
        label.new(bar_index, high, eTxt, yloc = yloc.abovebar, style = label.style_label_down, color = color.new(color.red, 10), textcolor = color.white, size = size.small, textalign = text.align_left)
    if showZones
        stopBox := box.new(bar_index, math.max(entryPx, stopPx), bar_index + 1, math.min(entryPx, stopPx), border_color = color.new(color.red, 30), bgcolor = color.new(color.red, 80))
        tgtBox  := box.new(bar_index, math.max(entryPx, tgtPx), bar_index + 1, math.min(entryPx, tgtPx), border_color = color.new(color.teal, 30), bgcolor = color.new(color.teal, 80))
 
// 2. Check exits on this bar
bool   exitedNow = false
float  exitPx    = na
string exitWhy   = ""
int    barsHeld  = pos != 0 ? bar_index - entryBar + 1 : 0
if pos != 0
    bool gapStop = bar_index > entryBar and (pos > 0 ? open <= stopPx : open >= stopPx)
    bool hitStop = pos > 0 ? low <= stopPx : high >= stopPx
    bool hitTgt  = pos > 0 ? high >= tgtPx : low <= tgtPx
    if gapStop
        exitPx  := open
        exitWhy := "STOP HIT"
    else if hitStop
        exitPx  := stopPx
        exitWhy := "STOP HIT"
    else if hitTgt
        exitPx  := tgtPx
        exitWhy := "TARGET HIT"
    else if barsHeld >= maxBars
        exitPx  := close
        exitWhy := "TIME EXIT"
    if not na(exitPx)
        float resultPips = (exitPx - entryPx) * pos / pip - spreadPips
        nTrades   += 1
        nWins     += resultPips > 0 ? 1 : 0
        totalPips += resultPips
        label.new(bar_index, exitPx, exitWhy + " " + str.tostring(exitPx, format.mintick) + "\n" + fmtPips(resultPips), style = label.style_label_left, color = color.new(resultPips > 0 ? color.teal : color.red, 10), textcolor = color.white, size = size.small, textalign = text.align_left)
        if not na(stopBox)
            box.set_right(stopBox, math.max(bar_index, entryBar + 1))
            box.set_right(tgtBox, math.max(bar_index, entryBar + 1))
            stopBox := na
            tgtBox  := na
        pos := 0
        exitedNow := true
    else if not na(stopBox)
        box.set_right(stopBox, bar_index + 1)
        box.set_right(tgtBox, bar_index + 1)
 
// 3. Look for a new signal at the close of this bar (only when flat)
bool newBuy  = false
bool newSell = false
if pos == 0 and pending == 0 and barstate.isconfirmed and not na(atr[1])
    if bearImp
        pending := 1
        pendAtr := atr
        newBuy  := true
    else if bullImp
        pending := -1
        pendAtr := atr
        newSell := true
 
plotshape(newSell, title = "Sell signal", style = shape.triangledown, location = location.abovebar, color = color.red, size = size.tiny, text = "SELL\nnext open", textcolor = color.red)
plotshape(newBuy, title = "Buy signal", style = shape.triangleup, location = location.belowbar, color = color.teal, size = size.tiny, text = "BUY\nnext open", textcolor = color.teal)
 
// Alerts
alertcondition(newBuy, "Buy signal", "EUR/USD 4H Impulse Fade: BUY at the next bar open")
alertcondition(newSell, "Sell signal", "EUR/USD 4H Impulse Fade: SELL at the next bar open")
alertcondition(exitedNow, "Trade closed", "EUR/USD 4H Impulse Fade: trade closed (stop, target or time exit)")
 
// Current trade idea
var table t = table.new(position.top_right, 2, 8, bgcolor = color.new(color.gray, 85), border_width = 1)
if showTable and barstate.islast
    string idea = pos > 0 ? "LONG" : pos < 0 ? "SHORT" : pending > 0 ? "BUY at next bar open" : pending < 0 ? "SELL at next bar open" : "No trade - wait for impulse bar"
    bool inPos = pos != 0
    bool isPend = pending != 0
    float showEntry = inPos ? entryPx : close
    float showStop  = inPos ? stopPx : close - pending * stopMult * pendAtr
    float showTgt   = inPos ? tgtPx : close + pending * tgtMult * pendAtr
    float openPips  = inPos ? (close - entryPx) * pos / pip : na
    table.cell(t, 0, 0, "Trade idea", text_halign = text.align_left)
    table.cell(t, 1, 0, idea)
    table.cell(t, 0, 1, "Entry", text_halign = text.align_left)
    table.cell(t, 1, 1, inPos ? str.tostring(showEntry, format.mintick) : isPend ? "about " + str.tostring(showEntry, format.mintick) : "-")
    table.cell(t, 0, 2, "Stop loss", text_halign = text.align_left)
    table.cell(t, 1, 2, inPos ? str.tostring(showStop, format.mintick) : isPend ? "about " + str.tostring(showStop, format.mintick) : "-")
    table.cell(t, 0, 3, "Profit target", text_halign = text.align_left)
    table.cell(t, 1, 3, inPos ? str.tostring(showTgt, format.mintick) : isPend ? "about " + str.tostring(showTgt, format.mintick) : "-")
    table.cell(t, 0, 4, "Time exit", text_halign = text.align_left)
    table.cell(t, 1, 4, inPos ? "Bar " + str.tostring(barsHeld) + " of " + str.tostring(maxBars) : isPend ? "Close of bar " + str.tostring(maxBars) : "-")
    table.cell(t, 0, 5, "Open result", text_halign = text.align_left)
    table.cell(t, 1, 5, inPos ? fmtPips(openPips) : "-")
    table.cell(t, 0, 6, "Closed trades on chart", text_halign = text.align_left)
    table.cell(t, 1, 6, str.tostring(nTrades) + " (" + str.tostring(nWins) + " winners)")
    table.cell(t, 0, 7, "Total result", text_halign = text.align_left)
    table.cell(t, 1, 7, fmtPips(totalPips))

← 返回列表