You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何向现有交易策略做多条件添加ADX指标+DI高于28的判断条件

修改步骤
  • 调用Pine Script内置的dmi()函数获取定向运动指标的相关数值,该函数会同时返回+DI、-DI、ADX三个计算结果
  • 新增ADX计算周期、+DI阈值的可配置输入项,方便后续参数优化
  • 在原有做多触发条件longCondition中追加+DI高于28的判断规则,两个条件同时满足才会触发做多入场信号
完整修改后代码
//突破交易系统在Nifty和BankNifty的周线、日线图表中表现最优
//@version=4
strategy("Donchain BO",shorttitle = "DBO",default_qty_type = strategy.percent_of_equity,default_qty_value = 100, overlay=true)
length = input(20, minval=1)
exit = input(1, minval=1, maxval=2,title = "Exit Option") // 选项1使用下轨作为离场条件;选项2使用中线作为离场条件

// ---------------新增ADX相关代码开始---------------
adx_length = input(14, minval=1, title="ADX计算周期")
di_plus_threshold = input(28, minval=1, title="+DI触发阈值")
// 计算DMI指标,返回+DI、-DI、ADX三个值
[di_plus, di_minus, adx_val] = dmi(adx_length, adx_length)
// ---------------新增ADX相关代码结束---------------

lower = lowest(length)
upper = highest(length)
basis = avg(upper, lower)
l = plot(lower, color=color.blue)
u = plot(upper, color=color.blue)
plot(basis, color=color.orange)
fill(u, l, color=color.blue)

// 修改做多触发条件,新增+DI高于阈值的判断
longCondition = crossover(close,upper[1]) and di_plus > di_plus_threshold
if (longCondition)
    strategy.entry("Long", strategy.long)
if(exit==1)
    if (crossunder(close,lower[1]))
        strategy.close("Long")
if(exit==2) 
    if (crossunder(close,basis[1]))
        strategy.close("Long")

// 可选:副图显示+DI和阈值线,不需要可以删除
plot(di_plus, color=color.green, title="+DI数值", display=display.data_window)
hline(28, color=color.red, title="+DI阈值线")
补充说明
  • ADX的计算周期默认用行业通用的14周期,你可以根据自己的交易周期调整参数
  • 阈值28做成了可配置输入项,后续回测优化时不需要改核心逻辑,直接调整参数即可

内容的提问来源于stack exchange,提问作者akash bari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 01:09:03