Aggressive Pullback Indicator

ZenAndTheArtOfTrading · study · 119 行 · 点赞 2,562 · TradingView 原页

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

Pine Script

// API v2.0 Created by Matthew J. Slabosz
// Designed for Steven Hart's 4HR Aggressive Pullback Strategy
// www.thetradingchannel.net
// To create Alerts use Condition: API, Frequency: Once Per Bar (on Close)
//@version=3
study(title = "Aggressive Pullback Indicator", shorttitle="API", overlay = true)
// Strategy settings
pullback = input(2, minval=1, title="Minimum Candle Pullback?")
maxEngulf = input(3, minval=1, title="How Many Candles Can We Engulf?")
atrEnabled = input(title="Allow Rule #2 ^ to be Overridden by ATR?", type=bool, defval=true)
maxATR = input(0.99, minval=0.01, title="Override Rule #2 If Candle Size <= #.##% of ATR")
atrLength = input(14, minval=1, title="ATR Length")
emaLength = input(50, minval=1, title="EMA Length")
emaSource = input(close, title="EMA Source")
lookback = input(5, minval=1, title="How Far To Look Back For Swing High/Lows?")
stopSizeInput = input(1.0, minval=0.0, title="Stop Distance (in pips or ATR):")
stopSizeUseATR = input(title="Use ATR x ^ for stop loss?", type=bool, defval=true)
maximumCandleSize = input(2.0, minval=0.0, title="Candle Size Must Be <= ? x ATR:")
riskToReward = input(1.0, minval=0.1, title="Reward To Risk Ratio (Stop Loss X ?):")
drawOnRT = input(title="Draw Signal on Bars Yet To Close?", type=bool, defval=true)
drawPositions = input(title="Draw Stops & Targets?", type=bool, defval=true)
drawAllSignals = input(title="Draw Invalidated Signals?", type=bool, defval=false)
// Get EMA and calculate ATR validity
ema = ema(emaSource, emaLength)
currentATR = atr(atrLength)
candleBodySize = abs(open - close)
candleSizePercentOfATR = (candleBodySize / currentATR)
validATR = atrEnabled and (candleSizePercentOfATR <= maxATR)
// Calculate stop loss info
stopSize = stopSizeInput
if(stopSizeUseATR)
    stopSize := currentATR * stopSizeInput
// Long strategy variables
activeLongTrade = false
activeLongTrade := activeLongTrade[1]
longStopPrice = 0.0
longStopPrice := longStopPrice[1]
longTargetPrice = 0.0
longTargetPrice := longTargetPrice[1]
longTargetPrice2 = 0.0
longTargetPrice2 := longTargetPrice2[1]
waitForLongEmaTouch = false
longEntrySize = (close - lowest(low, lookback)) + stopSize
shortEntrySize = (highest(high, lookback) - close) + stopSize
// Wait for stop loss or target to be hit
if(activeLongTrade and low <= longStopPrice)
    waitForLongEmaTouch := true
if(activeLongTrade and high >= longTargetPrice)
    activeLongTrade := false
// If we've been stopped out, wait for EMA touch or higher high
if(waitForLongEmaTouch and (low <= ema))
    activeLongTrade := false
if(waitForLongEmaTouch and (low <= ema))
    waitForLongEmaTouch := false
// Short strategy variables
activeShortTrade = false
activeShortTrade := activeShortTrade[1]
shortStopPrice = 0.0
shortStopPrice := shortStopPrice[1]
shortTargetPrice = 0.0
shortTargetPrice := shortTargetPrice[1]
waitForShortEmaTouch = false
// Wait for stop loss or target to be hit
if(activeShortTrade and high >= shortStopPrice)
    waitForShortEmaTouch := true
if(activeShortTrade and low <= shortTargetPrice)
    activeShortTrade := false
// If we've been stopped out, wait for EMA touch or lower low
if(waitForShortEmaTouch and (high >= ema or (low <= lowest(low, lookback*3))))
    activeShortTrade := false
