本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
strategy("Sadashi Market Reversion Strategy",
overlay = true,
initial_capital = 10000,
default_qty_type = strategy.percent_of_equity,
default_qty_value = 10,
commission_type = strategy.commission.percent,
commission_value = 0.075,
pyramiding = 0)
//====================================================
// 1. SYSTEM INPUTS
//====================================================
groupPrice = "1. Price Engine"
zLength = input.int(20, "Price Z-Score Length", minval = 5, group = groupPrice)
priceWeight = input.float(30.0, "Price Weight (0-100)", minval = 0, maxval = 100, step = 5, group = groupPrice)
priceScale = input.float(1.5, "Price Exponential Scale (Divisor)", minval = 0.1, step = 0.1, group = groupPrice)
minZ = input.float(1.5, "Minimum |Z| Price Floor", step = 0.1, group = groupPrice)
groupVol = "2. Volatility Engine"
volLength = input.int(20, "Volatility Length", minval = 5, group = groupVol)
volNorm = input.int(100, "Volatility Normalization Length", minval = 20, group = groupVol)
volWeight = input.float(25.0, "Volatility Weight (0-100)", minval = 0, maxval = 100, step = 5, group = groupVol)
volScale = input.float(1.2, "Volatility Exp Scale (Divisor)", minval = 0.1, step = 0.1, group = groupVol)
minVolZ = input.float(1.0, "Minimum |Z| Volatility Floor", step = 0.1, group = groupVol)
groupPart = "3. Participation Engine"
partNorm = input.int(100, "Participation Normalization Length", minval = 20, group = groupPart)
partWeight = input.float(25.0, "Participation Weight (0-100)", minval = 0, maxval = 100, step = 5, group = groupPart)
partScale = input.float(2.0, "Participation Exp Scale (Divisor)", minval = 0.1, step = 0.1, group = groupPart)
minPartZ = input.float(0.5, "Minimum |Z| Participation Floor", step = 0.1, group = groupPart)
groupTrend = "4. Trend Engine (Pure Price Structural Slope)"
trendLength = input.int(60, "Trend Lookback Length", minval = 10, group = groupTrend)
trendWeight = input.float(20.0, "Trend Weight (0-100)", minval = 0, maxval = 100, step = 5, group = groupTrend)
trendScale = input.float(20.0, "Trend Slope Scale Factor", minval = 1.0, step = 1.0, group = groupTrend)
minTrendLvl = input.float(50.0, "Minimum Trend Score Floor (0-100)", minval = 0, maxval = 100, step = 5, group = groupTrend)
groupSystem = "5. Thresholds & Bidirectional States"
edgeBuy = input.float(70.0, "LONG Entry Edge Threshold", step = 1.0, group = groupSystem)
edgeExitLong = input.float(45.0, "LONG Exit Edge Threshold", step = 1.0, group = groupSystem)
edgeSell = input.float(70.0, "SHORT Entry Edge Threshold", step = 1.0, group = groupSystem)
edgeExitShort= input.float(45.0, "SHORT Exit Edge Threshold", step = 1.0, group = groupSystem)
groupDisplay = "Display Settings"
showSignals = input.bool(true, "Show Entry/Exit Signals", group = groupDisplay)
showPanel = input.bool(true, "Show Information Panel", group = groupDisplay)
showEdge = input.bool(true, "Show Edge Curve", group = groupDisplay)
panelPosition = input.string("Top Right", "Panel Position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group = groupDisplay)
// INPUTS DE COLOR DE CONFIGURACIÓN
colorLong = input.color(#009688, "LONG Entry Color", group = groupDisplay)
colorExitL = input.color(#ff9800, "LONG Exit Color", group = groupDisplay)
colorShort = input.color(#e91e63, "SHORT Entry Color", group = groupDisplay)
colorExitS = input.color(#ff9800, "SHORT Exit Color", group = groupDisplay)
//====================================================
// 2. PRICE ENGINE
//====================================================
priceMean = ta.sma(close, zLength)
priceDev = ta.stdev(close, zLength)
zPrice = priceDev == 0 ? 0.0 : (close - priceMean) / priceDev
scorePrice = 100.0 * (1.0 - math.exp(-math.abs(zPrice) / priceScale))
//====================================================
// 3. VOLATILITY ENGINE
//====================================================
ret = (close - close[1]) / close[1]
volatility = ta.stdev(ret, volLength)
volMean = ta.sma(volatility, volNorm)
volStd = ta.stdev(volatility, volNorm)
zVol = volStd == 0 ? 0.0 : (volatility - volMean) / volStd
scoreVol = 100.0 * (1.0 - math.exp(-math.abs(zVol) / volScale))
//====================================================
// 4. PARTICIPATION ENGINE (Dollar Volume)
//====================================================
dollarVolume = close * volume
partMean = ta.sma(dollarVolume, partNorm)
partStd = ta.stdev(dollarVolume, partNorm)
zPart = partStd == 0 ? 0.0 : (dollarVolume - partMean) / partStd
scorePart = 100.0 * (1.0 - math.exp(-math.abs(zPart) / partScale))
//====================================================
// 5. TREND ENGINE v5 (Structural Slope & Pivots)
//====================================================
slope = ta.linreg(close, trendLength, 0) - ta.linreg(close, trendLength, 1)
normalizedSlope = (slope / close) * 10000.0
highestHigh = ta.highest(high, trendLength)
lowestLow = ta.lowest(low, trendLength)
prevHighest = ta.highest(high, trendLength)[trendLength]
prevLowest = ta.lowest(low, trendLength)[trendLength]
hh = highestHigh > prevHighest ? 25.0 : 0.0
hl = lowestLow > prevLowest ? 25.0 : 0.0
slopeScore = 50.0 * (1.0 / (1.0 + math.exp(-normalizedSlope / trendScale)))
scoreTrend = math.max(0.0, math.min(100.0, slopeScore + hh + hl))
//====================================================
// 6. EDGE ENGINE (Weighted Average)
//====================================================
totalWeight = priceWeight + volWeight + partWeight + trendWeight
edge = totalWeight == 0 ? 0.0 : ((scorePrice * priceWeight) + (scoreVol * volWeight) + (scorePart * partWeight) + (scoreTrend * trendWeight)) / totalWeight
//====================================================
// 7. STRATEGY EXECUTION ENGINE (LONG & SHORT)
//====================================================
edgeCrossBuy = ta.crossover(edge, edgeBuy)
edgeCrossExitL = ta.crossunder(edge, edgeExitLong)
edgeCrossSell = ta.crossover(edge, edgeSell)
edgeCrossExitS = ta.crossunder(edge, edgeExitShort)
// Estado actual de la posición de la estrategia
isLong = strategy.position_size > 0
isShort = strategy.position_size < 0
isFlat = strategy.position_size == 0
// Condiciones de entrada y salida
canEnterLong = isFlat and edgeCrossBuy and zPrice < -minZ and zVol >= minVolZ and zPart >= minPartZ and scoreTrend >= minTrendLvl
canExitLong = isLong and edgeCrossExitL
canEnterShort = isFlat and edgeCrossSell and zPrice > minZ and zVol >= minVolZ and zPart >= minPartZ and scoreTrend < (100.0 - minTrendLvl)
canExitShort = isShort and edgeCrossExitS
// Órdenes de Estrategia
if canEnterLong
strategy.entry("Long", strategy.long)
if canExitLong
strategy.close("Long", comment="Exit Long")
if canEnterShort
strategy.entry("Short", strategy.short)
if canExitShort
strategy.close("Short", comment="Exit Short")
//====================================================
// 8. MARKET QUALITY FILTER
//====================================================
string marketQuality = "NORMAL"
color qualityColor = #d97706
if zVol < 0.0 and zPart < -0.5
marketQuality := "POOR"
qualityColor := #dc2626
else if zVol >= 1.0 and zPart >= 0.5 and scoreTrend >= 40.0 and scoreTrend <= 60.0
marketQuality := "GOOD"
qualityColor := #16a34a
//====================================================
// 9. VISUALS AND PLOTS
//====================================================
plotshape(
showSignals and canEnterLong,
style = shape.triangleup,
location = location.belowbar,
color = colorLong,
size = size.small,
text = "BUY",
textcolor = colorLong
)
plotshape(
showSignals and canExitLong,
style = shape.triangledown,
location = location.abovebar,
color = colorExitL,
size = size.small,
text = "EXIT",
textcolor = colorExitL
)
plotshape(
showSignals and canEnterShort,
style = shape.triangledown,
location = location.abovebar,
color = colorShort,
size = size.small,
text = "SELL",
textcolor = colorShort
)
plotshape(
showSignals and canExitShort,
style = shape.triangleup,
location = location.belowbar,
color = colorExitS,
size = size.small,
text = "EXIT",
textcolor = colorExitS
)
edgeColor = isLong ? color.new(colorLong, 30) : isShort ? color.new(colorShort, 30) : color.new(colorExitL, 50)
plot(showEdge ? edge : na, color = edgeColor, linewidth = 1)
//====================================================
// 10. DYNAMIC INFORMATION PANEL
//====================================================
getPos(string posStr) =>
switch posStr
"Top Left" => position.top_left
"Bottom Left" => position.bottom_left
"Bottom Right" => position.bottom_right
=> position.top_right
var table panel = table.new(getPos(panelPosition), 2, 10, border_width = 1, border_color = color.gray)
status = isLong ? "LONG" : isShort ? "SHORT" : "WAIT"
statusColor = isLong ? colorLong : isShort ? colorShort : #4b5563
textColor = #1f2937
color zPriceColor = math.abs(zPrice) >= 2.0 ? colorLong : math.abs(zPrice) >= 1.0 ? #d97706 : #4b5563
color zVolColor = zVol >= 2.0 ? colorLong : zVol >= 1.0 ? #d97706 : #4b5563
color zPartColor = zPart >= 1.5 ? colorLong : zPart >= 0.5 ? #d97706 : #4b5563
color trendColor = scoreTrend >= 70.0 ? colorLong : scoreTrend <= 30.0 ? colorShort : #d97706
if barstate.islast and showPanel
table.clear(panel, 0, 0, 1, 9)
table.cell(panel, 0, 0, "Sadashi Engine v6 (Strat)", bgcolor = #1f2937, text_color = color.white)
table.cell(panel, 1, 0, "", bgcolor = #1f2937)
table.cell(panel, 0, 1, "EDGE", text_color = textColor, bgcolor = #ffffff)
table.cell(panel, 1, 1, str.tostring(edge, "#.0") + "%", text_color = statusColor, bgcolor = #ffffff)
table.cell(panel, 0, 2, "PRICE SCORE", text_color = textColor, bgcolor = #ffffff)
table.cell(panel, 1, 2, str.tostring(scorePrice, "#.0") + "% (Z: " + str.tostring(zPrice, "#.00") + ")", text_color = zPriceColor, bgcolor = #ffffff)
table.cell(panel, 0, 3, "VOL SCORE", text_color = textColor, bgcolor = #ffffff)
table.cell(panel, 1, 3, str.tostring(scoreVol, "#.0") + "% (Z: " + str.tostring(zVol, "#.00") + ")", text_color = zVolColor, bgcolor = #ffffff)
table.cell(panel, 0, 4, "PART SCORE", text_color = textColor, bgcolor = #ffffff)
table.cell(panel, 1, 4, str.tostring(scorePart, "#.0") + "% (Z: " + str.tostring(zPart, "#.00") + ")", text_color = zPartColor, bgcolor = #ffffff)
table.cell(panel, 0, 5, "TREND SCORE", text_color = textColor, bgcolor = #ffffff)
table.cell(panel, 1, 5, str.tostring(scoreTrend, "#.0") + "%", text_color = trendColor, bgcolor = #ffffff)
table.cell(panel, 0, 6, "MARKET QUALITY", text_color = textColor, bgcolor = #ffffff)
table.cell(panel, 1, 6, marketQuality, text_color = qualityColor, bgcolor = color.new(qualityColor, 85))
table.cell(panel, 0, 7, "LONG STATE", text_color = textColor, bgcolor = #ffffff)
table.cell(panel, 1, 7, isLong ? "ACTIVE" : "INACTIVE", text_color = isLong ? colorLong : #6b7280, bgcolor = #ffffff)
table.cell(panel, 0, 8, "SHORT STATE", text_color = textColor, bgcolor = #ffffff)
table.cell(panel, 1, 8, isShort ? "ACTIVE" : "INACTIVE", text_color = isShort ? colorShort : #6b7280, bgcolor = #ffffff)
table.cell(panel, 0, 9, "CURRENT STATE", text_color = textColor, bgcolor = #ffffff)
table.cell(panel, 1, 9, status, text_color = statusColor, bgcolor = #ffffff)