PineScript v1挤压动量指标(SQZMOM_LB)转v5及添加买卖预警条件技术求助
Got it, let's tackle this problem for you. I'll cover two approaches: converting the v1 indicator to Pine Script v5 (the modern, supported version) with your requested alert conditions, and also modifying the original v1 code to add those alerts directly, in case you need to stick with the older version.
Solution 1: Pine Script v5 Conversion with Alert Conditions
Pine Script v5 has cleaner syntax and better documentation, so this is the recommended approach. I've converted the original code, updated all v1 functions to their v5 equivalents, and added alert conditions that trigger when the momentum area turns lime (buy) or red/maroon (sell).
//@version=5 indicator(shorttitle = "SQZMOM_LB", title="Squeeze Momentum Indicator", overlay=false) // Inputs length = input.int(20, title="BB Length") mult = input.float(2.0, title="BB MultFactor") lengthKC = input.int(20, title="KC Length") multKC = input.float(1.5, title="KC MultFactor") useTrueRange = input.bool(true, title="Use TrueRange (KC)") // Calculate Bollinger Bands (BB) source = close basis = ta.sma(source, length) dev = mult * ta.stdev(source, length) upperBB = basis + dev lowerBB = basis - dev // Calculate Keltner Channels (KC) ma = ta.sma(source, lengthKC) range = useTrueRange ? ta.tr : (high - low) rangema = ta.sma(range, lengthKC) upperKC = ma + rangema * multKC lowerKC = ma - rangema * multKC // Squeeze status sqzOn = (lowerBB > lowerKC) and (upperBB < upperKC) sqzOff = (lowerBB < lowerKC) and (upperBB > upperKC) noSqz = not sqzOn and not sqzOff // Momentum value val = ta.linreg(source - ta.avg(ta.avg(ta.highest(high, lengthKC), ta.lowest(low, lengthKC)), ta.sma(close, lengthKC)), lengthKC, 0) // Color logic bcolor = val > 0 ? (val > val[1] ? color.lime : color.maroon) : (val < val[1] ? color.red : color.new(color.green, 0)) scolor = noSqz ? color.blue : sqzOn ? color.black : color.gray // Plotting plot(val, color=bcolor, style=plot.style_area, linewidth=4) plot(0, color=scolor, style=plot.style_cross, linewidth=2) // Alert Conditions // Buy alert: Momentum area turns lime (bullish momentum increasing) alertcondition(bcolor == color.lime, title="Buy Alert", message="Squeeze Momentum: Bullish signal (lime color)") // Sell alert: Momentum area turns red or maroon (bearish momentum increasing) alertcondition(bcolor == color.red or bcolor == color.maroon, title="Sell Alert", message="Squeeze Momentum: Bearish signal (red/maroon color)")
Key Notes for v5 Version:
- Replaced
study()withindicator()(v5's standard declaration) - Updated input types to v5-specific
input.int()/input.float()/input.bool()for better type safety - Prefixed all built-in functions with
ta.(v5 requires explicit namespace for technical analysis functions) - Adjusted color handling to use v5's
colorobject - Added clear alert conditions that trigger when the momentum area matches your target colors
Solution 2: Add Alert Conditions to Original Pine Script v1 Code
If you need to keep using the v1 version, here's the modified code with alert conditions added. The core logic stays the same, we just add alertcondition() calls to match your color-based triggers.
study(shorttitle = "SQZMOM_LB", title="Squeeze Momentum Indicator", overlay=false) length = input(20, title="BB Length") mult = input(2.0,title="BB MultFactor") lengthKC=input(20, title="KC Length") multKC = input(1.5, title="KC MultFactor") useTrueRange = input(true, title="Use TrueRange (KC)", type=bool) // Calculate Bollinger Bands (BB) source = close basis = sma(source, length) dev = multKC * stdev(source, length) upperBB = basis + dev lowerBB = basis - dev // Calculate Keltner Channels (KC) ma = sma(source, lengthKC) range = useTrueRange ? tr : (high - low) rangema = sma(range, lengthKC) upperKC = ma + rangema * multKC lowerKC = ma - rangema * multKC sqzOn = (lowerBB > lowerKC) and (upperBB < upperKC) sqzOff = (lowerBB < lowerKC) and (upperBB > upperKC) noSqz = (sqzOn == false) and (sqzOff == false) val = linreg(source - avg(avg(highest(high, lengthKC), lowest(low, lengthKC)),sma(close,lengthKC)), lengthKC,0) bcolor = iff( val > 0, iff( val > nz(val[1]), lime, maroon), iff( val < nz(val[1]), red, green)) scolor = noSqz ? blue : sqzOn ? black : gray plot(val, color=bcolor, style=area, linewidth=4) plot(0, color=scolor, style=cross, linewidth=2) // Added Alert Conditions alertcondition(bcolor == lime, title="Buy Alert", message="Squeeze Momentum: Buy Signal (Lime Color)") alertcondition(bcolor == red or bcolor == maroon, title="Sell Alert", message="Squeeze Momentum: Sell Signal (Red/Maroon Color)")
Bonus: Avoid Repeated Alerts
If you want to trigger alerts only when the color changes (not every bar the color stays the same), modify the alert conditions to check for a transition:
// Buy alert only when color switches to lime alertcondition(bcolor == lime and bcolor[1] != lime, title="Buy Alert (New Signal)", message="Squeeze Momentum: New Bullish Signal") // Sell alert only when color switches to red/maroon alertcondition((bcolor == red or bcolor == maroon) and (bcolor[1] != red and bcolor[1] != maroon), title="Sell Alert (New Signal)", message="Squeeze Momentum: New Bearish Signal")
内容的提问来源于stack exchange,提问作者AlexSP

