策略作者:投机实验室
@LabSpeculation
相关帖子:https://x.com/LabSpeculation/status/1702504457689333884?s=20
我就是用 TV 的 Pine 语言实现出来回测而已
效果展示:30mK 线上效果惊人……
胜率 98% ………………
声明:我不对脚本实际运行后带来的交易后果负责啊 球球了
//@version=5
strategy ("AI 交易", overlay=true, shorttitle="AI 交易策略")
k = input.int (3, title = "邻居", minval=1, maxval=100,inline="AI", group="AI 设置")
n_ = input.int (10, title ="数据", minval=1, maxval=100,inline="AI", group="AI 设置")
n = math.max(k,n_)
DEMA_len1 = input.int (144, "DEMA 长度 1")
DEMA_len2 = input.int (169, "DEMA 长度 2")
EMA_144 = ta.ema(close, DEMA_len1)
EMAofEMA_144 = ta.ema(EMA_144, DEMA_len1)
DEMA_144 = 2 * EMA_144 - EMAofEMA_144
EMA_169 = ta.ema(close, DEMA_len2)
EMAofEMA_169 = ta.ema(EMA_169, DEMA_len2)
DEMA_169 = 2 * EMA_169 - EMAofEMA_169
KNN_PriceLen = input.int (20, title="价格趋势", minval=2, maxval=500, step=10,inline="AITrend", group="AI 趋势")
KNN_STLen = input.int (100, title="预测趋势", minval=2, maxval=500, step=10, inline="AITrend", group="AI 趋势")
aisignals = input.bool (true,title="AI 趋势信号",inline="signal", group="AI 趋势")
len = input.int (10, "长度", minval=1,inline="SuperTrend", group="超级趋势设置")
factor = input.float (2.5,step=.1,inline="SuperTrend", group="超级趋势设置")
maSrc = input.string ("WMA","移动平均来源",["SMA","EMA","WMA","RMA","VWMA"],inline="", group=" 超级趋势设置 ")
upCol = input.color (color.lime,"看涨颜色",inline="col", group="超级趋势着色")
dnCol = input.color (color.red,"看跌颜色",inline="col", group="超级趋势着色")
neCol = input.color (color.blue,"中性颜色",inline="col", group="超级趋势着色")
vwma = switch maSrc
"SMA" => ta.sma(closevolume, len) / ta.sma(volume, len)
"EMA" => ta.ema(closevolume, len) / ta.ema(volume, len)
"WMA" => ta.wma(closevolume, len) / ta.wma(volume, len)
"RMA" => ta.rma(closevolume, len) / ta.rma(volume, len)
"VWMA" => ta.vwma(close*volume, len) / ta.vwma(volume, len)
atr = ta.atr(len)
upperBand = vwma + factor * atr
lowerBand = vwma - factor * atr
prevLowerBand = nz(lowerBand[1])
prevUpperBand = nz(upperBand[1])
lowerBand := lowerBand > prevLowerBand or close[1] < prevLowerBand ? lowerBand : prevLowerBand
upperBand := upperBand < prevUpperBand or close[1] > prevUpperBand ? upperBand : prevUpperBand
int direction = na
float superTrend = na
prevSuperTrend = superTrend[1]
if na(atr[1])
direction := 1
else if prevSuperTrend == prevUpperBand
direction := close > upperBand ? -1 : 1
else
direction := close < lowerBand ? 1 : -1
superTrend := direction == -1 ? lowerBand : upperBand
price = ta.wma(close,KNN_PriceLen)
sT = ta.wma(superTrend,KNN_STLen)
data = array.new_float(n)
labels = array.new_int(n)
for i = 0 to n - 1
data.set(i, superTrend[i])
label_i = price[i] > sT[i] ? 1 : 0
labels.set(i, label_i)
distance(x1, x2) =>
math.abs(x1 - x2)
knn_weighted(data, labels, k, x) =>
n1 = data.size()
distances = array.new_float(n1)
indices = array.new_int(n1)
for i = 0 to n1 - 1
x_i = data.get(i)
dist = distance(x, x_i)
distances.set(i, dist)
indices.set(i, i)
for i = 0 to n1 - 2
for j = 0 to n1 - i - 2
if distances.get(j) > distances.get(j + 1)
tempDist = distances.get(j)
distances.set(j, distances.get(j + 1))
distances.set(j + 1, tempDist)
tempIndex = indices.get(j)
indices.set(j, indices.get(j + 1))
indices.set(j + 1, tempIndex)
weighted_sum = 0.
total_weight = 0.
for i = 0 to k - 1
index = indices.get(i)
label_i = labels.get(index)
weight_i = 1 / (distances.get(i) + 1e-6)
weighted_sum += weight_i * label_i
total_weight += weight_i
weighted_sum / total_weight
current_superTrend = superTrend
label_ = knn_weighted(data, labels, k, current_superTrend)
col = label_ == 1?upCol:label_ == 0?dnCol
plot (current_superTrend, color=col, title="成交量超级趋势 AI")
upTrend = plot(superTrend==lowerBand?current_superTrend, title="上涨成交量超级趋势 AI", color=col, style=plot.style_linebr)
Middle = plot((open + close) / 2, display=display.none, editable=false)
downTrend = plot(superTrend==upperBand?current_superTrend, title="下跌成交量超级趋势 AI", color=col, style=plot.style_linebr)
fill_col = color.new(col,90)
fill (Middle, upTrend, fill_col, fillgaps=false,title="上涨成交量超级趋势 AI")
fill (Middle, downTrend, fill_col, fillgaps=false, title="下跌成交量超级趋势 AI")
Start_TrendUp = col==upCol and (col[1]!=upCol or col[1]==neCol) and aisignals
Start_TrendDn = col==dnCol and (col[1]!=dnCol or col[1]==neCol) and aisignals
TrendUp = direction == -1 and direction[1] == 1 and label_ == 1 and aisignals
TrendDn = direction == 1 and direction[1] ==-1 and label_ == 0 and aisignals
longCondition = (Start_TrendUp or TrendUp) and close > DEMA_144 and DEMA_144 > DEMA_169 and strategy.position_size == 0
shortCondition = (Start_TrendDn or TrendDn) and close < DEMA_144 and DEMA_144 < DEMA_169 and strategy.position_size == 0
longStopLoss = current_superTrend
longTakeProfit = close + (close - longStopLoss)
shortStopLoss = current_superTrend
shortTakeProfit = close - (shortStopLoss - close)
if (longCondition)
strategy.entry ("多头", strategy.long)
strategy.exit ("止盈多头", "多头", limit=longTakeProfit)
strategy.exit ("止损多头", "多头", stop=longStopLoss)
if (shortCondition)
strategy.entry ("空头", strategy.short)
strategy.exit ("止盈空头", "空头", limit=shortTakeProfit)
strategy.exit ("止损空头", "空头", stop=shortStopLoss)