修改错误//@version=2
strategy("增强AI交易策略", overlay=true, pyramiding=1, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.1)
// 输入参数
stopLoss = input(2, title="止损百分比(%)")
takeProfit = input(4, title="止盈百分比(%)")
maFastLen = input(8, title="快速EMA周期")
maSlowLen = input(21, title="慢速EMA周期")
rsiLen = input(14, title="RSI周期")
macdFast = input(12, title="MACD快线")
macdSlow = input(26, title="MACD慢线")
macdSignal = input(9, title="MACD信号线")
atrLen = input(14, title="ATR周期")
adxLen = input(14, title="ADX周期")
// 移动平均线
emaFast = ema(close, maFastLen)
emaSlow = ema(close, maSlowLen)
maTrend = emaFast > emaSlow
// RSI
rsi = rsi(close, rsiLen)
rsiBullish = rsi > 50
rsiBearish = rsi < 50
// MACD
[macdLine, signalLine, _] = macd(close, macdFast, macdSlow, macdSignal)
macdBullish = macdLine > signalLine
macdHistogram = macdLine - signalLine
// ATR用于波动性分析
atr = atr(atrLen)
volatility = atr / close * 100
// ADX用于趋势强度
adx = adx(adxLen)
adxTrend = adx > 25
// 成交量分析
volumeSma = sma(volume, 20)
volumeHigh = volume > volumeSma * 1.2
// 价格行为
higherHigh = high > high[1]
higherLow = low > low[1]
lowerHigh = high < high[1]
lowerLow = low < low[1]
// 多时间框架分析
rsiDaily = security(tickerid, 'D', rsi(close, rsiLen))
rsiTrend = rsiDaily > 50
// AI信号评分系统
score = 0
// 趋势得分 (最大30分)
if maTrend
score := score + 15
if adxTrend
score := score + 15
// 动量得分 (最大30分)
if macdBullish
score := score + 15
if rsiBullish
score := score + 15
// 成交量确认 (最大20分)
if volumeHigh and close > open
score := score + 20
// 价格行为得分 (最大20分)
if higherHigh and higherLow
score := score + 20 AICoin自定义指标函数文档 - AiCoin https://www.aicoin.com/article/355796.html
// 多时间框架确认 (额外加分)
if rsiTrend
score := score + 10
// 风险管理 - 根据波动性调整仓位
positionSize = volatility < 2 ? strategy.equity * 0.15 : volatility < 5 ? strategy.equity * 0.1 : strategy.equity * 0.05
// 生成交易信号
buySignal = score >= 70 and not maTrend[1] and maTrend
sellSignal = score < 30 and maTrend[1] and not maTrend
// 止损止盈计算
longStop = strategy.position_avg_price * (1 - stopLoss/100)
longTakeProfit = strategy.position_avg_price * (1 + takeProfit/100)
// 执行交易
if (buySignal)
strategy.entry("Long", strategy.long, qty=positionSize)
if (strategy.position_size > 0)
if (low <= longStop)
strategy.close("Long", comment="止损")
else if (high >= longTakeProfit)
strategy.close("Long", comment="止盈")
else if (sellSignal)
strategy.close("Long", comment="趋势反转")
// 可视化
plot(emaFast, color=blue, linewidth=2, title="快速EMA")
plot(emaSlow, color=red, linewidth=2, title="慢速EMA")
// 信号标记
plotshape(buySignal, style=shape.triangleup, location=location.belowbar, color=green, size=size.small, title="买入信号")
plotshape(sellSignal, style=shape.triangledown, location=location.abovebar, color=red, size=size.small, title="卖出信号")
// 背景色
bgcolor(buySignal ? lime : sellSignal ? red : na, transp=80)
// 显示分数
var label scoreLabel = na
if barstate.islast
scoreLabel := label.new(bar_index, high, text="AI分数: " + tostring(score), color=score >= 70 ? green : score < 30 ? red : gray, style=label.style_label_down, textcolor=white)
// 警报
alertcondition(buySignal, title="AI买入信号", message="AI策略建议买入")
alertcondition(sellSignal, title="AI卖出信号", message="AI策略建议卖出")
最新发布