MetaAI writes code: Now I have to teach it the bullish application. Here's the modified code for both PineScript (TradingView) and ThinkScript (Thinkorswim) to flag individual stocks within 2 days of breaching the signal line:
PineScript (TradingView):
//@version=5 indicator("Local Lows and Highs", overlay=true)
// Input parameters length = input.int(13, title="Local Low/High Length")
// Calculate local lows and highs l1 = ta.lowest(low, length) l2 = ta.lowest(low, length)[1] h1 = ta.highest(high, length) h2 = ta.highest(high, length)[1]
// Determine which local low to use for signal line signalLow = if low[l1] < low[l2] then l1 else l2
// Plot local lows and highs plot(l1, color=color.red, linewidth=2) plot(l2, color=color.red, linewidth=2) plot(h1, color=color.green, linewidth=2) plot(h2, color=color.green, linewidth=2)
// Calculate target line targetLine = line.new(bar_index[h2], h2, bar_index[l2], l2, extend=extend.right)
// Plot signal line signalLine = line.new(bar_index[l1], l1, bar_index, l1, extend=extend.right)
// Flag stocks within 2 days of breaching signal line breachCondition = close < signalLine breachAlert = ta.crossover(breachCondition, 0) if breachAlert alert("Stock is within 2 days of breaching signal line", alert.freq=alert.freq_once) ______________________________________________________________________________________________________________________________________
ThinkScript (Thinkorswim):
Define the length for local lows and highs input length = 13;
Calculate local lows def l1 = Lowest(low, length); def l2 = Lowest(low, length)[1];
Calculate local highs def h1 = Highest(high, length); def h2 = Highest(high, length)[1];
Determine which local low to use for signal line def signalLow = if low[l1] < low[l2] then low[l1] else low[l2];
Plot local lows and highs plot L1 = l1; L1.SetDefaultColor(Color.RED); L1.SetPaintingStrategy(PaintingStrategy.LINE); plot L2 = l2; L2.SetDefaultColor(Color.RED); L2.SetPaintingStrategy(PaintingStrategy.LINE); plot H1 = h1; H1.SetDefaultColor(Color.GREEN); H1.SetPaintingStrategy(PaintingStrategy.LINE); plot H2 = h2; H2.SetDefaultColor(Color.GREEN); H2.SetPaintingStrategy(PaintingStrategy.LINE);
Calculate target line def targetLine = h2 - (h2 - l2);
Plot signal line plot SignalLine = signalLow; SignalLine.SetDefaultColor(Color.YELLOW); SignalLine.SetPaintingStrategy(PaintingStrategy.LINE);
Flag stocks within 2 days of breaching signal line def breachCondition = close < signalLow; def breachAlert = breachCondition and !breachCondition[1]; alert(breachAlert, "Stock is within 2 days of breaching signal line", Alert.BAR, Sound.Chimes);
To limit alerts to once per stock, use the following code: def breachAlertOnce = if breachAlert then 1 else if !breachAlert and breachAlert[1] then 0 else breachAlertOnce[1]; alert(breachAlertOnce == 1, "Stock is within 2 days of breaching signal line", Alert.BAR, Sound.Chimes); |