本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Heikin Ashi Reversal Setup with EMA, PDH and PDL RSP VF", shorttitle="HA Reversal + PDH/PDL", overlay=true)
// ================= INPUTS =================
slThreshold = input.float(20.0, "Max HA Range %", step=0.1)
maxSLAmount = input.float(5000, "Max Stop Loss Amount (₹)", step=500)
manualLot = input.int(1, "Manual Lot Size", minval=1)
capitalInput = input.float(100000, "Total Capital (₹)", step=10000)
capitalPercent = input.float(90.0, "Capital Usage (%)", step=5)
showLabels = input.bool(true, "Show PDH/PDL Labels")
// ================= HEIKIN ASHI OHLC =================
haTicker = ticker.heikinashi(syminfo.tickerid)
[haOpen, haHigh, haLow, haClose] = request.security(haTicker, timeframe.period, [open, high, low, close])
// Previous HA
haOpenPrev = haOpen[1]
haClosePrev = haClose[1]
haHighPrev = haHigh[1]
haLowPrev = haLow[1]
// ================= EMA LINES (optional) =================
ema5 = ta.ema(close, 5)
ema13 = ta.ema(low, 13)
plot(ema5, "EMA 5 Close", color=color.orange, linewidth=2)
plot(ema13, "EMA 13 Low", color=color.green, linewidth=2)
// ================= AUTO LOT SIZE =================
tickerName = str.upper(syminfo.ticker)
lotSize = str.contains(tickerName, "BANKNIFTY") ? 30 : str.contains(tickerName, "SENSEX") ? 20 : str.contains(tickerName, "NIFTY") ? 65 : manualLot
// ================= HA RANGE % =================
slActual = ((haHigh - haLow) / haHigh) * 100
// ================= HEIKIN ASHI CRITERIA =================
criteria1 = (haOpen > haClose) and (haHigh > math.max(haOpen, haClose)) and (haLow < math.min(haOpen, haClose))
criteria2 = (haOpen > haClose) and (haHigh < haHighPrev) and (haOpenPrev > haClosePrev) and (haHighPrev > haHigh) and (haLowPrev < haLow)
// Base signal
signal = (criteria1 or criteria2) and slActual < slThreshold
earlySignal = signal
confirmedSignal = signal and barstate.isconfirmed
// Background
bgcolor(confirmedSignal ? color.new(color.red, 75) : na)
// Arrow
plotshape(earlySignal,
title="Reversal Arrow Early",
style=shape.arrowup,
location=location.belowbar,
color=color.red,
size=size.small)
// ================= PERSISTENT STORAGE =================
var float entry = na
var float sl = na
var float exit1 = na
var float exit2 = na
var float totalCapital = na
var float totalLoss = na
var float profit1 = na
var float profit2 = na
var int lots = na
var float slStored = na
var int qty = na
var int lotsByCapital = na
var int qtyByCapital = na
// Update ONLY on candle close
if confirmedSignal
entry := haHigh
sl := haLow
slStored := slActual
riskPerUnit = entry - sl
riskPerLot = riskPerUnit * lotSize
// Risk-based lots
lots := riskPerLot > 0 ? int(math.floor(maxSLAmount / riskPerLot)) : 0
totalLoss := lots * riskPerLot
totalCapital := entry * lotSize * lots
qty := lots * lotSize
exit1 := entry + riskPerUnit
exit2 := entry + riskPerUnit * 2
profit1 := lots * riskPerLot
profit2 := lots * riskPerLot * 2
// Capital-based sizing
usableCapital = capitalInput * (capitalPercent / 100)
lotsByCapital := int(math.floor(usableCapital / (entry * lotSize)))
qtyByCapital := lotsByCapital * lotSize
// ================= DASHBOARD =================
var table dash = table.new(position.top_right, 2, 7, border_width=1)
if barstate.islast and not na(entry)
table.cell(dash, 0, 0, "Metric", bgcolor=color.gray, text_color=color.white)
table.cell(dash, 1, 0, "Value", bgcolor=color.gray, text_color=color.white)
table.cell(dash, 0, 1, "Entry | SL")
table.cell(dash, 1, 1, str.tostring(entry, format.mintick) + " | " + str.tostring(sl, format.mintick) + " (" + str.tostring(slStored, "#.##") + "%)")
table.cell(dash, 0, 2, "Lots | Qty | Capital")
table.cell(dash, 1, 2, str.tostring(lots) + " | " + str.tostring(qty) + " | ₹" + str.tostring(totalCapital, format.mintick))
table.cell(dash, 0, 3, "Loss @ SL")
table.cell(dash, 1, 3, "₹" + str.tostring(totalLoss, format.mintick))
table.cell(dash, 0, 4, "RR 1:1")
table.cell(dash, 1, 4, "Exit " + str.tostring(exit1, format.mintick) + " | ₹" + str.tostring(profit1, format.mintick))
table.cell(dash, 0, 5, "RR 1:2")
table.cell(dash, 1, 5, "Exit " + str.tostring(exit2, format.mintick) + " | ₹" + str.tostring(profit2, format.mintick))
table.cell(dash, 0, 6, "Capital Usage")
table.cell(dash, 1, 6, str.tostring(capitalPercent) + "% → " + str.tostring(lotsByCapital) + " lots | " + str.tostring(qtyByCapital) + " qty")
// ================= PREVIOUS DAY HIGH / LOW =================
realTicker = ticker.standard(syminfo.tickerid)
pdh = request.security(realTicker, "D", high[1], lookahead=barmerge.lookahead_on)
pdl = request.security(realTicker, "D", low[1], lookahead=barmerge.lookahead_on)
plot(pdh, title="Previous Day High (PDH)", color=color.blue, linewidth=2, style=plot.style_linebr)
plot(pdl, title="Previous Day Low (PDL)", color=color.red, linewidth=2, style=plot.style_linebr)
// ================= LABELS =================
var label pdhLabel = na
var label pdlLabel = na
if showLabels and barstate.islast
label.delete(pdhLabel)
label.delete(pdlLabel)
pdhLabel := label.new(bar_index, pdh, "PDH", style=label.style_label_down, color=color.blue, textcolor=color.white)
pdlLabel := label.new(bar_index, pdl, "PDL", style=label.style_label_up, color=color.red, textcolor=color.white)