if(waitForShortEmaTouch and (high >= ema or (low <= lowest(low, lookback*3))))
    waitForShortEmaTouch := false
// Plot data
plot(ema, title="EMA", color=gray, transp=0)
// Validate long signals
rule1L = (close > ema and (open[1] > close[1] and close > open and close >= open[1] and close[1] >= open and close - open > open[1] - close[1])) // Bullish engulfing?
rule2L = open < open[pullback] // Satisfies pullback rule?
rule3L = close < open[maxEngulf] // Satisfies maximum engulfing rule?
rule4L = close < highest(open, lookback) and validATR // Satisfies swing high and ATR validation?
rule5L = highest(high, lookback) > ema and (high[1] > ema or high[2] > ema) // Make sure the past 2 candles have at least touched the 50ema, cleans up some invalid signals around EMA
rule6L = not (activeLongTrade or activeShortTrade) or drawAllSignals // Make sure we're not currently involved in a trade
rule7L = not waitForLongEmaTouch or drawAllSignals // Make sure we aren't waiting on an EMA test
rule8L = abs(high - low) <= currentATR * maximumCandleSize // Make sure candle is not too big
validLong = rule1L and rule2L and (rule3L or rule4L) and rule5L and rule6L and rule7L and rule8L
// Draw long signal only if rules are met
plotshape(validLong and (barstate.ishistory or drawOnRT), title= "Bullish Engulfing", location=location.belowbar, color=lime, transp=0, style=shape.arrowup, text="Long")
// Validate short signals
rule1S = (close < ema and (close[1] > open[1] and open > close and open >= close[1] and open[1] >= close and open - close > close[1] - open[1])) // Bearish engulfing?
rule2S = open > open[pullback] // Satisfies pullback rule?
rule3S = close > open[maxEngulf] // Satisfies maximum engulfing rule?
rule4S = close > lowest(open, lookback) and validATR // Satisfies swing low and ATR validation?
rule5S = lowest(low, lookback) < ema and (low[1] < ema or low[2] < ema) // Make sure the past 2 candles have at least touched the 50ema, cleans up some invalid signals around EMA
rule6S = not (activeShortTrade or activeLongTrade) or drawAllSignals // make sure we're not currently involved in a trade
rule7S = not waitForShortEmaTouch or drawAllSignals // Make sure we aren't waiting on an EMA test
rule8S = abs(high - low) <= currentATR * maximumCandleSize // Make sure candle is not too big
validShort = rule1S and rule2S and (rule3S or rule4S) and rule5S and rule6S and rule7S and rule8S
// Draw short signal only if rules are met
plotshape(validShort and (barstate.ishistory or drawOnRT),  title= "Bearish Engulfing", color=red, transp=0, style=shape.arrowdown, text="Short")
// Activate long flag and store position info
if(validLong)
    activeLongTrade := true
if(validLong)
    longStopPrice := close - longEntrySize
if(validLong)
    longTargetPrice := close + (longEntrySize * riskToReward)
// Activate short flag and store position info
if(validShort)
    activeShortTrade := true
if(validShort)
    shortStopPrice := close + shortEntrySize
if(validShort)
    shortTargetPrice := close - (shortEntrySize * riskToReward)
// Draw stops & targets
plot(validLong and drawPositions ? longStopPrice : na, title="Long Stop Price", color=red, style=cross, transp=0)
plot(validShort and drawPositions ? shortStopPrice : na, title="Short Stop Price", color=red, style=cross, transp=0)
plot(validLong and drawPositions ? longTargetPrice : na, title="Long Target Price", color=lime, style=cross, transp=0)
plot(validShort and drawPositions ? shortTargetPrice : na, title="Short Target Price", color=lime, style=cross, transp=0)
// Send out an alert if this candle meets our conditions
alertcondition(validLong or validShort, title="API Alert!", message="API Engulfing Signal!")

← 返回列表