oklover

oklover

Ai+交易量+超級趨勢指標組成的策略腳本

策略作者:投機實驗室
@LabSpeculation
相關帖子:https://x.com/LabSpeculation/status/1702504457689333884?s=20
我就是用 TV 的 Pine 語言實現出來回測而已
效果展示:30mK 線上效果驚人……
勝率 98% ………………
image
聲明:我不對腳本實際運行後帶來的交易後果負責啊 球球了

//@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(close
volume, len) / ta.ema(volume, len)
"WMA" => ta.wma(closevolume, len) / ta.wma(volume, len)
"RMA" => ta.rma(close
volume, 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)

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。