策略作者:投机实验室
@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 = "Neighbors", minval=1, maxval=100,inline="AI", group="AI Settings")
n_ = input.int(10, title ="Data", minval=1, maxval=100,inline="AI", group="AI Settings")
n = math.max(k,n_)
DEMA_len1 = input.int(144, "DEMA Length 1")
DEMA_len2 = input.int(169, "DEMA Length 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="Price Trend", minval=2, maxval=500, step=10,inline="AITrend", group="AI Trend")
KNN_STLen = input.int(100, title="Prediction Trend", minval=2, maxval=500, step=10, inline="AITrend", group="AI Trend")
aisignals = input.bool(true,title="AI Trend Signals",inline="signal", group="AI Trend")
len = input.int(10, "Length", minval=1,inline="SuperTrend", group="Super Trend Settings")
factor = input.float(2.5,step=.1,inline="SuperTrend", group="Super Trend Settings")
maSrc = input.string("WMA","Moving Average Source",["SMA","EMA","WMA","RMA","VWMA"],inline="", group="Super Trend Settings")
upCol = input.color(color.lime,"Bullish Color",inline="col", group="Super Trend Coloring")
dnCol = input.color(color.red,"Bearish Color",inline="col", group="Super Trend Coloring")
neCol = input.color(color.blue,"Neutral Color",inline="col", group="Super Trend Coloring")
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="Volume Super Trend AI")
upTrend = plot(superTrend==lowerBand?current_superTrend, title="Up Volume Super Trend AI", color=col, style=plot.style_linebr)
Middle = plot((open + close) / 2, display=display.none, editable=false)
downTrend = plot(superTrend==upperBand?current_superTrend, title="Down Volume Super Trend AI", color=col, style=plot.style_linebr)
fill_col = color.new(col,90)
fill(Middle, upTrend, fill_col, fillgaps=false,title="Up Volume Super Trend AI")
fill(Middle, downTrend, fill_col, fillgaps=false, title="Down Volume Super Trend 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("Long", strategy.long)
strategy.exit("Take Profit Long", "Long", limit=longTakeProfit)
strategy.exit("Stop Loss Long", "Long", stop=longStopLoss)
if (shortCondition)
strategy.entry("Short", strategy.short)
strategy.exit("Take Profit Short", "Short", limit=shortTakeProfit)
strategy.exit("Stop Loss Short", "Short", stop=shortStopLoss)