ADX趋势线突破交易系统

一套原创的ADX趋势突破策略。

1.ADX的算法:

TR = AvgTrueRange(14);

HD = HIGH[1]-HIGH[2];

LD = LOW[2]-LOW[1];

DMP = XAverage(IIF(HD>0&&HD>LD,HD,0),14);

DMM = XAverage(IIF(LD>0&&LD>HD,LD,0),14);

PDI = DMP*100/TR[1];

MDI = DMM*100/TR[1];

ADX = ABS(MDI-PDI)/(MDI+PDI)*100;

SADXR = Average(ADX,6);

  1. 策略思想

第一,进场条件

1)SADXR> ADXVALUE,ADX大于ADX平均线

2)PDI > MDI,做多要求PDI在MDI上方

3)PDI < MDI,做空要求PDI在MDI下方

4)High>=h[N],做多要求突破上沿趋势线进场。

5)Low<= l[N],做空要求突破下沿趋势进场。

第二,出场条件

1)初始止损出场,当前进场点位的N%。

2)跟踪止损出场,当前最高价的M%。

3.策略开发平台为Traderblazer,信号和指标如下图。

  1. 回测结果

螺纹钢

聚丙烯

源代码:

https://www.fageka.com/i/hhmyMLg4258

//@version=6 strategy("复合波段交易策略[优化版]", overlay=true, pyramiding=1, initial_capital=100000, currency=currency.USD, calc_on_every_tick=false) // 防止重复计算 // ======= 输入参数模块 ======= fastLength = input.int(10, "短期均线周期", minval=1, group="趋势参数") slowLength = input.int(20, "长期均线周期", minval=1, group="趋势参数") rsiLength = input.int(14, "RSI周期", minval=1, maxval=100, group="震荡指标") bbLength = input.int(20, "布林带周期", minval=1, group="波动通道") mult = input.float(2.0, "标准差倍数", step=0.1, minval=1.5, maxval=3.0, group="波动通道") stopLossPct = input.float(1.5, "止损比例 (%)", step=0.1, minval=0.5, maxval=5)/100 takeProfitPct = input.float(3.5, "止盈比例 (%)", step=0.1, minval=1.0, maxval=10)/100 tradeSizePct = input.float(15, "单笔风险比例 (%)", step=0.5, minval=5, maxval=30)/100 // ======= 核心指标计算 ======= // EMA双线系统 fastMA = ta.ema(close, fastLength) slowMA = ta.ema(close, slowLength) // RSI动量指标 rsi = ta.rsi(math.min(close, high), rsiLength) // 防止极端值 // 布林带通道(使用标准函数) [bbUpper, bbMiddle, bbLower] = ta.bollinger(close, bbLength, mult) // ======= 信号生成系统 ======= bullishCross = ta.crossover(fastMA, slowMA) bearishCross = ta.crossunder(fastMA, slowMA) // 增强型过滤条件 oversoldCondition = rsi <= 35 and rsi > rsi[1] // RSI回升中 overboughtCondition = rsi >= 65 and rsi < rsi[1] // RSI回落中 priceBelowBB = low < bbLower and close > bbLower // 假突破过滤 priceAboveBB = high > bbUpper and close < bbUpper longCondition = bullishCross and (oversoldCondition or priceBelowBB) shortCondition = bearishCross and (overboughtCondition or priceAboveBB) // ======= 风险管理模块 ======= var float positionSize = na positionSize := strategy.equity * tradeSizePct / close // 止损止盈计算(入场时锁定) var float stopLossLevel = na var float takeProfitLevel = na if (longCondition or shortCondition) stopLossLevel := close * (1 - (strategy.position_size > 0 ? stopLossPct : -stopLossPct)) takeProfitLevel := close * (1 + (strategy.position_size > 0 ? takeProfitPct : -takeProfitPct)) // ======= 交易执行系统 ======= // 多单逻辑 if (longCondition and strategy.position_size <= 0) strategy.entry("Long", strategy.long, qty=math.floor(positionSize)) strategy.exit("Exit Long", "Long", stop=stopLossLevel, limit=takeProfitLevel, comment="多头平仓") // 空单逻辑 if (shortCondition and strategy.position_size >= 0) strategy.entry("Short", strategy.short, qty=math.floor(positionSize)) strategy.exit("Exit Short", "Short", stop=stopLossLevel, limit=takeProfitLevel, comment="空头平仓") // ======= 可视化增强系统 ======= // 均线系统 plot(fastMA, "快线", #00BCD4, 2, plot.style_linebr) plot(slowMA, "慢线", #FF9800, 2, plot.style_linebr) // 布林通道填充 bbFill = plot(bbUpper, "布林上轨", #2196F3, 1, transp=70, display=display.none) plot(bbLower, "布林下轨", #4CAF50, 1, transp=70, display=display.none) fill(bbFill, bbFill, color=color.new(#2196F3, 90), title="通道填充") // 信号标记 plotshape(longCondition, "买入信号", shape.triangleup, location.belowbar, #4CAF50, 0, "", text="B") plotshape(shortCondition, "卖出信号", shape.triangledown, location.abovebar, #F44336, 0, "", text="S") // ======= 动态监控面板 ======= var table infoTable = table.new(position.top_right, 3, 1, bgcolor=color.rgb(33, 33, 33)) if barstate.islast table.cell(infoTable, 0, 0, "策略状态:" + (strategy.position_size > 0 ? "▶ 持多单" : strategy.position_size < 0 ? "▶ 持空单" : "▷ 中立"), text_color=strategy.position_size != 0 ? color.yellow : color.white) table.cell(infoTable, 1, 0, "RSI:" + str.tostring(rsi, "#.##") + " | 通道偏离:" + str.tostring((close-bbMiddle)/bbMiddle*100, "+#.##") + "%", text_color=color.silver) table.cell(infoTable, 2, 0, "止损:" + str.tostring(stopLossLevel, "#.##") + " | 止盈:" + str.tostring(takeProfitLevel, "#.##"), text_color=color.gray) 2:Syntax error at input "end of line without line continuation"
03-12
<think>好的,我现在需要帮助用户解决他们提供的Pine Script代码中的语法错误。首先,我会仔细阅读用户提供的代码,寻找可能的语法错误。用户提到错误是“Syntax error at input 'end of line without line continuation'”,这通常意味着在代码的某处存在不正确的换行或缺少的续行符。 首先,我会检查代码的结构。注意到代码中有一个长字符串的拼接,尤其是在动态监控面板部分的`table.cell`函数调用中。例如: ```pine table.cell(infoTable, 1, 0, "RSI:" + str.tostring(rsi, "#.##") + " | 通道偏离:" + str.tostring((close-bbMiddle)/bbMiddle*100, "+#.##") + "%", text_color=color.silver) ``` 这里,用户可能在没有正确使用续行符的情况下断行了字符串。在Pine Script中,如果要在多行中编写一个长字符串,需要使用`+\`来连接各行的字符串,否则会导致语法错误。 另一个需要注意的地方是最后一个`table.cell`调用: ```pine table.cell(infoTable, 2, 0, "止损:" + str.tostring(stopLossLevel, "#.##") + " | 止盈:" + str.tostring(takeProfitLevel, "#.##"), text_color=color.gray) 2:Syntax error at input "end of line without line continuation" ``` 这里末尾有一个多余的“2:Syntax error...”文本,这显然是用户将错误信息误写到了代码中,导致代码无法正确解析。需要删除这部分。 此外,检查所有字符串拼接的地方,确保每一行的末尾都正确使用了`+`和`\`来连接下一行。例如,在Pine Script中,虽然可以使用`+`来连接字符串,但换行时可能需要使用反斜杠`\`来续行,尤其是在参数列表中。 总结可能的问题点: 1. 字符串拼接时换行未正确使用续行符`\`。 2. 代码末尾错误地包含了错误信息文本。 3. 检查所有函数调用的括号是否匹配,参数是否正确分隔。 修正步骤: 1. 在长字符串拼接的每一行末尾添加`+\`,确保正确换行。 2. 删除末尾多余的“2:Syntax error...”文本。 3. 确保所有函数调用的参数正确闭合括号,尤其是`table.cell`的参数是否正确分隔。 修改后的代码部分应该如下: ```pine table.cell(infoTable, 1, 0, "RSI:" + str.tostring(rsi, "#.##") + \ " | 通道偏离:" + str.tostring((close - bbMiddle) / bbMiddle * 100, "+#.##") + "%", \ text_color=color.silver) table.cell(infoTable, 2, 0, "止损:" + str.tostring(stopLossLevel, "#.##") + \ " | 止盈:" + str.tostring(takeProfitLevel, "#.##"), \ text_color=color.gray) ``` 通过添加反斜杠`\`来续行,并删除多余的文本,应该可以解决语法错误。同时,检查其他可能的括号不匹配或参数分隔问题,确保代码结构正确。</think>这个策略代码存在两个主要语法错误需要修正: $$修正方案:$$ 1. **字符串拼接续行符缺失**(关键错误): ```pine // 原错误代码: table.cell(infoTable, 1, 0, "RSI:" + str.tostring(rsi, "#.##") + " | 通道偏离:" + str.tostring((close-bbMiddle)/bbMiddle*100, "+#.##") + "%", text_color=color.silver) // 修正后代码(增加\续行符): table.cell(infoTable, 1, 0, "RSI:" + str.tostring(rsi, "#.##") + \ " | 通道偏离:" + str.tostring((close-bbMiddle)/bbMiddle*100, "+#.##") + "%", \ text_color=color.silver) ``` 2. **代码尾部残留错误信息**(致命错误): ```pine // 原错误代码末尾: text_color=color.gray) 2:Syntax error at input "end of line without line continuation" // 修正后代码: text_color=color.gray) ``` $$策略优化建议$$(修正后可选择实施): 1. **增加趋势过滤**:建议在入场条件中增加`ta.adx(14) > 25`判断,避免在震荡市频繁交易 2. **动态仓位管理**:可将仓位计算改为: $$positionSize = math.floor(strategy.equity \times tradeSizePct / (close \times atr(14)/close))$$ 实现波动率调整头寸 3. **时间周期过滤**:增加`timeframe.isdaily`判断,避免在低时间周期误触发信号 $$修正后完整代码$$(关键部分): ```pine // ======= 动态监控面板 ======= var table infoTable = table.new(position.top_right, 3, 1, bgcolor=color.rgb(33, 33, 33)) if barstate.islast table.cell(infoTable, 0, 0, "策略状态:" + (strategy.position_size > 0 ? "▶ 持多单" : strategy.position_size < 0 ? "▶ 持空单" : "▷ 中立"), text_color=strategy.position_size != 0 ? color.yellow : color.white) table.cell(infoTable, 1, 0, "RSI:" + str.tostring(rsi, "#.##") + \ " | 通道偏离:" + str.tostring((close-bbMiddle)/bbMiddle*100, "+#.##") + "%", \ text_color=color.silver) table.cell(infoTable, 2, 0, "止损:" + str.tostring(stopLossLevel, "#.##") + \ " | 止盈:" + str.tostring(takeProfitLevel, "#.##"), \ text_color=color.gray) ``` 修正后的代码通过: 1. 使用`\`续行符正确处理多行字符串 2. 删除代码尾部残留的错误描述文本 3. 保持所有函数参数的正确结构 建议在TV的策略测试器中先进行回测,验证修正效果后再投入实盘。回测时建议关注2018-2022年包含不同市场周期的数据,并使用Walk Forward分析方法验证参数鲁棒性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值