Dynamic Support & Resistance + Advance/Decline Indicator + Buy/Sell Labels 代码介绍 以下代码由今日美股网(www.TodayUSStock.com)代码学院提供,这个脚本结合了支撑和阻力水平、进退指标以及买卖信号标签。它通过计算一定时间范围内的最高和最低点来确定支撑和阻力水平,并在突破这些水平时发出信号。此外,还包括了一个进退指标,它显示了市场的上涨和下跌趋势,并且在图表上标出买卖信号。 代码分析 以下是这个脚本的详细代码分析: //@version=5 indicator("Dynamic Support & Resistance + Advance/Decline Indicator + Buy/Sell Labels", overlay=true) // User-defined lookback period lookback = input(20, title="Lookback Period for Levels") // Calculate support and resistance resistance = ta.highest(high, lookback) support = ta.lowest(low, lookback) // Plot Support and Resistance Levels plot(resistance, title="Resistance", color=color.red, linewidth=2, style=plot.style_stepline) plot(support, title="Support", color=color.green, linewidth=2, style=plot.style_stepline) // Optional: Show breakout alerts alertcondition(close > resistance, title="Breakout Alert", message="Price broke resistance!") alertcondition(close < support, title="Breakdown Alert", message="Price broke support!") // Optional: Highlight breakout candles plotshape(series=close > resistance, location=location.abovebar, color=color.red, style=shape.triangleup, title="Resistance Break") plotshape(series=close < support, location=location.belowbar, color=color.green, style=shape.triangledown, title="Support Break") // Advance/Decline Indicator advancing = ta.change(close) > 0 declining = ta.change(close) < 0 advance_decline = ta.cum(advancing ? 1 : declining ? -1 : 0) // Plot Advance/Decline Line plot(advance_decline, title="Advance/Decline Line", color=color.blue, linewidth=2) // Buy & Sell Signal Logic buySignal = ta.crossover(close, support) // Buy when price crosses above support sellSignal = ta.crossunder(close, resistance) // Sell when price crosses below resistance // Plot Buy & Sell Labels labelSize = size.small plotshape(series=buySignal, location=location.belowbar, color=color.blue, style=shape.labelup, title="Buy Signal", size=labelSize, text="BUY") plotshape(series=sellSignal, location=location.abovebar, color=color.blue, style=shape.labeldown, title="Sell Signal", size=labelSize, text="SELL") // Display Buy & Sell Labels bgcolor(buySignal ? color.green : na, transp=85) bgcolor(sellSignal ? color.red : na, transp=85) // Alerts for Buy & Sell Signals alertcondition(buySignal, title="Buy Alert", message="Buy Signal Triggered!") alertcondition(sellSignal, title="Sell Alert", message="Sell Signal Triggered!") 代码解读(每行注释) 下面是每行代码的详细注释: lookback = input(20, title="Lookback Period for Levels"): 定义用户输入的回溯周期,用于计算支撑和阻力的最高点和最低点。 resistance = ta.highest(high, lookback): 计算过去20个周期内的最高价作为阻力位。 support = ta.lowest(low, lookback): 计算过去20个周期内的最低价作为支撑位。 plot(resistance, title="Resistance", color=color.red, linewidth=2, style=plot.style_stepline): 在图表上绘制阻力线,颜色为红色。 plot(support, title="Support", color=color.green, linewidth=2, style=plot.style_stepline): 在图表上绘制支撑线,颜色为绿色。 alertcondition(close > resistance, title="Breakout Alert", message="Price broke resistance!"): 当价格突破阻力位时触发突破警报。 alertcondition(close < support, title="Breakdown Alert", message="Price broke support!"): 当价格跌破支撑位时触发跌破警报。 plotshape(series=close > resistance, location=location.abovebar, color=color.red, style=shape.triangleup, title="Resistance Break"): 在突破阻力位的蜡烛上方绘制一个红色的三角形。 plotshape(series=close < support, location=location.belowbar, color=color.green, style=shape.triangledown, title="Support Break"): 在跌破支撑位的蜡烛下方绘制一个绿色的三角形。 advance_decline = ta.cum(advancing ? 1 : declining ? -1 : 0): 计算上涨和下跌的累计数量,用于绘制进退指标。 plot(advance_decline, title="Advance/Decline Line", color=color.blue, linewidth=2): 绘制进退指标。 buySignal = ta.crossover(close, support): 定义买入信号,当价格突破支撑位时触发买入信号。 sellSignal = ta.crossunder(close, resistance): 定义卖出信号,当价格跌破阻力位时触发卖出信号。 plotshape(series=buySignal, location=location.belowbar, color=color.blue, style=shape.labelup, title="Buy Signal", size=labelSize, text="BUY"): 在买入信号处绘制一个标签。 plotshape(series=sellSignal, location=location.abovebar, color=color.blue, style=shape.labeldown, title="Sell Signal", size=labelSize, text="SELL"): 在卖出信号处绘制一个标签。 bgcolor(buySignal ? color.green : na, transp=85): 在买入信号发生时改变背景颜色。 bgcolor(sellSignal ? color.red : na, transp=85): 在卖出信号发生时改变背景颜色。 alertcondition(buySignal, title="Buy Alert", message="Buy Signal Triggered!"): 创建买入警报。 alertcondition(sellSignal, title="Sell Alert", message="Sell Signal Triggered!"): 创建卖出警报。 代码优缺点 优点 简单易用,适用于不同的市场。 能够有效地识别支撑、阻力以及市场进退情况。 提供了买卖信号,适合做短期交易决策。 集成了警报功能,可以实时监控市场。 缺点 依赖回溯周期,回溯期过短可能会导致信号噪音,过长则可能滞后。 未考虑其他指标的结合,可能会出现错误信号。 不适用于所有市场条件,特别是在震荡市场中可能产生较多虚假信号。 代码优化建议 可以加入更多的趋势过滤条件,例如与其他技术指标结合使用。 增加动态调整回溯期的功能,允许用户根据市场的变化自动调整。 增强警报功能,例如通过邮件或消息推送等方式通知。 代码的应用品种及参数建议 这个脚本适用于各类金融品种,例如股票、期货和外汇等。参数建议如下: 回溯期 (Lookback Period): 根据品种的波动性调整,期货和外汇适合使用较短的回溯期。 支撑和阻力线: 可以适当调整显示的线条颜色和粗细,以适应不同的图表风格。 进退指标: 对于较活跃的市场,建议采用较短时间的进退指标。 代码调试方法 调试时可以通过以下方法优化: 观察买卖信号是否频繁或过于滞后,可以调整回溯期或灵敏度。 检查警报功能是否正常工作,确保在突破支撑和阻力时能够及时触发。 来源:今日美股网lg...