Pine Script编码技巧:如何让RSI超卖策略仅在股价高于200MA时提交买单
Pine Script 交易策略实现代码(V5版本)
以下是完整可直接运行的代码,已内置200MA过滤逻辑,仅当价格高于200MA时RSI超卖信号才会触发买入:
//@version=5 strategy("RSI超卖+200MA过滤买入策略", overlay=true, initial_capital=10000, default_qty_value=1000, commission_type=strategy.commission.percent, commission_value=0.1) // 参数设置(可根据自身需求调整) rsi_period = input.int(14, title="RSI周期") rsi_oversold_threshold = input.int(30, title="RSI超卖阈值") ma_length = input.int(200, title="均线周期") ma_type = input.string("SMA", title="均线类型", options=["SMA", "EMA"]) // 计算指标 ma200 = ma_type == "SMA" ? ta.sma(close, ma_length) : ta.ema(close, ma_length) rsi_value = ta.rsi(close, rsi_period) // 信号条件定义 // 价格高于200MA过滤条件 price_above_ma = close > ma200 // RSI从超卖区间向上突破信号(避免超卖区间重复触发,若需要只要在超卖区间就触发可改为 rsi_value < rsi_oversold_threshold) rsi_buy_signal = ta.crossover(rsi_value, rsi_oversold_threshold) // 合并条件:两个条件同时满足才触发买入 long_condition = price_above_ma and rsi_buy_signal // 执行买入订单 if long_condition strategy.entry("多单入场", strategy.long) // 可视化指标 plot(ma200, color=color.new(color.blue, 0), title="200MA", linewidth=2) plot(rsi_value, "RSI", display=display.data_window, color=color.purple) hline(rsi_oversold_threshold, "超卖线", display=display.data_window, color=color.red)
关键逻辑说明
- 200MA过滤通过
price_above_ma变量实现,不满足该条件时,即使RSI触发超卖信号也不会提交买入订单 - 代码内置了参数调整面板,你可以直接在TradingView指标设置页修改周期、阈值、均线类型,不需要改代码
- 如果需要用开盘价/最高价/最低价判断是否在均线上方,只需把
price_above_ma = close > ma200中的close替换为open/high/low即可
内容的提问来源于stack exchange,提问作者nezas4444
相关产品推荐
相关产品推荐

